@safepassage/sdk 3.4.9 → 3.4.10
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/README.md +14 -10
- package/brands/safepassage/index.d.ts +12 -0
- package/brands/safepassage/urls.d.ts +3 -0
- package/core/VerificationSDK.d.ts +132 -0
- package/index.d.ts +2 -0
- package/index.js +1091 -0
- package/package.json +14 -47
- package/safepassage.min.js +3 -0
- package/sdk.min.js +3 -0
- package/{dist/types/index.d.ts → types/base.d.ts} +27 -10
- package/utils/crypto.d.ts +31 -0
- package/utils/environment.d.ts +8 -0
- package/{dist/utils → utils}/polyfills.d.ts +1 -2
- package/{dist/utils → utils}/security.d.ts +11 -11
- package/{dist/utils → utils}/validation.d.ts +9 -12
- package/dist/core/SafePassageSDK.d.ts +0 -231
- package/dist/core/SafePassageSDK.js +0 -656
- package/dist/index.d.ts +0 -21
- package/dist/index.js +0 -1100
- package/dist/safepassage.min.js +0 -3
- package/dist/types/index.js +0 -4
- package/dist/utils/__mocks__/polyfills.d.ts +0 -3
- package/dist/utils/__mocks__/polyfills.js +0 -10
- package/dist/utils/crypto.d.ts +0 -105
- package/dist/utils/crypto.js +0 -216
- package/dist/utils/environment.d.ts +0 -13
- package/dist/utils/environment.js +0 -75
- package/dist/utils/polyfills.js +0 -58
- package/dist/utils/security.js +0 -190
- package/dist/utils/validation.js +0 -158
package/README.md
CHANGED
|
@@ -67,12 +67,13 @@ That's it! The SDK handles session creation automatically.
|
|
|
67
67
|
|--------|------|----------|-------------|
|
|
68
68
|
| apiKey | string | Yes | Your public API key (`pk_...`) |
|
|
69
69
|
| returnUrl | string | Yes | URL to redirect after verification |
|
|
70
|
+
| cancelUrl | string | No | URL to redirect to if user closes the verification window (new-tab mode) |
|
|
70
71
|
| environment | string | No | `'production'` or `'staging'` (auto-detected) |
|
|
71
72
|
| mode | string | No | `'redirect'` (default) or `'new-tab'` |
|
|
72
73
|
| defaultChallengeAge | number | No | Default minimum age (25 or higher) |
|
|
73
74
|
| defaultVerificationMode | string | No | `'L1'` or `'L2'` |
|
|
74
75
|
| onComplete | function | No | Callback for new-tab mode |
|
|
75
|
-
| onCancel | function | No | Called when user closes popup (new-tab mode) |
|
|
76
|
+
| onCancel | function | No | Called when user closes popup (new-tab mode). Return `false` to suppress automatic `cancelUrl` redirect. |
|
|
76
77
|
| onError | function | No | Error handler |
|
|
77
78
|
|
|
78
79
|
## Verification Options
|
|
@@ -119,6 +120,7 @@ Verification opens in a popup window:
|
|
|
119
120
|
const sp = new SafePassage({
|
|
120
121
|
apiKey: 'pk_...',
|
|
121
122
|
returnUrl: '/age-verified',
|
|
123
|
+
cancelUrl: '/age-cancelled',
|
|
122
124
|
mode: 'new-tab',
|
|
123
125
|
onComplete: (result) => {
|
|
124
126
|
console.log('Verification complete:', result.sessionId, result.status);
|
|
@@ -126,6 +128,8 @@ const sp = new SafePassage({
|
|
|
126
128
|
},
|
|
127
129
|
onCancel: () => {
|
|
128
130
|
console.log('User closed the verification window');
|
|
131
|
+
// Return false if you want to handle navigation manually
|
|
132
|
+
// return false;
|
|
129
133
|
},
|
|
130
134
|
onError: (error) => {
|
|
131
135
|
console.error('Verification error:', error.message);
|
|
@@ -135,6 +139,10 @@ const sp = new SafePassage({
|
|
|
135
139
|
await sp.verify();
|
|
136
140
|
```
|
|
137
141
|
|
|
142
|
+
If `cancelUrl` is provided, the SDK will redirect the opener to `cancelUrl` with
|
|
143
|
+
`status=cancelled` and `sessionId` when the user closes the verification window.
|
|
144
|
+
Return `false` from `onCancel` to suppress the automatic redirect.
|
|
145
|
+
|
|
138
146
|
## Server-Side Validation (Required!)
|
|
139
147
|
|
|
140
148
|
After verification completes, **always validate the result on your server** before granting access:
|
|
@@ -146,7 +154,7 @@ app.get('/age-verified', async (req, res) => {
|
|
|
146
154
|
|
|
147
155
|
// Validate with your SECRET key (sk_...)
|
|
148
156
|
const response = await fetch(
|
|
149
|
-
`https://api.
|
|
157
|
+
`https://api.safepassage.live/api/v1/sessions/${sessionId}`,
|
|
150
158
|
{
|
|
151
159
|
headers: {
|
|
152
160
|
'Authorization': `Bearer ${process.env.SAFEPASSAGE_SECRET_KEY}`
|
|
@@ -183,8 +191,6 @@ For reliable verification tracking, configure webhooks in your dashboard:
|
|
|
183
191
|
}
|
|
184
192
|
```
|
|
185
193
|
|
|
186
|
-
Webhook `timestamp` values are ISO 8601 strings. SDK `onComplete` results use milliseconds since epoch.
|
|
187
|
-
|
|
188
194
|
## Complete Example
|
|
189
195
|
|
|
190
196
|
### HTML + CDN
|
|
@@ -253,8 +259,6 @@ import { SafePassage, SafePassageConfig, VerificationResult } from '@safepassage
|
|
|
253
259
|
const config: SafePassageConfig = {
|
|
254
260
|
apiKey: process.env.NEXT_PUBLIC_SAFEPASSAGE_KEY!,
|
|
255
261
|
returnUrl: '/verified',
|
|
256
|
-
// Optional: used as a fallback redirect for Emblem login failures
|
|
257
|
-
cancelUrl: '/verify-cancelled',
|
|
258
262
|
mode: 'new-tab',
|
|
259
263
|
onComplete: (result: VerificationResult) => {
|
|
260
264
|
console.log(`Session ${result.sessionId}: ${result.status}`);
|
|
@@ -306,7 +310,7 @@ SafePassage uses two types of API keys:
|
|
|
306
310
|
| Public Key | `pk_` | Client-side SDK (this package) |
|
|
307
311
|
| Secret Key | `sk_` | Server-side validation only |
|
|
308
312
|
|
|
309
|
-
> **Important**: This SDK only works with public keys (`pk_`). For server-side integrations using secret keys, use the [Direct API](https://docs.
|
|
313
|
+
> **Important**: This SDK only works with public keys (`pk_`). For server-side integrations using secret keys, use the [Direct API](https://docs.safepassage.live/api) instead.
|
|
310
314
|
|
|
311
315
|
## Browser Support
|
|
312
316
|
|
|
@@ -348,6 +352,6 @@ The SDK now creates sessions automatically via the API when using public keys.
|
|
|
348
352
|
|
|
349
353
|
## Support
|
|
350
354
|
|
|
351
|
-
- [Documentation](https://docs.
|
|
352
|
-
- [API Reference](https://docs.
|
|
353
|
-
- [Dashboard](https://portal.
|
|
355
|
+
- [Documentation](https://docs.safepassage.live)
|
|
356
|
+
- [API Reference](https://docs.safepassage.live/api)
|
|
357
|
+
- [Dashboard](https://portal.safepassage.live)
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SafePassage SDK - Redirect-based age verification
|
|
3
|
+
*/
|
|
4
|
+
import { VerificationSDK } from '../../core/VerificationSDK';
|
|
5
|
+
import type { SDKConfig, VerificationOptions, VerificationResult, SessionValidationResponse, SessionCreationResponse, CreateSessionRequest } from '../../types/base';
|
|
6
|
+
export type SafePassageConfig = SDKConfig;
|
|
7
|
+
export declare class SafePassage extends VerificationSDK {
|
|
8
|
+
constructor(config: SafePassageConfig);
|
|
9
|
+
}
|
|
10
|
+
export declare const VERSION = "3.4.9";
|
|
11
|
+
export type { SDKConfig, VerificationOptions, VerificationResult, SessionValidationResponse, SessionCreationResponse, CreateSessionRequest, };
|
|
12
|
+
export default SafePassage;
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Verification SDK - Redirect-based age verification
|
|
3
|
+
*
|
|
4
|
+
* Lightweight SDK for integrating age verification using redirect flow.
|
|
5
|
+
* Provides a secure, easy-to-implement solution with comprehensive
|
|
6
|
+
* security features and flexible integration options.
|
|
7
|
+
*/
|
|
8
|
+
import type { SDKConfig, VerificationOptions } from '../types/base';
|
|
9
|
+
export interface UrlConfig {
|
|
10
|
+
apiUrl: string;
|
|
11
|
+
verifyUiUrl: string;
|
|
12
|
+
engineUrl: string;
|
|
13
|
+
wsUrl: string;
|
|
14
|
+
trustedOrigins: string[];
|
|
15
|
+
}
|
|
16
|
+
export interface BrandUrls {
|
|
17
|
+
production: UrlConfig;
|
|
18
|
+
staging: UrlConfig;
|
|
19
|
+
}
|
|
20
|
+
export interface BrandConstants {
|
|
21
|
+
name: string;
|
|
22
|
+
hmacSecretProd: string;
|
|
23
|
+
hmacSecretStaging: string;
|
|
24
|
+
messageType: string;
|
|
25
|
+
legacyMessageType?: string;
|
|
26
|
+
popupName: string;
|
|
27
|
+
docsUrl: string;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* SDK Main Class
|
|
31
|
+
*
|
|
32
|
+
* Primary SDK class that manages verification sessions with comprehensive
|
|
33
|
+
* security and error handling. Supports both redirect and new-tab modes with
|
|
34
|
+
* automatic session management and PostMessage communication.
|
|
35
|
+
*/
|
|
36
|
+
export declare class VerificationSDK {
|
|
37
|
+
private config;
|
|
38
|
+
private readonly brandUrls;
|
|
39
|
+
private readonly brandConstants;
|
|
40
|
+
private popupWindow;
|
|
41
|
+
private messageListener;
|
|
42
|
+
private popupMonitorInterval;
|
|
43
|
+
private unloadListener;
|
|
44
|
+
private isVerificationInProgress;
|
|
45
|
+
private currentSessionId;
|
|
46
|
+
private hasReceivedResult;
|
|
47
|
+
private lastVerifyUrl;
|
|
48
|
+
private lastSessionToken;
|
|
49
|
+
private lastExternalUserId;
|
|
50
|
+
private temporaryHandoffToken;
|
|
51
|
+
private static readonly LOCAL_HOSTNAMES;
|
|
52
|
+
/**
|
|
53
|
+
* Initialize SDK
|
|
54
|
+
*
|
|
55
|
+
* Validates configuration, sets up security measures, and prepares the SDK
|
|
56
|
+
* for verification operations. Performs comprehensive environment validation
|
|
57
|
+
* and security initialization.
|
|
58
|
+
*/
|
|
59
|
+
constructor(config: SDKConfig, brandUrls: BrandUrls, brandConstants: BrandConstants);
|
|
60
|
+
/**
|
|
61
|
+
* Initiate verification with race condition protection
|
|
62
|
+
*/
|
|
63
|
+
verify(options?: VerificationOptions): Promise<void>;
|
|
64
|
+
/**
|
|
65
|
+
* Build verification URL with HMAC-signed state
|
|
66
|
+
*/
|
|
67
|
+
private buildVerificationUrl;
|
|
68
|
+
/**
|
|
69
|
+
* Redirect in same tab
|
|
70
|
+
*/
|
|
71
|
+
private redirect;
|
|
72
|
+
/**
|
|
73
|
+
* Open in new tab with PostMessage communication and proper cleanup
|
|
74
|
+
*/
|
|
75
|
+
private openNewTab;
|
|
76
|
+
/**
|
|
77
|
+
* Set up automatic cleanup on page unload to prevent memory leaks
|
|
78
|
+
*/
|
|
79
|
+
private setupAutoCleanup;
|
|
80
|
+
/**
|
|
81
|
+
* Auto-detect environment based on current URL
|
|
82
|
+
*/
|
|
83
|
+
private detectEnvironment;
|
|
84
|
+
/**
|
|
85
|
+
* Get the current environment
|
|
86
|
+
*/
|
|
87
|
+
getEnvironment(): 'production' | 'staging';
|
|
88
|
+
/**
|
|
89
|
+
* Unlock verification process to allow new verifications
|
|
90
|
+
*/
|
|
91
|
+
private unlockVerification;
|
|
92
|
+
/**
|
|
93
|
+
* Internal cleanup method to prevent memory leaks
|
|
94
|
+
*/
|
|
95
|
+
private cleanup;
|
|
96
|
+
private handleCancellation;
|
|
97
|
+
private redirectToCancelUrl;
|
|
98
|
+
/**
|
|
99
|
+
* Remove auto-cleanup listeners
|
|
100
|
+
*/
|
|
101
|
+
private removeAutoCleanupListeners;
|
|
102
|
+
/**
|
|
103
|
+
* Public cleanup method for manual resource management
|
|
104
|
+
*/
|
|
105
|
+
destroy(): void;
|
|
106
|
+
/**
|
|
107
|
+
* Get Portal API URL based on environment and brand
|
|
108
|
+
*/
|
|
109
|
+
private getPortalApiUrl;
|
|
110
|
+
/**
|
|
111
|
+
* Get Engine URL based on environment and brand
|
|
112
|
+
*/
|
|
113
|
+
private getEngineUrl;
|
|
114
|
+
/**
|
|
115
|
+
* Get WebSocket URL based on environment and brand
|
|
116
|
+
*/
|
|
117
|
+
private getWebSocketUrl;
|
|
118
|
+
/**
|
|
119
|
+
* Detect if this is a public key (pk_ prefix) vs private key (sk_ prefix)
|
|
120
|
+
*/
|
|
121
|
+
private isPublicKey;
|
|
122
|
+
/**
|
|
123
|
+
* Create session internally for public keys
|
|
124
|
+
*/
|
|
125
|
+
private createInternalSession;
|
|
126
|
+
private getUrlConfig;
|
|
127
|
+
private getTrustedOrigins;
|
|
128
|
+
private getAllowedCustomOrigins;
|
|
129
|
+
private getLocalOrigin;
|
|
130
|
+
private applyLocalVerifyOverride;
|
|
131
|
+
private getHmacSecret;
|
|
132
|
+
}
|
package/index.d.ts
ADDED