Skip to content

Commit b3f6ce3

Browse files
committed
introduce first state of offline validation
1 parent 8a9d74a commit b3f6ce3

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

src/index.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const axios = require('axios')
2+
const jwt = require('jsonwebtoken')
23
const cache = require('./cache')
34
const token = require('./token')
45
const { error, fakeReply, verify } = require('./utils')
@@ -12,6 +13,40 @@ const pkg = require('../package.json')
1213
*/
1314
let options
1415

16+
/**
17+
* @function
18+
* @private
19+
*
20+
* Validate the token offline with help of
21+
* the related public key. Resolve if the
22+
* verification succeeded.
23+
*
24+
* @param {string} token The token to be validated
25+
* @returns {Promise} The error-handled promise
26+
*/
27+
function validateOffline (token) {
28+
return new Promise((resolve, reject) => {
29+
jwt.verify(token, options.publicKey, options.verifyOpts, (err, decoded) => {
30+
if (err) {
31+
reject(err)
32+
}
33+
34+
resolve(decoded)
35+
})
36+
})
37+
}
38+
39+
/**
40+
* @function
41+
* @private
42+
*
43+
* Validate the token online with help of
44+
* the related Keycloak server. Resolve if
45+
* the request succeeded and token is valid.
46+
*
47+
* @param {string} token The token to be validated
48+
* @returns {Promise} The error-handled promise
49+
*/
1550
function validateOnline (token) {
1651
return axios.post(`${options.realmUrl}/protocol/openid-connect/token/introspect`, {
1752
token,
@@ -30,13 +65,17 @@ function validateOnline (token) {
3065
* @function
3166
* @public
3267
*
33-
* Validate a token with help of Keycloak.
68+
* Validate a token either with the help of Keycloak
69+
* or a related public key. Store the user data in
70+
* cache if enabled.
3471
*
3572
* @param {string} token The token to be validated
3673
* @param {Function} reply The callback handler
3774
*/
3875
function handleKeycloakValidation (tkn, reply) {
39-
validateOnline(tkn.get()).then((res) => {
76+
const validateFn = options.secret ? validateOnline : validateOffline
77+
78+
validateFn(tkn.get()).then(() => {
4079
const { expiresIn, credentials } = tkn.getData(options.userInfo)
4180
const userData = { credentials }
4281

src/utils.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@ const joi = require('joi')
1010
const scheme = joi.object({
1111
realmUrl: joi.string().uri().required(),
1212
clientId: joi.string().min(1).required(),
13-
secret: joi.string().min(1).required(),
13+
secret: joi.string().min(1),
1414
cache: joi.alternatives().try(joi.object({
1515
segment: joi.string().default('keycloakJwt')
1616
}), joi.boolean()).default(false),
1717
userInfo: joi.array().items(joi.string().min(1))
18-
}).required()
18+
})
19+
.xor('secret', 'publicKey')
20+
.without('secret', 'verifyOpts')
21+
.required()
1922

2023
/**
2124
* @function

0 commit comments

Comments
 (0)