Skip to content

Commit de039ef

Browse files
authored
[service-utils] fix: Don't hash cache key (#6023)
1 parent 35fd3bc commit de039ef

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

.changeset/rotten-vans-sleep.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@thirdweb-dev/service-utils": patch
3+
---
4+
5+
Use a cache key that doesn't involve hashing

packages/service-utils/src/core/authorize/index.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
import { authorizeClient } from "./client.js";
77
import { authorizeService } from "./service.js";
88
import type { AuthorizationResult } from "./types.js";
9-
import { hashKey } from "./utils.js";
109

1110
export type AuthorizationInput = {
1211
secretKey: string | null;
@@ -42,16 +41,23 @@ export async function authorize(
4241
cacheOptions?: CacheOptions,
4342
): Promise<AuthorizationResult> {
4443
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+
4854
// TODO if we have cache options we want to check the cache first
49-
if (cacheOptions) {
55+
if (cacheOptions && cacheKey) {
5056
try {
51-
const cachedKey = await cacheOptions.get(cacheKey);
52-
if (cachedKey) {
57+
const cachedValue = await cacheOptions.get(cacheKey);
58+
if (cachedValue) {
5359
const parsed = JSON.parse(
54-
cachedKey,
60+
cachedValue,
5561
) as TeamAndProjectCacheWithPossibleTTL;
5662
if ("updatedAt" in parsed) {
5763
// we want to compare the updatedAt time to the current time
@@ -99,7 +105,7 @@ export async function authorize(
99105
teamAndProjectResponse = data;
100106

101107
// cache the retrieved key if we have cache options
102-
if (cacheOptions) {
108+
if (cacheOptions && cacheKey) {
103109
// we await this always because it can be a promise or not
104110
await cacheOptions.put(cacheKey, data);
105111
}

packages/service-utils/src/core/authorize/utils.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)