Skip to content

Commit 35f4d27

Browse files
committed
Populate service token user
1 parent cf123d1 commit 35f4d27

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

backend/src/helpers/auth.ts

+18-18
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const validateAuthMode = ({
4141
// case: no auth or X-API-KEY header present
4242
throw BadRequestError({ message: 'Missing Authorization or X-API-KEY in request header.' });
4343
}
44-
44+
4545
if (typeof apiKey === 'string') {
4646
// case: treat request authentication type as via X-API-KEY (i.e. API Key)
4747
authTokenType = 'apiKey';
@@ -50,13 +50,13 @@ const validateAuthMode = ({
5050

5151
if (typeof authHeader === 'string') {
5252
// 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]
5454
if (tokenType === null)
5555
throw BadRequestError({ message: `Missing Authorization Header in the request header.` });
5656
if (tokenType.toLowerCase() !== 'bearer')
5757
throw BadRequestError({ message: `The provided authentication type '${tokenType}' is not supported.` });
5858
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.' });
6060

6161
switch (tokenValue.split('.', 1)[0]) {
6262
case 'st':
@@ -67,11 +67,11 @@ const validateAuthMode = ({
6767
}
6868
authTokenValue = tokenValue;
6969
}
70-
70+
7171
if (!authTokenType || !authTokenValue) throw BadRequestError({ message: 'Missing valid Authorization or X-API-KEY in request header.' });
72-
72+
7373
if (!acceptedAuthModes.includes(authTokenType)) throw BadRequestError({ message: 'The provided authentication type is not supported.' });
74-
74+
7575
return ({
7676
authTokenType,
7777
authTokenValue
@@ -108,7 +108,7 @@ const getAuthUserPayload = async ({
108108
message: 'Failed to authenticate JWT token'
109109
});
110110
}
111-
111+
112112
return user;
113113
}
114114

@@ -130,7 +130,7 @@ const getAuthSTDPayload = async ({
130130
// TODO: optimize double query
131131
serviceTokenData = await ServiceTokenData
132132
.findById(TOKEN_IDENTIFIER, '+secretHash +expiresAt');
133-
133+
134134
if (!serviceTokenData) {
135135
throw ServiceTokenDataNotFoundError({ message: 'Failed to find service token data' });
136136
} else if (serviceTokenData?.expiresAt && new Date(serviceTokenData.expiresAt) < new Date()) {
@@ -148,14 +148,14 @@ const getAuthSTDPayload = async ({
148148

149149
serviceTokenData = await ServiceTokenData
150150
.findById(TOKEN_IDENTIFIER)
151-
.select('+encryptedKey +iv +tag');
151+
.select('+encryptedKey +iv +tag').populate('user');
152152

153153
} catch (err) {
154154
throw UnauthorizedRequestError({
155155
message: 'Failed to authenticate service token'
156156
});
157157
}
158-
158+
159159
return serviceTokenData;
160160
}
161161

@@ -173,11 +173,11 @@ const getAuthAPIKeyPayload = async ({
173173
let user;
174174
try {
175175
const [_, TOKEN_IDENTIFIER, TOKEN_SECRET] = <[string, string, string]>authTokenValue.split('.', 3);
176-
176+
177177
const apiKeyData = await APIKeyData
178178
.findById(TOKEN_IDENTIFIER, '+secretHash +expiresAt')
179179
.populate('user', '+publicKey');
180-
180+
181181
if (!apiKeyData) {
182182
throw APIKeyDataNotFoundError({ message: 'Failed to find API key data' });
183183
} else if (apiKeyData?.expiresAt && new Date(apiKeyData.expiresAt) < new Date()) {
@@ -192,14 +192,14 @@ const getAuthAPIKeyPayload = async ({
192192
if (!isMatch) throw UnauthorizedRequestError({
193193
message: 'Failed to authenticate API key'
194194
});
195-
195+
196196
user = apiKeyData.user;
197197
} catch (err) {
198198
throw UnauthorizedRequestError({
199199
message: 'Failed to authenticate API key'
200200
});
201201
}
202-
202+
203203
return user;
204204
}
205205

@@ -292,12 +292,12 @@ const createToken = ({
292292
}
293293
};
294294

295-
export {
295+
export {
296296
validateAuthMode,
297297
getAuthUserPayload,
298298
getAuthSTDPayload,
299299
getAuthAPIKeyPayload,
300-
createToken,
301-
issueTokens,
302-
clearTokens
300+
createToken,
301+
issueTokens,
302+
clearTokens
303303
};

backend/src/middleware/requireServiceTokenDataAuth.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ const requireServiceTokenDataAuth = ({
1717

1818
const serviceTokenData = await ServiceTokenData
1919
.findById(req[location].serviceTokenDataId)
20-
.select('+encryptedKey +iv +tag');
20+
.select('+encryptedKey +iv +tag').populate('user');
2121

2222
if (!serviceTokenData) {
23-
return next(AccountNotFoundError({message: 'Failed to locate service token data'}));
23+
return next(AccountNotFoundError({ message: 'Failed to locate service token data' }));
2424
}
2525

2626
if (req.user) {
@@ -31,9 +31,9 @@ const requireServiceTokenDataAuth = ({
3131
acceptedRoles
3232
});
3333
}
34-
34+
3535
req.serviceTokenData = serviceTokenData;
36-
36+
3737
next();
3838
}
3939
}

0 commit comments

Comments
 (0)