npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@pixelygroup/keycloak-koa-connect

v0.9.8

Published

keycloak koa oauth jsonWebToken

Downloads

3,588

Readme

keycloak-koa-connect

This is a fork of https://github.com/xrian/keycloak-koa-connect

Introduction

It is very convenient to integrate keycloak with express js by keycloak-nodejs-connect directly. However, when we started a new project, we adopted koa as the server-side development framework and found that the conventional method of converting express middleware to koa middleware was not suitable for the keycloak-nodejs-connect library. We will try to extend this documentation with every release. And cover some scenarios not mentioned in the original project.

Installation

$ npm i @pixelygroup/keycloak-koa-connect --save

Instructions

Because the library is implemented with Typescript, if you directly import (require) in nodejs using ES syntax, you will get no value, thus you need to import it's .default attribute

In root folder create a keycloak.js file with your configuration and init-keycloak.js.

keycloak.js

// keycloak.js

module.exports = {
  'realm': '', // realm
  'auth-server-url': '', // keycloak URL: http://127.0.0.1:8080/auth
  'ssl-required': 'external',
  'resource': '', // client ID
  'bearer-only': true,
  'credentials': {
    'secret': 'if-enabled client-secret ,then-need-to-fill-in-here secret'
  },
  'use-resource-role-mappings': true,
  'confidential-port': 0,
  'realm-public-key': ''
}

init-keycloak.js

// init-keycloak.js

const KeycloakConnect = require('@pixelygroup/keycloak-koa-connect').default
const bodyStore = require('@pixelygroup/keycloak-koa-connect/stores/body-store').default // If this option is used, it is legal to include the value of jwt in the body
const queryStore = require('@pixelygroup/keycloak-koa-connect/stores/query-store').default // If this option is used, it is also legal to pass a token at http://a.com?jwt=token

const Keycloak = require('./keycloak.js')
const keycloak = new KeycloakConnect({ store: [queryStore, bodyStore,]}, Keycloak)

module.exports = { keycloak }

Then in:

nodejs

// index.js

const Koa = require('koa');
const app = new Koa();

const { keycloak } = require('./init-keycloak.js')

keycloak.middleware()
  .map(item => {
    app.use(item)
})

app.listen(3000);

typescript

// index.ts
import * as Koa from 'koa';
const app = new Koa();

const { keycloak } = require('./init-keycloak.js')

keycloak.middleware()
  .map(item => {
    app.use(item)
})

app.listen(3000)

Notes

If you define your routes in routes/index.js, you need to import init-keycloack.js there as well

nodejs

// routes/index.js

const Router = require('koa-router')
const router = new Router()

const { keycloak } = require('../init-keycloak.js')

// ## To secure a resource with an application role for the current app:
router.get( '/special', keycloak.protect('special'), specialHandler )

// ## To secure a resource with an application role for a different app:
router.get( '/extra-special', keycloak.protect('other-app:special'), extraSpecialHandler )

// ## To secure a resource with a realm role:
router.get( '/admin', keycloak.protect( 'realm:admin' ), adminHandler )

Do you enjoy this package? Help us keep it maintained!

Buy us a coffee or become a sponsor