Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into harshbhat/recoverable…
Browse files Browse the repository at this point in the history
…-keys
  • Loading branch information
harshsbhat committed Sep 18, 2024
2 parents c8d08d0 + bdbae75 commit babbde4
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 53 deletions.
2 changes: 1 addition & 1 deletion apps/api/src/pkg/cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function initCache(c: Context<HonoEnv>, metrics: Metrics): C<CacheNamespa
cloudflareApiKey: c.env.CLOUDFLARE_API_KEY,
zoneId: c.env.CLOUDFLARE_ZONE_ID,
domain: "cache.unkey.dev",
cacheBuster: "v5",
cacheBuster: "v6",
})
: undefined;

Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/pkg/cache/namespaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export type CacheNamespaces = {
api: Api;
permissions: string[];
roles: string[];
ratelimits: { [name: string]: Ratelimit };
ratelimits: { [name: string]: Pick<Ratelimit, "name" | "limit" | "duration"> };
identity: CachedIdentity | null;
} | null;
apiById: (Api & { keyAuth: KeyAuth | null }) | null;
Expand Down
33 changes: 18 additions & 15 deletions apps/api/src/pkg/keys/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,19 @@ export class KeyService {
* Merge ratelimits from the identity and the key
* Key limits take pecedence
*/
const ratelimits: { [name: string]: Ratelimit } = {};
const ratelimits: { [name: string]: Pick<Ratelimit, "name" | "limit" | "duration"> } = {};

if (
dbRes.ratelimitAsync !== null &&
dbRes.ratelimitDuration !== null &&
dbRes.ratelimitLimit !== null
) {
ratelimits.default = {
name: "default",
limit: dbRes.ratelimitLimit,
duration: dbRes.ratelimitDuration,
};
}
for (const rl of dbRes.identity?.ratelimits ?? []) {
ratelimits[rl.name] = rl;
}
Expand Down Expand Up @@ -457,26 +469,17 @@ export class KeyService {
const ratelimits: {
[name: string | "default"]: Required<RatelimitRequest>;
} = {};
if (
data.key.ratelimitAsync !== null &&
data.key.ratelimitDuration !== null &&
data.key.ratelimitLimit !== null
) {
if ("default" in data.ratelimits) {
ratelimits.default = {
identity: data.identity?.id ?? data.key.id,
name: "default",
identity: data.key.id,
name: data.ratelimits.default.name,
cost: req.ratelimit?.cost ?? 1,
limit: data.key.ratelimitLimit,
duration: data.key.ratelimitDuration,
limit: data.ratelimits.default.limit,
duration: data.ratelimits.default.duration,
};
}

for (const r of req.ratelimits ?? []) {
if (r.name === "default" && "default" in ratelimits) {
// it's already added above
continue;
}

if (typeof r.limit !== "undefined" && typeof r.duration !== "undefined") {
ratelimits[r.name] = {
identity: data.identity?.id ?? data.key.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,15 +252,21 @@ export const CreateKey: React.FC<Props> = ({ apiId, keyAuthId }) => {
{key.data ? (
<div className="w-full max-sm:p-4">
<div>
<p className="mb-4 text-xl font-bold">Your API Key</p>
<div className="flex flex-col sm:flex-row justify-between sm:items-center mb-4">
<p className="mb-4 sm:mb-0 text-xl font-bold">Your API Key</p>
<Code className="h-8 w-full sm:w-auto flex gap-1.5 justify-between">
<pre className="truncate">{key.data.keyId}</pre>
<CopyButton value={key.data.keyId} />
</Code>
</div>
<Alert>
<AlertCircle className="w-4 h-4" />
<AlertTitle>This key is only shown once and can not be recovered </AlertTitle>
<AlertDescription>
Please pass it on to your user or store it somewhere safe.
</AlertDescription>
</Alert>
<Code className="flex items-center justify-between w-full gap-4 my-8 ph-no-capture max-sm:text-xs sm:overflow-hidden">
<Code className="flex items-center justify-between w-full gap-4 mt-2 my-8 ph-no-capture max-sm:text-xs sm:overflow-hidden">
<pre>{showKey ? key.data.key : maskedKey}</pre>
<div className="flex items-start justify-between gap-4 max-sm:absolute max-sm:right-11">
<VisibleButton isVisible={showKey} setIsVisible={setShowKey} />
Expand Down
2 changes: 1 addition & 1 deletion apps/www/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"drizzle-orm": "generated",
"framer-motion": "11.0.23",
"fslightbox-react": "^1.7.6",
"fumadocs-core": "^12.5.4",
"fumadocs-core": "^13.4.10",
"geist": "^1.3.0",
"github-slugger": "^2.0.0",
"lucide-react": "^0.378.0",
Expand Down
Loading

0 comments on commit babbde4

Please sign in to comment.