@nibssplc/cams-sdk 0.0.1-beta.2 โ 0.0.1-beta.21
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 +107 -18
- package/dist/auth.d.ts +1 -1
- package/dist/index.cjs.js +418 -159
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.esm.js +416 -158
- package/dist/index.esm.js.map +1 -1
- package/dist/index.umd.js +418 -159
- package/dist/index.umd.js.map +1 -1
- package/dist/logger.d.ts +20 -0
- package/dist/sessionManager.d.ts +37 -0
- package/dist/types.d.ts +6 -4
- package/dist/validators.d.ts +13 -2
- package/package.json +13 -4
- package/dist/session.d.ts +0 -19
- package/dist/tokenManager.d.ts +0 -12
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
|
-
- ๐ก๏ธ
|
|
9
|
-
- โฑ๏ธ Configurable
|
|
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 {
|
|
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
|
-
|
|
30
|
+
messageOrigin: 'your-cams-instance.com',
|
|
31
31
|
windowWidth: 500,
|
|
32
32
|
windowHeight: 600,
|
|
33
|
-
|
|
34
|
-
|
|
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 {
|
|
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
|
|
58
|
+
const session = new CAMSSessionManager(localStorage, 'my_app', events);
|
|
58
59
|
|
|
59
60
|
// Login with retry logic
|
|
60
61
|
const config = {
|
|
61
|
-
|
|
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
|
-
|
|
90
|
+
messageOrigin: string; // Expected message origin (hostname)
|
|
87
91
|
windowWidth: number; // Popup window width
|
|
88
92
|
windowHeight: number; // Popup window height
|
|
89
|
-
|
|
90
|
-
|
|
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
|
-
- **
|
|
129
|
-
- **
|
|
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,3 @@
|
|
|
1
1
|
import { CAMSConfig, CAMSTokenResponse } from "./types";
|
|
2
|
-
export declare function validateConfig(config: CAMSConfig):
|
|
2
|
+
export declare function validateConfig(config: CAMSConfig): URL;
|
|
3
3
|
export declare function openCAMSPopUpLogin(config: CAMSConfig): Promise<CAMSTokenResponse>;
|