@nibssplc/cams-sdk 0.0.1-beta.3 โ†’ 0.0.1-beta.30

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 CHANGED
@@ -5,11 +5,11 @@ Central Authentication Management Service (CAMS) SDK for popup-based authenticat
5
5
  ## Features
6
6
 
7
7
  - ๐Ÿ” Secure popup-based authentication
8
- - ๐Ÿ›ก๏ธ URL validation and domain allowlisting
9
- - โฑ๏ธ Configurable timeouts and retry logic
10
- - ๐Ÿ”„ Automatic token management with expiration checking
8
+ - ๐Ÿ›ก๏ธ Origin validation and security controls
9
+ - โฑ๏ธ Configurable auth and idle timeouts with retry logic
10
+ - ๐Ÿ”„ Automatic token management with JWT expiration checking
11
11
  - ๐Ÿ“ฑ Session management with event callbacks
12
- - ๐Ÿงช Comprehensive TypeScript support
12
+ - ๐Ÿงช Comprehensive TypeScript support with Zod validation
13
13
  - โœ… Full test coverage
14
14
 
15
15
  ## Installation
@@ -23,15 +23,16 @@ npm install @nibssplc/cams-sdk
23
23
  ### Basic Usage
24
24
 
25
25
  ```typescript
26
- import { openCAMSLogin, CAMSConfig } from '@nibssplc/cams-sdk';
26
+ import { openCAMSPopUpLogin, CAMSConfig } from '@nibssplc/cams-sdk';
27
27
 
28
28
  const config: CAMSConfig = {
29
29
  camsUrl: 'https://your-cams-instance.com/auth',
30
- allowedOrigin: 'https://your-cams-instance.com',
30
+ messageOrigin: 'your-cams-instance.com',
31
31
  windowWidth: 500,
32
32
  windowHeight: 600,
33
- timeout: 300000, // 5 minutes
34
- allowedDomains: ['your-cams-instance.com'], // Security allowlist
33
+ authTimeout: 300000, // 5 minutes auth timeout
34
+ idleTimeout: 120000, // 2 minutes idle timeout
35
+ debug: true, // Enable debug logging
35
36
  };
36
37
 
37
38
  try {
@@ -45,7 +46,7 @@ try {
45
46
  ### Session Management (Recommended)
46
47
 
47
48
  ```typescript
48
- import { CAMSSession, CAMSEvents } from '@nibssplc/cams-sdk';
49
+ import { CAMSSessionManager, CAMSEvents } from '@nibssplc/cams-sdk';
49
50
 
50
51
  const events: CAMSEvents = {
51
52
  onAuthStart: () => console.log('Authentication started'),
@@ -54,11 +55,14 @@ const events: CAMSEvents = {
54
55
  onTokenExpired: () => console.log('Token expired'),
55
56
  };
56
57
 
57
- const session = new CAMSSession(sessionStorage, 'my_app_token', events);
58
+ const session = new CAMSSessionManager(localStorage, 'my_app', events);
58
59
 
59
60
  // Login with retry logic
60
61
  const config = {
61
- // ... your config
62
+ camsUrl: 'https://your-cams-instance.com/auth',
63
+ messageOrigin: 'your-cams-instance.com',
64
+ windowWidth: 500,
65
+ windowHeight: 600,
62
66
  retryAttempts: 2, // Retry twice on failure
63
67
  };
64
68
 
@@ -83,13 +87,14 @@ await session.logout();
83
87
  ```typescript
84
88
  interface CAMSConfig {
85
89
  camsUrl: string; // CAMS authentication URL
86
- allowedOrigin: string; // Expected message origin
90
+ messageOrigin: string; // Expected message origin (hostname)
87
91
  windowWidth: number; // Popup window width
88
92
  windowHeight: number; // Popup window height
89
- timeout?: number; // Auth timeout in ms (default: 300000)
90
- allowedDomains?: string[]; // URL allowlist for security
93
+ authTimeout?: number; // Auth timeout in ms (default: 300000)
94
+ idleTimeout?: number; // Idle timeout in ms (default: 120000)
91
95
  storageKey?: string; // Custom storage key
92
96
  retryAttempts?: number; // Auto-retry on failure (default: 0)
97
+ debug?: boolean; // Enable debug logging (default: false)
93
98
  }
94
99
  ```
95
100
 
@@ -114,9 +119,18 @@ try {
114
119
  case CAMSErrorType.INVALID_URL:
115
120
  // Handle invalid URL
116
121
  break;
122
+ case CAMSErrorType.INVALID_ORIGIN:
123
+ // Handle invalid message origin
124
+ break;
117
125
  case CAMSErrorType.USER_CANCELLED:
118
126
  // Handle user cancellation
119
127
  break;
128
+ case CAMSErrorType.MAX_RETRIES_EXCEEDED:
129
+ // Handle retry limit exceeded
130
+ break;
131
+ case CAMSErrorType.API_VALIDATION_ERROR:
132
+ // Handle API validation errors
133
+ break;
120
134
  }
121
135
  }
122
136
  }
@@ -124,11 +138,86 @@ try {
124
138
 
125
139
  ## Security Features
126
140
 
127
- - **URL Validation**: Prevents open redirect attacks
128
- - **Domain Allowlisting**: Restricts authentication to approved domains
129
- - **Origin Validation**: Validates message origins
130
- - **Token Validation**: Uses Zod schemas for runtime validation
141
+ - **URL Validation**: Prevents open redirect attacks using Zod schemas
142
+ - **Origin Validation**: Validates message origins with subdomain support
143
+ - **Token Validation**: JWT parsing with expiration checking
131
144
  - **Secure Storage**: Configurable storage with automatic cleanup
145
+ - **Input Sanitization**: Log injection prevention
146
+
147
+ ## Session Manager API
148
+
149
+ ### Constructor
150
+ ```typescript
151
+ new CAMSSessionManager(
152
+ storage?: StorageLike | null, // Storage implementation (default: localStorage)
153
+ keyPrefix?: string, // Storage key prefix (default: "CAMS-SDK")
154
+ events?: CAMSEvents // Event callbacks
155
+ )
156
+ ```
157
+
158
+ ### Methods
159
+ - `login(config: CAMSConfig): Promise<CAMSTokenResponse>` - Authenticate user
160
+ - `logout(): Promise<void>` - Clear session
161
+ - `isAuthenticated(): boolean` - Check if user is authenticated
162
+ - `getAccessToken(): string | null` - Get current token
163
+ - `getProfile(): Promise<Profile | null>` - Extract user profile from JWT
164
+ - `setToken(token: string): void` - Store token
165
+ - `clearToken(): void` - Clear stored token
166
+
167
+ ## Debugging
168
+
169
+ The SDK includes comprehensive logging to aid in debugging:
170
+
171
+ ```typescript
172
+ import { Logger, LogLevel } from '@nibssplc/cams-sdk';
173
+
174
+ // Set global log level
175
+ Logger.setLevel(LogLevel.DEBUG);
176
+
177
+ // Or enable debug mode in config
178
+ const config = {
179
+ // ... other config
180
+ debug: true // Automatically sets debug level
181
+ };
182
+ ```
183
+
184
+ **Log Levels:**
185
+ - `ERROR`: Critical errors only
186
+ - `WARN`: Warnings and errors (default)
187
+ - `INFO`: General information, warnings, and errors
188
+ - `DEBUG`: All logs including detailed flow information
189
+
190
+ ## Message Protocol
191
+
192
+ The SDK expects specific message formats from the authentication window:
193
+
194
+ ### Success Message
195
+ ```typescript
196
+ {
197
+ type: "AUTH_SUCCESS",
198
+ accessToken: string,
199
+ user?: {
200
+ name: string,
201
+ image: string | null
202
+ }
203
+ }
204
+ ```
205
+
206
+ ### Failure Message
207
+ ```typescript
208
+ {
209
+ type: "AUTH_FAILURE",
210
+ error: string,
211
+ errorCode?: string
212
+ }
213
+ ```
214
+
215
+ ### Heartbeat (Optional)
216
+ ```typescript
217
+ {
218
+ type: "heartbeat"
219
+ }
220
+ ```
132
221
 
133
222
  ## Development
134
223
 
package/dist/auth.d.ts CHANGED
@@ -1,3 +1,4 @@
1
- import { CAMSConfig, CAMSTokenResponse } from "./types";
2
- export declare function validateConfig(config: CAMSConfig): void;
3
- export declare function openCAMSPopUpLogin(config: CAMSConfig): Promise<CAMSTokenResponse>;
1
+ import { CAMSConfig } from "./types";
2
+ import { Profile } from "./validators";
3
+ export declare function validateConfig(config: CAMSConfig): URL;
4
+ export declare function openCAMSPopUpLogin(config: CAMSConfig): Promise<Profile>;