@block-auth.io/node 0.0.0-stage → 0.1.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.
@@ -0,0 +1,132 @@
1
+ declare const DEFAULT_WS_URL = "wss://wss.block-auth.io";
2
+ interface BlockAuthRole {
3
+ slug?: string;
4
+ name?: string;
5
+ [key: string]: unknown;
6
+ }
7
+ interface AuthorizeResult {
8
+ url: string;
9
+ expiresAt: string;
10
+ }
11
+ interface TokenResult {
12
+ address: string;
13
+ did?: string;
14
+ signedNonce?: number;
15
+ signedAt?: string;
16
+ signedExpiredAt: string;
17
+ roles: BlockAuthRole[];
18
+ externalId?: string | null;
19
+ }
20
+ interface WebhookEvent {
21
+ event: string;
22
+ apiKey: string;
23
+ address: string;
24
+ externalId?: string | null;
25
+ sessionId?: string | null;
26
+ reason?: string;
27
+ iat: number;
28
+ jti: string;
29
+ }
30
+ interface AuthCodeClientOptions {
31
+ wsUrl?: string;
32
+ timeoutMs?: number;
33
+ fetch?: typeof fetch;
34
+ }
35
+ declare function sha256Hex(value: string): string;
36
+ declare function buildWsServerApiUrl(path: string, wsUrl?: string): string;
37
+ declare class AuthCodeClient {
38
+ readonly apiKey: string;
39
+ private readonly secretHash;
40
+ private readonly wsUrl;
41
+ private readonly timeoutMs;
42
+ private readonly fetchImpl;
43
+ constructor(apiKey: string, apiSecret: string, options?: AuthCodeClientOptions);
44
+ authorize(redirectUri: string, state: string, responseMode?: string): Promise<AuthorizeResult>;
45
+ exchangeCode(code: string, redirectUri: string, codeVerifier?: string, linkExternalId?: string): Promise<TokenResult>;
46
+ signOut(address: string): Promise<{
47
+ closed?: number;
48
+ }>;
49
+ revokeLink(externalId: string): Promise<{
50
+ revoked: boolean;
51
+ }>;
52
+ verifyWebhook(rawBody: string | Buffer, timestamp: string | undefined, signature: string | undefined, maxSkewSeconds?: number): WebhookEvent;
53
+ private post;
54
+ }
55
+
56
+ declare const DEFAULT_BASE_PATH = "/auth/blockauth";
57
+ declare const ROUTE_NAMES: readonly ["code", "me", "logout", "link", "webhook"];
58
+ type RouteName = (typeof ROUTE_NAMES)[number];
59
+ interface SessionUser {
60
+ address: string;
61
+ did: string;
62
+ roles: BlockAuthRole[];
63
+ externalId: string | null;
64
+ loggedInAt: number;
65
+ expiresAt: number;
66
+ data?: Record<string, unknown>;
67
+ }
68
+ interface SessionStore {
69
+ get(key: string): unknown | Promise<unknown>;
70
+ set(key: string, value: unknown): void | Promise<void>;
71
+ remove(key: string): void | Promise<void>;
72
+ regenerate(): void | Promise<void>;
73
+ }
74
+ interface AppUser {
75
+ id: string | number;
76
+ linked?: boolean;
77
+ }
78
+ interface RouteContext {
79
+ session: SessionStore;
80
+ request?: unknown;
81
+ }
82
+ interface RouteResponse {
83
+ status: number;
84
+ body: Record<string, unknown>;
85
+ }
86
+ type MaybePromise<T> = T | Promise<T>;
87
+ interface BlockAuthRoutesOptions {
88
+ apiKey: string;
89
+ apiSecret: string;
90
+ appUrl?: string;
91
+ allowedOrigins?: string[];
92
+ sessionKey?: string;
93
+ maxSessionSeconds?: number;
94
+ basePath?: string;
95
+ routes?: Partial<Record<RouteName, boolean>>;
96
+ onLogin?: (user: SessionUser, context: RouteContext) => MaybePromise<Record<string, unknown> | false | void>;
97
+ onLogout?: (user: SessionUser, context: RouteContext) => MaybePromise<void>;
98
+ appUser?: (context: RouteContext) => MaybePromise<AppUser | null | undefined>;
99
+ onLinked?: (appUserId: string | number, user: TokenResult, context: RouteContext) => MaybePromise<void>;
100
+ onSignout?: (event: WebhookEvent) => MaybePromise<void>;
101
+ onUnlinked?: (event: WebhookEvent) => MaybePromise<void>;
102
+ isReplay?: (jti: string, event: WebhookEvent) => MaybePromise<boolean>;
103
+ isRevoked?: (user: SessionUser) => MaybePromise<boolean>;
104
+ webhookMaxSkewSeconds?: number;
105
+ wsUrl?: string;
106
+ timeoutMs?: number;
107
+ client?: AuthCodeClient;
108
+ logger?: (message: string, error?: unknown) => void;
109
+ }
110
+ declare class BlockAuthRoutes {
111
+ readonly client: AuthCodeClient;
112
+ readonly basePath: string;
113
+ readonly allowedOrigins: ReadonlySet<string>;
114
+ private readonly options;
115
+ private readonly sessionKey;
116
+ private readonly enabled;
117
+ constructor(options: BlockAuthRoutesOptions);
118
+ currentUser(context: RouteContext): Promise<SessionUser | null>;
119
+ code(body: unknown, context: RouteContext): Promise<RouteResponse>;
120
+ me(context: RouteContext): Promise<RouteResponse>;
121
+ logout(context: RouteContext): Promise<RouteResponse>;
122
+ linkStatus(context: RouteContext): Promise<RouteResponse>;
123
+ link(body: unknown, context: RouteContext): Promise<RouteResponse>;
124
+ webhook(rawBody: string | Buffer, timestamp?: string, signature?: string): Promise<RouteResponse>;
125
+ dispatch(method: string, path: string, rawBody: string | Buffer, header: (name: string) => string | undefined, context: RouteContext): Promise<RouteResponse | null>;
126
+ private codeParams;
127
+ private expiresAt;
128
+ private resolveAppUser;
129
+ private log;
130
+ }
131
+
132
+ export { type AppUser as A, BlockAuthRoutes as B, DEFAULT_BASE_PATH as D, type RouteResponse as R, type SessionStore as S, type TokenResult as T, type WebhookEvent as W, AuthCodeClient as a, type AuthCodeClientOptions as b, type AuthorizeResult as c, type BlockAuthRole as d, type BlockAuthRoutesOptions as e, DEFAULT_WS_URL as f, type RouteContext as g, type RouteName as h, type SessionUser as i, buildWsServerApiUrl as j, sha256Hex as s };
@@ -0,0 +1,132 @@
1
+ declare const DEFAULT_WS_URL = "wss://wss.block-auth.io";
2
+ interface BlockAuthRole {
3
+ slug?: string;
4
+ name?: string;
5
+ [key: string]: unknown;
6
+ }
7
+ interface AuthorizeResult {
8
+ url: string;
9
+ expiresAt: string;
10
+ }
11
+ interface TokenResult {
12
+ address: string;
13
+ did?: string;
14
+ signedNonce?: number;
15
+ signedAt?: string;
16
+ signedExpiredAt: string;
17
+ roles: BlockAuthRole[];
18
+ externalId?: string | null;
19
+ }
20
+ interface WebhookEvent {
21
+ event: string;
22
+ apiKey: string;
23
+ address: string;
24
+ externalId?: string | null;
25
+ sessionId?: string | null;
26
+ reason?: string;
27
+ iat: number;
28
+ jti: string;
29
+ }
30
+ interface AuthCodeClientOptions {
31
+ wsUrl?: string;
32
+ timeoutMs?: number;
33
+ fetch?: typeof fetch;
34
+ }
35
+ declare function sha256Hex(value: string): string;
36
+ declare function buildWsServerApiUrl(path: string, wsUrl?: string): string;
37
+ declare class AuthCodeClient {
38
+ readonly apiKey: string;
39
+ private readonly secretHash;
40
+ private readonly wsUrl;
41
+ private readonly timeoutMs;
42
+ private readonly fetchImpl;
43
+ constructor(apiKey: string, apiSecret: string, options?: AuthCodeClientOptions);
44
+ authorize(redirectUri: string, state: string, responseMode?: string): Promise<AuthorizeResult>;
45
+ exchangeCode(code: string, redirectUri: string, codeVerifier?: string, linkExternalId?: string): Promise<TokenResult>;
46
+ signOut(address: string): Promise<{
47
+ closed?: number;
48
+ }>;
49
+ revokeLink(externalId: string): Promise<{
50
+ revoked: boolean;
51
+ }>;
52
+ verifyWebhook(rawBody: string | Buffer, timestamp: string | undefined, signature: string | undefined, maxSkewSeconds?: number): WebhookEvent;
53
+ private post;
54
+ }
55
+
56
+ declare const DEFAULT_BASE_PATH = "/auth/blockauth";
57
+ declare const ROUTE_NAMES: readonly ["code", "me", "logout", "link", "webhook"];
58
+ type RouteName = (typeof ROUTE_NAMES)[number];
59
+ interface SessionUser {
60
+ address: string;
61
+ did: string;
62
+ roles: BlockAuthRole[];
63
+ externalId: string | null;
64
+ loggedInAt: number;
65
+ expiresAt: number;
66
+ data?: Record<string, unknown>;
67
+ }
68
+ interface SessionStore {
69
+ get(key: string): unknown | Promise<unknown>;
70
+ set(key: string, value: unknown): void | Promise<void>;
71
+ remove(key: string): void | Promise<void>;
72
+ regenerate(): void | Promise<void>;
73
+ }
74
+ interface AppUser {
75
+ id: string | number;
76
+ linked?: boolean;
77
+ }
78
+ interface RouteContext {
79
+ session: SessionStore;
80
+ request?: unknown;
81
+ }
82
+ interface RouteResponse {
83
+ status: number;
84
+ body: Record<string, unknown>;
85
+ }
86
+ type MaybePromise<T> = T | Promise<T>;
87
+ interface BlockAuthRoutesOptions {
88
+ apiKey: string;
89
+ apiSecret: string;
90
+ appUrl?: string;
91
+ allowedOrigins?: string[];
92
+ sessionKey?: string;
93
+ maxSessionSeconds?: number;
94
+ basePath?: string;
95
+ routes?: Partial<Record<RouteName, boolean>>;
96
+ onLogin?: (user: SessionUser, context: RouteContext) => MaybePromise<Record<string, unknown> | false | void>;
97
+ onLogout?: (user: SessionUser, context: RouteContext) => MaybePromise<void>;
98
+ appUser?: (context: RouteContext) => MaybePromise<AppUser | null | undefined>;
99
+ onLinked?: (appUserId: string | number, user: TokenResult, context: RouteContext) => MaybePromise<void>;
100
+ onSignout?: (event: WebhookEvent) => MaybePromise<void>;
101
+ onUnlinked?: (event: WebhookEvent) => MaybePromise<void>;
102
+ isReplay?: (jti: string, event: WebhookEvent) => MaybePromise<boolean>;
103
+ isRevoked?: (user: SessionUser) => MaybePromise<boolean>;
104
+ webhookMaxSkewSeconds?: number;
105
+ wsUrl?: string;
106
+ timeoutMs?: number;
107
+ client?: AuthCodeClient;
108
+ logger?: (message: string, error?: unknown) => void;
109
+ }
110
+ declare class BlockAuthRoutes {
111
+ readonly client: AuthCodeClient;
112
+ readonly basePath: string;
113
+ readonly allowedOrigins: ReadonlySet<string>;
114
+ private readonly options;
115
+ private readonly sessionKey;
116
+ private readonly enabled;
117
+ constructor(options: BlockAuthRoutesOptions);
118
+ currentUser(context: RouteContext): Promise<SessionUser | null>;
119
+ code(body: unknown, context: RouteContext): Promise<RouteResponse>;
120
+ me(context: RouteContext): Promise<RouteResponse>;
121
+ logout(context: RouteContext): Promise<RouteResponse>;
122
+ linkStatus(context: RouteContext): Promise<RouteResponse>;
123
+ link(body: unknown, context: RouteContext): Promise<RouteResponse>;
124
+ webhook(rawBody: string | Buffer, timestamp?: string, signature?: string): Promise<RouteResponse>;
125
+ dispatch(method: string, path: string, rawBody: string | Buffer, header: (name: string) => string | undefined, context: RouteContext): Promise<RouteResponse | null>;
126
+ private codeParams;
127
+ private expiresAt;
128
+ private resolveAppUser;
129
+ private log;
130
+ }
131
+
132
+ export { type AppUser as A, BlockAuthRoutes as B, DEFAULT_BASE_PATH as D, type RouteResponse as R, type SessionStore as S, type TokenResult as T, type WebhookEvent as W, AuthCodeClient as a, type AuthCodeClientOptions as b, type AuthorizeResult as c, type BlockAuthRole as d, type BlockAuthRoutesOptions as e, DEFAULT_WS_URL as f, type RouteContext as g, type RouteName as h, type SessionUser as i, buildWsServerApiUrl as j, sha256Hex as s };
package/package.json CHANGED
@@ -1,6 +1,55 @@
1
1
  {
2
2
  "name": "@block-auth.io/node",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Block-Auth backend SDK for Node.js: login code exchange, backend routes, sessions and webhooks.",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "require": "./dist/index.cjs"
14
+ },
15
+ "./express": {
16
+ "types": "./dist/express.d.ts",
17
+ "import": "./dist/express.js",
18
+ "require": "./dist/express.cjs"
19
+ }
20
+ },
21
+ "files": [
22
+ "dist/**/*"
23
+ ],
24
+ "engines": {
25
+ "node": ">=18.17"
26
+ },
27
+ "scripts": {
28
+ "build": "tsup",
29
+ "dev": "tsup --watch --no-clean",
30
+ "test": "vitest run",
31
+ "typecheck": "tsc --noEmit",
32
+ "lint": "eslint src"
33
+ },
34
+ "peerDependencies": {
35
+ "express": "^4.18.0 || ^5.0.0"
36
+ },
37
+ "peerDependenciesMeta": {
38
+ "express": {
39
+ "optional": true
40
+ }
41
+ },
42
+ "devDependencies": {
43
+ "@types/express": "^5.0.0",
44
+ "@types/express-session": "^1.18.0",
45
+ "@types/node": "^22.0.0",
46
+ "express": "^5.1.0",
47
+ "express-session": "^1.18.0",
48
+ "tsup": "^8.0.0",
49
+ "typescript": "^5.4.0",
50
+ "vitest": "^1.5.0"
51
+ },
52
+ "publishConfig": {
53
+ "access": "public"
54
+ }
55
+ }