Spaces:
Sleeping

File size: 2,484 Bytes
4e83772
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a1ffec
4e83772
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a1ffec
4e83772
 
 
 
 
 
 
 
 
 
 
 
 
 
5a1ffec
4e83772
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import jwt, { JwtPayload } from "jsonwebtoken";

import { ENVS } from "@/config/env.config.js";
import AppError from "./AppError.js";
import { githubAppId } from "@/constants/gh.js";
export interface RefreshTokenOptions {
	currentRefresh?: number;
	originalIssuedAt: number;
}
export type TokenType = JwtPayload & {
	id: string;
	plan: string;
};
export type RefresheTokenType = TokenType & {
	crntRfrshCount: number;
	oIAT: number;
};
export const generateAccessToken = (user: Express.User | undefined) => {
	if (!user) {
		throw new Error("Error creating token");
	}
	const token = jwt.sign(
		{
			id: user?.id,
			plan: user?.plan,
		},
		ENVS.ACCESS_TOKEN_SECRET as string,
		{ algorithm: "HS256", expiresIn: ENVS.NODE_ENV === "production" ? "15m" : "2h" },
	);
	return token;
};

export const generateRefreshToken = (user: Express.User | undefined, refreshTokenOptions: RefreshTokenOptions) => {
	if (!user) {
		throw new Error("Error creating token");
	}
	if (refreshTokenOptions.currentRefresh && refreshTokenOptions.currentRefresh >= 10) {
		throw new AppError("Max Refreshes; Please login again", 401);
	}
	const token = jwt.sign(
		{
			id: user?.id,
			plan: user?.plan,
			crntRfrshCount: refreshTokenOptions.currentRefresh,
			oIAT: refreshTokenOptions.originalIssuedAt,
		},
		ENVS.REFRESH_TOKEN_SECRET as string,
		{ algorithm: "HS256", expiresIn: "1d" },
	);
	return token;
};

export const generateOtpToken = (userId: string) => {
	if (!userId) {
		throw new Error("Server error");
	}
	const token = jwt.sign(
		{
			id: userId,
			purpose: "OTP_resend",
		},
		ENVS.VERIFICATION_TOKEN_SECRET as string,
		{ algorithm: "HS256", expiresIn: "20m" },
	);
	return token;
};

export function generateTokenContainerAccessToken(projectId: string, deploymentId: string) {
	return jwt.sign(
		{
			sub: "build-container",
			pId: projectId,
			dId: deploymentId,
		},
		ENVS.SERVICE_JWT_SECRET,
		{
			algorithm: "HS256",
			expiresIn: "8m",
		},
	);
}

export function generateTokenForGithubAppInstallation(userId: string, redirectPath: string) {
	return jwt.sign(
		{
			user: userId,
			redirectPath,
		},
		ENVS.GITHUB_CLIENT_ID + ENVS.GITHUB_WEBHOOK_SECRET,
		{
			algorithm: "HS256",
			expiresIn: "72h",
		},
	);
}
export function generateTokenForGithubAppServer() {
	return jwt.sign(
		{
			iss: githubAppId,
			iat: Math.floor(Date.now() / 1000),
			exp: Math.floor(Date.now() / 1000) + 550,
		},
		ENVS.GITHUB_APP_PRIVATE_KEY,
		{
			algorithm: "RS256",
		},
	);
}