@@ -3,7 +3,7 @@ const { GrantManager } = require('keycloak-auth-utils')
3
3
const KeycloakToken = require ( 'keycloak-auth-utils/lib/token' )
4
4
const cache = require ( './cache' )
5
5
const token = require ( './token' )
6
- const { error , fakeToolkit, verify } = require ( './utils' )
6
+ const { raiseError , errors , fakeToolkit, verify } = require ( './utils' )
7
7
const pkg = require ( '../package.json' )
8
8
9
9
/**
@@ -48,10 +48,11 @@ async function verifySignedJwt (tkn) {
48
48
* @throws {Error } If token is invalid
49
49
*/
50
50
async function introspect ( tkn ) {
51
- const res = await manager . validateAccessToken ( tkn )
52
-
53
- if ( res === false ) {
54
- throw Error ( error . msg . invalid )
51
+ try {
52
+ const isValid = await manager . validateAccessToken ( tkn )
53
+ if ( isValid === false ) throw Error ( errors . invalid )
54
+ } catch ( err ) {
55
+ throw Error ( errors . invalid )
55
56
}
56
57
57
58
return tkn
@@ -69,9 +70,15 @@ async function introspect (tkn) {
69
70
* @throws {Error } If request failed or token is invalid
70
71
*/
71
72
async function getRpt ( tkn ) {
72
- const { data } = await axios . get ( `${ options . realmUrl } /authz/entitlement/${ options . clientId } ` , {
73
- headers : { authorization : `bearer ${ tkn } ` }
74
- } )
73
+ let data = { }
74
+
75
+ try {
76
+ ( { data } = await axios . get ( `${ options . realmUrl } /authz/entitlement/${ options . clientId } ` , {
77
+ headers : { authorization : `bearer ${ tkn } ` }
78
+ } ) )
79
+ } catch ( err ) {
80
+ throw Error ( errors . rpt )
81
+ }
75
82
76
83
return data . rpt
77
84
}
@@ -111,7 +118,7 @@ async function handleKeycloakValidation (tkn, h) {
111
118
await cache . set ( store , tkn , userData , expiresIn )
112
119
return h . authenticated ( userData )
113
120
} catch ( err ) {
114
- throw error ( 'unauthorized' , null , error . msg . invalid )
121
+ throw raiseError ( 'unauthorized' , err , errors . invalid )
115
122
}
116
123
}
117
124
@@ -131,7 +138,7 @@ async function validate (field, h = (data) => data) {
131
138
const reply = fakeToolkit ( h )
132
139
133
140
if ( ! tkn ) {
134
- throw error ( 'unauthorized' , error . msg . missing )
141
+ throw raiseError ( 'unauthorized' , null , errors . missing )
135
142
}
136
143
137
144
const cached = await cache . get ( store , tkn )
0 commit comments