Skip to content

Commit f23c531

Browse files
committed
refactor
1 parent ffef2ce commit f23c531

File tree

7 files changed

+159
-110
lines changed

7 files changed

+159
-110
lines changed

package-lock.json

Lines changed: 81 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@
4141
"ava": "^0.20.0",
4242
"coveralls": "^2.13.1",
4343
"hapi": "^16.4.3",
44+
"jsonwebtoken": "^7.4.1",
4445
"nock": "^9.0.14",
4546
"nyc": "^11.0.3",
4647
"standard": "^10.0.2"
4748
},
4849
"dependencies": {
49-
"axios": "^0.16.2",
5050
"boom": "^5.2.0",
5151
"joi": "^10.6.0",
52-
"jsonwebtoken": "^7.4.1",
52+
"keycloak-auth-utils": "^3.2.1",
5353
"lodash": "^4.17.4"
5454
},
5555
"peerDependencies": {

src/index.js

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
const axios = require('axios')
2-
const jwt = require('jsonwebtoken')
1+
const { GrantManager } = require('keycloak-auth-utils')
2+
const Token = require('keycloak-auth-utils/lib/token')
33
const cache = require('./cache')
44
const token = require('./token')
55
const { error, fakeReply, verify } = require('./utils')
66
const pkg = require('../package.json')
77

88
/**
9-
* @type Object
9+
* @type {Object|GrantManager}
1010
* @private
1111
*
12-
* The plugin related options
12+
* The plugin related options and GrantManager instance.
1313
*/
1414
let options
15+
let manager
1516

1617
/**
1718
* @function
@@ -25,16 +26,7 @@ let options
2526
* @returns {Promise} The error-handled promise
2627
*/
2728
function validateOffline (token) {
28-
const {
29-
publicKey,
30-
verifyOpts = { algorithms: ['RS256', 'RS384', 'RS512'] }
31-
} = options
32-
33-
return new Promise((resolve, reject) => {
34-
jwt.verify(token, publicKey, verifyOpts, (err, decoded) => {
35-
err ? reject(err) : resolve(decoded)
36-
})
37-
})
29+
return manager.validateToken(new Token(token, options.clientId))
3830
}
3931

4032
/**
@@ -49,12 +41,8 @@ function validateOffline (token) {
4941
* @returns {Promise} The error-handled promise
5042
*/
5143
function validateOnline (token) {
52-
return axios.post(`${options.realmUrl}/protocol/openid-connect/token/introspect`, {
53-
token,
54-
client_secret: options.secret,
55-
client_id: options.clientId
56-
}).then(({ data }) => {
57-
if (!data.active) {
44+
return manager.validateAccessToken(token).then((res) => {
45+
if (res === false) {
5846
throw Error(error.msg.invalid)
5947
}
6048

@@ -149,6 +137,8 @@ function strategy (server) {
149137
*/
150138
function plugin (server, opts, next) {
151139
options = verify(opts)
140+
manager = new GrantManager(options)
141+
152142
cache.init(server, options.cache)
153143

154144
server.auth.scheme('keycloak-jwt', strategy)

src/utils.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,13 @@ const scheme = joi.object({
1515
joi.string().regex(/^-----BEGIN(?: RSA)? PUBLIC KEY-----[\s\S]*-----END(?: RSA)? PUBLIC KEY-----\s?$/ig, 'PEM'),
1616
joi.object().type(Buffer)
1717
),
18-
verifyOpts: joi.object({
19-
ignoreExpiration: joi.any().forbidden(),
20-
ignoreNotBefore: joi.any().forbidden()
21-
}).unknown(true),
18+
minTimeBetweenJwksRequests: joi.number().integer().positive().allow(0).default(0),
2219
cache: joi.alternatives().try(joi.object({
2320
segment: joi.string().default('keycloakJwt')
2421
}), joi.boolean()).default(false),
2522
userInfo: joi.array().items(joi.string().min(1))
2623
})
27-
.xor('secret', 'publicKey')
28-
.without('secret', 'verifyOpts')
24+
.nand('secret', 'publicKey')
2925
.required()
3026

3127
/**

0 commit comments

Comments
 (0)