@@ -41,7 +41,7 @@ const validateAuthMode = ({
41
41
// case: no auth or X-API-KEY header present
42
42
throw BadRequestError ( { message : 'Missing Authorization or X-API-KEY in request header.' } ) ;
43
43
}
44
-
44
+
45
45
if ( typeof apiKey === 'string' ) {
46
46
// case: treat request authentication type as via X-API-KEY (i.e. API Key)
47
47
authTokenType = 'apiKey' ;
@@ -50,13 +50,13 @@ const validateAuthMode = ({
50
50
51
51
if ( typeof authHeader === 'string' ) {
52
52
// case: treat request authentication type as via Authorization header (i.e. either JWT or service token)
53
- const [ tokenType , tokenValue ] = < [ string , string ] > authHeader . split ( ' ' , 2 ) ?? [ null , null ]
53
+ const [ tokenType , tokenValue ] = < [ string , string ] > authHeader . split ( ' ' , 2 ) ?? [ null , null ]
54
54
if ( tokenType === null )
55
55
throw BadRequestError ( { message : `Missing Authorization Header in the request header.` } ) ;
56
56
if ( tokenType . toLowerCase ( ) !== 'bearer' )
57
57
throw BadRequestError ( { message : `The provided authentication type '${ tokenType } ' is not supported.` } ) ;
58
58
if ( tokenValue === null )
59
- throw BadRequestError ( { message : 'Missing Authorization Body in the request header.' } ) ;
59
+ throw BadRequestError ( { message : 'Missing Authorization Body in the request header.' } ) ;
60
60
61
61
switch ( tokenValue . split ( '.' , 1 ) [ 0 ] ) {
62
62
case 'st' :
@@ -67,11 +67,11 @@ const validateAuthMode = ({
67
67
}
68
68
authTokenValue = tokenValue ;
69
69
}
70
-
70
+
71
71
if ( ! authTokenType || ! authTokenValue ) throw BadRequestError ( { message : 'Missing valid Authorization or X-API-KEY in request header.' } ) ;
72
-
72
+
73
73
if ( ! acceptedAuthModes . includes ( authTokenType ) ) throw BadRequestError ( { message : 'The provided authentication type is not supported.' } ) ;
74
-
74
+
75
75
return ( {
76
76
authTokenType,
77
77
authTokenValue
@@ -108,7 +108,7 @@ const getAuthUserPayload = async ({
108
108
message : 'Failed to authenticate JWT token'
109
109
} ) ;
110
110
}
111
-
111
+
112
112
return user ;
113
113
}
114
114
@@ -130,7 +130,7 @@ const getAuthSTDPayload = async ({
130
130
// TODO: optimize double query
131
131
serviceTokenData = await ServiceTokenData
132
132
. findById ( TOKEN_IDENTIFIER , '+secretHash +expiresAt' ) ;
133
-
133
+
134
134
if ( ! serviceTokenData ) {
135
135
throw ServiceTokenDataNotFoundError ( { message : 'Failed to find service token data' } ) ;
136
136
} else if ( serviceTokenData ?. expiresAt && new Date ( serviceTokenData . expiresAt ) < new Date ( ) ) {
@@ -148,14 +148,14 @@ const getAuthSTDPayload = async ({
148
148
149
149
serviceTokenData = await ServiceTokenData
150
150
. findById ( TOKEN_IDENTIFIER )
151
- . select ( '+encryptedKey +iv +tag' ) ;
151
+ . select ( '+encryptedKey +iv +tag' ) . populate ( 'user' ) ;
152
152
153
153
} catch ( err ) {
154
154
throw UnauthorizedRequestError ( {
155
155
message : 'Failed to authenticate service token'
156
156
} ) ;
157
157
}
158
-
158
+
159
159
return serviceTokenData ;
160
160
}
161
161
@@ -173,11 +173,11 @@ const getAuthAPIKeyPayload = async ({
173
173
let user ;
174
174
try {
175
175
const [ _ , TOKEN_IDENTIFIER , TOKEN_SECRET ] = < [ string , string , string ] > authTokenValue . split ( '.' , 3 ) ;
176
-
176
+
177
177
const apiKeyData = await APIKeyData
178
178
. findById ( TOKEN_IDENTIFIER , '+secretHash +expiresAt' )
179
179
. populate ( 'user' , '+publicKey' ) ;
180
-
180
+
181
181
if ( ! apiKeyData ) {
182
182
throw APIKeyDataNotFoundError ( { message : 'Failed to find API key data' } ) ;
183
183
} else if ( apiKeyData ?. expiresAt && new Date ( apiKeyData . expiresAt ) < new Date ( ) ) {
@@ -192,14 +192,14 @@ const getAuthAPIKeyPayload = async ({
192
192
if ( ! isMatch ) throw UnauthorizedRequestError ( {
193
193
message : 'Failed to authenticate API key'
194
194
} ) ;
195
-
195
+
196
196
user = apiKeyData . user ;
197
197
} catch ( err ) {
198
198
throw UnauthorizedRequestError ( {
199
199
message : 'Failed to authenticate API key'
200
200
} ) ;
201
201
}
202
-
202
+
203
203
return user ;
204
204
}
205
205
@@ -292,12 +292,12 @@ const createToken = ({
292
292
}
293
293
} ;
294
294
295
- export {
295
+ export {
296
296
validateAuthMode ,
297
297
getAuthUserPayload ,
298
298
getAuthSTDPayload ,
299
299
getAuthAPIKeyPayload ,
300
- createToken ,
301
- issueTokens ,
302
- clearTokens
300
+ createToken ,
301
+ issueTokens ,
302
+ clearTokens
303
303
} ;
0 commit comments