1 | const got = require('got')
|
2 | const GrantManager = require('keycloak-connect/middleware/auth-utils/grant-manager')
|
3 | const KeycloakToken = require('keycloak-connect/middleware/auth-utils/token')
|
4 | const apiKey = require('./apiKey')
|
5 | const cache = require('./cache')
|
6 | const token = require('./token')
|
7 | const { raiseUnauthorized, errorMessages, fakeToolkit, verify } = require('./utils')
|
8 | const pkg = require('../package.json')
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | let options
|
17 | let manager
|
18 | let store
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | async function verifySignedJwt (tkn) {
|
32 | const kcTkn = new KeycloakToken(tkn, options.clientId)
|
33 | await manager.validateToken(kcTkn, 'Bearer')
|
34 |
|
35 | return tkn
|
36 | }
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | async function introspect (tkn) {
|
52 | try {
|
53 | const isValid = await manager.validateAccessToken(tkn)
|
54 | if (isValid === false) throw Error(errorMessages.invalid)
|
55 | } catch (err) {
|
56 | throw Error(errorMessages.invalid)
|
57 | }
|
58 |
|
59 | return tkn
|
60 | }
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | async function getRpt (tkn) {
|
74 | let body = {}
|
75 |
|
76 | try {
|
77 | ({ body } = await got.get(`${options.realmUrl}/authz/entitlement/${options.clientId}`, {
|
78 | headers: { authorization: `bearer ${tkn}` }
|
79 | }))
|
80 | } catch (err) {
|
81 | throw Error(errorMessages.rpt)
|
82 | }
|
83 |
|
84 | return body.rpt
|
85 | }
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | function getValidateFn () {
|
99 | return options.secret ? introspect : options.entitlement ? getRpt : verifySignedJwt
|
100 | }
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | async function handleKeycloakValidation (tkn, h) {
|
116 | try {
|
117 | const info = await getValidateFn()(tkn)
|
118 | const { expiresIn, credentials } = token.getData(info || tkn, options)
|
119 | const userData = { credentials }
|
120 |
|
121 | await cache.set(store, tkn, userData, expiresIn)
|
122 | return h.authenticated(userData)
|
123 | } catch (err) {
|
124 | throw raiseUnauthorized(errorMessages.invalid, err.message)
|
125 | }
|
126 | }
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | async function validate (field, h = (data) => data) {
|
142 | if (!field) {
|
143 | throw raiseUnauthorized(errorMessages.missing)
|
144 | }
|
145 |
|
146 | const tkn = token.create(field)
|
147 | const reply = fakeToolkit(h)
|
148 |
|
149 | if (!tkn) {
|
150 | throw raiseUnauthorized(errorMessages.invalid)
|
151 | }
|
152 |
|
153 | const cached = await cache.get(store, tkn)
|
154 | return cached ? reply.authenticated(cached) : handleKeycloakValidation(tkn, reply)
|
155 | }
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 | function strategy (server) {
|
170 | return {
|
171 | authenticate (request, h) {
|
172 | return validate(request.raw.req.headers.authorization, h)
|
173 | }
|
174 | }
|
175 | }
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 | function register (server, opts) {
|
189 | options = verify(opts)
|
190 | manager = new GrantManager(options)
|
191 | store = cache.create(server, options.cache)
|
192 |
|
193 | apiKey.init(server, options)
|
194 |
|
195 | if (options.schemeName in server.auth._schemes) {
|
196 | throw Error(`Authentication strategy named ${options.schemeName} already exists.`)
|
197 | }
|
198 |
|
199 | if (options.decoratorName in server._core._decorations['server']) {
|
200 | throw Error(`Server decorator named ${options.decoratorName} already exists.`)
|
201 | }
|
202 |
|
203 | server.auth.scheme(options.schemeName, strategy)
|
204 | server.decorate('server', options.decoratorName, { validate })
|
205 | }
|
206 |
|
207 | module.exports = { register, pkg, multiple: true }
|