Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

접속을 제한하는 학부가 없는 경우 로그인이 불가능한 문제 수정 #28

Merged
merged 1 commit into from
Aug 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}