Begin service account middleware

This commit is contained in:
Tuan Dang
2023-03-30 23:55:24 +07:00
parent 54e099f8a8
commit 9c18adf35f
31 changed files with 279 additions and 146 deletions
+12 -2
View File
@@ -4,7 +4,8 @@ import {
validateAuthMode,
getAuthUserPayload,
getAuthSTDPayload,
getAuthAPIKeyPayload
getAuthAPIKeyPayload,
getAuthSAAKPayload
} from '../helpers/auth';
import {
UnauthorizedRequestError
@@ -41,14 +42,22 @@ const requireAuth = ({
acceptedAuthModes
});
req.authTokenType = authTokenType;
// attach auth payloads
let serviceTokenData: any;
switch (authTokenType) {
case 'serviceAccount':
req.serviceAccount = await getAuthSAAKPayload({
authTokenValue
});
break;
case 'serviceToken':
serviceTokenData = await getAuthSTDPayload({
authTokenValue
});
// TODO: bring this into a separate collection
requiredServiceTokenPermissions.forEach((requiredServiceTokenPermission) => {
if (!serviceTokenData.permissions.includes(requiredServiceTokenPermission)) {
return next(UnauthorizedRequestError({ message: 'Failed to authorize service token for endpoint' }));
@@ -60,6 +69,7 @@ const requireAuth = ({
break;
case 'apiKey':
// TODO: deprecate API key
req.user = await getAuthAPIKeyPayload({
authTokenValue
});
@@ -70,7 +80,7 @@ const requireAuth = ({
});
break;
}
return next();
}
}