-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
64 lines (52 loc) · 1.51 KB
/
index.ts
File metadata and controls
64 lines (52 loc) · 1.51 KB
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
import jwt from "jsonwebtoken";
import {
getClientAppName,
getLoginUri,
getRedirectUri,
SearchParams,
} from "./utils.js";
interface Options {
email: string;
workspace?: string;
privateKey?: string;
}
class NeetoJWT {
private email: string;
private workspace: string;
private privateKey: string;
constructor(options: Options) {
const {
email,
workspace = process.env.NEETO_JWT_WORKSPACE,
privateKey = process.env.NEETO_JWT_PRIVATE_KEY,
} = options || {};
if (!email) throw new Error("Email is required.");
if (!workspace) throw new Error("Workspace is required.");
if (!privateKey) throw new Error("Private key is required.");
this.email = email;
this.workspace = workspace;
this.privateKey = privateKey;
}
generateJWT = () => {
const iat = Math.floor(Date.now() / 1000); // Current date in seconds.
const exp = iat + 2 * 60; // Expiry 2 minutes from now.
const payload = {
email: this.email,
workspace: this.workspace,
iat,
exp,
};
const token = jwt.sign(payload, this.privateKey, { algorithm: "ES256" });
return token;
};
generateLoginUrl = (redirectUri: string) => {
if (!redirectUri) throw new Error("Redirect URI is required");
const searchParams: SearchParams = {
jwt: this.generateJWT(),
redirect_uri: getRedirectUri(redirectUri),
client_app_name: getClientAppName(redirectUri),
};
return getLoginUri(this.workspace, searchParams);
};
}
export default NeetoJWT;