@frogbotai/plugin-api-keys 0.4.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 FrogBot
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # @frogbotai/plugin-api-keys
2
+
3
+ Add multiple named, independently revocable API keys to a FrogBot application.
4
+
5
+ ```ts
6
+ import { apiKeysPlugin } from '@frogbotai/plugin-api-keys';
7
+ import { buildConfig } from 'frogbot';
8
+
9
+ export default buildConfig({
10
+ secret: process.env.FROGBOT_SECRET!,
11
+ db: databaseAdapter,
12
+ collections: [{ slug: 'users', auth: true, fields: [] }],
13
+ plugins: [apiKeysPlugin()],
14
+ });
15
+ ```
16
+
17
+ The plugin adds an `api-keys` collection, owner-scoped management endpoints, API-key authentication, and Payload-default admin controls. Create a key in the API Keys admin collection, then authenticate requests with either header:
18
+
19
+ ```http
20
+ Authorization: Bearer fbt_...
21
+ X-API-Key: fbt_...
22
+ ```
23
+
24
+ Plaintext keys are returned only when created. The collection stores SHA-256 hashes and supports multiple independently revoked keys per user.
25
+
26
+ ```ts
27
+ apiKeysPlugin({
28
+ authCollection: 'accounts',
29
+ collectionSlug: 'credentials',
30
+ tokenPrefix: 'acme',
31
+ headerNames: ['x-service-key'],
32
+ collection: {
33
+ admin: { group: 'Security' },
34
+ fields: [{ name: 'environment', type: 'text' }],
35
+ },
36
+ });
37
+ ```
38
+
39
+ Collection overrides merge with the generated fields, access, endpoints, hooks, admin components, and transforms applied by other plugins. The manager uses Payload controls without plugin CSS, so global admin styles continue to apply.
@@ -0,0 +1,2 @@
1
+ export declare function ApiKeysManager(): import("react/jsx-runtime").JSX.Element;
2
+ //# sourceMappingURL=ApiKeysManager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ApiKeysManager.d.ts","sourceRoot":"","sources":["../../src/client/ApiKeysManager.tsx"],"names":[],"mappings":"AAYA,wBAAgB,cAAc,4CAmF7B"}
@@ -0,0 +1,55 @@
1
+ 'use client';
2
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
+ import { Button, TextInput, useConfig, useListQuery } from '@payloadcms/ui';
4
+ import { useState } from 'react';
5
+ export function ApiKeysManager() {
6
+ const { config } = useConfig();
7
+ const { collectionSlug, data, query, refineListData } = useListQuery();
8
+ const [name, setName] = useState('');
9
+ const [token, setToken] = useState();
10
+ const [error, setError] = useState();
11
+ const [loading, setLoading] = useState(false);
12
+ const api = config.routes.api;
13
+ async function refresh() {
14
+ await refineListData(query);
15
+ }
16
+ async function createKey() {
17
+ setLoading(true);
18
+ setError(undefined);
19
+ const response = await fetch(`${api}/${collectionSlug}/mint`, {
20
+ method: 'POST',
21
+ headers: { 'content-type': 'application/json' },
22
+ body: JSON.stringify({ name }),
23
+ });
24
+ const result = (await response.json());
25
+ setLoading(false);
26
+ if (!response.ok || !result.token) {
27
+ setError(result.error ?? 'Unable to create API key');
28
+ return;
29
+ }
30
+ setName('');
31
+ setToken(result.token);
32
+ await refresh();
33
+ }
34
+ async function revokeKey(id) {
35
+ setLoading(true);
36
+ setError(undefined);
37
+ const response = await fetch(`${api}/${collectionSlug}/${id}/revoke`, { method: 'POST' });
38
+ setLoading(false);
39
+ if (!response.ok) {
40
+ const result = (await response.json());
41
+ setError(result.error ?? 'Unable to revoke API key');
42
+ return;
43
+ }
44
+ await refresh();
45
+ }
46
+ async function copyToken() {
47
+ if (token)
48
+ await navigator.clipboard.writeText(token);
49
+ }
50
+ const docs = (data?.docs ?? []);
51
+ return (_jsxs("div", { children: [_jsx(TextInput, { label: "Key name", path: "api-key-name", value: name, onChange: (event) => setName(event.target.value) }), _jsx(Button, { disabled: loading || !name.trim(), onClick: createKey, type: "button", children: "Create API key" }), token ? (_jsxs("div", { children: [_jsx("p", { children: "This key is shown once. Copy it now." }), _jsx(TextInput, { label: "API key", path: "new-api-key", readOnly: true, value: token }), _jsx(Button, { onClick: copyToken, type: "button", children: "Copy API key" }), _jsx(Button, { onClick: () => setToken(undefined), type: "button", children: "Dismiss" })] })) : null, error ? _jsx("p", { role: "alert", children: error }) : null, docs
52
+ .filter((doc) => !doc.revokedAt)
53
+ .map((doc) => (_jsxs(Button, { disabled: loading, onClick: () => revokeKey(doc.id), type: "button", children: ["Revoke ", doc.name ?? doc.prefix ?? String(doc.id)] }, doc.id)))] }));
54
+ }
55
+ //# sourceMappingURL=ApiKeysManager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ApiKeysManager.js","sourceRoot":"","sources":["../../src/client/ApiKeysManager.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAEb,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC5E,OAAO,EAAoB,QAAQ,EAAE,MAAM,OAAO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,0BACE,+DAA2C,EAC3C,KAAC,SAAS,IAAC,KAAK,EAAC,SAAS,EAAC,IAAI,EAAC,aAAa,EAAC,QAAQ,QAAC,KAAK,EAAE,KAAK,GAAI,EACvE,KAAC,MAAM,IAAC,OAAO,EAAE,SAAS,EAAE,IAAI,EAAC,QAAQ,6BAEhC,EACT,KAAC,MAAM,IAAC,OAAO,EAAE,GAAG,EAAE,CAAC,QAAQ,CAAC,SAAS,CAAC,EAAE,IAAI,EAAC,QAAQ,wBAEhD,IACL,CACP,CAAC,CAAC,CAAC,IAAI,EACP,KAAK,CAAC,CAAC,CAAC,YAAG,IAAI,EAAC,OAAO,YAAE,KAAK,GAAK,CAAC,CAAC,CAAC,IAAI,EAC1C,IAAI;iBACF,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,SAAS,CAAC;iBAC/B,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CACZ,MAAC,MAAM,IAAC,QAAQ,EAAE,OAAO,EAAe,OAAO,EAAE,GAAG,EAAE,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,IAAI,EAAC,QAAQ,wBAC7E,GAAG,CAAC,IAAI,IAAI,GAAG,CAAC,MAAM,IAAI,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,KADlB,GAAG,CAAC,EAAE,CAE7B,CACV,CAAC,IACA,CACP,CAAC;AACJ,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { ApiKeysManager } from './client/ApiKeysManager.js';
2
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC"}
package/dist/client.js ADDED
@@ -0,0 +1,2 @@
1
+ export { ApiKeysManager } from './client/ApiKeysManager.js';
2
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC"}
@@ -0,0 +1,11 @@
1
+ import type { CollectionConfig } from 'frogbot';
2
+ type CollectionOptions = {
3
+ authCollection: string;
4
+ collectionSlug: string;
5
+ tokenPrefix: string;
6
+ collection?: Partial<CollectionConfig>;
7
+ existing?: CollectionConfig;
8
+ };
9
+ export declare function createApiKeysCollection(options: CollectionOptions): CollectionConfig;
10
+ export {};
11
+ //# sourceMappingURL=collection.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"collection.d.ts","sourceRoot":"","sources":["../src/collection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAmC,MAAM,SAAS,CAAC;AAIjF,KAAK,iBAAiB,GAAG;IACvB,cAAc,EAAE,MAAM,CAAC;IACvB,cAAc,EAAE,MAAM,CAAC;IACvB,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,CAAC,EAAE,OAAO,CAAC,gBAAgB,CAAC,CAAC;IACvC,QAAQ,CAAC,EAAE,gBAAgB,CAAC;CAC7B,CAAC;AAgFF,wBAAgB,uBAAuB,CAAC,OAAO,EAAE,iBAAiB,GAAG,gBAAgB,CAmDpF"}
@@ -0,0 +1,128 @@
1
+ import { createApiKeyToken, getApiKeyPrefix, hashApiKeyToken } from './server/token.js';
2
+ function mergeFields(...groups) {
3
+ const fields = new Map();
4
+ for (const group of groups) {
5
+ for (const field of group ?? []) {
6
+ const key = 'name' in field && field.name ? field.name : JSON.stringify(field);
7
+ fields.set(key, field);
8
+ }
9
+ }
10
+ return [...fields.values()];
11
+ }
12
+ function getOwnerID(req) {
13
+ return req.user?.id ?? null;
14
+ }
15
+ function createEndpoints({ collectionSlug, tokenPrefix }) {
16
+ return [
17
+ {
18
+ method: 'post',
19
+ path: '/mint',
20
+ handler: async (req) => {
21
+ const owner = getOwnerID(req);
22
+ if (owner === null)
23
+ return Response.json({ error: 'Authentication required' }, { status: 401 });
24
+ const body = (await req.json?.().catch(() => null) ?? null);
25
+ const name = typeof body?.name === 'string' ? body.name.trim() : '';
26
+ if (!name)
27
+ return Response.json({ error: 'Name is required' }, { status: 400 });
28
+ const token = createApiKeyToken({ tokenPrefix });
29
+ const prefix = getApiKeyPrefix(token);
30
+ const doc = (await req.frogbot.create({
31
+ collection: collectionSlug,
32
+ data: {
33
+ name,
34
+ owner,
35
+ prefix,
36
+ tokenHash: hashApiKeyToken(token),
37
+ },
38
+ overrideAccess: true,
39
+ req,
40
+ }));
41
+ return Response.json({ id: doc.id, name, prefix, token, createdAt: doc.createdAt }, { status: 201 });
42
+ },
43
+ },
44
+ {
45
+ method: 'post',
46
+ path: '/:id/revoke',
47
+ handler: async (req) => {
48
+ const owner = getOwnerID(req);
49
+ if (owner === null)
50
+ return Response.json({ error: 'Authentication required' }, { status: 401 });
51
+ const id = req.routeParams?.id;
52
+ if (typeof id !== 'string' || !id)
53
+ return Response.json({ error: 'API key not found' }, { status: 404 });
54
+ const result = await req.frogbot.find({
55
+ collection: collectionSlug,
56
+ limit: 1,
57
+ overrideAccess: true,
58
+ req,
59
+ where: { and: [{ id: { equals: id } }, { owner: { equals: owner } }] },
60
+ });
61
+ const key = result.docs[0];
62
+ if (!key)
63
+ return Response.json({ error: 'API key not found' }, { status: 404 });
64
+ const revokedAt = typeof key.revokedAt === 'string' ? key.revokedAt : new Date().toISOString();
65
+ await req.frogbot.update({
66
+ collection: collectionSlug,
67
+ id: key.id,
68
+ data: { revokedAt },
69
+ overrideAccess: true,
70
+ req,
71
+ });
72
+ return Response.json({ id: key.id, revokedAt });
73
+ },
74
+ },
75
+ ];
76
+ }
77
+ export function createApiKeysCollection(options) {
78
+ const { authCollection, collectionSlug, collection, existing } = options;
79
+ const ownerAccess = ({ req }) => {
80
+ const owner = getOwnerID(req);
81
+ return owner === null ? false : { owner: { equals: owner } };
82
+ };
83
+ const fields = [
84
+ { name: 'name', type: 'text', required: true },
85
+ { name: 'owner', type: 'relationship', relationTo: authCollection, required: true, index: true },
86
+ { name: 'prefix', type: 'text', required: true, index: true, admin: { readOnly: true } },
87
+ { name: 'tokenHash', type: 'text', required: true, unique: true, index: true, access: { read: () => false }, admin: { hidden: true } },
88
+ { name: 'lastUsedAt', type: 'date', admin: { readOnly: true } },
89
+ { name: 'revokedAt', type: 'date', index: true, admin: { readOnly: true } },
90
+ ];
91
+ const endpoints = createEndpoints(options);
92
+ return {
93
+ slug: collectionSlug,
94
+ labels: { singular: 'API Key', plural: 'API Keys' },
95
+ timestamps: true,
96
+ ...existing,
97
+ ...collection,
98
+ access: {
99
+ create: () => false,
100
+ delete: () => false,
101
+ read: ownerAccess,
102
+ update: () => false,
103
+ ...existing?.access,
104
+ ...collection?.access,
105
+ },
106
+ admin: {
107
+ useAsTitle: 'name',
108
+ ...existing?.admin,
109
+ ...collection?.admin,
110
+ components: {
111
+ ...existing?.admin?.components,
112
+ ...collection?.admin?.components,
113
+ beforeListTable: [
114
+ '@frogbotai/plugin-api-keys/client#ApiKeysManager',
115
+ ...(existing?.admin?.components?.beforeListTable ?? []),
116
+ ...(collection?.admin?.components?.beforeListTable ?? []),
117
+ ],
118
+ },
119
+ },
120
+ endpoints: [...endpoints, ...(existing?.endpoints ?? []), ...(collection?.endpoints ?? [])],
121
+ fields: mergeFields(fields, existing?.fields, collection?.fields),
122
+ hooks: {
123
+ ...existing?.hooks,
124
+ ...collection?.hooks,
125
+ },
126
+ };
127
+ }
128
+ //# sourceMappingURL=collection.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"collection.js","sourceRoot":"","sources":["../src/collection.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,iBAAiB,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAUxF,SAAS,WAAW,CAAC,GAAG,MAA+B;IACrD,MAAM,MAAM,GAAG,IAAI,GAAG,EAAiB,CAAC;IACxC,KAAK,MAAM,KAAK,IAAI,M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ashApiKeyToken, } from './server/token.js';
3
+ export type { ApiKeyHeaderOptions, ApiKeyTokenOptions } from './server/token.js';
4
+ export type ApiKeysPluginOptions = {
5
+ authCollection?: string;
6
+ collectionSlug?: string;
7
+ tokenPrefix?: string;
8
+ headerNames?: string[];
9
+ collection?: Partial<CollectionConfig>;
10
+ };
11
+ export declare function apiKeysPlugin(options?: ApiKeysPluginOptions): Plugin;
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAIxD,OAAO,EACL,iBAAiB,EACjB,kBAAkB,EAClB,eAAe,EACf,eAAe,GAChB,MAAM,mBAAmB,CAAC;AAC3B,YAAY,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAEjF,MAAM,MAAM,oBAAoB,GAAG;IACjC,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;IACvB,UAAU,CAAC,EAAE,OAAO,CAAC,gBAAgB,CAAC,CAAC;CACxC,CAAC;AAEF,wBAAgB,aAAa,CAAC,OAAO,GAAE,oBAAyB,GAAG,MAAM,CAyCxE"}
package/dist/index.js ADDED
@@ -0,0 +1,48 @@
1
+ import { createApiKeysCollection } from './collection.js';
2
+ import { createApiKeyStrategy } from './strategy.js';
3
+ export { createApiKeyToken, extractApiKeyToken, getApiKeyPrefix, hashApiKeyToken, } from './server/token.js';
4
+ export function apiKeysPlugin(options = {}) {
5
+ return (config) => {
6
+ const authCollection = options.authCollection ?? 'users';
7
+ const collectionSlug = options.collectionSlug ?? 'api-keys';
8
+ const auth = config.collections.find((collection) => collection.slug === authCollection);
9
+ if (!auth || auth.auth === undefined || auth.auth === false) {
10
+ throw new Error(`[plugin-api-keys] Auth collection '${authCollection}' must exist and have auth enabled.`);
11
+ }
12
+ const existing = config.collections.find((collection) => collection.slug === collectionSlug);
13
+ const collection = createApiKeysCollection({
14
+ authCollection,
15
+ collectionSlug,
16
+ tokenPrefix: options.tokenPrefix ?? 'fbt',
17
+ collection: options.collection,
18
+ existing,
19
+ });
20
+ const strategy = createApiKeyStrategy({
21
+ authCollection,
22
+ collectionSlug,
23
+ headerNames: options.headerNames,
24
+ tokenPrefix: options.tokenPrefix ?? 'fbt',
25
+ });
26
+ return {
27
+ ...config,
28
+ collections: [
29
+ ...config.collections.map((item) => {
30
+ if (item.slug === collectionSlug)
31
+ return collection;
32
+ if (item.slug !== authCollection)
33
+ return item;
34
+ const authConfig = typeof item.auth === 'object' ? item.auth : {};
35
+ return {
36
+ ...item,
37
+ auth: {
38
+ ...authConfig,
39
+ strategies: [...(authConfig.strategies ?? []), strategy],
40
+ },
41
+ };
42
+ }),
43
+ ...(existing ? [] : [collection]),
44
+ ],
45
+ };
46
+ };
47
+ }
48
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,uBAAuB,EAAE,MAAM,iBAAiB,CAAC;AAC1D,OAAO,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC;AAErD,OAAO,EACL,iBAAiB,EACjB,kBAAkB,EAClB,eAAe,EACf,eAAe,GAChB,MAAM,mBAAmB,CAAC;AAW3B,MAAM,UAAU,aAAa,CAAC,UAAgC,EAAE;IAC9D,OAAO,CAAC,MAAM,EAAE,EAAE;QAChB,MAAM,cAAc,GAAG,OAAO,CAAC,cAAc,IAAI,OAAO,CAAC;QACzD,MAAM,cAAc,GAAG,OAAO,CAAC,cAAc,IAAI,UAAU,CAAC;QAC5D,MAAM,IAAI,GAAG,MAAM,CAAC,WAAW,CAAC,IAAI,CAA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AC,UAAU,IAAI,EAAE,CAAC,EAAE,QAAQ,CAAC;yBACzD;qBACF,CAAC;gBACJ,CAAC,CAAC;gBACF,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC;aAClC;SACF,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,11 @@
1
+ export type ApiKeyTokenOptions = {
2
+ tokenPrefix?: string;
3
+ };
4
+ export type ApiKeyHeaderOptions = {
5
+ headerNames?: string[];
6
+ };
7
+ export declare function createApiKeyToken({ tokenPrefix }?: ApiKeyTokenOptions): string;
8
+ export declare function hashApiKeyToken(token: string): string;
9
+ export declare function getApiKeyPrefix(token: string): string;
10
+ export declare function extractApiKeyToken(headers: Headers, { headerNames }?: ApiKeyHeaderOptions): string | null;
11
+ //# sourceMappingURL=token.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"token.d.ts","sourceRoot":"","sources":["../../src/server/token.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,kBAAkB,GAAG;IAC/B,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,mBAAmB,GAAG;IAChC,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;CACxB,CAAC;AAEF,wBAAgB,iBAAiB,CAAC,EAAE,WAAmB,EAAE,GAAE,kBAAuB,GAAG,MAAM,CAE1F;AAED,wBAAgB,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAErD;AAED,wBAAgB,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAErD;AAED,wBAAgB,kBAAkB,CAChC,OAAO,EAAE,OAAO,EAChB,EAAE,WAA2B,EAAE,GAAE,mBAAwB,GACxD,MAAM,GAAG,IAAI,CAWf"}
@@ -0,0 +1,23 @@
1
+ import { createHash, randomBytes } from 'node:crypto';
2
+ export function createApiKeyToken({ tokenPrefix = 'fbt' } = {}) {
3
+ return `${tokenPrefix}_${randomBytes(32).toString('base64url')}`;
4
+ }
5
+ export function hashApiKeyToken(token) {
6
+ return createHash('sha256').update(token).digest('hex');
7
+ }
8
+ export function getApiKeyPrefix(token) {
9
+ return token.slice(0, 12);
10
+ }
11
+ export function extractApiKeyToken(headers, { headerNames = ['x-api-key'] } = {}) {
12
+ const authorization = headers.get('authorization');
13
+ const bearer = authorization?.match(/^Bearer ([^\s]+)$/i)?.[1];
14
+ if (bearer)
15
+ return bearer;
16
+ for (const headerName of headerNames) {
17
+ const value = headers.get(headerName)?.trim();
18
+ if (value && !/\s/.test(value))
19
+ return value;
20
+ }
21
+ return null;
22
+ }
23
+ //# sourceMappingURL=token.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"token.js","sourceRoot":"","sources":["../../src/server/token.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAUtD,MAAM,UAAU,iBAAiB,CAAC,EAAE,WAAW,GAAG,KAAK,KAAyB,EAAE;IAChF,OAAO,GAAG,WAAW,IAAI,WAAW,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,WAAW,CAAC,EAAE,CAAC;AACnE,CAAC;AAED,MAAM,UAAU,eAAe,CAAC,KAAa;IAC3C,OAAO,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAC1D,CAAC;AAED,MAAM,UAAU,eAAe,CAAC,KAAa;IAC3C,OAAO,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;AAC5B,CAAC;AAED,MAAM,UAAU,kBAAkB,CAChC,OAAgB,EAChB,EAAE,WAAW,GAAG,CAAC,WAAW,CAAC,KAA0B,EAAE;IAEzD,MAAM,aAAa,GAAG,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC;IACnD,MAAM,MAAM,GAAG,aAAa,EAAE,KAAK,CAAC,oBAAoB,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;IAC/D,IAAI,MAAM;QAAE,OAAO,MAAM,CAAC;IAE1B,KAAK,MAAM,UAAU,IAAI,WAAW,EAAE,CAAC;QACrC,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,EAAE,IAAI,EAAE,CAAC;QAC9C,IAAI,KAAK,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC;YAAE,OAAO,KAAK,CAAC;IAC/C,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC"}
@@ -0,0 +1,10 @@
1
+ import type { AuthConfig } from 'frogbot';
2
+ type StrategyOptions = {
3
+ authCollection: string;
4
+ collectionSlug: string;
5
+ headerNames?: string[];
6
+ tokenPrefix: string;
7
+ };
8
+ export declare function createApiKeyStrategy(options: StrategyOptions): NonNullable<AuthConfig['strategies']>[number];
9
+ export {};
10
+ //# sourceMappingURL=strategy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"strategy.d.ts","sourceRoot":"","sources":["../src/strategy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAI1C,KAAK,eAAe,GAAG;IACrB,cAAc,EAAE,MAAM,CAAC;IACvB,cAAc,EAAE,MAAM,CAAC;IACvB,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;IACvB,WAAW,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,eAAe,GAAG,WAAW,CAAC,UAAU,CAAC,YAAY,CAAC,CAAC,CAAC,MAAM,CAAC,CA2C5G"}
@@ -0,0 +1,44 @@
1
+ import { extractApiKeyToken, hashApiKeyToken } from './server/token.js';
2
+ export function createApiKeyStrategy(options) {
3
+ const { authCollection, collectionSlug, headerNames, tokenPrefix } = options;
4
+ return {
5
+ name: 'api-key',
6
+ authenticate: async ({ headers, payload }) => {
7
+ const token = extractApiKeyToken(headers, { headerNames });
8
+ if (!token || !new RegExp(`^${tokenPrefix.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}_[A-Za-z0-9_-]{43}$`).test(token)) {
9
+ return { user: null };
10
+ }
11
+ const keys = await payload.find({
12
+ collection: collectionSlug,
13
+ depth: 0,
14
+ limit: 1,
15
+ overrideAccess: true,
16
+ where: {
17
+ and: [{ tokenHash: { equals: hashApiKeyToken(token) } }, { revokedAt: { exists: false } }],
18
+ },
19
+ });
20
+ const key = keys.docs[0];
21
+ if (!key?.owner)
22
+ return { user: null };
23
+ const user = await payload
24
+ .findByID({ collection: authCollection, id: key.owner, depth: 0, overrideAccess: true })
25
+ .catch(() => null);
26
+ if (!user)
27
+ return { user: null };
28
+ await payload.update({
29
+ collection: collectionSlug,
30
+ id: key.id,
31
+ data: { lastUsedAt: new Date().toISOString() },
32
+ overrideAccess: true,
33
+ });
34
+ return {
35
+ user: {
36
+ ...user,
37
+ collection: authCollection,
38
+ _strategy: 'api-key',
39
+ },
40
+ };
41
+ },
42
+ };
43
+ }
44
+ //# sourceMappingURL=strategy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"strategy.js","sourceRoot":"","sources":["../src/strategy.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AASxE,MAAM,UAAU,oBAAoB,CAAC,OAAwB;IAC3D,MAAM,EAAE,cAAc,EAAE,cAAc,EAAE,WAAW,EAAE,WAAW,EAAE,GAAG,OAAO,CAAC;IAC7E,OAAO;QACL,IAAI,EAAE,SAAS;QACf,YAAY,EAAE,KAAK,EAAE,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE,EAAE;YAC3C,MAAM,KAAK,GAAG,kBAAkB,CAAC,OAAO,EAAE,EAAE,WAAW,EAAE,CAAC,CAAC;YAC3D,IAAI,CAAC,KAAK,IAAI,CAAC,IAAI,MAAM,CAAC,IAAI,WAAW,CAAC,OAAO,CAAC,qB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I;oBACP,UAAU,EAAE,cAAc;oBAC1B,SAAS,EAAE,SAAS;iBACrB;aACF,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC"}
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@frogbotai/plugin-api-keys",
3
+ "version": "0.4.0",
4
+ "description": "Multiple named API keys for FrogBot.",
5
+ "license": "MIT",
6
+ "author": "Colby Gilbert",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/frogbotai/frogbot.git",
10
+ "directory": "packages/plugin-api-keys"
11
+ },
12
+ "homepage": "https://github.com/frogbotai/frogbot#readme",
13
+ "type": "module",
14
+ "main": "./dist/index.js",
15
+ "types": "./dist/index.d.ts",
16
+ "exports": {
17
+ ".": {
18
+ "import": "./dist/index.js",
19
+ "types": "./dist/index.d.ts",
20
+ "default": "./dist/index.js"
21
+ },
22
+ "./client": {
23
+ "import": "./dist/client.js",
24
+ "types": "./dist/client.d.ts",
25
+ "default": "./dist/client.js"
26
+ }
27
+ },
28
+ "files": [
29
+ "dist"
30
+ ],
31
+ "sideEffects": false,
32
+ "peerDependencies": {
33
+ "@payloadcms/ui": "3.85.1",
34
+ "react": "^19.0.1 || ^19.1.2 || ^19.2.1",
35
+ "react-dom": "^19.0.1 || ^19.1.2 || ^19.2.1",
36
+ "frogbot": "0.4.0"
37
+ },
38
+ "devDependencies": {
39
+ "@payloadcms/ui": "3.85.1",
40
+ "@types/react": "19.2.14",
41
+ "react": "19.2.6",
42
+ "react-dom": "19.2.6",
43
+ "typescript": "5.6.2",
44
+ "frogbot": "0.4.0"
45
+ },
46
+ "publishConfig": {
47
+ "access": "public"
48
+ },
49
+ "scripts": {
50
+ "build": "tsc -p tsconfig.json",
51
+ "clean": "rm -rf dist",
52
+ "typecheck": "tsc --noEmit"
53
+ }
54
+ }