@insureco/bio 0.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/index.d.mts +385 -0
- package/dist/index.d.ts +385 -0
- package/dist/index.js +654 -0
- package/dist/index.mjs +611 -0
- package/package.json +48 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
/** Configuration for BioAuth (OAuth flow client) */
|
|
2
|
+
interface BioAuthConfig {
|
|
3
|
+
/** OAuth client ID (env: BIO_CLIENT_ID) */
|
|
4
|
+
clientId: string;
|
|
5
|
+
/** OAuth client secret (env: BIO_CLIENT_SECRET) */
|
|
6
|
+
clientSecret: string;
|
|
7
|
+
/** Bio-ID issuer URL (env: BIO_ID_URL, default: https://bio.tawa.insureco.io) */
|
|
8
|
+
issuer?: string;
|
|
9
|
+
/** Number of retry attempts on transient failures (default: 2) */
|
|
10
|
+
retries?: number;
|
|
11
|
+
/** Request timeout in milliseconds (default: 10000) */
|
|
12
|
+
timeoutMs?: number;
|
|
13
|
+
}
|
|
14
|
+
/** Configuration for BioAdmin (admin API client) */
|
|
15
|
+
interface BioAdminConfig {
|
|
16
|
+
/** Bio-ID base URL (env: BIO_ID_URL, default: https://bio.tawa.insureco.io) */
|
|
17
|
+
baseUrl?: string;
|
|
18
|
+
/** Internal API key for service-to-service auth (env: INTERNAL_API_KEY) */
|
|
19
|
+
internalKey?: string;
|
|
20
|
+
/** Async function returning a Bearer token (alternative to internalKey) */
|
|
21
|
+
accessTokenFn?: () => Promise<string>;
|
|
22
|
+
/** Number of retry attempts on transient failures (default: 2) */
|
|
23
|
+
retries?: number;
|
|
24
|
+
/** Request timeout in milliseconds (default: 10000) */
|
|
25
|
+
timeoutMs?: number;
|
|
26
|
+
}
|
|
27
|
+
/** Options for building an authorization URL */
|
|
28
|
+
interface AuthorizeOptions {
|
|
29
|
+
/** Callback URL where Bio-ID redirects after authorization */
|
|
30
|
+
redirectUri: string;
|
|
31
|
+
/** OAuth scopes to request (default: ['openid', 'profile', 'email']) */
|
|
32
|
+
scopes?: string[];
|
|
33
|
+
/** CSRF state parameter (auto-generated if not provided) */
|
|
34
|
+
state?: string;
|
|
35
|
+
}
|
|
36
|
+
/** Result from getAuthorizationUrl() */
|
|
37
|
+
interface AuthorizeResult {
|
|
38
|
+
/** Full authorization URL to redirect the user to */
|
|
39
|
+
url: string;
|
|
40
|
+
/** State parameter (for CSRF validation on callback) */
|
|
41
|
+
state: string;
|
|
42
|
+
/** PKCE code verifier (store securely, send during token exchange) */
|
|
43
|
+
codeVerifier: string;
|
|
44
|
+
/** PKCE code challenge (included in the URL) */
|
|
45
|
+
codeChallenge: string;
|
|
46
|
+
}
|
|
47
|
+
/** Response from the /api/oauth/token endpoint */
|
|
48
|
+
interface TokenResponse {
|
|
49
|
+
access_token: string;
|
|
50
|
+
token_type: 'Bearer';
|
|
51
|
+
expires_in: number;
|
|
52
|
+
refresh_token?: string;
|
|
53
|
+
scope: string;
|
|
54
|
+
id_token?: string;
|
|
55
|
+
}
|
|
56
|
+
/** Response from the /api/auth/introspect endpoint */
|
|
57
|
+
interface IntrospectResult {
|
|
58
|
+
active: boolean;
|
|
59
|
+
user?: {
|
|
60
|
+
id: string;
|
|
61
|
+
email: string;
|
|
62
|
+
name: string;
|
|
63
|
+
org: string;
|
|
64
|
+
roles: string[];
|
|
65
|
+
};
|
|
66
|
+
tokenType?: 'client_credentials';
|
|
67
|
+
clientId?: string;
|
|
68
|
+
scopes?: string[];
|
|
69
|
+
orgId?: string;
|
|
70
|
+
orgSlug?: string;
|
|
71
|
+
organizationName?: string;
|
|
72
|
+
}
|
|
73
|
+
/** Decoded access token payload (user auth) */
|
|
74
|
+
interface BioTokenPayload {
|
|
75
|
+
iss: string;
|
|
76
|
+
sub: string;
|
|
77
|
+
aud: string;
|
|
78
|
+
exp: number;
|
|
79
|
+
iat: number;
|
|
80
|
+
bioId: string;
|
|
81
|
+
email: string;
|
|
82
|
+
name: string;
|
|
83
|
+
userType: string;
|
|
84
|
+
roles: string[];
|
|
85
|
+
permissions: string[];
|
|
86
|
+
orgId?: string;
|
|
87
|
+
orgSlug?: string;
|
|
88
|
+
client_id: string;
|
|
89
|
+
scope: string;
|
|
90
|
+
enabled_modules?: string[];
|
|
91
|
+
onboarding?: {
|
|
92
|
+
platform: boolean;
|
|
93
|
+
modules: Record<string, {
|
|
94
|
+
completed: string[];
|
|
95
|
+
due: string[];
|
|
96
|
+
}>;
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
/** Decoded client credentials token payload (service-to-service) */
|
|
100
|
+
interface BioClientTokenPayload {
|
|
101
|
+
iss: string;
|
|
102
|
+
exp: number;
|
|
103
|
+
iat: number;
|
|
104
|
+
client_id: string;
|
|
105
|
+
scope: string;
|
|
106
|
+
token_type: 'client_credentials';
|
|
107
|
+
orgId?: string;
|
|
108
|
+
orgSlug?: string;
|
|
109
|
+
}
|
|
110
|
+
/** Options for local JWT verification */
|
|
111
|
+
interface VerifyOptions {
|
|
112
|
+
/** Expected issuer (default: config issuer) */
|
|
113
|
+
issuer?: string;
|
|
114
|
+
/** Expected audience (client_id) */
|
|
115
|
+
audience?: string;
|
|
116
|
+
}
|
|
117
|
+
/** User profile from /api/oauth/userinfo or admin API */
|
|
118
|
+
interface BioUser {
|
|
119
|
+
sub: string;
|
|
120
|
+
bioId: string;
|
|
121
|
+
email: string;
|
|
122
|
+
emailVerified: boolean;
|
|
123
|
+
name: string;
|
|
124
|
+
firstName?: string;
|
|
125
|
+
lastName?: string;
|
|
126
|
+
userType: string;
|
|
127
|
+
roles: string[];
|
|
128
|
+
permissions: string[];
|
|
129
|
+
status: string;
|
|
130
|
+
orgId?: string;
|
|
131
|
+
orgSlug?: string;
|
|
132
|
+
organizationId?: string;
|
|
133
|
+
organizationName?: string;
|
|
134
|
+
departmentId?: string;
|
|
135
|
+
departmentName?: string;
|
|
136
|
+
managerId?: string;
|
|
137
|
+
enabledModules?: string[];
|
|
138
|
+
jobTitle?: string;
|
|
139
|
+
phoneHome?: string;
|
|
140
|
+
phoneWork?: string;
|
|
141
|
+
phoneCell?: string;
|
|
142
|
+
phone?: string;
|
|
143
|
+
addressHome?: BioAddress;
|
|
144
|
+
addressWork?: BioAddress;
|
|
145
|
+
messaging?: BioMessaging;
|
|
146
|
+
preferences?: Record<string, unknown>;
|
|
147
|
+
lastLoginAt?: number;
|
|
148
|
+
}
|
|
149
|
+
interface BioAddress {
|
|
150
|
+
street?: string;
|
|
151
|
+
city?: string;
|
|
152
|
+
state?: string;
|
|
153
|
+
zip?: string;
|
|
154
|
+
country?: string;
|
|
155
|
+
}
|
|
156
|
+
interface BioMessaging {
|
|
157
|
+
slack?: string;
|
|
158
|
+
teams?: string;
|
|
159
|
+
skype?: string;
|
|
160
|
+
whatsapp?: string;
|
|
161
|
+
}
|
|
162
|
+
/** Filters for listing users */
|
|
163
|
+
interface UserFilters {
|
|
164
|
+
search?: string;
|
|
165
|
+
status?: string;
|
|
166
|
+
userType?: string;
|
|
167
|
+
organizationId?: string;
|
|
168
|
+
page?: number;
|
|
169
|
+
limit?: number;
|
|
170
|
+
}
|
|
171
|
+
/** Data for updating a user */
|
|
172
|
+
interface UpdateUserData {
|
|
173
|
+
firstName?: string;
|
|
174
|
+
lastName?: string;
|
|
175
|
+
displayName?: string;
|
|
176
|
+
roles?: string[];
|
|
177
|
+
status?: string;
|
|
178
|
+
userType?: string;
|
|
179
|
+
departmentId?: string;
|
|
180
|
+
jobTitle?: string;
|
|
181
|
+
permissions?: string[];
|
|
182
|
+
enabled_modules?: string[];
|
|
183
|
+
}
|
|
184
|
+
/** Department from admin API */
|
|
185
|
+
interface BioDepartment {
|
|
186
|
+
id: string;
|
|
187
|
+
name: string;
|
|
188
|
+
description?: string;
|
|
189
|
+
organizationId: string;
|
|
190
|
+
headId?: string;
|
|
191
|
+
parentId?: string;
|
|
192
|
+
memberCount?: number;
|
|
193
|
+
}
|
|
194
|
+
/** Data for creating a department */
|
|
195
|
+
interface CreateDepartmentData {
|
|
196
|
+
name: string;
|
|
197
|
+
description?: string;
|
|
198
|
+
headId?: string;
|
|
199
|
+
parentId?: string;
|
|
200
|
+
}
|
|
201
|
+
/** Role from admin API */
|
|
202
|
+
interface BioRole {
|
|
203
|
+
id: string;
|
|
204
|
+
name: string;
|
|
205
|
+
description?: string;
|
|
206
|
+
permissions: string[];
|
|
207
|
+
isSystem?: boolean;
|
|
208
|
+
}
|
|
209
|
+
/** Data for creating a role */
|
|
210
|
+
interface CreateRoleData {
|
|
211
|
+
name: string;
|
|
212
|
+
description?: string;
|
|
213
|
+
permissions: string[];
|
|
214
|
+
}
|
|
215
|
+
/** OAuth client from admin API */
|
|
216
|
+
interface BioOAuthClient {
|
|
217
|
+
clientId: string;
|
|
218
|
+
name: string;
|
|
219
|
+
description?: string;
|
|
220
|
+
redirectUris: string[];
|
|
221
|
+
allowedScopes: string[];
|
|
222
|
+
allowedGrantTypes: string[];
|
|
223
|
+
isActive: boolean;
|
|
224
|
+
orgId?: string;
|
|
225
|
+
orgSlug?: string;
|
|
226
|
+
accessTokenTtl?: number;
|
|
227
|
+
refreshTokenTtl?: number;
|
|
228
|
+
}
|
|
229
|
+
/** Data for creating an OAuth client */
|
|
230
|
+
interface CreateClientData {
|
|
231
|
+
name: string;
|
|
232
|
+
description?: string;
|
|
233
|
+
redirectUris: string[];
|
|
234
|
+
allowedScopes?: string[];
|
|
235
|
+
allowedGrantTypes?: string[];
|
|
236
|
+
}
|
|
237
|
+
/** Admin API response wrapper */
|
|
238
|
+
interface AdminResponse<T> {
|
|
239
|
+
success: boolean;
|
|
240
|
+
data?: T;
|
|
241
|
+
error?: string;
|
|
242
|
+
meta?: {
|
|
243
|
+
total: number;
|
|
244
|
+
page: number;
|
|
245
|
+
limit: number;
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* OAuth flow client for Bio-ID SSO.
|
|
251
|
+
*
|
|
252
|
+
* Handles authorization URL generation, token exchange, refresh,
|
|
253
|
+
* userinfo fetching, revocation, and introspection.
|
|
254
|
+
*/
|
|
255
|
+
declare class BioAuth {
|
|
256
|
+
private readonly clientId;
|
|
257
|
+
private readonly clientSecret;
|
|
258
|
+
private readonly issuer;
|
|
259
|
+
private readonly retries;
|
|
260
|
+
private readonly timeoutMs;
|
|
261
|
+
constructor(config: BioAuthConfig);
|
|
262
|
+
/**
|
|
263
|
+
* Create a BioAuth from environment variables.
|
|
264
|
+
*
|
|
265
|
+
* Reads: BIO_CLIENT_ID, BIO_CLIENT_SECRET, BIO_ID_URL
|
|
266
|
+
*/
|
|
267
|
+
static fromEnv(): BioAuth;
|
|
268
|
+
/**
|
|
269
|
+
* Build an authorization URL with PKCE for redirecting the user to Bio-ID.
|
|
270
|
+
*
|
|
271
|
+
* Returns the URL, state, and PKCE verifier/challenge.
|
|
272
|
+
* Store the state and codeVerifier securely (e.g. in a cookie) for the callback.
|
|
273
|
+
*/
|
|
274
|
+
getAuthorizationUrl(opts: AuthorizeOptions): AuthorizeResult;
|
|
275
|
+
/**
|
|
276
|
+
* Exchange an authorization code for tokens.
|
|
277
|
+
*
|
|
278
|
+
* Called in your OAuth callback handler after the user authorizes.
|
|
279
|
+
*/
|
|
280
|
+
exchangeCode(code: string, codeVerifier: string, redirectUri: string): Promise<TokenResponse>;
|
|
281
|
+
/**
|
|
282
|
+
* Refresh an expired access token using a refresh token.
|
|
283
|
+
*
|
|
284
|
+
* Returns new access_token and a rotated refresh_token.
|
|
285
|
+
*/
|
|
286
|
+
refreshToken(refreshToken: string): Promise<TokenResponse>;
|
|
287
|
+
/**
|
|
288
|
+
* Get a client credentials token for service-to-service auth.
|
|
289
|
+
*
|
|
290
|
+
* No user context — the token identifies your OAuth client only.
|
|
291
|
+
*/
|
|
292
|
+
getClientCredentialsToken(scopes?: string[]): Promise<TokenResponse>;
|
|
293
|
+
/**
|
|
294
|
+
* Fetch the authenticated user's profile from the userinfo endpoint.
|
|
295
|
+
*/
|
|
296
|
+
getUserInfo(accessToken: string): Promise<BioUser>;
|
|
297
|
+
/**
|
|
298
|
+
* Revoke a token (typically the refresh token on logout).
|
|
299
|
+
*/
|
|
300
|
+
revokeToken(token: string, hint?: 'refresh_token' | 'access_token'): Promise<void>;
|
|
301
|
+
/**
|
|
302
|
+
* Introspect a token to check if it's active and get user/client context.
|
|
303
|
+
*
|
|
304
|
+
* Does not require JWT_SECRET — validates against Bio-ID server.
|
|
305
|
+
*/
|
|
306
|
+
introspect(token: string): Promise<IntrospectResult>;
|
|
307
|
+
private tokenRequest;
|
|
308
|
+
private request;
|
|
309
|
+
private fetchWithRetry;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Admin API client for Bio-ID.
|
|
314
|
+
*
|
|
315
|
+
* Manages users, departments, roles, and OAuth clients.
|
|
316
|
+
* Authenticates via X-Internal-Key header or Bearer token.
|
|
317
|
+
*/
|
|
318
|
+
declare class BioAdmin {
|
|
319
|
+
private readonly baseUrl;
|
|
320
|
+
private readonly internalKey?;
|
|
321
|
+
private readonly accessTokenFn?;
|
|
322
|
+
private readonly retries;
|
|
323
|
+
private readonly timeoutMs;
|
|
324
|
+
constructor(config: BioAdminConfig);
|
|
325
|
+
/**
|
|
326
|
+
* Create a BioAdmin from environment variables.
|
|
327
|
+
*
|
|
328
|
+
* Reads: BIO_ID_URL, INTERNAL_API_KEY
|
|
329
|
+
*/
|
|
330
|
+
static fromEnv(): BioAdmin;
|
|
331
|
+
/** List users with optional filters */
|
|
332
|
+
listUsers(filters?: UserFilters): Promise<BioUser[]>;
|
|
333
|
+
/** Get a single user by bioId */
|
|
334
|
+
getUser(bioId: string): Promise<BioUser>;
|
|
335
|
+
/** Update a user's profile, roles, or status */
|
|
336
|
+
updateUser(bioId: string, data: UpdateUserData): Promise<BioUser>;
|
|
337
|
+
/** List all departments */
|
|
338
|
+
listDepartments(): Promise<BioDepartment[]>;
|
|
339
|
+
/** Create a new department */
|
|
340
|
+
createDepartment(data: CreateDepartmentData): Promise<BioDepartment>;
|
|
341
|
+
/** List all roles */
|
|
342
|
+
listRoles(): Promise<BioRole[]>;
|
|
343
|
+
/** Create a new role */
|
|
344
|
+
createRole(data: CreateRoleData): Promise<BioRole>;
|
|
345
|
+
/** List all OAuth clients */
|
|
346
|
+
listClients(): Promise<BioOAuthClient[]>;
|
|
347
|
+
/** Create a new OAuth client */
|
|
348
|
+
createClient(data: CreateClientData): Promise<BioOAuthClient>;
|
|
349
|
+
private request;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/** Error thrown by Bio SDK operations */
|
|
353
|
+
declare class BioError extends Error {
|
|
354
|
+
/** HTTP status code (if from an API response) */
|
|
355
|
+
readonly statusCode?: number;
|
|
356
|
+
/** Machine-readable error code (e.g. 'invalid_grant', 'token_expired') */
|
|
357
|
+
readonly code: string;
|
|
358
|
+
/** Additional error details from the API */
|
|
359
|
+
readonly details?: Record<string, unknown>;
|
|
360
|
+
constructor(message: string, code: string, statusCode?: number, details?: Record<string, unknown>);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/** Generate a PKCE code verifier and S256 code challenge */
|
|
364
|
+
declare function generatePKCE(): {
|
|
365
|
+
codeVerifier: string;
|
|
366
|
+
codeChallenge: string;
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* Verify a Bio-ID JWT access token using HS256.
|
|
371
|
+
* Checks algorithm, signature (constant-time), expiration, issuer, and audience.
|
|
372
|
+
*/
|
|
373
|
+
declare function verifyToken(token: string, secret: string, options?: VerifyOptions): BioTokenPayload;
|
|
374
|
+
/**
|
|
375
|
+
* Decode a JWT without verifying its signature.
|
|
376
|
+
* Useful for reading claims from tokens you trust (e.g. from your own cookie).
|
|
377
|
+
*/
|
|
378
|
+
declare function decodeToken(token: string): BioTokenPayload | null;
|
|
379
|
+
/**
|
|
380
|
+
* Check if a JWT is expired (with optional buffer in seconds).
|
|
381
|
+
* Returns true if expired or unparseable.
|
|
382
|
+
*/
|
|
383
|
+
declare function isTokenExpired(token: string, bufferSeconds?: number): boolean;
|
|
384
|
+
|
|
385
|
+
export { type AdminResponse, type AuthorizeOptions, type AuthorizeResult, type BioAddress, BioAdmin, type BioAdminConfig, BioAuth, type BioAuthConfig, type BioClientTokenPayload, type BioDepartment, BioError, type BioMessaging, type BioOAuthClient, type BioRole, type BioTokenPayload, type BioUser, type CreateClientData, type CreateDepartmentData, type CreateRoleData, type IntrospectResult, type TokenResponse, type UpdateUserData, type UserFilters, type VerifyOptions, decodeToken, generatePKCE, isTokenExpired, verifyToken };
|