Add access control (#65)

This commit is contained in:
Niklas Fondberg
2021-10-11 16:53:18 +02:00
committed by GitHub
parent 1d5d75d354
commit 640849de45
12 changed files with 382 additions and 35 deletions
+35
View File
@@ -0,0 +1,35 @@
import { AuthenticationError } from 'apollo-server';
import { ClaimVerifyResult } from './cognito-client';
export const RESOURCE = 'https://graphql.photowall.com';
export enum SCOPES {
ORDERS_READ = 'orders.read',
ORDERS_WRITE = 'orders.write',
DESIGNERS_READ = 'designers.read',
DESIGNERS_WRITE = 'designers.write',
PRODUCTS_READ = 'products.read',
PRODUCTS_WRITE = 'products.write',
}
export function checkAccess(scopes: string[], auth: ClaimVerifyResult): void {
if (auth.userName === 'testuser') {
return;
}
if (auth.idToken) {
if (auth.idToken['custom:adgroups'].includes('WebAdmin')) {
return;
}
}
if (auth.accessToken) {
const authScopes = auth.accessToken.scope.split(' ');
const requiredScoped = scopes.map((s) => `${RESOURCE}/${s}`);
const checker = (arr: string[], target: string[]) =>
target.every((v) => arr.includes(v));
if (checker(authScopes, requiredScoped)) {
return;
}
}
throw new AuthenticationError('Not authorized for this endpoint');
}