@@ -4,7 +4,6 @@ const jwt = require('jsonwebtoken');
4
4
5
5
const mw = require ( '../../../src/middleware/authentication' ) ;
6
6
const { AuthType } = require ( '../../../src/components/constants' ) ;
7
- const keycloak = require ( '../../../src/components/keycloak' ) ;
8
7
const { userService } = require ( '../../../src/services' ) ;
9
8
10
9
// Mock config library - @see {@link https://stackoverflow.com/a/64819698}
@@ -17,8 +16,6 @@ jest.mock('express-basic-auth', () => {
17
16
buildMiddleware . safeCompare = jest . requireActual ( 'express-basic-auth' ) . safeCompare ;
18
17
return buildMiddleware ;
19
18
} ) ;
20
- // Mock out keycloak library and use a spy to observe behavior
21
- jest . mock ( '../../../src/components/keycloak' ) ;
22
19
23
20
beforeEach ( ( ) => {
24
21
jest . resetAllMocks ( ) ;
@@ -90,7 +87,6 @@ describe('currentUser', () => {
90
87
const jwtVerifySpy = jest . spyOn ( jwt , 'verify' ) ;
91
88
const loginSpy = jest . spyOn ( userService , 'login' ) ;
92
89
const problemSendSpy = jest . spyOn ( Problem . prototype , 'send' ) ;
93
- const validateAccessTokenSpy = jest . spyOn ( keycloak . grantManager , 'validateAccessToken' ) ;
94
90
95
91
let req , res , next ;
96
92
@@ -159,7 +155,7 @@ describe('currentUser', () => {
159
155
it . each ( [
160
156
[ 'SPKI' , spki ] ,
161
157
[ 'PEM' , publicKey ]
162
- ] ) ( 'sets authType to BEARER with keycloak.publicKey %s' , async ( _desc , pkey ) => {
158
+ ] ) ( 'sets authType to BEARER with keycloak.publicKey %s and valid auth token ' , async ( _desc , pkey ) => {
163
159
jwtVerifySpy . mockReturnValue ( { sub : 'sub' } ) ; // return truthy value
164
160
loginSpy . mockImplementation ( ( ) => { } ) ;
165
161
config . has
@@ -187,7 +183,6 @@ describe('currentUser', () => {
187
183
expect ( config . get ) . toHaveBeenNthCalledWith ( 1 , 'keycloak.publicKey' ) ;
188
184
expect ( config . get ) . toHaveBeenNthCalledWith ( 2 , 'keycloak.serverUrl' ) ;
189
185
expect ( config . get ) . toHaveBeenNthCalledWith ( 3 , 'keycloak.realm' ) ;
190
- expect ( validateAccessTokenSpy ) . toHaveBeenCalledTimes ( 0 ) ;
191
186
expect ( checkBasicAuthSpy ) . toHaveBeenCalledTimes ( 0 ) ;
192
187
expect ( jwtVerifySpy ) . toHaveBeenCalledTimes ( 1 ) ;
193
188
expect ( jwtVerifySpy ) . toHaveBeenCalledWith ( expect . any ( String ) , publicKey , expect . objectContaining ( {
@@ -200,42 +195,43 @@ describe('currentUser', () => {
200
195
expect ( problemSendSpy ) . toHaveBeenCalledTimes ( 0 ) ;
201
196
} ) ;
202
197
203
- it ( 'sets authType to BEARER without keycloak.publicKey and valid token' , async ( ) => {
204
- jwtVerifySpy . mockReturnValue ( { sub : 'sub' } ) ;
205
- loginSpy . mockImplementation ( ( ) => { } ) ;
206
- validateAccessTokenSpy . mockResolvedValue ( 'tokenstring' ) ;
198
+ it ( 'short circuits with invalid auth token' , async ( ) => {
199
+ const authorization = 'bearer ' ;
200
+
201
+ problemSendSpy . mockImplementation ( ( ) => { } ) ;
207
202
config . has
208
203
. mockReturnValueOnce ( false ) // basicAuth.enabled
209
204
. mockReturnValueOnce ( true ) // keycloak.enabled
210
- . mockReturnValueOnce ( false ) ; // keycloak.publicKey
205
+ . mockReturnValueOnce ( true ) ; // keycloak.publicKey
206
+ config . get
207
+ . mockReturnValueOnce ( spki ) // keycloak.publicKey
208
+ . mockReturnValueOnce ( serverUrl ) // keycloak.serverUrl
209
+ . mockReturnValueOnce ( realm ) ; // keycloak.realm
211
210
req . get . mockReturnValueOnce ( authorization ) ;
212
211
213
212
await mw . currentUser ( req , res , next ) ;
214
213
215
- expect ( req . currentUser ) . toBeTruthy ( ) ;
216
- expect ( req . currentUser ) . toHaveProperty ( 'authType' , AuthType . BEARER ) ;
217
- expect ( req . currentUser ) . toHaveProperty ( 'tokenPayload' ) ;
214
+ expect ( req . currentUser ) . toBeFalsy ( ) ;
218
215
expect ( req . get ) . toHaveBeenCalledTimes ( 1 ) ;
219
216
expect ( req . get ) . toHaveBeenCalledWith ( 'Authorization' ) ;
220
217
expect ( config . has ) . toHaveBeenCalledTimes ( 3 ) ;
221
218
expect ( config . has ) . toHaveBeenNthCalledWith ( 1 , 'basicAuth.enabled' ) ;
222
219
expect ( config . has ) . toHaveBeenNthCalledWith ( 2 , 'keycloak.enabled' ) ;
223
220
expect ( config . has ) . toHaveBeenNthCalledWith ( 3 , 'keycloak.publicKey' ) ;
224
- expect ( validateAccessTokenSpy ) . toHaveBeenCalledTimes ( 1 ) ;
225
- expect ( validateAccessTokenSpy ) . toHaveBeenCalledWith ( expect . any ( String ) ) ;
226
221
expect ( checkBasicAuthSpy ) . toHaveBeenCalledTimes ( 0 ) ;
227
- expect ( jwtVerifySpy ) . toHaveBeenCalledTimes ( 0 ) ;
228
- expect ( loginSpy ) . toHaveBeenCalledTimes ( 1 ) ;
229
- expect ( next ) . toHaveBeenCalledTimes ( 1 ) ;
230
- expect ( next ) . toHaveBeenCalledWith ( ) ;
231
- expect ( problemSendSpy ) . toHaveBeenCalledTimes ( 0 ) ;
222
+ expect ( jwtVerifySpy ) . toHaveBeenCalledTimes ( 1 ) ;
223
+ expect ( jwtVerifySpy ) . toHaveBeenCalledWith ( expect . any ( String ) , publicKey , expect . objectContaining ( {
224
+ issuer : `${ serverUrl } /realms/${ realm } `
225
+ } ) ) ;
226
+ expect ( loginSpy ) . toHaveBeenCalledTimes ( 0 ) ;
227
+ expect ( next ) . toHaveBeenCalledTimes ( 0 ) ;
228
+ expect ( problemSendSpy ) . toHaveBeenCalledTimes ( 1 ) ;
229
+ expect ( problemSendSpy ) . toHaveBeenCalledWith ( res ) ;
232
230
} ) ;
233
231
234
- it ( 'short circuits without keycloak.publicKey and invalid token' , async ( ) => {
235
- const authorization = 'bearer ' ;
236
-
237
- problemSendSpy . mockImplementation ( ( ) => { } ) ;
238
- validateAccessTokenSpy . mockResolvedValue ( false ) ;
232
+ it ( 'short circuits without keycloak.publicKey' , async ( ) => {
233
+ jwtVerifySpy . mockReturnValue ( { sub : 'sub' } ) ;
234
+ loginSpy . mockImplementation ( ( ) => { } ) ;
239
235
config . has
240
236
. mockReturnValueOnce ( false ) // basicAuth.enabled
241
237
. mockReturnValueOnce ( true ) // keycloak.enabled
@@ -251,8 +247,6 @@ describe('currentUser', () => {
251
247
expect ( config . has ) . toHaveBeenNthCalledWith ( 1 , 'basicAuth.enabled' ) ;
252
248
expect ( config . has ) . toHaveBeenNthCalledWith ( 2 , 'keycloak.enabled' ) ;
253
249
expect ( config . has ) . toHaveBeenNthCalledWith ( 3 , 'keycloak.publicKey' ) ;
254
- expect ( validateAccessTokenSpy ) . toHaveBeenCalledTimes ( 1 ) ;
255
- expect ( validateAccessTokenSpy ) . toHaveBeenCalledWith ( expect . any ( String ) ) ;
256
250
expect ( checkBasicAuthSpy ) . toHaveBeenCalledTimes ( 0 ) ;
257
251
expect ( jwtVerifySpy ) . toHaveBeenCalledTimes ( 0 ) ;
258
252
expect ( loginSpy ) . toHaveBeenCalledTimes ( 0 ) ;
0 commit comments