-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { AppIdToken } from '@src/core/entities/auth.entity'; | ||
import { AccessLevelEnum, IdpEnum } from '@src/core/types'; | ||
import { isStringEnumValue } from '@src/util/guard'; | ||
import { addTime, getCurrentTimeAsSeconds } from '@src/util/time'; | ||
|
||
export const mrcIssuer = 'movie-reivew-comment'; | ||
|
||
export interface AppPayload { | ||
iss: string; | ||
iat: number; | ||
nbf: number; | ||
exp: number; | ||
userId: string; | ||
nickname: string; | ||
tag: string; | ||
idp: IdpEnum; | ||
email: string; | ||
accessLevel: AccessLevelEnum; | ||
} | ||
|
||
export function createAppPayload( | ||
appIdToken: AppIdToken, | ||
ttl: number | ||
): AppPayload { | ||
const cur = getCurrentTimeAsSeconds(); | ||
const exp = addTime(`${cur}s`, `${ttl}h`, 's'); | ||
|
||
return { | ||
iss: mrcIssuer, | ||
iat: cur, | ||
nbf: cur, | ||
exp: exp, | ||
userId: appIdToken.userId, | ||
nickname: appIdToken.nickname, | ||
tag: appIdToken.tag, | ||
idp: appIdToken.idp, | ||
email: appIdToken.email, | ||
accessLevel: appIdToken.accessLevel | ||
}; | ||
} | ||
|
||
export function converToAppIdToken(appPayload: AppPayload): AppIdToken { | ||
return { | ||
userId: appPayload.userId, | ||
nickname: appPayload.nickname, | ||
tag: appPayload.tag, | ||
idp: appPayload.idp, | ||
email: appPayload.email, | ||
accessLevel: appPayload.accessLevel | ||
}; | ||
} | ||
|
||
export function isAppPayload(payload: unknown): payload is AppPayload { | ||
return ( | ||
typeof payload === 'object' && | ||
payload !== null && | ||
'iss' in payload && | ||
typeof (payload as Record<string, unknown>).iss === 'string' && | ||
'iat' in payload && | ||
typeof (payload as Record<string, unknown>).iat === 'number' && | ||
'nbf' in payload && | ||
typeof (payload as Record<string, unknown>).nbf === 'number' && | ||
'exp' in payload && | ||
typeof (payload as Record<string, unknown>).exp === 'number' && | ||
'userId' in payload && | ||
typeof (payload as Record<string, unknown>).userId === 'string' && | ||
'nickname' in payload && | ||
typeof (payload as Record<string, unknown>).nickname === 'string' && | ||
'tag' in payload && | ||
typeof (payload as Record<string, unknown>).tag === 'string' && | ||
'idp' in payload && | ||
typeof (payload as Record<string, unknown>).idp === 'string' && | ||
isStringEnumValue(payload.idp, IdpEnum) && | ||
'email' in payload && | ||
typeof (payload as Record<string, unknown>).email === 'string' && | ||
'accessLevel' in payload && | ||
typeof (payload as Record<string, unknown>).accessLevel === 'string' && | ||
isStringEnumValue(payload.accessLevel, AccessLevelEnum) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import ms from 'ms'; | ||
|
||
export function getCurrentTimeAsSeconds(): number { | ||
const now = Date.now(); | ||
return Math.floor(now / 1000); | ||
} | ||
|
||
export function addTime(base: string, amount: string, unit: string): number { | ||
const baseMiliSeconds = getTimeAsMiliSeconds(base); | ||
const amountMiliSeconds = getTimeAsMiliSeconds(amount); | ||
const calculatedMiliSeconds = baseMiliSeconds + amountMiliSeconds; | ||
|
||
switch (unit) { | ||
case 'ms': | ||
return calculatedMiliSeconds; | ||
case 's': | ||
return Math.floor(calculatedMiliSeconds / 1000); | ||
case 'm': | ||
return Math.floor(calculatedMiliSeconds / (1000 * 60)); | ||
case 'h': | ||
return Math.floor(calculatedMiliSeconds / (1000 * 60 * 60)); | ||
default: | ||
return calculatedMiliSeconds; | ||
} | ||
} | ||
|
||
export function getTimeAsMiliSeconds(time: string): number { | ||
return ms(time); | ||
} |