|
6 | 6 | import { authorizeClient } from "./client.js"; |
7 | 7 | import { authorizeService } from "./service.js"; |
8 | 8 | import type { AuthorizationResult } from "./types.js"; |
9 | | -import { hashKey } from "./utils.js"; |
10 | 9 |
|
11 | 10 | export type AuthorizationInput = { |
12 | 11 | secretKey: string | null; |
@@ -42,16 +41,23 @@ export async function authorize( |
42 | 41 | cacheOptions?: CacheOptions, |
43 | 42 | ): Promise<AuthorizationResult> { |
44 | 43 | let teamAndProjectResponse: TeamAndProjectResponse | null = null; |
45 | | - const cacheKey = hashKey( |
46 | | - `key_v2_:${authData.secretKeyHash}:${authData.hashedJWT}:${authData.clientId}`, |
47 | | - ); |
| 44 | + |
| 45 | + // Use a separate cache key per auth method. |
| 46 | + const cacheKey = authData.secretKeyHash |
| 47 | + ? `key-v2:secret-key:${authData.secretKeyHash}` |
| 48 | + : authData.hashedJWT |
| 49 | + ? `key-v2:dashboard-jwt:${authData.hashedJWT}` |
| 50 | + : authData.clientId |
| 51 | + ? `key-v2:client-id:${authData.clientId}` |
| 52 | + : null; |
| 53 | + |
48 | 54 | // TODO if we have cache options we want to check the cache first |
49 | | - if (cacheOptions) { |
| 55 | + if (cacheOptions && cacheKey) { |
50 | 56 | try { |
51 | | - const cachedKey = await cacheOptions.get(cacheKey); |
52 | | - if (cachedKey) { |
| 57 | + const cachedValue = await cacheOptions.get(cacheKey); |
| 58 | + if (cachedValue) { |
53 | 59 | const parsed = JSON.parse( |
54 | | - cachedKey, |
| 60 | + cachedValue, |
55 | 61 | ) as TeamAndProjectCacheWithPossibleTTL; |
56 | 62 | if ("updatedAt" in parsed) { |
57 | 63 | // we want to compare the updatedAt time to the current time |
@@ -99,7 +105,7 @@ export async function authorize( |
99 | 105 | teamAndProjectResponse = data; |
100 | 106 |
|
101 | 107 | // cache the retrieved key if we have cache options |
102 | | - if (cacheOptions) { |
| 108 | + if (cacheOptions && cacheKey) { |
103 | 109 | // we await this always because it can be a promise or not |
104 | 110 | await cacheOptions.put(cacheKey, data); |
105 | 111 | } |
|
0 commit comments