-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddlewares.js
51 lines (38 loc) · 1.3 KB
/
middlewares.js
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
import { DIDSession } from 'did-session'
export const authenticateMiddleware = async (req, res, next) => {
try{
const authHeader = req.headers.authorization;
// Check if the Authorization header is present
if (!authHeader) {
// Authorization header is missing
return next()
}
// Split the Authorization header to extract the token
const parts = authHeader.split(' ');
// Check if the header has the correct format ('Bearer TOKEN')
if (parts.length !== 2 || parts[0] !== 'Bearer') {
return next()
}
// Extract the token
const token = parts[1];
if(token){
const session = await DIDSession.fromSession(token);
await session.did.authenticate()
req.session = session;
req.did = session.did.parent;
console.log("Session Authenticated", req.session.did.parent);
}
} catch (e){
console.log(e)
console.log("Authorization error", e);
}
next();
}
export const authCheckMiddleware = (req, res, next) => {
if(!req.session || req.session.isExpired || !req.session.did.authenticated){
return res.status(401).send({
error: "Authorization error"
});
}
next();
}