1
- import { type OrganizationAccessToken } from "@trigger.dev/database" ;
2
1
import { customAlphabet } from "nanoid" ;
3
2
import { z } from "zod" ;
4
3
import { prisma } from "~/db.server" ;
5
4
import { logger } from "./logger.server" ;
6
- import { decryptToken , encryptToken , hashToken } from "~/utils/tokens.server" ;
7
- import { env } from "~/env.server" ;
5
+ import { hashToken } from "~/utils/tokens.server" ;
8
6
9
7
const tokenValueLength = 40 ;
10
8
//lowercase only, removed 0 and l to avoid confusion
@@ -21,7 +19,6 @@ export async function getValidOrganizationAccessTokens(organizationId: string) {
21
19
select : {
22
20
id : true ,
23
21
name : true ,
24
- obfuscatedToken : true ,
25
22
createdAt : true ,
26
23
lastAccessedAt : true ,
27
24
expiresAt : true ,
@@ -36,7 +33,6 @@ export async function getValidOrganizationAccessTokens(organizationId: string) {
36
33
return organizationAccessTokens . map ( ( oat ) => ( {
37
34
id : oat . id ,
38
35
name : oat . name ,
39
- obfuscatedToken : oat . obfuscatedToken ,
40
36
createdAt : oat . createdAt ,
41
37
lastAccessedAt : oat . lastAccessedAt ,
42
38
expiresAt : oat . expiresAt ,
@@ -63,12 +59,6 @@ export type OrganizationAccessTokenAuthenticationResult = {
63
59
organizationId : string ;
64
60
} ;
65
61
66
- const EncryptedSecretValueSchema = z . object ( {
67
- nonce : z . string ( ) ,
68
- ciphertext : z . string ( ) ,
69
- tag : z . string ( ) ,
70
- } ) ;
71
-
72
62
const AuthorizationHeaderSchema = z . string ( ) . regex ( / ^ B e a r e r .+ $ / ) ;
73
63
74
64
export async function authenticateApiRequestWithOrganizationAccessToken (
@@ -125,15 +115,6 @@ export async function authenticateOrganizationAccessToken(
125
115
} ,
126
116
} ) ;
127
117
128
- const decryptedToken = decryptOrganizationAccessToken ( organizationAccessToken ) ;
129
-
130
- if ( decryptedToken !== token ) {
131
- logger . error (
132
- `OrganizationAccessToken with id: ${ organizationAccessToken . id } was found in the database with hash ${ hashedToken } , but the decrypted token did not match the provided token.`
133
- ) ;
134
- return ;
135
- }
136
-
137
118
return {
138
119
organizationId : organizationAccessToken . organizationId ,
139
120
} ;
@@ -149,14 +130,11 @@ export async function createOrganizationAccessToken({
149
130
expiresAt,
150
131
} : CreateOrganizationAccessTokenOptions ) {
151
132
const token = createToken ( ) ;
152
- const encryptedToken = encryptToken ( token , env . ENCRYPTION_KEY ) ;
153
133
154
134
const organizationAccessToken = await prisma . organizationAccessToken . create ( {
155
135
data : {
156
136
name,
157
137
organizationId,
158
- encryptedToken,
159
- obfuscatedToken : obfuscateToken ( token ) ,
160
138
hashedToken : hashToken ( token ) ,
161
139
expiresAt,
162
140
} ,
@@ -167,7 +145,6 @@ export async function createOrganizationAccessToken({
167
145
name,
168
146
organizationId,
169
147
token,
170
- obfuscatedToken : organizationAccessToken . obfuscatedToken ,
171
148
expiresAt : organizationAccessToken . expiresAt ,
172
149
} ;
173
150
}
@@ -181,28 +158,3 @@ const tokenPrefix = "tr_oat_";
181
158
function createToken ( ) {
182
159
return `${ tokenPrefix } ${ tokenGenerator ( ) } ` ;
183
160
}
184
-
185
- function obfuscateToken ( token : string ) {
186
- const withoutPrefix = token . replace ( tokenPrefix , "" ) ;
187
- const obfuscated = `${ withoutPrefix . slice ( 0 , 4 ) } ${ "•" . repeat ( 18 ) } ${ withoutPrefix . slice ( - 4 ) } ` ;
188
- return `${ tokenPrefix } ${ obfuscated } ` ;
189
- }
190
-
191
- function decryptOrganizationAccessToken ( organizationAccessToken : OrganizationAccessToken ) {
192
- const encryptedData = EncryptedSecretValueSchema . safeParse (
193
- organizationAccessToken . encryptedToken
194
- ) ;
195
- if ( ! encryptedData . success ) {
196
- throw new Error (
197
- `Unable to parse encrypted OrganizationAccessToken with id: ${ organizationAccessToken . id } : ${ encryptedData . error . message } `
198
- ) ;
199
- }
200
-
201
- const decryptedToken = decryptToken (
202
- encryptedData . data . nonce ,
203
- encryptedData . data . ciphertext ,
204
- encryptedData . data . tag ,
205
- env . ENCRYPTION_KEY
206
- ) ;
207
- return decryptedToken ;
208
- }
0 commit comments