postex-auth-sdk 1.0.0 → 1.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.
package/dist/auth.d.ts ADDED
@@ -0,0 +1,189 @@
1
+ interface AuthSDKConfig {
2
+ apiKey?: string;
3
+ }
4
+ type AuthStatusType = "no_session" | "session_found" | "webauthn_ready";
5
+ interface AuthStatusResponse {
6
+ status: AuthStatusType;
7
+ email?: string;
8
+ webauthn?: boolean;
9
+ challenge?: string;
10
+ credentialIds?: string[];
11
+ rp?: {
12
+ name: string;
13
+ host: string;
14
+ };
15
+ }
16
+ /** Response from POST /auth/initiate */
17
+ type InitiateAuthStatusType = "webauthn_challenge" | "otp_sent";
18
+ interface InitiateAuthResponse {
19
+ status: InitiateAuthStatusType;
20
+ challenge?: string;
21
+ credentialIds?: string[];
22
+ rp?: {
23
+ name: string;
24
+ host: string;
25
+ };
26
+ }
27
+ interface OTPVerifyResponse {
28
+ access_token: string;
29
+ id_token: string;
30
+ refresh_token: string;
31
+ expires_in: number;
32
+ token_type: string;
33
+ verified: boolean;
34
+ email: string;
35
+ [key: string]: any;
36
+ }
37
+ interface PasskeyRegistrationChallenge {
38
+ challenge: string;
39
+ rp: {
40
+ name: string;
41
+ id: string;
42
+ };
43
+ user: {
44
+ id: string;
45
+ name: string;
46
+ displayName: string;
47
+ };
48
+ pubKeyCredParams?: Array<{
49
+ type: string;
50
+ alg: number;
51
+ }>;
52
+ authenticatorSelection?: AuthenticatorSelectionCriteria;
53
+ timeout?: number;
54
+ attestation?: AttestationConveyancePreference;
55
+ /** Existing credential IDs to exclude (prevents duplicate registration) */
56
+ excludeCredentials?: Array<{
57
+ id: string;
58
+ type: "public-key";
59
+ transports?: AuthenticatorTransport[];
60
+ }>;
61
+ }
62
+ interface PasskeyRegisterResponse {
63
+ registered: boolean;
64
+ [key: string]: any;
65
+ }
66
+ interface PasskeyStatusResponse {
67
+ credentialId?: string;
68
+ hasCredentials: boolean;
69
+ [key: string]: any;
70
+ }
71
+ interface AuthResponse {
72
+ access_token: string;
73
+ refresh_token: string;
74
+ id_token?: string;
75
+ email: string;
76
+ name: string;
77
+ expiresIn?: number;
78
+ tokenType?: string;
79
+ [key: string]: any;
80
+ }
81
+ /** Response from POST /auth/refresh */
82
+ interface RefreshTokenResponse {
83
+ access_token: string;
84
+ id_token?: string;
85
+ token_type: string;
86
+ expires_in: number;
87
+ }
88
+ export declare class AuthSDKFetchError extends Error {
89
+ response: {
90
+ status: number;
91
+ data?: any;
92
+ };
93
+ constructor(status: number, data?: any, message?: string);
94
+ }
95
+ export declare class AuthSDK {
96
+ private config;
97
+ constructor(config: AuthSDKConfig);
98
+ private buildUrl;
99
+ private request;
100
+ /**
101
+ * Returns auth headers (Authorization + DPoP) for the given request.
102
+ * Use this to attach auth to your own HTTP client (e.g. axios request interceptor).
103
+ * @param method - HTTP method (e.g. "GET", "POST")
104
+ * @param url - Full request URL
105
+ */
106
+ getRequestAuthHeaders(method: string, url: string): Promise<Record<string, string>>;
107
+ /**
108
+ * GET /auth/status - Check if client has trusted device session and what auth method is available.
109
+ * Returns no_session | session_found | webauthn_ready per PostEx Auth BFF spec.
110
+ */
111
+ getStatus(email: string): Promise<AuthStatusResponse>;
112
+ /**
113
+ * POST /auth/initiate - Unified entry: returns webauthn_challenge or otp_sent.
114
+ * Sets auth_session cookie when otp_sent.
115
+ */
116
+ initiateAuth(email: string): Promise<InitiateAuthResponse>;
117
+ /**
118
+ * POST /otp/verify - Verifies the OTP code entered by the user.
119
+ * Stores tokens from the response.
120
+ */
121
+ verifyOTP(otp: string): Promise<OTPVerifyResponse>;
122
+ /**
123
+ * GET /magic-link - Verify a magic link token.
124
+ * Sets auth_session cookie on success.
125
+ */
126
+ verifyMagicLink(token: string, email: string): Promise<void>;
127
+ /**
128
+ * POST /magic-link/complete - Complete magic link authentication.
129
+ * Requires auth_session cookie set by verifyMagicLink.
130
+ * Stores tokens from the response.
131
+ */
132
+ completeMagicLink(): Promise<OTPVerifyResponse>;
133
+ initiatePasskeyRegistration(): Promise<PasskeyRegistrationChallenge>;
134
+ registerPasskey(email: string): Promise<PasskeyRegisterResponse>;
135
+ /**
136
+ * POST /webauthn/authenticate/challenge - Authenticate with passkey.
137
+ */
138
+ authenticateWithPasskey({ challenge, rp, credentialIds, }: {
139
+ challenge: string;
140
+ rp: {
141
+ name: string;
142
+ host: string;
143
+ };
144
+ credentialIds: string[];
145
+ }): Promise<AuthResponse>;
146
+ /**
147
+ * GET /webauthn/credentials/:username - Get user's passkey status.
148
+ */
149
+ getPasskeyStatus(username: string): Promise<PasskeyStatusResponse>;
150
+ /**
151
+ * DELETE /webauthn/credentials/:username - Remove passkey.
152
+ */
153
+ removePasskey(username: string): Promise<void>;
154
+ /**
155
+ * Signs a request with DPoP and Authorization headers (internal use).
156
+ */
157
+ signRequest(method: string, url: string, config?: any): Promise<any>;
158
+ /**
159
+ * Replaces native fetch or Axios with a DPoP-signed version.
160
+ */
161
+ authenticatedFetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response>;
162
+ /**
163
+ * Store tokens from /webauthn/authenticate (per spec: sessionStorage preferred).
164
+ */
165
+ storeTokens(tokens: {
166
+ accessToken: string;
167
+ refreshToken?: string;
168
+ idToken?: string;
169
+ expiresIn?: number;
170
+ tokenType?: string;
171
+ }): Promise<void>;
172
+ /**
173
+ * Clear stored tokens (call on logout).
174
+ */
175
+ clearTokens(): Promise<void>;
176
+ /**
177
+ * POST /auth/refresh - Refresh access token using server-stored refresh token.
178
+ * Requires trusted device cookie (td). Refresh token is stored server-side.
179
+ * Rate limit: 10 requests per minute.
180
+ */
181
+ refreshToken(): Promise<RefreshTokenResponse>;
182
+ /**
183
+ * POST /auth/logout - Logout from the current device.
184
+ * Revokes the current token and trusted device, then clears local tokens.
185
+ */
186
+ logout(): Promise<void>;
187
+ getAccessToken(): Promise<string | null>;
188
+ }
189
+ export {};
package/dist/main.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./auth";
2
+ export * from "./webauthn";
@@ -0,0 +1,75 @@
1
+ export declare function isWebAuthnSupported(): boolean;
2
+ export declare function isConditionalUISupported(): Promise<boolean>;
3
+ export declare function arrayBufferToBase64(buffer: ArrayBuffer): string;
4
+ export declare function base64ToArrayBuffer(base64: string): ArrayBuffer;
5
+ export declare function arrayBufferToBase64url(buffer: ArrayBuffer): string;
6
+ export declare function base64urlToArrayBuffer(input: string): ArrayBuffer;
7
+ export declare function stringToArrayBuffer(str: string): ArrayBuffer;
8
+ export declare function uint8ArrayToString(arr: Uint8Array): string;
9
+ /**
10
+ * Get the stored passkey email from IndexedDB
11
+ */
12
+ export declare function getPasskeyEmail(): Promise<string | null>;
13
+ export declare function setPasskeyEmail(email: string): Promise<void>;
14
+ /**
15
+ * Remove the passkey email from IndexedDB
16
+ */
17
+ export declare function clearPasskeyEmail(): Promise<void>;
18
+ /**
19
+ * Minimal JWK for EC P-256 public key (only required fields per RFC 7517)
20
+ */
21
+ export interface MinimalECPublicKeyJWK {
22
+ kty: "EC";
23
+ crv: "P-256";
24
+ x: string;
25
+ y: string;
26
+ }
27
+ /**
28
+ * Create a minimal JWK from a full JWK (removes ext, key_ops, etc.)
29
+ */
30
+ export declare function toMinimalJWK(jwk: JsonWebKey): MinimalECPublicKeyJWK;
31
+ /**
32
+ * Calculate JWK Thumbprint (SHA-256) per RFC 7638
33
+ * Uses canonical JSON with lexicographically ordered required members
34
+ */
35
+ export declare function calculateThumbprint(jwk: MinimalECPublicKeyJWK): Promise<string>;
36
+ /**
37
+ * Store DPoP Private Key in IndexedDB
38
+ */
39
+ declare function storeDPoPPrivateKey(key: CryptoKey): Promise<void>;
40
+ /**
41
+ * Retrieve DPoP Private Key from IndexedDB
42
+ */
43
+ export declare function getDPoPKey(): Promise<CryptoKey | null>;
44
+ /**
45
+ * Retrieve DPoP Public Key JWK from IndexedDB
46
+ */
47
+ export declare function getDPoPPublicKeyJWK(): Promise<MinimalECPublicKeyJWK | null>;
48
+ /**
49
+ * Clear stored DPoP Keys from IndexedDB
50
+ */
51
+ export declare function clearDPoPKey(): Promise<void>;
52
+ /**
53
+ * Generate a new ECDSA P-256 Key Pair for DPoP.
54
+ * Stores both Private Key (CryptoKey) and Public Key (JWK) in IndexedDB.
55
+ * Returns minimal Public Key JWK and its Thumbprint for backend transmission.
56
+ */
57
+ export declare function generateDPoPKeyPair(): Promise<{
58
+ publicKey: MinimalECPublicKeyJWK;
59
+ thumbprint: string;
60
+ }>;
61
+ /**
62
+ * Generate a DPoP proof JWT using ECDSA P-256 (ES256)
63
+ * Per RFC 9449 Section 4.2
64
+ *
65
+ * @param httpMethod - HTTP method (GET, POST, etc.)
66
+ * @param httpUri - Full URL of the request (without query/fragment for htu)
67
+ * @param accessToken - Optional access token to bind (adds ath claim)
68
+ */
69
+ export declare function generateDPoPProof(httpMethod: string, httpUri: string, accessToken?: string): Promise<string | null>;
70
+ /**
71
+ * Check if DPoP is enabled (Keys are stored)
72
+ */
73
+ export declare function isDPoPEnabled(): Promise<boolean>;
74
+ export declare const storeDPoPKey: typeof storeDPoPPrivateKey;
75
+ export {};
package/package.json CHANGED
@@ -1,11 +1,13 @@
1
1
  {
2
2
  "name": "postex-auth-sdk",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "PostEx Auth SDK for authentication flows: OTP, magic links, passkeys, and token management",
5
5
  "main": "./dist/postex-auth-sdk.umd.js",
6
6
  "module": "./dist/postex-auth-sdk.es.js",
7
7
  "types": "./dist/main.d.ts",
8
- "files": ["dist"],
8
+ "files": [
9
+ "dist"
10
+ ],
9
11
  "exports": {
10
12
  ".": {
11
13
  "types": "./dist/main.d.ts",
@@ -13,7 +15,14 @@
13
15
  "require": "./dist/postex-auth-sdk.umd.js"
14
16
  }
15
17
  },
16
- "keywords": ["auth", "webauthn", "passkey", "otp", "sdk", "postex"],
18
+ "keywords": [
19
+ "auth",
20
+ "webauthn",
21
+ "passkey",
22
+ "otp",
23
+ "sdk",
24
+ "postex"
25
+ ],
17
26
  "author": "",
18
27
  "license": "MIT",
19
28
  "repository": {
@@ -22,11 +31,14 @@
22
31
  },
23
32
  "scripts": {
24
33
  "dev": "vite",
25
- "build": "tsc && vite build",
34
+ "build": "vite build && tsc",
26
35
  "preview": "vite preview"
27
36
  },
28
37
  "devDependencies": {
29
38
  "typescript": "~5.9.3",
30
39
  "vite": "^7.3.1"
40
+ },
41
+ "dependencies": {
42
+ "postex-auth-sdk": "^1.0.0"
31
43
  }
32
44
  }