@krak-stack/auth 0.1.56 → 0.1.58
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/access.d.ts +93 -0
- package/dist/access.js +97 -0
- package/dist/admin/api.group.d.ts +7 -0
- package/dist/admin/api.group.js +1 -1
- package/dist/admin/schema.d.ts +2 -0
- package/dist/admin/schema.js +4 -0
- package/dist/api.d.ts +23 -0
- package/dist/components/api-key-edit-form.d.ts +20 -0
- package/dist/components/api-key-edit-form.js +50 -0
- package/dist/components/api-key-rate-limit.d.ts +18 -0
- package/dist/components/api-key-rate-limit.js +29 -0
- package/dist/components/api-key-referrers.d.ts +8 -0
- package/dist/components/api-key-referrers.js +24 -0
- package/dist/components/api-key.d.ts +1 -0
- package/dist/components/api-key.js +20 -1
- package/dist/components/auth-client-api.d.ts +15 -0
- package/dist/components/auth-provider.d.ts +11 -1
- package/dist/components/auth-provider.js +29 -1
- package/dist/components/index.d.ts +3 -1
- package/dist/components/index.js +2 -1
- package/dist/components/organization-switcher.d.ts +58 -3
- package/dist/components/organization-switcher.js +253 -70
- package/dist/components/user-button.d.ts +41 -3
- package/dist/components/user-button.js +134 -33
- package/dist/extra/api.group.d.ts +112 -103
- package/dist/extra/api.group.js +20 -1
- package/dist/extra/schema.d.ts +7 -0
- package/dist/extra/schema.js +18 -0
- package/dist/server/index.d.ts +2 -0
- package/dist/server/index.js +1 -0
- package/dist/service.d.ts +26 -0
- package/package.json +5 -1
package/dist/access.d.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { Context, Effect, Schema } from "effect";
|
|
2
|
+
export declare const ApiKeyPermissionGrant: Schema.$Record<Schema.String, Schema.$Array<Schema.String>>;
|
|
3
|
+
export type ApiKeyPermissionGrant = typeof ApiKeyPermissionGrant.Type;
|
|
4
|
+
export type UserPrincipal = {
|
|
5
|
+
readonly type: "user";
|
|
6
|
+
readonly userId: string;
|
|
7
|
+
readonly organizationId: string | null;
|
|
8
|
+
readonly roles: ReadonlyArray<string>;
|
|
9
|
+
};
|
|
10
|
+
export type ApiKeyOwner = {
|
|
11
|
+
readonly type: "user";
|
|
12
|
+
readonly userId: string;
|
|
13
|
+
readonly organizationId: string | null;
|
|
14
|
+
readonly roles: ReadonlyArray<string>;
|
|
15
|
+
} | {
|
|
16
|
+
readonly type: "organization";
|
|
17
|
+
readonly organizationId: string;
|
|
18
|
+
} | {
|
|
19
|
+
readonly type: "service";
|
|
20
|
+
readonly serviceId: string;
|
|
21
|
+
};
|
|
22
|
+
export type ApiKeyPrincipal = {
|
|
23
|
+
readonly type: "apiKey";
|
|
24
|
+
readonly apiKeyId: string;
|
|
25
|
+
readonly owner: ApiKeyOwner;
|
|
26
|
+
};
|
|
27
|
+
export type Principal = UserPrincipal | ApiKeyPrincipal;
|
|
28
|
+
export type CurrentPrincipal = {
|
|
29
|
+
readonly principal: Principal;
|
|
30
|
+
readonly organizationId: string | null;
|
|
31
|
+
readonly permissions: ReadonlySet<string>;
|
|
32
|
+
};
|
|
33
|
+
export declare const CurrentPrincipal: Context.Service<CurrentPrincipal, CurrentPrincipal>;
|
|
34
|
+
declare const Forbidden_base: Schema.Class<Forbidden, Schema.Struct<{
|
|
35
|
+
readonly _tag: Schema.tag<"Forbidden">;
|
|
36
|
+
}>, import("effect/Cause").YieldableError>;
|
|
37
|
+
export declare class Forbidden extends Forbidden_base {
|
|
38
|
+
}
|
|
39
|
+
export type Policy<E = never, R = never> = Effect.Effect<void, Forbidden | E, CurrentPrincipal | R>;
|
|
40
|
+
export declare const policy: <E, R>(predicate: (principal: CurrentPrincipal) => Effect.Effect<boolean, E, R>) => Policy<E, R>;
|
|
41
|
+
export declare const permission: (requiredPermission: string) => Policy;
|
|
42
|
+
export declare const all: <E, R>(...policies: readonly [Policy<E, R>, ...Array<Policy<E, R>>]) => Policy<E, R>;
|
|
43
|
+
export declare const any: <E, R>(...policies: readonly [Policy<E, R>, ...Array<Policy<E, R>>]) => Policy<E, R>;
|
|
44
|
+
export declare const withPolicy: <E, R>(accessPolicy: Policy<E, R>) => <A, E2, R2>(self: Effect.Effect<A, E2, R2>) => Effect.Effect<A, E | E2 | Forbidden, R | R2 | CurrentPrincipal>;
|
|
45
|
+
type ProjectPermission<Project extends string, Action extends string> = `${Project}:${Action}`;
|
|
46
|
+
export type ProjectAccessDefinition<Project extends string = string, Action extends string = string> = {
|
|
47
|
+
readonly project: Project;
|
|
48
|
+
readonly actions: ReadonlyArray<Action>;
|
|
49
|
+
readonly roles: Readonly<Record<string, ReadonlyArray<Action> | undefined>>;
|
|
50
|
+
readonly apiKeys: {
|
|
51
|
+
readonly user: ReadonlyArray<Action>;
|
|
52
|
+
readonly organization: ReadonlyArray<Action>;
|
|
53
|
+
};
|
|
54
|
+
readonly qualify: (action: Action) => ProjectPermission<Project, Action>;
|
|
55
|
+
readonly permission: (action: Action) => Policy;
|
|
56
|
+
readonly permissionsForRoles: (roles: unknown) => ReadonlySet<ProjectPermission<Project, Action>>;
|
|
57
|
+
readonly permissionsForGrant: (grant: ApiKeyPermissionGrant) => ReadonlySet<ProjectPermission<Project, Action>>;
|
|
58
|
+
readonly permissionsForUserKey: (input: {
|
|
59
|
+
readonly grant: ApiKeyPermissionGrant;
|
|
60
|
+
readonly roles: unknown;
|
|
61
|
+
}) => ReadonlySet<ProjectPermission<Project, Action>>;
|
|
62
|
+
readonly decodeGrant: (input: unknown) => Effect.Effect<ReadonlySet<ProjectPermission<Project, Action>>, Schema.SchemaError>;
|
|
63
|
+
readonly encodeGrant: (actions: ReadonlyArray<Action>) => ApiKeyPermissionGrant;
|
|
64
|
+
readonly userApiKeyPermissions: ApiKeyPermissionGrant;
|
|
65
|
+
readonly organizationApiKeyPermissions: ApiKeyPermissionGrant;
|
|
66
|
+
readonly principalForUser: (input: {
|
|
67
|
+
readonly userId: string;
|
|
68
|
+
readonly organizationId: string | null;
|
|
69
|
+
readonly roles: unknown;
|
|
70
|
+
}) => CurrentPrincipal;
|
|
71
|
+
readonly principalForApiKey: (input: {
|
|
72
|
+
readonly apiKeyId: string;
|
|
73
|
+
readonly owner: ApiKeyOwner;
|
|
74
|
+
readonly grant: ApiKeyPermissionGrant;
|
|
75
|
+
}) => CurrentPrincipal;
|
|
76
|
+
};
|
|
77
|
+
export type ProjectAccessCatalog = {
|
|
78
|
+
readonly project: string;
|
|
79
|
+
readonly roles: Readonly<Record<string, ReadonlyArray<string> | undefined>>;
|
|
80
|
+
readonly userApiKeyPermissions: ApiKeyPermissionGrant;
|
|
81
|
+
readonly organizationApiKeyPermissions: ApiKeyPermissionGrant;
|
|
82
|
+
};
|
|
83
|
+
export type ProjectAccessConfig<Project extends string, Action extends string> = {
|
|
84
|
+
readonly project: Project;
|
|
85
|
+
readonly permissions: ReadonlyArray<Action>;
|
|
86
|
+
readonly roles: Readonly<Record<string, ReadonlyArray<Action> | undefined>>;
|
|
87
|
+
readonly apiKeys?: {
|
|
88
|
+
readonly user?: ReadonlyArray<Action>;
|
|
89
|
+
readonly organization?: ReadonlyArray<Action>;
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
export declare const defineProjectAccess: <const Project extends string, const Action extends string>(config: ProjectAccessConfig<Project, Action>) => ProjectAccessDefinition<Project, Action>;
|
|
93
|
+
export {};
|
package/dist/access.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { Context, Effect, Schema } from "effect";
|
|
2
|
+
import { parseRoleList } from "./roles";
|
|
3
|
+
export const ApiKeyPermissionGrant = Schema.Record(Schema.String, Schema.Array(Schema.String)).annotate({
|
|
4
|
+
identifier: "ApiKeyPermissionGrant",
|
|
5
|
+
title: "API key permission grant",
|
|
6
|
+
description: "Better Auth resource and action permissions assigned to a key.",
|
|
7
|
+
});
|
|
8
|
+
export const CurrentPrincipal = Context.Service("@krak-stack/auth/CurrentPrincipal");
|
|
9
|
+
export class Forbidden extends Schema.ErrorClass("Forbidden")({
|
|
10
|
+
_tag: Schema.tag("Forbidden"),
|
|
11
|
+
}, {
|
|
12
|
+
identifier: "Forbidden",
|
|
13
|
+
title: "Forbidden",
|
|
14
|
+
description: "The authenticated principal lacks a required permission.",
|
|
15
|
+
httpApiStatus: 403,
|
|
16
|
+
}) {
|
|
17
|
+
}
|
|
18
|
+
export const policy = (predicate) => Effect.flatMap(CurrentPrincipal, (principal) => Effect.flatMap(predicate(principal), (allowed) => allowed ? Effect.void : Effect.fail(new Forbidden())));
|
|
19
|
+
export const permission = (requiredPermission) => policy((principal) => Effect.succeed(principal.permissions.has(requiredPermission)));
|
|
20
|
+
export const all = (...policies) => Effect.all(policies, { concurrency: 1, discard: true });
|
|
21
|
+
export const any = (...policies) => Effect.firstSuccessOf(policies);
|
|
22
|
+
export const withPolicy = (accessPolicy) => (self) => Effect.andThen(accessPolicy, self);
|
|
23
|
+
const intersection = (left, right) => new Set(Array.from(left).filter((item) => right.has(item)));
|
|
24
|
+
const roleList = (input) => Array.isArray(input)
|
|
25
|
+
? input.flatMap((role) => parseRoleList(role))
|
|
26
|
+
: parseRoleList(input);
|
|
27
|
+
export const defineProjectAccess = (config) => {
|
|
28
|
+
const allowedActions = new Set(config.permissions);
|
|
29
|
+
const qualify = (action) => `${config.project}:${action}`;
|
|
30
|
+
const permissionsForActions = (actions) => new Set(Array.from(actions, (action) => qualify(action)));
|
|
31
|
+
const permissionsForRoles = (input) => {
|
|
32
|
+
const actions = new Set();
|
|
33
|
+
for (const role of roleList(input)) {
|
|
34
|
+
for (const action of config.roles[role] ?? [])
|
|
35
|
+
actions.add(action);
|
|
36
|
+
}
|
|
37
|
+
return permissionsForActions(actions);
|
|
38
|
+
};
|
|
39
|
+
const permissionsForGrant = (grant) => {
|
|
40
|
+
const actions = (grant[config.project] ?? []).filter((action) => allowedActions.has(action));
|
|
41
|
+
return permissionsForActions(actions);
|
|
42
|
+
};
|
|
43
|
+
const encodeGrant = (actions) => ({
|
|
44
|
+
[config.project]: actions.filter((action) => allowedActions.has(action)),
|
|
45
|
+
});
|
|
46
|
+
const userApiKeyActions = config.apiKeys?.user ?? [];
|
|
47
|
+
const organizationApiKeyActions = config.apiKeys?.organization ?? [];
|
|
48
|
+
const principalForUser = (input) => {
|
|
49
|
+
const roles = roleList(input.roles);
|
|
50
|
+
return {
|
|
51
|
+
principal: {
|
|
52
|
+
type: "user",
|
|
53
|
+
userId: input.userId,
|
|
54
|
+
organizationId: input.organizationId,
|
|
55
|
+
roles,
|
|
56
|
+
},
|
|
57
|
+
organizationId: input.organizationId,
|
|
58
|
+
permissions: permissionsForRoles(roles),
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
const principalForApiKey = (input) => {
|
|
62
|
+
const keyPermissions = permissionsForGrant(input.grant);
|
|
63
|
+
const permissions = input.owner.type === "user"
|
|
64
|
+
? intersection(permissionsForRoles(input.owner.roles), keyPermissions)
|
|
65
|
+
: keyPermissions;
|
|
66
|
+
const organizationId = input.owner.type === "service" ? null : input.owner.organizationId;
|
|
67
|
+
return {
|
|
68
|
+
principal: {
|
|
69
|
+
type: "apiKey",
|
|
70
|
+
apiKeyId: input.apiKeyId,
|
|
71
|
+
owner: input.owner,
|
|
72
|
+
},
|
|
73
|
+
organizationId,
|
|
74
|
+
permissions,
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
return {
|
|
78
|
+
project: config.project,
|
|
79
|
+
actions: config.permissions,
|
|
80
|
+
roles: config.roles,
|
|
81
|
+
apiKeys: {
|
|
82
|
+
user: userApiKeyActions,
|
|
83
|
+
organization: organizationApiKeyActions,
|
|
84
|
+
},
|
|
85
|
+
qualify,
|
|
86
|
+
permission: (action) => permission(qualify(action)),
|
|
87
|
+
permissionsForRoles,
|
|
88
|
+
permissionsForGrant,
|
|
89
|
+
permissionsForUserKey: ({ grant, roles }) => intersection(permissionsForRoles(roles), permissionsForGrant(grant)),
|
|
90
|
+
decodeGrant: (input) => Schema.decodeUnknownEffect(ApiKeyPermissionGrant)(input).pipe(Effect.map(permissionsForGrant)),
|
|
91
|
+
encodeGrant,
|
|
92
|
+
userApiKeyPermissions: encodeGrant(userApiKeyActions),
|
|
93
|
+
organizationApiKeyPermissions: encodeGrant(organizationApiKeyActions),
|
|
94
|
+
principalForUser,
|
|
95
|
+
principalForApiKey,
|
|
96
|
+
};
|
|
97
|
+
};
|
|
@@ -88,6 +88,7 @@ export declare const AdminApiGroup: HttpApiGroup.HttpApiGroup<"admin", HttpApiEn
|
|
|
88
88
|
readonly remaining: Schema.NullOr<Schema.Number>;
|
|
89
89
|
readonly lastRequest: Schema.NullOr<Schema.Date>;
|
|
90
90
|
readonly expiresAt: Schema.NullOr<Schema.Date>;
|
|
91
|
+
readonly referrers: Schema.$Array<Schema.String>;
|
|
91
92
|
readonly createdAt: Schema.Date;
|
|
92
93
|
readonly updatedAt: Schema.Date;
|
|
93
94
|
}>>, HttpApiEndpoint.Json<typeof HttpApiError.Unauthorized | typeof HttpApiError.Forbidden | typeof HttpApiError.NotFound | typeof HttpApiError.InternalServerError>, never, never> | HttpApiEndpoint.HttpApiEndpoint<"deleteDomain", "DELETE", "/admin/domains/:id", HttpApiEndpoint.StringTree<Schema.Struct<{
|
|
@@ -146,6 +147,7 @@ export declare const AdminApiGroup: HttpApiGroup.HttpApiGroup<"admin", HttpApiEn
|
|
|
146
147
|
readonly remaining: Schema.NullOr<Schema.Number>;
|
|
147
148
|
readonly lastRequest: Schema.NullOr<Schema.Date>;
|
|
148
149
|
readonly expiresAt: Schema.NullOr<Schema.Date>;
|
|
150
|
+
readonly referrers: Schema.$Array<Schema.String>;
|
|
149
151
|
readonly createdAt: Schema.Date;
|
|
150
152
|
readonly updatedAt: Schema.Date;
|
|
151
153
|
}>>, HttpApiEndpoint.Json<typeof HttpApiError.Unauthorized | typeof HttpApiError.Forbidden | typeof HttpApiError.NotFound | typeof HttpApiError.InternalServerError>, never, never> | HttpApiEndpoint.HttpApiEndpoint<"enableApiKeyRateLimit", "POST", "/admin/api-keys/:id/enable-rate-limit", HttpApiEndpoint.StringTree<Schema.Struct<{
|
|
@@ -165,6 +167,7 @@ export declare const AdminApiGroup: HttpApiGroup.HttpApiGroup<"admin", HttpApiEn
|
|
|
165
167
|
readonly remaining: Schema.NullOr<Schema.Number>;
|
|
166
168
|
readonly lastRequest: Schema.NullOr<Schema.Date>;
|
|
167
169
|
readonly expiresAt: Schema.NullOr<Schema.Date>;
|
|
170
|
+
readonly referrers: Schema.$Array<Schema.String>;
|
|
168
171
|
readonly createdAt: Schema.Date;
|
|
169
172
|
readonly updatedAt: Schema.Date;
|
|
170
173
|
}>>, HttpApiEndpoint.Json<typeof HttpApiError.Unauthorized | typeof HttpApiError.Forbidden | typeof HttpApiError.NotFound | typeof HttpApiError.InternalServerError>, never, never> | HttpApiEndpoint.HttpApiEndpoint<"getDomainRecords", "GET", "/admin/domains/:id/records", HttpApiEndpoint.StringTree<Schema.Struct<{
|
|
@@ -190,6 +193,7 @@ export declare const AdminApiGroup: HttpApiGroup.HttpApiGroup<"admin", HttpApiEn
|
|
|
190
193
|
readonly remaining: Schema.NullOr<Schema.Number>;
|
|
191
194
|
readonly lastRequest: Schema.NullOr<Schema.Date>;
|
|
192
195
|
readonly expiresAt: Schema.NullOr<Schema.Date>;
|
|
196
|
+
readonly referrers: Schema.$Array<Schema.String>;
|
|
193
197
|
readonly createdAt: Schema.Date;
|
|
194
198
|
readonly updatedAt: Schema.Date;
|
|
195
199
|
}>>>, HttpApiEndpoint.Json<typeof HttpApiError.Unauthorized | typeof HttpApiError.Forbidden | typeof HttpApiError.InternalServerError>, never, never> | HttpApiEndpoint.HttpApiEndpoint<"listDomains", "GET", "/admin/domains", HttpApiEndpoint.StringTree<never>, HttpApiEndpoint.StringTree<never>, HttpApiEndpoint.StringTree<never>, HttpApiEndpoint.StringTree<never>, HttpApiEndpoint.Json<Schema.$Array<Schema.Struct<{
|
|
@@ -300,6 +304,7 @@ export declare const AdminApiGroup: HttpApiGroup.HttpApiGroup<"admin", HttpApiEn
|
|
|
300
304
|
readonly remaining: Schema.NullOr<Schema.Number>;
|
|
301
305
|
readonly lastRequest: Schema.NullOr<Schema.Date>;
|
|
302
306
|
readonly expiresAt: Schema.NullOr<Schema.Date>;
|
|
307
|
+
readonly referrers: Schema.$Array<Schema.String>;
|
|
303
308
|
readonly createdAt: Schema.Date;
|
|
304
309
|
readonly updatedAt: Schema.Date;
|
|
305
310
|
}>>, HttpApiEndpoint.Json<typeof HttpApiError.Unauthorized | typeof HttpApiError.Forbidden | typeof HttpApiError.NotFound | typeof HttpApiError.InternalServerError>, never, never> | HttpApiEndpoint.HttpApiEndpoint<"updateApiKey", "PATCH", "/admin/api-keys/:id", HttpApiEndpoint.StringTree<Schema.Struct<{
|
|
@@ -310,6 +315,7 @@ export declare const AdminApiGroup: HttpApiGroup.HttpApiGroup<"admin", HttpApiEn
|
|
|
310
315
|
readonly rateLimitEnabled: Schema.optional<Schema.Boolean>;
|
|
311
316
|
readonly rateLimitMax: Schema.optional<Schema.NullOr<Schema.Number>>;
|
|
312
317
|
readonly rateLimitTimeWindow: Schema.optional<Schema.NullOr<Schema.Number>>;
|
|
318
|
+
readonly referrers: Schema.optional<Schema.$Array<Schema.String>>;
|
|
313
319
|
}>>, HttpApiEndpoint.StringTree<never>, HttpApiEndpoint.Json<Schema.Struct<{
|
|
314
320
|
readonly id: Schema.String;
|
|
315
321
|
readonly configId: Schema.String;
|
|
@@ -325,6 +331,7 @@ export declare const AdminApiGroup: HttpApiGroup.HttpApiGroup<"admin", HttpApiEn
|
|
|
325
331
|
readonly remaining: Schema.NullOr<Schema.Number>;
|
|
326
332
|
readonly lastRequest: Schema.NullOr<Schema.Date>;
|
|
327
333
|
readonly expiresAt: Schema.NullOr<Schema.Date>;
|
|
334
|
+
readonly referrers: Schema.$Array<Schema.String>;
|
|
328
335
|
readonly createdAt: Schema.Date;
|
|
329
336
|
readonly updatedAt: Schema.Date;
|
|
330
337
|
}>>, HttpApiEndpoint.Json<typeof HttpApiError.Unauthorized | typeof HttpApiError.Forbidden | typeof HttpApiError.NotFound | typeof HttpApiError.InternalServerError>, never, never> | HttpApiEndpoint.HttpApiEndpoint<"updateDomain", "PATCH", "/admin/domains/:id", HttpApiEndpoint.StringTree<Schema.Struct<{
|
package/dist/admin/api.group.js
CHANGED
|
@@ -55,7 +55,7 @@ export const AdminApiGroup = HttpApiGroup.make("admin")
|
|
|
55
55
|
}).annotateMerge(OpenApi.annotations({
|
|
56
56
|
title: "Update API key",
|
|
57
57
|
summary: "Update an API key",
|
|
58
|
-
description: "Updates API key metadata and rate-limit settings for authenticated administrators.",
|
|
58
|
+
description: "Updates API key metadata, referrer restrictions, and rate-limit settings for authenticated administrators.",
|
|
59
59
|
})))
|
|
60
60
|
.add(HttpApiEndpoint.post("resetApiKeyRateLimit", "/admin/api-keys/:id/reset-rate-limit", {
|
|
61
61
|
params: AdminApiKeyIdParams,
|
package/dist/admin/schema.d.ts
CHANGED
|
@@ -90,6 +90,7 @@ export declare const AdminApiKey: Schema.Struct<{
|
|
|
90
90
|
readonly remaining: Schema.NullOr<Schema.Number>;
|
|
91
91
|
readonly lastRequest: Schema.NullOr<Schema.Date>;
|
|
92
92
|
readonly expiresAt: Schema.NullOr<Schema.Date>;
|
|
93
|
+
readonly referrers: Schema.$Array<Schema.String>;
|
|
93
94
|
readonly createdAt: Schema.Date;
|
|
94
95
|
readonly updatedAt: Schema.Date;
|
|
95
96
|
}>;
|
|
@@ -99,6 +100,7 @@ export declare const AdminUpdateApiKeyPayload: Schema.Struct<{
|
|
|
99
100
|
readonly rateLimitEnabled: Schema.optional<Schema.Boolean>;
|
|
100
101
|
readonly rateLimitMax: Schema.optional<Schema.NullOr<Schema.Number>>;
|
|
101
102
|
readonly rateLimitTimeWindow: Schema.optional<Schema.NullOr<Schema.Number>>;
|
|
103
|
+
readonly referrers: Schema.optional<Schema.$Array<Schema.String>>;
|
|
102
104
|
}>;
|
|
103
105
|
export declare const AdminCreateOrganizationPayload: Schema.Struct<{
|
|
104
106
|
readonly name: Schema.NonEmptyString;
|
package/dist/admin/schema.js
CHANGED
|
@@ -136,6 +136,7 @@ export const AdminApiKey = Schema.Struct({
|
|
|
136
136
|
remaining: Schema.NullOr(Schema.Number),
|
|
137
137
|
lastRequest: Schema.NullOr(Schema.Date),
|
|
138
138
|
expiresAt: Schema.NullOr(Schema.Date),
|
|
139
|
+
referrers: Schema.Array(Schema.String),
|
|
139
140
|
createdAt: Schema.Date,
|
|
140
141
|
updatedAt: Schema.Date,
|
|
141
142
|
}).annotate({
|
|
@@ -158,6 +159,7 @@ export const AdminApiKey = Schema.Struct({
|
|
|
158
159
|
remaining: null,
|
|
159
160
|
lastRequest: null,
|
|
160
161
|
expiresAt: null,
|
|
162
|
+
referrers: ["https://app.example.com"],
|
|
161
163
|
createdAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
162
164
|
updatedAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
163
165
|
},
|
|
@@ -169,6 +171,7 @@ export const AdminUpdateApiKeyPayload = Schema.Struct({
|
|
|
169
171
|
rateLimitEnabled: Schema.optional(Schema.Boolean),
|
|
170
172
|
rateLimitMax: Schema.optional(Schema.NullOr(Schema.Number)),
|
|
171
173
|
rateLimitTimeWindow: Schema.optional(Schema.NullOr(Schema.Number)),
|
|
174
|
+
referrers: Schema.optional(Schema.Array(Schema.String)),
|
|
172
175
|
}).annotate({
|
|
173
176
|
identifier: "AdminUpdateApiKeyPayload",
|
|
174
177
|
title: "Update API key payload",
|
|
@@ -180,6 +183,7 @@ export const AdminUpdateApiKeyPayload = Schema.Struct({
|
|
|
180
183
|
rateLimitEnabled: true,
|
|
181
184
|
rateLimitMax: 10000,
|
|
182
185
|
rateLimitTimeWindow: 86400000,
|
|
186
|
+
referrers: ["https://app.example.com"],
|
|
183
187
|
},
|
|
184
188
|
],
|
|
185
189
|
});
|
package/dist/api.d.ts
CHANGED
|
@@ -61,6 +61,7 @@ export declare const AuthServiceApi: HttpApi.HttpApi<"AuthServiceApi", import("e
|
|
|
61
61
|
readonly name: import("effect/Schema").optional<import("effect/Schema").NonEmptyString>;
|
|
62
62
|
readonly organizationId: import("effect/Schema").optional<import("effect/Schema").String>;
|
|
63
63
|
readonly permissions: import("effect/Schema").optional<import("effect/Schema").$Record<import("effect/Schema").String, import("effect/Schema").$Array<import("effect/Schema").String>>>;
|
|
64
|
+
readonly referrers: import("effect/Schema").optional<import("effect/Schema").$Array<import("effect/Schema").String>>;
|
|
64
65
|
}>>, import("effect/unstable/httpapi/HttpApiEndpoint").StringTree<never>, import("effect/unstable/httpapi/HttpApiEndpoint").Json<import("effect/Schema").Struct<{
|
|
65
66
|
readonly id: import("effect/Schema").String;
|
|
66
67
|
readonly key: import("effect/Schema").String;
|
|
@@ -140,6 +141,13 @@ export declare const AuthServiceApi: HttpApi.HttpApi<"AuthServiceApi", import("e
|
|
|
140
141
|
readonly newPassword: import("effect/Schema").NonEmptyString;
|
|
141
142
|
}>>, import("effect/unstable/httpapi/HttpApiEndpoint").StringTree<never>, import("effect/unstable/httpapi/HttpApiEndpoint").Json<import("effect/Schema").Struct<{
|
|
142
143
|
readonly ok: import("effect/Schema").Boolean;
|
|
144
|
+
}>>, import("effect/unstable/httpapi/HttpApiEndpoint").Json<typeof import("effect/unstable/httpapi/HttpApiError").Unauthorized | typeof import("effect/unstable/httpapi/HttpApiError").Forbidden | typeof import("effect/unstable/httpapi/HttpApiError").InternalServerError | typeof import("./extra/schema").ExtraBadRequest>, never, never> | import("effect/unstable/httpapi/HttpApiEndpoint").HttpApiEndpoint<"updateApiKeyPermissions", "PATCH", "/api/auth/api-key/:keyId/permissions", import("effect/unstable/httpapi/HttpApiEndpoint").StringTree<import("effect/Schema").Struct<{
|
|
145
|
+
readonly keyId: import("effect/Schema").NonEmptyString;
|
|
146
|
+
}>>, import("effect/unstable/httpapi/HttpApiEndpoint").StringTree<never>, import("effect/unstable/httpapi/HttpApiEndpoint").Json<import("effect/Schema").Struct<{
|
|
147
|
+
readonly configId: import("effect/Schema").Union<readonly [import("effect/Schema").Literal<"user">, import("effect/Schema").Literal<"organization">, import("effect/Schema").Literal<"service">]>;
|
|
148
|
+
readonly permissions: import("effect/Schema").$Record<import("effect/Schema").String, import("effect/Schema").$Array<import("effect/Schema").String>>;
|
|
149
|
+
}>>, import("effect/unstable/httpapi/HttpApiEndpoint").StringTree<never>, import("effect/unstable/httpapi/HttpApiEndpoint").Json<import("effect/Schema").Struct<{
|
|
150
|
+
readonly ok: import("effect/Schema").Boolean;
|
|
143
151
|
}>>, import("effect/unstable/httpapi/HttpApiEndpoint").Json<typeof import("effect/unstable/httpapi/HttpApiError").Unauthorized | typeof import("effect/unstable/httpapi/HttpApiError").Forbidden | typeof import("effect/unstable/httpapi/HttpApiError").InternalServerError | typeof import("./extra/schema").ExtraBadRequest>, never, never> | import("effect/unstable/httpapi/HttpApiEndpoint").HttpApiEndpoint<"uploadUserImage", "POST", "/api/auth/image/upload", import("effect/unstable/httpapi/HttpApiEndpoint").StringTree<never>, import("effect/unstable/httpapi/HttpApiEndpoint").StringTree<never>, import("effect/unstable/httpapi/HttpApiEndpoint").Json<import("effect/unstable/httpapi/HttpApiSchema").asMultipartStream<import("effect/Schema").Struct<{
|
|
144
152
|
readonly file: import("effect/Schema").decodeTo<import("effect/unstable/http/Multipart").PersistedFileSchema, import("effect/Schema").$Array<import("effect/unstable/http/Multipart").PersistedFileSchema>, never, never>;
|
|
145
153
|
}>>>, import("effect/unstable/httpapi/HttpApiEndpoint").StringTree<never>, import("effect/unstable/httpapi/HttpApiEndpoint").Json<import("effect/Schema").Struct<{
|
|
@@ -598,6 +606,7 @@ export declare const AuthClientApi: HttpApi.HttpApi<"AuthClientApi", import("eff
|
|
|
598
606
|
readonly remaining: import("effect/Schema").NullOr<import("effect/Schema").Number>;
|
|
599
607
|
readonly lastRequest: import("effect/Schema").NullOr<import("effect/Schema").Date>;
|
|
600
608
|
readonly expiresAt: import("effect/Schema").NullOr<import("effect/Schema").Date>;
|
|
609
|
+
readonly referrers: import("effect/Schema").$Array<import("effect/Schema").String>;
|
|
601
610
|
readonly createdAt: import("effect/Schema").Date;
|
|
602
611
|
readonly updatedAt: import("effect/Schema").Date;
|
|
603
612
|
}>>, import("effect/unstable/httpapi/HttpApiEndpoint").Json<typeof import("effect/unstable/httpapi/HttpApiError").Unauthorized | typeof import("effect/unstable/httpapi/HttpApiError").Forbidden | typeof import("effect/unstable/httpapi/HttpApiError").NotFound | typeof import("effect/unstable/httpapi/HttpApiError").InternalServerError>, never, never> | import("effect/unstable/httpapi/HttpApiEndpoint").HttpApiEndpoint<"deleteDomain", "DELETE", "/api/auth/admin/domains/:id", import("effect/unstable/httpapi/HttpApiEndpoint").StringTree<import("effect/Schema").Struct<{
|
|
@@ -656,6 +665,7 @@ export declare const AuthClientApi: HttpApi.HttpApi<"AuthClientApi", import("eff
|
|
|
656
665
|
readonly remaining: import("effect/Schema").NullOr<import("effect/Schema").Number>;
|
|
657
666
|
readonly lastRequest: import("effect/Schema").NullOr<import("effect/Schema").Date>;
|
|
658
667
|
readonly expiresAt: import("effect/Schema").NullOr<import("effect/Schema").Date>;
|
|
668
|
+
readonly referrers: import("effect/Schema").$Array<import("effect/Schema").String>;
|
|
659
669
|
readonly createdAt: import("effect/Schema").Date;
|
|
660
670
|
readonly updatedAt: import("effect/Schema").Date;
|
|
661
671
|
}>>, import("effect/unstable/httpapi/HttpApiEndpoint").Json<typeof import("effect/unstable/httpapi/HttpApiError").Unauthorized | typeof import("effect/unstable/httpapi/HttpApiError").Forbidden | typeof import("effect/unstable/httpapi/HttpApiError").NotFound | typeof import("effect/unstable/httpapi/HttpApiError").InternalServerError>, never, never> | import("effect/unstable/httpapi/HttpApiEndpoint").HttpApiEndpoint<"enableApiKeyRateLimit", "POST", "/api/auth/admin/api-keys/:id/enable-rate-limit", import("effect/unstable/httpapi/HttpApiEndpoint").StringTree<import("effect/Schema").Struct<{
|
|
@@ -675,6 +685,7 @@ export declare const AuthClientApi: HttpApi.HttpApi<"AuthClientApi", import("eff
|
|
|
675
685
|
readonly remaining: import("effect/Schema").NullOr<import("effect/Schema").Number>;
|
|
676
686
|
readonly lastRequest: import("effect/Schema").NullOr<import("effect/Schema").Date>;
|
|
677
687
|
readonly expiresAt: import("effect/Schema").NullOr<import("effect/Schema").Date>;
|
|
688
|
+
readonly referrers: import("effect/Schema").$Array<import("effect/Schema").String>;
|
|
678
689
|
readonly createdAt: import("effect/Schema").Date;
|
|
679
690
|
readonly updatedAt: import("effect/Schema").Date;
|
|
680
691
|
}>>, import("effect/unstable/httpapi/HttpApiEndpoint").Json<typeof import("effect/unstable/httpapi/HttpApiError").Unauthorized | typeof import("effect/unstable/httpapi/HttpApiError").Forbidden | typeof import("effect/unstable/httpapi/HttpApiError").NotFound | typeof import("effect/unstable/httpapi/HttpApiError").InternalServerError>, never, never> | import("effect/unstable/httpapi/HttpApiEndpoint").HttpApiEndpoint<"getDomainRecords", "GET", "/api/auth/admin/domains/:id/records", import("effect/unstable/httpapi/HttpApiEndpoint").StringTree<import("effect/Schema").Struct<{
|
|
@@ -700,6 +711,7 @@ export declare const AuthClientApi: HttpApi.HttpApi<"AuthClientApi", import("eff
|
|
|
700
711
|
readonly remaining: import("effect/Schema").NullOr<import("effect/Schema").Number>;
|
|
701
712
|
readonly lastRequest: import("effect/Schema").NullOr<import("effect/Schema").Date>;
|
|
702
713
|
readonly expiresAt: import("effect/Schema").NullOr<import("effect/Schema").Date>;
|
|
714
|
+
readonly referrers: import("effect/Schema").$Array<import("effect/Schema").String>;
|
|
703
715
|
readonly createdAt: import("effect/Schema").Date;
|
|
704
716
|
readonly updatedAt: import("effect/Schema").Date;
|
|
705
717
|
}>>>, import("effect/unstable/httpapi/HttpApiEndpoint").Json<typeof import("effect/unstable/httpapi/HttpApiError").Unauthorized | typeof import("effect/unstable/httpapi/HttpApiError").Forbidden | typeof import("effect/unstable/httpapi/HttpApiError").InternalServerError>, never, never> | import("effect/unstable/httpapi/HttpApiEndpoint").HttpApiEndpoint<"listDomains", "GET", "/api/auth/admin/domains", import("effect/unstable/httpapi/HttpApiEndpoint").StringTree<never>, import("effect/unstable/httpapi/HttpApiEndpoint").StringTree<never>, import("effect/unstable/httpapi/HttpApiEndpoint").StringTree<never>, import("effect/unstable/httpapi/HttpApiEndpoint").StringTree<never>, import("effect/unstable/httpapi/HttpApiEndpoint").Json<import("effect/Schema").$Array<import("effect/Schema").Struct<{
|
|
@@ -810,6 +822,7 @@ export declare const AuthClientApi: HttpApi.HttpApi<"AuthClientApi", import("eff
|
|
|
810
822
|
readonly remaining: import("effect/Schema").NullOr<import("effect/Schema").Number>;
|
|
811
823
|
readonly lastRequest: import("effect/Schema").NullOr<import("effect/Schema").Date>;
|
|
812
824
|
readonly expiresAt: import("effect/Schema").NullOr<import("effect/Schema").Date>;
|
|
825
|
+
readonly referrers: import("effect/Schema").$Array<import("effect/Schema").String>;
|
|
813
826
|
readonly createdAt: import("effect/Schema").Date;
|
|
814
827
|
readonly updatedAt: import("effect/Schema").Date;
|
|
815
828
|
}>>, import("effect/unstable/httpapi/HttpApiEndpoint").Json<typeof import("effect/unstable/httpapi/HttpApiError").Unauthorized | typeof import("effect/unstable/httpapi/HttpApiError").Forbidden | typeof import("effect/unstable/httpapi/HttpApiError").NotFound | typeof import("effect/unstable/httpapi/HttpApiError").InternalServerError>, never, never> | import("effect/unstable/httpapi/HttpApiEndpoint").HttpApiEndpoint<"updateApiKey", "PATCH", "/api/auth/admin/api-keys/:id", import("effect/unstable/httpapi/HttpApiEndpoint").StringTree<import("effect/Schema").Struct<{
|
|
@@ -820,6 +833,7 @@ export declare const AuthClientApi: HttpApi.HttpApi<"AuthClientApi", import("eff
|
|
|
820
833
|
readonly rateLimitEnabled: import("effect/Schema").optional<import("effect/Schema").Boolean>;
|
|
821
834
|
readonly rateLimitMax: import("effect/Schema").optional<import("effect/Schema").NullOr<import("effect/Schema").Number>>;
|
|
822
835
|
readonly rateLimitTimeWindow: import("effect/Schema").optional<import("effect/Schema").NullOr<import("effect/Schema").Number>>;
|
|
836
|
+
readonly referrers: import("effect/Schema").optional<import("effect/Schema").$Array<import("effect/Schema").String>>;
|
|
823
837
|
}>>, import("effect/unstable/httpapi/HttpApiEndpoint").StringTree<never>, import("effect/unstable/httpapi/HttpApiEndpoint").Json<import("effect/Schema").Struct<{
|
|
824
838
|
readonly id: import("effect/Schema").String;
|
|
825
839
|
readonly configId: import("effect/Schema").String;
|
|
@@ -835,6 +849,7 @@ export declare const AuthClientApi: HttpApi.HttpApi<"AuthClientApi", import("eff
|
|
|
835
849
|
readonly remaining: import("effect/Schema").NullOr<import("effect/Schema").Number>;
|
|
836
850
|
readonly lastRequest: import("effect/Schema").NullOr<import("effect/Schema").Date>;
|
|
837
851
|
readonly expiresAt: import("effect/Schema").NullOr<import("effect/Schema").Date>;
|
|
852
|
+
readonly referrers: import("effect/Schema").$Array<import("effect/Schema").String>;
|
|
838
853
|
readonly createdAt: import("effect/Schema").Date;
|
|
839
854
|
readonly updatedAt: import("effect/Schema").Date;
|
|
840
855
|
}>>, import("effect/unstable/httpapi/HttpApiEndpoint").Json<typeof import("effect/unstable/httpapi/HttpApiError").Unauthorized | typeof import("effect/unstable/httpapi/HttpApiError").Forbidden | typeof import("effect/unstable/httpapi/HttpApiError").NotFound | typeof import("effect/unstable/httpapi/HttpApiError").InternalServerError>, never, never> | import("effect/unstable/httpapi/HttpApiEndpoint").HttpApiEndpoint<"updateDomain", "PATCH", "/api/auth/admin/domains/:id", import("effect/unstable/httpapi/HttpApiEndpoint").StringTree<import("effect/Schema").Struct<{
|
|
@@ -949,6 +964,7 @@ export declare const AuthClientApi: HttpApi.HttpApi<"AuthClientApi", import("eff
|
|
|
949
964
|
readonly name: import("effect/Schema").optional<import("effect/Schema").NonEmptyString>;
|
|
950
965
|
readonly organizationId: import("effect/Schema").optional<import("effect/Schema").String>;
|
|
951
966
|
readonly permissions: import("effect/Schema").optional<import("effect/Schema").$Record<import("effect/Schema").String, import("effect/Schema").$Array<import("effect/Schema").String>>>;
|
|
967
|
+
readonly referrers: import("effect/Schema").optional<import("effect/Schema").$Array<import("effect/Schema").String>>;
|
|
952
968
|
}>>, import("effect/unstable/httpapi/HttpApiEndpoint").StringTree<never>, import("effect/unstable/httpapi/HttpApiEndpoint").Json<import("effect/Schema").Struct<{
|
|
953
969
|
readonly id: import("effect/Schema").String;
|
|
954
970
|
readonly key: import("effect/Schema").String;
|
|
@@ -1028,6 +1044,13 @@ export declare const AuthClientApi: HttpApi.HttpApi<"AuthClientApi", import("eff
|
|
|
1028
1044
|
readonly newPassword: import("effect/Schema").NonEmptyString;
|
|
1029
1045
|
}>>, import("effect/unstable/httpapi/HttpApiEndpoint").StringTree<never>, import("effect/unstable/httpapi/HttpApiEndpoint").Json<import("effect/Schema").Struct<{
|
|
1030
1046
|
readonly ok: import("effect/Schema").Boolean;
|
|
1047
|
+
}>>, import("effect/unstable/httpapi/HttpApiEndpoint").Json<typeof import("effect/unstable/httpapi/HttpApiError").Unauthorized | typeof import("effect/unstable/httpapi/HttpApiError").Forbidden | typeof import("effect/unstable/httpapi/HttpApiError").InternalServerError | typeof import("./extra/schema").ExtraBadRequest>, never, never> | import("effect/unstable/httpapi/HttpApiEndpoint").HttpApiEndpoint<"updateApiKeyPermissions", "PATCH", "/api/auth/api-key/:keyId/permissions", import("effect/unstable/httpapi/HttpApiEndpoint").StringTree<import("effect/Schema").Struct<{
|
|
1048
|
+
readonly keyId: import("effect/Schema").NonEmptyString;
|
|
1049
|
+
}>>, import("effect/unstable/httpapi/HttpApiEndpoint").StringTree<never>, import("effect/unstable/httpapi/HttpApiEndpoint").Json<import("effect/Schema").Struct<{
|
|
1050
|
+
readonly configId: import("effect/Schema").Union<readonly [import("effect/Schema").Literal<"user">, import("effect/Schema").Literal<"organization">, import("effect/Schema").Literal<"service">]>;
|
|
1051
|
+
readonly permissions: import("effect/Schema").$Record<import("effect/Schema").String, import("effect/Schema").$Array<import("effect/Schema").String>>;
|
|
1052
|
+
}>>, import("effect/unstable/httpapi/HttpApiEndpoint").StringTree<never>, import("effect/unstable/httpapi/HttpApiEndpoint").Json<import("effect/Schema").Struct<{
|
|
1053
|
+
readonly ok: import("effect/Schema").Boolean;
|
|
1031
1054
|
}>>, import("effect/unstable/httpapi/HttpApiEndpoint").Json<typeof import("effect/unstable/httpapi/HttpApiError").Unauthorized | typeof import("effect/unstable/httpapi/HttpApiError").Forbidden | typeof import("effect/unstable/httpapi/HttpApiError").InternalServerError | typeof import("./extra/schema").ExtraBadRequest>, never, never> | import("effect/unstable/httpapi/HttpApiEndpoint").HttpApiEndpoint<"uploadUserImage", "POST", "/api/auth/image/upload", import("effect/unstable/httpapi/HttpApiEndpoint").StringTree<never>, import("effect/unstable/httpapi/HttpApiEndpoint").StringTree<never>, import("effect/unstable/httpapi/HttpApiEndpoint").Json<import("effect/unstable/httpapi/HttpApiSchema").asMultipartStream<import("effect/Schema").Struct<{
|
|
1032
1055
|
readonly file: import("effect/Schema").decodeTo<import("effect/unstable/http/Multipart").PersistedFileSchema, import("effect/Schema").$Array<import("effect/unstable/http/Multipart").PersistedFileSchema>, never, never>;
|
|
1033
1056
|
}>>>, import("effect/unstable/httpapi/HttpApiEndpoint").StringTree<never>, import("effect/unstable/httpapi/HttpApiEndpoint").Json<import("effect/Schema").Struct<{
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ApiKey } from "@better-auth/api-key/client";
|
|
2
|
+
import type { AuthUiClient } from "./auth-client";
|
|
3
|
+
type ApiKeySummary = Omit<ApiKey, "key">;
|
|
4
|
+
export type ApiKeyEditMessages = {
|
|
5
|
+
enabled: string;
|
|
6
|
+
name: string;
|
|
7
|
+
referrers: string;
|
|
8
|
+
referrersDescription: string;
|
|
9
|
+
referrersError: string;
|
|
10
|
+
referrersPlaceholder: string;
|
|
11
|
+
updateError: string;
|
|
12
|
+
};
|
|
13
|
+
export declare const ApiKeyEditForm: ({ authClient, configId, keyData, messages, onSaved, }: {
|
|
14
|
+
authClient: AuthUiClient;
|
|
15
|
+
configId: "organization" | "user";
|
|
16
|
+
keyData: ApiKeySummary;
|
|
17
|
+
messages: ApiKeyEditMessages;
|
|
18
|
+
onSaved: () => void;
|
|
19
|
+
}) => import("react").JSX.Element;
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useAtomSet, useAtomValue } from "@effect/atom-react";
|
|
3
|
+
import { FormBuilder, FormReact } from "@lucas-barake/effect-form-react";
|
|
4
|
+
import { Effect, Schema } from "effect";
|
|
5
|
+
import { useState } from "react";
|
|
6
|
+
import { CheckboxField, SubmitButton, SubmitError, TextAreaField, TextField, } from "@/components/ui/effect-form";
|
|
7
|
+
import { parseApiKeyReferrers } from "./api-key";
|
|
8
|
+
import { apiKeyReferrers, withApiKeyReferrers } from "./api-key-referrers";
|
|
9
|
+
const editApiKeyFormBuilder = FormBuilder.empty
|
|
10
|
+
.addField("name", Schema.NonEmptyString)
|
|
11
|
+
.addField("enabled", Schema.Boolean)
|
|
12
|
+
.addField("referrers", Schema.String);
|
|
13
|
+
export const ApiKeyEditForm = ({ authClient, configId, keyData, messages, onSaved, }) => {
|
|
14
|
+
const [form] = useState(() => FormReact.make(editApiKeyFormBuilder, {
|
|
15
|
+
fields: {
|
|
16
|
+
name: TextField,
|
|
17
|
+
enabled: CheckboxField,
|
|
18
|
+
referrers: TextAreaField,
|
|
19
|
+
},
|
|
20
|
+
mode: { validation: "onSubmit" },
|
|
21
|
+
onSubmit: (_, { decoded }) => Effect.tryPromise({
|
|
22
|
+
try: async () => {
|
|
23
|
+
const referrers = parseApiKeyReferrers(decoded.referrers, messages.referrersError);
|
|
24
|
+
const result = await authClient.apiKey.update({
|
|
25
|
+
configId,
|
|
26
|
+
keyId: keyData.id,
|
|
27
|
+
name: decoded.name.trim(),
|
|
28
|
+
enabled: decoded.enabled,
|
|
29
|
+
metadata: withApiKeyReferrers(keyData.metadata, referrers),
|
|
30
|
+
});
|
|
31
|
+
if (result.error) {
|
|
32
|
+
throw new Error(result.error.message ?? messages.updateError);
|
|
33
|
+
}
|
|
34
|
+
onSaved();
|
|
35
|
+
},
|
|
36
|
+
catch: (cause) => cause instanceof Error ? cause : new Error(messages.updateError),
|
|
37
|
+
}),
|
|
38
|
+
}));
|
|
39
|
+
const submit = useAtomSet(form.submit);
|
|
40
|
+
const submitResult = useAtomValue(form.submit);
|
|
41
|
+
return (_jsx("section", { className: "w-full", children: _jsx(form.Initialize, { defaultValues: {
|
|
42
|
+
name: keyData.name ?? "",
|
|
43
|
+
enabled: keyData.enabled,
|
|
44
|
+
referrers: apiKeyReferrers(keyData.metadata).join("\n"),
|
|
45
|
+
}, children: _jsxs("form", { className: "flex flex-col gap-4", onSubmit: (event) => {
|
|
46
|
+
event.preventDefault();
|
|
47
|
+
event.stopPropagation();
|
|
48
|
+
submit();
|
|
49
|
+
}, children: [_jsx(form.name, { label: messages.name, required: true }), _jsx(form.enabled, { label: messages.enabled }), _jsx(form.referrers, { label: messages.referrers, description: messages.referrersDescription, placeholder: messages.referrersPlaceholder, rows: 3 }), _jsx(SubmitError, { result: submitResult }), _jsx(SubmitButton, { form: form })] }) }) }));
|
|
50
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ApiKey } from "@better-auth/api-key/client";
|
|
2
|
+
type ApiKeyRateLimitMessages = {
|
|
3
|
+
disabled: string;
|
|
4
|
+
none: string;
|
|
5
|
+
unlimited: string;
|
|
6
|
+
usage: string;
|
|
7
|
+
windowDays: (count: number) => string;
|
|
8
|
+
windowHours: (count: number) => string;
|
|
9
|
+
windowMinutes: (count: number) => string;
|
|
10
|
+
windowSeconds: (count: number) => string;
|
|
11
|
+
};
|
|
12
|
+
type ApiKeyRateLimitValue = Pick<ApiKey, "rateLimitEnabled" | "rateLimitMax" | "rateLimitTimeWindow" | "requestCount">;
|
|
13
|
+
export declare const apiKeyUsagePercent: (keyData: ApiKeyRateLimitValue) => number;
|
|
14
|
+
export declare const ApiKeyRateLimit: ({ keyData, messages, }: {
|
|
15
|
+
keyData: ApiKeyRateLimitValue;
|
|
16
|
+
messages: ApiKeyRateLimitMessages;
|
|
17
|
+
}) => import("react").JSX.Element;
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Badge } from "@/components/ui/badge";
|
|
3
|
+
export const apiKeyUsagePercent = (keyData) => {
|
|
4
|
+
if (!keyData.rateLimitMax)
|
|
5
|
+
return 0;
|
|
6
|
+
return Math.min(100, Math.round((keyData.requestCount / keyData.rateLimitMax) * 100));
|
|
7
|
+
};
|
|
8
|
+
const formatWindow = (value, messages) => {
|
|
9
|
+
if (!value)
|
|
10
|
+
return messages.none;
|
|
11
|
+
const seconds = Math.round(value / 1000);
|
|
12
|
+
if (seconds % 86400 === 0)
|
|
13
|
+
return messages.windowDays(seconds / 86400);
|
|
14
|
+
if (seconds % 3600 === 0)
|
|
15
|
+
return messages.windowHours(seconds / 3600);
|
|
16
|
+
if (seconds % 60 === 0)
|
|
17
|
+
return messages.windowMinutes(seconds / 60);
|
|
18
|
+
return messages.windowSeconds(seconds);
|
|
19
|
+
};
|
|
20
|
+
export const ApiKeyRateLimit = ({ keyData, messages, }) => {
|
|
21
|
+
if (!keyData.rateLimitEnabled) {
|
|
22
|
+
return _jsx(Badge, { variant: "secondary", children: messages.disabled });
|
|
23
|
+
}
|
|
24
|
+
if (!keyData.rateLimitMax) {
|
|
25
|
+
return (_jsx("span", { className: "text-muted-foreground text-sm", children: messages.unlimited }));
|
|
26
|
+
}
|
|
27
|
+
const percent = apiKeyUsagePercent(keyData);
|
|
28
|
+
return (_jsxs("div", { className: "min-w-28 space-y-1", children: [_jsxs("div", { className: "text-muted-foreground flex justify-between gap-2 text-xs", children: [_jsxs("span", { children: [keyData.requestCount, "/", keyData.rateLimitMax] }), _jsxs("span", { children: [percent, "%"] })] }), _jsx("div", { "aria-label": messages.usage, "aria-valuemax": 100, "aria-valuemin": 0, "aria-valuenow": percent, className: "bg-muted h-2 overflow-hidden rounded-full", role: "progressbar", children: _jsx("div", { className: "bg-primary h-full rounded-full transition-[width]", style: { width: `${percent}%` } }) }), _jsx("p", { className: "text-muted-foreground text-xs", children: formatWindow(keyData.rateLimitTimeWindow, messages) })] }));
|
|
29
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare const apiKeyReferrers: (metadata: unknown) => never[] | readonly string[];
|
|
2
|
+
export declare const withApiKeyReferrers: (metadata: unknown, referrers: ReadonlyArray<string>) => {
|
|
3
|
+
allowedOrigins: string[];
|
|
4
|
+
};
|
|
5
|
+
export declare const ApiKeyReferrers: ({ metadata, unrestrictedLabel, }: {
|
|
6
|
+
metadata: unknown;
|
|
7
|
+
unrestrictedLabel: string;
|
|
8
|
+
}) => import("react").JSX.Element;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { Option, Schema } from "effect";
|
|
3
|
+
import { Badge } from "@/components/ui/badge";
|
|
4
|
+
const Metadata = Schema.Record(Schema.String, Schema.Unknown);
|
|
5
|
+
const MetadataJson = Schema.fromJsonString(Metadata);
|
|
6
|
+
const Referrers = Schema.Array(Schema.String);
|
|
7
|
+
const apiKeyMetadata = (metadata) => {
|
|
8
|
+
const decodedMetadata = typeof metadata === "string"
|
|
9
|
+
? Schema.decodeUnknownOption(MetadataJson)(metadata)
|
|
10
|
+
: Schema.decodeUnknownOption(Metadata)(metadata);
|
|
11
|
+
return Option.getOrElse(decodedMetadata, () => ({}));
|
|
12
|
+
};
|
|
13
|
+
export const apiKeyReferrers = (metadata) => Option.getOrElse(Schema.decodeUnknownOption(Referrers)(apiKeyMetadata(metadata).allowedOrigins), () => []);
|
|
14
|
+
export const withApiKeyReferrers = (metadata, referrers) => ({
|
|
15
|
+
...apiKeyMetadata(metadata),
|
|
16
|
+
allowedOrigins: Array.from(referrers),
|
|
17
|
+
});
|
|
18
|
+
export const ApiKeyReferrers = ({ metadata, unrestrictedLabel, }) => {
|
|
19
|
+
const referrers = apiKeyReferrers(metadata);
|
|
20
|
+
if (referrers.length === 0) {
|
|
21
|
+
return (_jsx("span", { className: "text-muted-foreground text-sm", children: unrestrictedLabel }));
|
|
22
|
+
}
|
|
23
|
+
return (_jsx("div", { className: "flex max-w-sm flex-wrap gap-1.5", children: referrers.map((referrer) => (_jsx(Badge, { variant: "secondary", children: referrer }, referrer))) }));
|
|
24
|
+
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { ExtraCreateApiKeyPayload, ExtraCreateApiKeyResponse } from "../extra/schema";
|
|
2
2
|
export type CreateApiKeyPayload = typeof ExtraCreateApiKeyPayload.Type;
|
|
3
3
|
export type CreatedApiKey = typeof ExtraCreateApiKeyResponse.Type;
|
|
4
|
+
export declare const parseApiKeyReferrers: (value: string, errorMessage: string) => string[];
|
|
4
5
|
export declare const createApiKey: (payload: CreateApiKeyPayload, fallbackError: string) => Promise<CreatedApiKey>;
|
|
@@ -1,5 +1,24 @@
|
|
|
1
|
-
import { Schema } from "effect";
|
|
1
|
+
import { Option, Schema } from "effect";
|
|
2
2
|
import { ExtraCreateApiKeyPayload, ExtraCreateApiKeyResponse, } from "../extra/schema";
|
|
3
|
+
const decodeUrl = Schema.decodeUnknownOption(Schema.URLFromString);
|
|
4
|
+
export const parseApiKeyReferrers = (value, errorMessage) => {
|
|
5
|
+
const referrers = value
|
|
6
|
+
.split(/[\n,]+/)
|
|
7
|
+
.map((item) => item.trim())
|
|
8
|
+
.filter(Boolean)
|
|
9
|
+
.map((item) => {
|
|
10
|
+
const decoded = decodeUrl(item);
|
|
11
|
+
if (Option.isNone(decoded)) {
|
|
12
|
+
throw new Error(errorMessage.replace("{referrer}", item));
|
|
13
|
+
}
|
|
14
|
+
const url = decoded.value;
|
|
15
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") {
|
|
16
|
+
throw new Error(errorMessage.replace("{referrer}", item));
|
|
17
|
+
}
|
|
18
|
+
return url.origin;
|
|
19
|
+
});
|
|
20
|
+
return Array.from(new Set(referrers));
|
|
21
|
+
};
|
|
3
22
|
const ErrorResponse = Schema.Struct({ message: Schema.String });
|
|
4
23
|
const errorMessage = async (value, fallback) => Schema.decodeUnknownPromise(ErrorResponse)(value)
|
|
5
24
|
.then((error) => error.message)
|