-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathentitlements.ts
More file actions
142 lines (124 loc) · 4.1 KB
/
entitlements.ts
File metadata and controls
142 lines (124 loc) · 4.1 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { base64Decode } from "./utils.js";
import { z } from "zod";
import { createLogger } from "./logger.js";
import { env } from "./env.server.js";
import { SOURCEBOT_SUPPORT_EMAIL, SOURCEBOT_UNLIMITED_SEATS } from "./constants.js";
import { verifySignature } from "./crypto.js";
const logger = createLogger('entitlements');
const eeLicenseKeyPrefix = "sourcebot_ee_";
const eeLicenseKeyPayloadSchema = z.object({
id: z.string(),
seats: z.number(),
// ISO 8601 date string
expiryDate: z.string().datetime(),
sig: z.string(),
});
type LicenseKeyPayload = z.infer<typeof eeLicenseKeyPayloadSchema>;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const planLabels = {
oss: "OSS",
"self-hosted:enterprise": "Enterprise (Self-Hosted)",
"self-hosted:enterprise-unlimited": "Enterprise (Self-Hosted) Unlimited",
} as const;
export type Plan = keyof typeof planLabels;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const entitlements = [
"search-contexts",
"billing",
"anonymous-access",
"multi-tenancy",
"sso",
"code-nav",
"audit",
"analytics",
"permission-syncing",
"github-app",
"chat-sharing",
"org-management",
"oauth",
] as const;
export type Entitlement = (typeof entitlements)[number];
const entitlementsByPlan: Record<Plan, Entitlement[]> = {
oss: [
"anonymous-access",
],
"self-hosted:enterprise": [
"search-contexts",
"sso",
"code-nav",
"audit",
"analytics",
"permission-syncing",
"github-app",
"chat-sharing",
"org-management",
"oauth",
],
"self-hosted:enterprise-unlimited": [
"anonymous-access",
"search-contexts",
"sso",
"code-nav",
"audit",
"analytics",
"permission-syncing",
"github-app",
"chat-sharing",
"org-management",
"oauth",
],
} as const;
const decodeLicenseKeyPayload = (payload: string): LicenseKeyPayload => {
try {
const decodedPayload = base64Decode(payload);
const payloadJson = JSON.parse(decodedPayload);
const licenseData = eeLicenseKeyPayloadSchema.parse(payloadJson);
const dataToVerify = JSON.stringify({
expiryDate: licenseData.expiryDate,
id: licenseData.id,
seats: licenseData.seats
});
const isSignatureValid = verifySignature(dataToVerify, licenseData.sig, env.SOURCEBOT_PUBLIC_KEY_PATH);
if (!isSignatureValid) {
logger.error('License key signature verification failed');
process.exit(1);
}
return licenseData;
} catch (error) {
logger.error(`Failed to decode license key payload: ${error}`);
process.exit(1);
}
}
export const getLicenseKey = (): LicenseKeyPayload | null => {
const licenseKey = env.SOURCEBOT_EE_LICENSE_KEY;
if (licenseKey && licenseKey.startsWith(eeLicenseKeyPrefix)) {
const payload = licenseKey.substring(eeLicenseKeyPrefix.length);
return decodeLicenseKeyPayload(payload);
}
return null;
}
export const getPlan = (): Plan => {
const licenseKey = getLicenseKey();
if (licenseKey) {
const expiryDate = new Date(licenseKey.expiryDate);
if (expiryDate.getTime() < new Date().getTime()) {
logger.error(`The provided license key has expired (${expiryDate.toLocaleString()}). Please contact ${SOURCEBOT_SUPPORT_EMAIL} for support.`);
process.exit(1);
}
return licenseKey.seats === SOURCEBOT_UNLIMITED_SEATS ? "self-hosted:enterprise-unlimited" : "self-hosted:enterprise";
} else {
return "oss";
}
}
export const getSeats = (): number => {
const licenseKey = getLicenseKey();
return licenseKey?.seats ?? SOURCEBOT_UNLIMITED_SEATS;
}
export const hasEntitlement = (entitlement: Entitlement) => {
const entitlements = getEntitlements();
return entitlements.includes(entitlement);
}
export const getEntitlements = (): Entitlement[] => {
const plan = getPlan();
return entitlementsByPlan[plan];
}