|
1 | | -import { v4, validate } from "uuid"; |
2 | | -import crypto from "crypto"; |
| 1 | +import { v4 } from "uuid"; |
3 | 2 | import Error from "./Error.js"; |
| 3 | +import { |
| 4 | + decrypt, |
| 5 | + encrypt, |
| 6 | + isAccessToken, |
| 7 | + isUuid, |
| 8 | +} from "../utils/AccessToken.js"; |
4 | 9 |
|
5 | | -const md5 = (text: string) => { |
6 | | - return crypto.createHash("md5").update(text).digest(); |
7 | | -}; |
8 | | - |
9 | | -interface IAccessToken { |
| 10 | +export interface IAccessToken { |
10 | 11 | uuid: typeof v4; |
11 | 12 | createdAt: string; |
12 | 13 | expired: boolean; |
13 | 14 | } |
14 | 15 |
|
15 | | -export function isAccessToken(obj: any): obj is IAccessToken { |
16 | | - if (obj instanceof Object) { |
17 | | - return "uuid" in obj; |
18 | | - } else { |
19 | | - return false; |
20 | | - } |
21 | | -} |
22 | | - |
23 | | -export function isUuid(text: any): text is typeof v4 { |
24 | | - return validate(text); |
25 | | -} |
26 | | - |
27 | | -// for encrypting AccessCode object into a string |
28 | | -export function encrypt(text: string) { |
29 | | - let secretKey = md5(process.env.SECRET as string); |
30 | | - secretKey = Buffer.concat([secretKey, secretKey.subarray(0, 8)]); |
31 | | - const cipher = crypto.createCipheriv("des-ede3", secretKey, ""); |
32 | | - const encrypted = cipher.update(text, "utf8", "hex"); |
33 | | - return encrypted + cipher.final("hex"); |
34 | | -} |
35 | | - |
36 | | -// for decrypting AccessCode string into an object |
37 | | -export function decrypt<T>(text: string) { |
38 | | - let secretKey = md5(process.env.SECRET as string); |
39 | | - secretKey = Buffer.concat([secretKey, secretKey.subarray(0, 8)]); |
40 | | - const decipher = crypto.createDecipheriv("des-ede3", secretKey, ""); |
41 | | - let decrypted = decipher.update(text, "hex"); |
42 | | - // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
43 | | - // @ts-ignore |
44 | | - decrypted += decipher.final(); |
45 | | - return JSON.parse(decrypted as unknown as string) as T; |
46 | | -} |
47 | | - |
48 | 16 | class AccessToken { |
49 | 17 | private uuid: typeof v4; |
50 | 18 | private createdAt: string; |
|
0 commit comments