Skip to content

Instantly share code, notes, and snippets.

@asaf
Last active October 22, 2022 23:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asaf/0bb2fb334077203e5957231f3985fede to your computer and use it in GitHub Desktop.
Save asaf/0bb2fb334077203e5957231f3985fede to your computer and use it in GitHub Desktop.
crossid-sample-nodejs-openid-connect
PORT=3005
ISSUER=https://org.us.crossid.io/oauth2
REDIRECT_URI=http://localhost:3005
CLIENT_ID=...
CLIENT_SECRET=...
require('dotenv').config()
const { PORT, ISSUER, CLIENT_ID, CLIENT_SECRET, REDIRECT_URI } = process.env
const server = require('http').createServer().listen(PORT)
const { Issuer, generators } = require('openid-client')
const open = require('open')
server.removeAllListeners('request')
server.once('listening', () => {
;(async () => {
const issuer = await Issuer.discover(ISSUER)
const client = new issuer.Client({
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
redirect_uris: [REDIRECT_URI],
response_types: ['code'],
})
const code_verifier = generators.codeVerifier()
const code_challenge = generators.codeChallenge(code_verifier)
const state = generators.nonce()
server.on('request', async (req, res) => {
res.setHeader('connection', 'close')
const params = client.callbackParams(req)
if (Object.keys(params).length) {
const tokenSet = await client.callback(REDIRECT_URI, params, {
code_verifier,
response_type: 'code',
state,
})
const userinfo = await client.userinfo(tokenSet)
res.end(
JSON.stringify({ response: tokenSet, claims: tokenSet.claims(), userInfo: userinfo })
)
server.close()
}
})
await open(
client.authorizationUrl({
redirect_uri: REDIRECT_URI,
code_challenge,
code_challenge_method: 'S256',
scope: 'openid email',
state,
}),
{ wait: false }
)
})().catch((err) => {
console.error(err)
process.exitCode = 1
server.close()
})
})
{
"name": "crossid-sample-nodejs-openid-connect",
"version": "1.0.0",
"description": "",
"main": "index.js",
"bin": "./index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Crossid",
"license": "MIT",
"dependencies": {
"dotenv": "^16.0.3",
"open": "^7.0.0",
"openid-client": "^3.8.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment