Skip to content

Commit

Permalink
Revised Clash config, added modules.
Browse files Browse the repository at this point in the history
  • Loading branch information
bia-pain-bache committed Oct 31, 2024
1 parent d08d302 commit 130ce5d
Show file tree
Hide file tree
Showing 16 changed files with 5,339 additions and 5,149 deletions.
36 changes: 36 additions & 0 deletions src/authentication/auth.js
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;
}
}
Loading

0 comments on commit 130ce5d

Please sign in to comment.