Skip to content

Commit

Permalink
Merge pull request #28 from EATSTEAK/fix/login_block
Browse files Browse the repository at this point in the history
접속을 제한하는 학부가 없는 경우 로그인이 불가능한 문제 수정
  • Loading branch information
EATSTEAK authored Aug 14, 2022
2 parents 1d98b67 + 8d8819a commit dc2086b
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions packages/server/src/auth/data.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import type { ExpressionAttributeValueMap, GetItemInput, UpdateItemInput } from 'aws-sdk/clients/dynamodb';
import type {
ExpressionAttributeValueMap,
GetItemInput,
UpdateItemInput
} from 'aws-sdk/clients/dynamodb';
import { UnauthorizedError } from '../util/error';
import { adminId, dynamoDB, TableName } from '../util/database';

/* ISSUE/REVOKE TOKEN */

export const revokeToken = async function(
export const revokeToken = async function (
id: string,
token: string
): Promise<{ accessToken: string }> {
Expand All @@ -29,27 +33,32 @@ export const revokeToken = async function(
}
};

export const issueToken = async function(
export const issueToken = async function (
id: string,
token: string,
blockedDepartments: Array<string>
): Promise<{ id: string; expires: number }> {
const expires = Date.now() + 3600 * 1000 * 24;
const conditionValues: ExpressionAttributeValueMap = Object.fromEntries(blockedDepartments.map(d => ([`:${d}`, { S: d }])));
const conditionValues: ExpressionAttributeValueMap = Object.fromEntries(
blockedDepartments.map((d) => [`:${d}`, { S: d }])
);
conditionValues[':true'] = { BOOL: true };
const condition = blockedDepartments.map(d => `(NOT d = :${d})`).join(' AND ');
const condition = blockedDepartments.map((d) => `(NOT d = :${d})`).join(' AND ');
const req: UpdateItemInput = {
TableName,
Key: { type: { S: 'user' }, id: { S: `${id}` } },
UpdateExpression: 'SET #aT = :token, eO = :expiresOn',
ExpressionAttributeNames: {
'#aT': 'aT'
},
...(id !== adminId && { ConditionExpression: `iA = :true OR (${condition})` }),
...(id !== adminId &&
condition && {
ConditionExpression: `iA = :true OR (${condition})`
}),
ExpressionAttributeValues: {
':token': { S: token },
':expiresOn': { N: `${expires}` },
...(id !== adminId && conditionValues)
...(id !== adminId && condition && conditionValues)
},
ReturnValues: 'UPDATED_NEW'
};
Expand All @@ -61,7 +70,11 @@ export const issueToken = async function(
}
};

export async function assertAccessible(id: string, token: string, adminOnly = false): Promise<boolean> {
export async function assertAccessible(
id: string,
token: string,
adminOnly = false
): Promise<boolean> {
const authReq: GetItemInput = {
TableName,
Key: {
Expand All @@ -80,4 +93,4 @@ export async function assertAccessible(id: string, token: string, adminOnly = fa
throw new UnauthorizedError('Unauthorized');
}
return true;
}
}

0 comments on commit dc2086b

Please sign in to comment.