Skip to content

Commit 380ce10

Browse files
committed
remove userInfo request
1 parent 3ea2745 commit 380ce10

File tree

3 files changed

+38
-26
lines changed

3 files changed

+38
-26
lines changed

src/index.js

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,15 @@ const token = require('./token')
44
const { error, fakeReply, verify } = require('./utils')
55
const pkg = require('../package.json')
66

7-
let manager
8-
97
/**
10-
* @function
11-
* @public
12-
*
13-
* Get user information based on token with help of Keycloak.
14-
* If all validations and requests are successful, save the
15-
* token and its user data in memory cache.
8+
* @type Object
9+
* @private
1610
*
17-
* @param {string} token The token to be validated
18-
* @param {Function} reply The callback handler
11+
* Internally used properties
1912
*/
20-
function handleKeycloakUserInfo (tkn, reply) {
21-
manager.userInfo(tkn.get()).then((userInfo) => {
22-
const { scope, expiresIn } = tkn.getData()
23-
const userData = { credentials: Object.assign({ scope }, userInfo) }
24-
25-
cache.set(tkn.get(), userData, expiresIn)
26-
reply.continue(userData)
27-
}).catch((err) => {
28-
reply(error('unauthorized', err))
29-
})
13+
const internals = {
14+
manager: undefined,
15+
userInfoFields: []
3016
}
3117

3218
/**
@@ -41,8 +27,16 @@ function handleKeycloakUserInfo (tkn, reply) {
4127
function handleKeycloakValidation (tkn, reply) {
4228
const invalidate = (err) => reply(error('unauthorized', err, error.msg.invalid))
4329

44-
manager.validateAccessToken(tkn.get()).then((res) => {
45-
res ? handleKeycloakUserInfo(tkn, reply) : invalidate()
30+
internals.manager.validateAccessToken(tkn.get()).then((res) => {
31+
if (!res) {
32+
return invalidate()
33+
}
34+
35+
const { expiresIn, ...credentials } = tkn.getData(internals.userInfoFields)
36+
const userData = { credentials }
37+
38+
cache.set(tkn.get(), userData, expiresIn)
39+
return reply.continue(userData)
4640
}).catch(invalidate)
4741
}
4842

@@ -108,9 +102,11 @@ function strategy (server) {
108102
*/
109103
function plugin (server, opts, next) {
110104
opts = verify(opts)
111-
manager = new GrantManager(opts)
112105
cache.init(server, opts.cache)
113106

107+
internals.manager = new GrantManager(opts.client)
108+
internals.userInfoFields = opts.userInfo
109+
114110
server.auth.scheme('keycloak-jwt', strategy)
115111
server.decorate('server', 'kjwt', { validate })
116112

src/token.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,20 @@ function token (field) {
6565
return (exp - iat) * 1000
6666
}
6767

68+
/**
69+
* @function
70+
* @private
71+
*
72+
* Get necessary user information out of token content.
73+
*
74+
* @param {Object} content The token its content
75+
* @param {Array.<?string>} fields The necessary fields
76+
* @returns {Object} The collection of requested user info
77+
*/
78+
function getUserInfo (content, fields) {
79+
return _.pick(content, _.uniq(['sub', ...fields]))
80+
}
81+
6882
/**
6983
* @function
7084
* @public
@@ -88,12 +102,13 @@ function token (field) {
88102
*
89103
* @returns {Object} The extracted data
90104
*/
91-
function getData () {
105+
function getData (userInfoFields) {
92106
const content = getContent()
93107

94108
return {
95109
scope: getScope(content),
96-
expiresIn: getExpiration(content)
110+
expiresIn: getExpiration(content),
111+
...getUserInfo(content, userInfoFields)
97112
}
98113
}
99114

src/utils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ const scheme = joi.object({
1414
}).unknown(true).required(),
1515
cache: joi.alternatives().try(joi.object({
1616
segment: joi.string().default('keycloakJwt')
17-
}), joi.boolean().invalid(true)).default(false)
17+
}), joi.boolean().invalid(true)).default(false),
18+
userInfo: joi.array().items(joi.string()).default([])
1819
}).unknown(true).required()
1920

2021
/**

0 commit comments

Comments
 (0)