Download src/utils/generateToken.ts from lynfera/api: direct link, hf CLI and curl.
- Browser
- Download file 2.48 kB
-
https://huggingface.co/spaces/lynfera/api/resolve/9711ca71d84debd7c5d12bfadebe2f693bc677fb/src/utils/generateToken.ts
- Command line
-
hf download hf://spaces/lynfera/api@9711ca71d84debd7c5d12bfadebe2f693bc677fb/src/utils/generateToken.ts
-
curl -L -o generateToken.ts https://huggingface.co/spaces/lynfera/api/resolve/9711ca71d84debd7c5d12bfadebe2f693bc677fb/src/utils/generateToken.ts
2.48 kB
| 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", | |
| }, | |
| ); | |
| } | |