forked from bia-pain-bache/BPB-Worker-Panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revised Clash config, added modules.
- Loading branch information
1 parent
d08d302
commit 130ce5d
Showing
16 changed files
with
5,339 additions
and
5,149 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,36 @@ | ||
import { SignJWT, jwtVerify } from 'jose'; | ||
|
||
export async function generateJWTToken (secretKey) { | ||
const secret = new TextEncoder().encode(secretKey); | ||
return await new SignJWT({ userID }) | ||
.setProtectedHeader({ alg: 'HS256' }) | ||
.setIssuedAt() | ||
.setExpirationTime('24h') | ||
.sign(secret); | ||
} | ||
|
||
export function generateSecretKey () { | ||
const key = nacl.randomBytes(32); | ||
return Array.from(key, byte => byte.toString(16).padStart(2, '0')).join(''); | ||
} | ||
|
||
export async function Authenticate (request, env) { | ||
try { | ||
const secretKey = await env.bpb.get('secretKey'); | ||
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) { | ||
console.log(error); | ||
return false; | ||
} | ||
} |
Oops, something went wrong.