-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(openid-client-helper.js): Added utility for openid client strate…
…gy initialization feat #206
- Loading branch information
Showing
3 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const { JWK: { generateSync }, JWKS: { KeyStore, asKeyStore } } = require('jose') | ||
const { Issuer } = require('openid-client') | ||
const path = require('path') | ||
const fs = require('fs') | ||
const fileUtils = require('../utils/file-utils') | ||
const clientJWKSFilePath = path.join(`${process.cwd()}/server`, 'jwks') | ||
|
||
/** | ||
* generate jwks and store it in file. file name will be like [provider.id].json | ||
* @param {*} provider | ||
* @returns undefined | ||
*/ | ||
async function generateJWKS (provider) { | ||
const keyType = generateSync('RSA') | ||
const keyStore = new KeyStore(keyType) | ||
const fileName = path.join(fileUtils.makeDir(clientJWKSFilePath), provider.id + '.json') | ||
// eslint-disable-next-line security/detect-non-literal-fs-filename | ||
if (!fs.existsSync(fileName)) { | ||
await fileUtils.writeDataToFile(fileName, JSON.stringify(keyStore.toJWKS(true))) | ||
} | ||
} | ||
|
||
const clients = [] | ||
|
||
/** | ||
* Initialize and get issuer.client object to initialize openid-client passport strategy | ||
* @param {*} provider | ||
* @returns issuer.client | ||
*/ | ||
async function getClient (provider) { | ||
const { options } = provider | ||
let client = clients.find(c => c.id === provider.id) | ||
if (client) { | ||
return client.client | ||
} | ||
|
||
const issuer = await Issuer.discover(options.issuer) | ||
|
||
if (options.token_endpoint_auth_method && options.token_endpoint_auth_method === 'private_key_jwt') { | ||
// generate jwks | ||
await generateJWKS(provider) | ||
// eslint-disable-next-line security/detect-non-literal-require | ||
const jwks = require(path.join(fileUtils.makeDir(clientJWKSFilePath), `${provider.id}.json`)) | ||
const ks = asKeyStore(jwks) | ||
client = new issuer.Client(options, ks.toJWKS(true)) | ||
} else { | ||
client = new issuer.Client(options) | ||
} | ||
|
||
clients.push({ id: provider.id, client }) | ||
return client | ||
} | ||
|
||
module.exports = { | ||
getClient, | ||
generateJWKS | ||
} |
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