forked from subnub/myDrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthStreamVideo.ts
89 lines (58 loc) · 2.39 KB
/
authStreamVideo.ts
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
import jwt from "jsonwebtoken";
import User, {UserInterface} from "../models/user";
import env from "../enviroment/env";
import {Request, Response, NextFunction} from "express";
import { ObjectID } from "mongodb";
interface RequestType extends Request {
user?: UserInterface,
token?: string,
encryptedToken?: string,
accessTokenStreamVideo?: string
}
type jwtType = {
iv: Buffer,
_id: string,
time: number
}
const removeOldTokens = async(userID: string, uuid: string | undefined, oldTime: number) => {
try {
const minusTime = oldTime - (60 * 1000 * 60 * 24);
uuid = uuid ? uuid : "unknown";
if (uuid === "unknown") return;
await User.updateOne({_id: userID}, {$pull: {tempTokens: {uuid, time: {$lt: minusTime}}}})
} catch (e) {
console.log("cannot remove old tokens", e);
}
}
const authStreamVideo = async(req: RequestType, res: Response, next: NextFunction) => {
try {
const accessTokenStreamVideo = req.cookies["video-access-token"];
const currentUUID = req.headers.uuid as string;
if (!accessTokenStreamVideo) throw new Error("No Access Token");
const decoded = jwt.verify(accessTokenStreamVideo, env.passwordAccess!) as jwtType;
const time = decoded.time;
const user = await User.findById(new ObjectID(decoded._id));
if (!user) throw new Error("No User");
const encrpytionKey = user.getEncryptionKey();
const encryptedToken = user.encryptToken(accessTokenStreamVideo, encrpytionKey, decoded.iv);
let tokenFound = false;
for (let i = 0; i < user.tempTokens.length; i++) {
const currentEncryptedToken = user.tempTokens[i].token;
if (currentEncryptedToken === encryptedToken) {
tokenFound = true;
removeOldTokens(user._id, currentUUID, time);
break;
}
}
if (!tokenFound) throw new Error("Access Token Not Found");
req.user = user;
req.accessTokenStreamVideo = encryptedToken;
next();
} catch (e) {
if (e.message !== "No Access Token" &&
e.message !== "No User" &&
e.message !== "Access Token Not Found") console.log("\nAuthorization Stream Video Middleware Error:", e.message);
res.status(401).send("Error Authenticating");
}
}
export default authStreamVideo;