Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
".": {
"import": "./dist/index.js",
"types": "./dist/index.d.ts"
},
"./providers/api_key": {
"import": "./dist/providers/api_key.js",
"types": "./dist/providers/api_key.d.ts"
}
},
"files": [
Expand Down
2 changes: 1 addition & 1 deletion src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export type ErrorPayload = {
export class RuntError extends Error {
constructor(
public type: ErrorType,
private options: ExtensionErrorOptions
private options: ExtensionErrorOptions = {}
) {
super(options.message ?? `RuntError: ${type}`, { cause: options.cause });
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ApiKeyProvider } from './providers/api_key';
export type * from './providers/shared';
export * from './providers/shared';
export * from './errors';

export type BackendExtension = {
Expand Down
17 changes: 11 additions & 6 deletions src/providers/api_key.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Response as WorkerResponse } from '@cloudflare/workers-types';
import type { Passport, ProviderContext, Scope, Resource } from './shared';
import type { Passport, ProviderContext, Scope, Resource, AuthenticatedProviderContext } from './shared';

export enum ApiKeyCapabilities {
Revoke = 'revoke',
Expand All @@ -21,14 +21,19 @@ export type ApiKey = CreateApiKeyRequest & {
revoked: boolean;
};

export type ListApiKeysRequest = {
limit?: number;
offset?: number;
};

export type ApiKeyProvider = {
capabilities: Set<ApiKeyCapabilities>;
overrideHandler?: (context: ProviderContext) => Promise<false | WorkerResponse>; // Any routes the provider wants to fully implement. Return false for any route not handled
isApiKey(context: ProviderContext): boolean; // Returns true if the auth token appears to be an API key (whether or not it is valid)
validateApiKey(context: ProviderContext): Promise<Passport>; // Ensure the API key is valid, and returns the Passport. Raise an error otherwise
createApiKey: (context: ProviderContext, request: CreateApiKeyRequest) => Promise<string>;
getApiKey: (context: ProviderContext, id: string) => Promise<ApiKey>; // Returns the API key matching the specific api key id
listApiKeys: (context: ProviderContext) => Promise<ApiKey[]>; // Return a list of all API keys for a user
revokeApiKey: (context: ProviderContext, id: string) => Promise<void>; // set the revoked flag for an API key, such that it will no longer be valid
deleteApiKey: (context: ProviderContext, id: string) => Promise<void>; // Delete an API key from the database
createApiKey: (context: AuthenticatedProviderContext, request: CreateApiKeyRequest) => Promise<string>;
getApiKey: (context: AuthenticatedProviderContext, id: string) => Promise<ApiKey>; // Returns the API key matching the specific api key id
listApiKeys: (context: AuthenticatedProviderContext, request: ListApiKeysRequest) => Promise<ApiKey[]>; // Return a list of all API keys for a user
revokeApiKey: (context: AuthenticatedProviderContext, id: string) => Promise<void>; // set the revoked flag for an API key, such that it will no longer be valid
deleteApiKey: (context: AuthenticatedProviderContext, id: string) => Promise<void>; // Delete an API key from the database
};
8 changes: 7 additions & 1 deletion src/providers/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export type User = {
email: string;
name?: string;
givenName?: string;
familyName: string;
familyName?: string;
};

export type Resource = {
Expand All @@ -46,3 +46,9 @@ export type ProviderContext = {
bearerToken: string | null;
passport: Passport | null;
};

// https://github.com/microsoft/TypeScript/issues/28374
type NonNullableValues<T> = {
[P in keyof T]-?: NonNullable<T[P]>;
};
export type AuthenticatedProviderContext = NonNullableValues<ProviderContext>;