forked from bia-pain-bache/BPB-Worker-Panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
93 lines (72 loc) · 2.96 KB
/
auth.ts
File metadata and controls
93 lines (72 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { SignJWT, jwtVerify } from 'jose';
import { HttpStatus, respond } from '@common';
export async function generateJWTToken(request: Request, env: Env): Promise<Response> {
if (request.method !== 'POST') {
return respond(false, 405, 'Method not allowed.');
}
const password = await request.text();
const savedPass = await env.kv.get('pwd');
if (password !== savedPass) {
return respond(false, HttpStatus.UNAUTHORIZED, 'Wrong password.');
}
let secretKey = await env.kv.get('secretKey');
if (!secretKey) {
secretKey = generateSecretKey();
await env.kv.put('secretKey', secretKey);
}
const secret = new TextEncoder().encode(secretKey);
const { userID } = globalThis.globalConfig;
const jwtToken = await new SignJWT({ userID })
.setProtectedHeader({ alg: 'HS256' })
.setIssuedAt()
.setExpirationTime('24h')
.sign(secret);
return respond(true, HttpStatus.OK, 'Successfully generated Auth token', null, {
'Set-Cookie': `jwtToken=${jwtToken}; HttpOnly; Secure; Max-Age=${7 * 24 * 60 * 60}; Path=/; SameSite=Strict`,
'Content-Type': 'text/plain',
});
}
function generateSecretKey(): string {
const array = new Uint8Array(32);
crypto.getRandomValues(array);
return Array.from(array, byte => byte.toString(16).padStart(2, '0')).join('');
}
export async function Authenticate(request: Request, env: Env): Promise<boolean> {
try {
const secretKey = await env.kv.get('secretKey');
if (secretKey === null) {
console.log("Secret key not found in KV.");
return false;
}
const secret = new TextEncoder().encode(secretKey);
const cookie = request.headers.get('Cookie')?.match(/(^|;\s*)jwtToken=([^;]*)/);
const token = cookie ? cookie[2] : null;
if (!token) {
console.log('Unauthorized: Token not available!');
return false;
}
const { payload } = await jwtVerify(token, secret);
console.log(`Successfully authenticated, User ID: ${payload.userID}`);
return true;
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.log(message);
return false;
}
}
export async function resetPassword(request: Request, env: Env): Promise<Response> {
let auth = await Authenticate(request, env);
const oldPwd = await env.kv.get('pwd');
if (oldPwd && !auth) {
return respond(false, HttpStatus.UNAUTHORIZED, 'Unauthorized.');
}
const newPwd = await request.text();
if (newPwd === oldPwd) {
return respond(false, HttpStatus.BAD_REQUEST, 'Please enter a new Password.');
}
await env.kv.put('pwd', newPwd);
return respond(true, HttpStatus.OK, 'Successfully logged in!', null, {
'Set-Cookie': 'jwtToken=; Path=/; Secure; SameSite=None; Expires=Thu, 01 Jan 1970 00:00:00 GMT',
'Content-Type': 'text/plain',
});
}