@safepassage/sdk 3.0.4 → 3.0.6
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 +2 -7
- package/dist/core/SafePassageSDK.d.ts +158 -5
- package/dist/core/SafePassageSDK.js +237 -42
- package/dist/index.d.ts +19 -115
- package/dist/index.js +9 -3
- package/dist/safepassage.min.js +2 -2
- package/dist/types/index.d.ts +10 -0
- package/dist/utils/__mocks__/polyfills.d.ts +3 -0
- package/dist/utils/__mocks__/polyfills.js +10 -0
- package/dist/utils/crypto.d.ts +80 -6
- package/dist/utils/crypto.js +92 -10
- package/dist/utils/environment.js +19 -11
- package/dist/utils/polyfills.js +20 -17
- package/dist/utils/security.d.ts +1 -1
- package/dist/utils/security.js +33 -21
- package/dist/utils/validation.js +20 -7
- package/package.json +17 -13
package/README.md
CHANGED
|
@@ -248,14 +248,9 @@ const config: SafePassageConfig = {
|
|
|
248
248
|
const safePassage = new SafePassage(config);
|
|
249
249
|
```
|
|
250
250
|
|
|
251
|
-
## Migration from v2
|
|
251
|
+
## Migration from v2
|
|
252
252
|
|
|
253
|
-
|
|
254
|
-
```javascript
|
|
255
|
-
// Complex iframe setup...
|
|
256
|
-
```
|
|
257
|
-
|
|
258
|
-
New redirect approach (10 lines):
|
|
253
|
+
New streamlined approach (10 lines):
|
|
259
254
|
```javascript
|
|
260
255
|
const safePassage = new SafePassage({
|
|
261
256
|
apiKey: 'sk_live_xxxxx',
|
|
@@ -1,8 +1,44 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* SafePassage SDK - Redirect-based age verification
|
|
3
|
-
*
|
|
3
|
+
*
|
|
4
|
+
* Lightweight SDK for integrating SafePassage age verification using redirect flow.
|
|
5
|
+
* Provides a secure, easy-to-implement solution for age verification with comprehensive
|
|
6
|
+
* security features and flexible integration options.
|
|
7
|
+
*
|
|
8
|
+
* Key Features:
|
|
9
|
+
* - Redirect and new-tab verification modes
|
|
10
|
+
* - Automatic session management for public API keys
|
|
11
|
+
* - HMAC-signed state parameters for security
|
|
12
|
+
* - Rate limiting and race condition protection
|
|
13
|
+
* - Comprehensive security validation and logging
|
|
14
|
+
* - PostMessage communication for new-tab mode
|
|
15
|
+
* - Automatic cleanup and resource management
|
|
16
|
+
* - Environment-specific configuration
|
|
17
|
+
*
|
|
18
|
+
* Security Features:
|
|
19
|
+
* - Origin validation for PostMessage communication
|
|
20
|
+
* - HTTPS enforcement in production
|
|
21
|
+
* - Rate limiting per API key and origin
|
|
22
|
+
* - State parameter signing with timestamps and nonces
|
|
23
|
+
* - Comprehensive security event logging
|
|
24
|
+
* - Protection against race conditions and replay attacks
|
|
25
|
+
*
|
|
26
|
+
* @author SafePassage Engineering
|
|
27
|
+
* @version 2.0.0
|
|
28
|
+
* @since 1.0.0
|
|
4
29
|
*/
|
|
5
30
|
import type { SafePassageConfig, VerificationOptions } from '../types';
|
|
31
|
+
/**
|
|
32
|
+
* SafePassage SDK Main Class
|
|
33
|
+
*
|
|
34
|
+
* Primary SDK class that manages age verification sessions with comprehensive
|
|
35
|
+
* security and error handling. Supports both redirect and new-tab modes with
|
|
36
|
+
* automatic session management and PostMessage communication.
|
|
37
|
+
*
|
|
38
|
+
* The class implements multiple security layers including origin validation,
|
|
39
|
+
* rate limiting, state signing, and comprehensive event logging to ensure
|
|
40
|
+
* secure verification flows.
|
|
41
|
+
*/
|
|
6
42
|
export declare class SafePassage {
|
|
7
43
|
private config;
|
|
8
44
|
private popupWindow;
|
|
@@ -11,58 +47,175 @@ export declare class SafePassage {
|
|
|
11
47
|
private unloadListener;
|
|
12
48
|
private isVerificationInProgress;
|
|
13
49
|
private currentSessionId;
|
|
50
|
+
/**
|
|
51
|
+
* Initialize SafePassage SDK
|
|
52
|
+
*
|
|
53
|
+
* Validates configuration, sets up security measures, and prepares the SDK
|
|
54
|
+
* for verification operations. Performs comprehensive environment validation
|
|
55
|
+
* and security initialization.
|
|
56
|
+
*
|
|
57
|
+
* @param {SafePassageConfig} config - SDK configuration object
|
|
58
|
+
* @throws {Error} If configuration validation fails
|
|
59
|
+
*/
|
|
14
60
|
constructor(config: SafePassageConfig);
|
|
15
61
|
/**
|
|
16
62
|
* Initiate age verification with race condition protection
|
|
63
|
+
*
|
|
64
|
+
* Main verification method that handles session creation, security validation,
|
|
65
|
+
* and verification flow initiation. Includes race condition protection and
|
|
66
|
+
* comprehensive error handling.
|
|
67
|
+
*
|
|
68
|
+
* For public keys (pk_*), automatically creates sessions via the portal API.
|
|
69
|
+
* For private keys (sk_*), requires a pre-created sessionId.
|
|
70
|
+
*
|
|
71
|
+
* @param {VerificationOptions} [options={}] - Verification options
|
|
72
|
+
* @param {string} [options.sessionId] - Session ID (required for private keys)
|
|
73
|
+
* @param {number} [options.challengeAge] - Age challenge override
|
|
74
|
+
* @param {string} [options.verificationMode] - Verification mode override
|
|
75
|
+
* @param {string} [options.externalUserId] - External user identifier
|
|
76
|
+
* @returns {Promise<void>} Promise that resolves when verification is initiated
|
|
77
|
+
* @throws {Error} If verification cannot be started or is already in progress
|
|
17
78
|
*/
|
|
18
79
|
verify(options?: VerificationOptions): Promise<void>;
|
|
19
80
|
/**
|
|
20
81
|
* Build verification URL with HMAC-signed state
|
|
82
|
+
*
|
|
83
|
+
* Constructs the verification URL with signed state parameter containing
|
|
84
|
+
* all necessary configuration and security information. The state parameter
|
|
85
|
+
* includes HMAC signature for integrity protection.
|
|
86
|
+
*
|
|
87
|
+
* @param {VerificationOptions} options - Verification options
|
|
88
|
+
* @returns {Promise<string>} Complete verification URL
|
|
89
|
+
* @private
|
|
21
90
|
*/
|
|
22
91
|
private buildVerificationUrl;
|
|
23
92
|
/**
|
|
24
93
|
* Redirect in same tab
|
|
94
|
+
*
|
|
95
|
+
* Performs a full page redirect to the verification URL.
|
|
96
|
+
* Used for redirect mode verification.
|
|
97
|
+
*
|
|
98
|
+
* @param {string} url - Verification URL to redirect to
|
|
99
|
+
* @private
|
|
25
100
|
*/
|
|
26
101
|
private redirect;
|
|
27
102
|
/**
|
|
28
103
|
* Open in new tab with PostMessage communication and proper cleanup
|
|
104
|
+
*
|
|
105
|
+
* Opens verification URL in a new tab/window and sets up secure PostMessage
|
|
106
|
+
* communication for receiving verification results. Includes comprehensive
|
|
107
|
+
* security validation and automatic cleanup.
|
|
108
|
+
*
|
|
109
|
+
* @param {string} url - Verification URL to open
|
|
110
|
+
* @param {string} sessionId - Session ID for result correlation
|
|
111
|
+
* @private
|
|
29
112
|
*/
|
|
30
113
|
private openNewTab;
|
|
31
114
|
/**
|
|
32
115
|
* Set up automatic cleanup on page unload to prevent memory leaks
|
|
116
|
+
*
|
|
117
|
+
* Registers event listeners for page unload events to ensure proper
|
|
118
|
+
* cleanup of resources and verification state. Handles both traditional
|
|
119
|
+
* page navigation and single-page application route changes.
|
|
120
|
+
*
|
|
121
|
+
* @private
|
|
33
122
|
*/
|
|
34
123
|
private setupAutoCleanup;
|
|
35
124
|
/**
|
|
36
125
|
* Auto-detect environment based on current URL
|
|
126
|
+
*
|
|
127
|
+
* Analyzes the current hostname to determine the appropriate environment
|
|
128
|
+
* configuration. Used when environment is not explicitly specified.
|
|
129
|
+
*
|
|
130
|
+
* @returns {'production' | 'staging' | 'development'} Detected environment
|
|
131
|
+
* @private
|
|
37
132
|
*/
|
|
38
133
|
private detectEnvironment;
|
|
39
134
|
/**
|
|
40
135
|
* Unlock verification process to allow new verifications
|
|
136
|
+
*
|
|
137
|
+
* Resets the verification lock state to allow new verification attempts.
|
|
138
|
+
* Called after successful completion, errors, or cleanup.
|
|
139
|
+
*
|
|
140
|
+
* @private
|
|
41
141
|
*/
|
|
42
142
|
private unlockVerification;
|
|
43
143
|
/**
|
|
44
144
|
* Internal cleanup method to prevent memory leaks
|
|
145
|
+
*
|
|
146
|
+
* Cleans up popup windows, event listeners, and intervals.
|
|
147
|
+
* Does not unlock verification state - that's handled by specific callers.
|
|
148
|
+
*
|
|
149
|
+
* @private
|
|
45
150
|
*/
|
|
46
151
|
private cleanup;
|
|
47
152
|
/**
|
|
48
153
|
* Remove auto-cleanup listeners
|
|
154
|
+
*
|
|
155
|
+
* Removes page unload event listeners that were set up for automatic cleanup.
|
|
156
|
+
*
|
|
157
|
+
* @private
|
|
49
158
|
*/
|
|
50
159
|
private removeAutoCleanupListeners;
|
|
51
160
|
/**
|
|
52
161
|
* Public cleanup method for manual resource management
|
|
162
|
+
*
|
|
163
|
+
* Completely destroys the SDK instance, cleaning up all resources and
|
|
164
|
+
* removing all event listeners. Should be called when the SDK is no longer needed.
|
|
165
|
+
*
|
|
166
|
+
* @public
|
|
53
167
|
*/
|
|
54
168
|
destroy(): void;
|
|
169
|
+
/**
|
|
170
|
+
* Get Portal API URL based on environment
|
|
171
|
+
*
|
|
172
|
+
* Returns the appropriate portal-api URL for the current environment.
|
|
173
|
+
*
|
|
174
|
+
* @returns {string} Portal API URL
|
|
175
|
+
* @private
|
|
176
|
+
*/
|
|
177
|
+
private getPortalApiUrl;
|
|
178
|
+
/**
|
|
179
|
+
* Get Engine URL based on environment
|
|
180
|
+
*
|
|
181
|
+
* Returns the appropriate verify-engine URL for the current environment.
|
|
182
|
+
* In production, engine access is proxied through portal-api.
|
|
183
|
+
*
|
|
184
|
+
* @returns {string} Engine URL
|
|
185
|
+
* @private
|
|
186
|
+
*/
|
|
187
|
+
private getEngineUrl;
|
|
188
|
+
/**
|
|
189
|
+
* Get WebSocket URL based on environment
|
|
190
|
+
*
|
|
191
|
+
* Returns the appropriate WebSocket URL for the current environment.
|
|
192
|
+
* In production, WebSocket connections are proxied through portal-api.
|
|
193
|
+
*
|
|
194
|
+
* @returns {string} WebSocket URL
|
|
195
|
+
* @private
|
|
196
|
+
*/
|
|
197
|
+
private getWebSocketUrl;
|
|
55
198
|
/**
|
|
56
199
|
* Detect if this is a public key (pk_ prefix) vs private key (sk_ prefix)
|
|
200
|
+
*
|
|
201
|
+
* Determines API key type based on prefix to handle different authentication flows.
|
|
202
|
+
*
|
|
203
|
+
* @returns {boolean} True if public key, false if private key
|
|
204
|
+
* @private
|
|
57
205
|
*/
|
|
58
206
|
private isPublicKey;
|
|
59
207
|
/**
|
|
60
208
|
* Create session internally for public keys
|
|
209
|
+
*
|
|
210
|
+
* Creates a verification session via the portal API for public key authentication.
|
|
211
|
+
* Generates a UUID session ID and submits session creation request with
|
|
212
|
+
* verification parameters.
|
|
213
|
+
*
|
|
214
|
+
* @param {VerificationOptions} options - Verification options
|
|
215
|
+
* @returns {Promise<string>} Created session ID
|
|
216
|
+
* @throws {Error} If session creation fails
|
|
217
|
+
* @private
|
|
61
218
|
*/
|
|
62
219
|
private createInternalSession;
|
|
63
|
-
/**
|
|
64
|
-
* Get portal API URL based on environment
|
|
65
|
-
*/
|
|
66
|
-
private getPortalApiUrl;
|
|
67
220
|
}
|
|
68
221
|
export default SafePassage;
|