-
-
Save asaf/0bb2fb334077203e5957231f3985fede to your computer and use it in GitHub Desktop.
crossid-sample-nodejs-openid-connect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PORT=3005 | |
ISSUER=https://org.us.crossid.io/oauth2 | |
REDIRECT_URI=http://localhost:3005 | |
CLIENT_ID=... | |
CLIENT_SECRET=... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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