-
-
Save asaf/e7ea48f86fb54fac27b22057912d369e to your computer and use it in GitHub Desktop.
crossid-sample-nodejs-openid-connect-form-post
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 body = [] | |
req.on('data', (chunk) => { | |
body.push(chunk) | |
}) | |
req.on('end', async () => { | |
const parsedBody = Buffer.concat(body).toString() | |
const params = client.callbackParams({ | |
method: 'POST', | |
body: parsedBody, | |
url: req.headers.host, | |
}) | |
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', | |
response_mode: 'form_post', | |
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