-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Swap out keycloak-connect for jsonwebtoken
Implementation taken from COMS
- Loading branch information
1 parent
81d9dff
commit 2472018
Showing
7 changed files
with
114 additions
and
34 deletions.
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 was deleted.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,19 +1,49 @@ | ||
const config = require('config'); | ||
const jwt = require('jsonwebtoken'); | ||
const Problem = require('api-problem'); | ||
|
||
const keycloak = require('../components/keycloak'); | ||
const { getConfigBoolean } = require('../components/utils'); | ||
|
||
/** | ||
* @function _spkiWrapper | ||
* Wraps an SPKI key with PEM header and footer | ||
* @param {string} spki The PEM-encoded Simple public-key infrastructure string | ||
* @returns {string} The PEM-encoded SPKI with PEM header and footer | ||
*/ | ||
const _spkiWrapper = (spki) => `-----BEGIN PUBLIC KEY-----\n${spki}\n-----END PUBLIC KEY-----`; | ||
|
||
module.exports = { | ||
/** | ||
* @function protect | ||
* Enables keycloak protect only if environment has it enabled | ||
* @param {string} [role=undefined] Keycloak protect role-based authorization | ||
* @returns {function} An express/connect compatible middleware function | ||
* Enables JWT verification only if environment has it enabled. | ||
*/ | ||
protect: (role = undefined) => { | ||
if (config.has('keycloak.enabled')) { | ||
return keycloak.protect(role); | ||
authenticate: (req, res, next) => { | ||
const authorization = req.get('Authorization'); | ||
|
||
if (getConfigBoolean('keycloak.enabled')) { | ||
const bearerToken = authorization.substring(7); | ||
|
||
if (config.has('keycloak.publicKey')) { | ||
const publicKey = config.get('keycloak.publicKey'); | ||
const pemKey = publicKey.startsWith('-----BEGIN') ? publicKey : _spkiWrapper(publicKey); | ||
|
||
try { | ||
jwt.verify(bearerToken, pemKey, { | ||
issuer: `${config.get('keycloak.serverUrl')}/realms/${config.get('keycloak.realm')}` | ||
}); | ||
|
||
next(); | ||
} catch (err) { | ||
return new Problem(401, { | ||
detail: err.message | ||
}).send(res); | ||
} | ||
|
||
} else { | ||
throw new Error('OIDC environment variable KC_PUBLICKEY or keycloak.publicKey must be defined'); | ||
} | ||
|
||
} else { | ||
return (_req, _res, next) => next(); | ||
next(); | ||
} | ||
} | ||
}; |
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