koa-openid-connect
OpenID Connect Middleware for use with Koa
Overview
We support Authorization Code Flow
Getting Started
Install with npm:
npm i koa-openid-connect --save
Require in your Node-Koa server.js file:
const KoaOpenIdConnect =
Define an openid const:
const openid = defaultConfig openidConfig
Call inside your home route:
homeRoute
See full example below:
Example
Create .env file in your root directory with the following config variables
# Node Evironment NODE_ENV=development # Koa OpenID Default Config Variables URI_BASE=https://mydomain.example.com/openId URI_AUTH=https://mydomain.example.com/openId/authenticate URI_TOKEN=https://mydomain.example.com/openId/token URI_USERINFO=https://mydomain.example.com/openId/userinfo URI_LOGOUT=https://mydomain.example.com/openId/logout SCOPE="openid profile email" # Koa OpenID Config Variables CLIENT_ID="<your_client_id>" CLIENT_SECRET="<your_client_secret>" URI_REDIRECT=https://mydomain.example.com/redirect
Node-Koa server.js
const Koa = const Router = const combineRouters = const logger = const cors = const bodyParser = const Chance = const chance = const _log = const KoaOpenIdConnect = const app = const defaultConfig = base_uri: processenvURI_BASE authorize_uri: processenvURI_AUTH token_uri: processenvURI_TOKEN userinfo_uri: processenvURI_USERINFO scope: processenvSCOPE const openidConfig = client_id: processenvCLIENT_ID client_secret: processenvCLIENT_SECRET redirect_uri: processenvURI_REDIRECT state: chance const openid = defaultConfig openidConfig const homeRoute = const loginRoute = const redirectRoute = const registerRoute = homeRoute loginRoute redirectRoute registerRoute const router = appappappapp app
When you start the app and navigate to http://localhost:6001 the KoaOpenIdConnect
checks if you are already authenticated and redirects you the URI_REDIRECT
you specified in the .env
file