-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathorganizationAccessToken.server.ts
More file actions
175 lines (148 loc) · 4.68 KB
/
organizationAccessToken.server.ts
File metadata and controls
175 lines (148 loc) · 4.68 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { customAlphabet } from "nanoid";
import { z } from "zod";
import { prisma } from "~/db.server";
import { logger } from "./logger.server";
import { hashToken } from "~/utils/tokens.server";
const tokenValueLength = 40;
//lowercase only, removed 0 and l to avoid confusion
const tokenGenerator = customAlphabet("123456789abcdefghijkmnopqrstuvwxyz", tokenValueLength);
// Skip the lastAccessedAt write if the existing value is already within this
// window. Eliminates per-auth UPDATE churn on a small narrow hot table; the
// settings UI reads this field at human granularity so a few-minute
// staleness is fine.
export const OAT_LAST_ACCESSED_THROTTLE_MS = 5 * 60 * 1000;
type CreateOrganizationAccessTokenOptions = {
name: string;
organizationId: string;
expiresAt?: Date;
};
export async function getValidOrganizationAccessTokens(organizationId: string) {
const organizationAccessTokens = await prisma.organizationAccessToken.findMany({
select: {
id: true,
name: true,
createdAt: true,
lastAccessedAt: true,
expiresAt: true,
},
where: {
organizationId,
revokedAt: null,
OR: [{ expiresAt: null }, { expiresAt: { gte: new Date() } }],
},
});
return organizationAccessTokens.map((oat) => ({
id: oat.id,
name: oat.name,
createdAt: oat.createdAt,
lastAccessedAt: oat.lastAccessedAt,
expiresAt: oat.expiresAt,
}));
}
export type ObfuscatedOrganizationAccessToken = Awaited<
ReturnType<typeof getValidOrganizationAccessTokens>
>[number];
export async function revokeOrganizationAccessToken(tokenId: string) {
await prisma.organizationAccessToken.update({
where: {
id: tokenId,
},
data: {
revokedAt: new Date(),
},
});
}
export type OrganizationAccessTokenAuthenticationResult = {
organizationId: string;
};
const AuthorizationHeaderSchema = z.string().regex(/^Bearer .+$/);
export async function authenticateApiRequestWithOrganizationAccessToken(
request: Request
): Promise<OrganizationAccessTokenAuthenticationResult | undefined> {
const token = getOrganizationAccessTokenFromRequest(request);
if (!token) {
return;
}
return authenticateOrganizationAccessToken(token);
}
function getOrganizationAccessTokenFromRequest(request: Request) {
const rawAuthorization = request.headers.get("Authorization");
const authorization = AuthorizationHeaderSchema.safeParse(rawAuthorization);
if (!authorization.success) {
return;
}
const organizationAccessToken = authorization.data.replace(/^Bearer /, "");
return organizationAccessToken;
}
export async function authenticateOrganizationAccessToken(
token: string
): Promise<OrganizationAccessTokenAuthenticationResult | undefined> {
if (!token.startsWith(tokenPrefix)) {
logger.warn(`OAT doesn't start with ${tokenPrefix}`);
return;
}
const hashedToken = hashToken(token);
const organizationAccessToken = await prisma.organizationAccessToken.findFirst({
where: {
hashedToken,
revokedAt: null,
OR: [{ expiresAt: null }, { expiresAt: { gte: new Date() } }],
},
});
if (!organizationAccessToken) {
return;
}
// Conditional updateMany — only writes if the existing lastAccessedAt is
// null or older than the throttle window. The WHERE runs inside the UPDATE
// so concurrent auths don't race into a double-write. `revokedAt: null`
// matches the findFirst guard above so a token revoked between the read
// and write doesn't get a stale lastAccessedAt update.
await prisma.organizationAccessToken.updateMany({
where: {
id: organizationAccessToken.id,
revokedAt: null,
OR: [
{ lastAccessedAt: null },
{ lastAccessedAt: { lt: new Date(Date.now() - OAT_LAST_ACCESSED_THROTTLE_MS) } },
],
},
data: {
lastAccessedAt: new Date(),
},
});
return {
organizationId: organizationAccessToken.organizationId,
};
}
export function isOrganizationAccessToken(token: string) {
return token.startsWith(tokenPrefix);
}
export async function createOrganizationAccessToken({
name,
organizationId,
expiresAt,
}: CreateOrganizationAccessTokenOptions) {
const token = createToken();
const organizationAccessToken = await prisma.organizationAccessToken.create({
data: {
name,
organizationId,
hashedToken: hashToken(token),
expiresAt,
},
});
return {
id: organizationAccessToken.id,
name,
organizationId,
token,
expiresAt: organizationAccessToken.expiresAt,
};
}
export type CreatedOrganizationAccessToken = Awaited<
ReturnType<typeof createOrganizationAccessToken>
>;
const tokenPrefix = "tr_oat_";
function createToken() {
return `${tokenPrefix}${tokenGenerator()}`;
}