-
Notifications
You must be signed in to change notification settings - Fork 9
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
1 parent
370b701
commit da9f337
Showing
1 changed file
with
35 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,35 @@ | ||
|
||
import { Request, Response, NextFunction } from 'express'; | ||
import { jwtVerify } from 'jose'; | ||
|
||
const JWT_SECRET = process.env.secret_key as string; | ||
|
||
export async function authenticateToken(req: Request, res: Response, next: NextFunction) { | ||
const authHeader = req.headers['authorization']; | ||
if (!authHeader) { | ||
return res.status(401).json({ error: '401.missing-token' }); | ||
} | ||
|
||
const token = authHeader.split(' ')[1]; | ||
if (!token) { | ||
return res.status(401).json({ error: '401.missing-token' }); | ||
} | ||
|
||
try { | ||
const secretKey = new TextEncoder().encode(JWT_SECRET); | ||
const {payload}: any = await jwtVerify(token, secretKey) | ||
|
||
if (typeof payload !== 'object' || !payload) { | ||
return res.status(403).json({ error: '403.invalid-token' }); | ||
} | ||
// const pld = jwtDecrypt(payload) | ||
// const { access_key, secret_key } = payload as { access_key: string, secret_key: string }; | ||
|
||
// Set the Authorization header in the format expected by _createAuthSubject | ||
// req.headers['authorization'] = `Basic ${Buffer.from(`${access_key}:${secret_key}`).toString('base64')}`; | ||
next(); | ||
} catch(err){ | ||
// return res.status(403).json({ error: '403.invalid-token2' }); | ||
console.log(err) | ||
} | ||
} |