@sphereon/ssi-express-support 0.33.1-next.3 → 0.33.1-next.73

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.
Files changed (42) hide show
  1. package/dist/index.cjs +944 -0
  2. package/dist/index.cjs.map +1 -0
  3. package/dist/index.d.cts +403 -0
  4. package/dist/index.d.ts +403 -9
  5. package/dist/index.js +911 -26
  6. package/dist/index.js.map +1 -1
  7. package/package.json +22 -10
  8. package/src/openid-connect-rp.ts +1 -0
  9. package/src/static-bearer-auth.ts +5 -3
  10. package/dist/auth-utils.d.ts +0 -21
  11. package/dist/auth-utils.d.ts.map +0 -1
  12. package/dist/auth-utils.js +0 -148
  13. package/dist/auth-utils.js.map +0 -1
  14. package/dist/entra-id-auth.d.ts +0 -10
  15. package/dist/entra-id-auth.d.ts.map +0 -1
  16. package/dist/entra-id-auth.js +0 -61
  17. package/dist/entra-id-auth.js.map +0 -1
  18. package/dist/express-builders.d.ts +0 -99
  19. package/dist/express-builders.d.ts.map +0 -1
  20. package/dist/express-builders.js +0 -281
  21. package/dist/express-builders.js.map +0 -1
  22. package/dist/express-utils.d.ts +0 -4
  23. package/dist/express-utils.d.ts.map +0 -1
  24. package/dist/express-utils.js +0 -55
  25. package/dist/express-utils.js.map +0 -1
  26. package/dist/functions.d.ts +0 -2
  27. package/dist/functions.d.ts.map +0 -1
  28. package/dist/functions.js +0 -10
  29. package/dist/functions.js.map +0 -1
  30. package/dist/index.d.ts.map +0 -1
  31. package/dist/openid-connect-rp.d.ts +0 -54
  32. package/dist/openid-connect-rp.d.ts.map +0 -1
  33. package/dist/openid-connect-rp.js +0 -214
  34. package/dist/openid-connect-rp.js.map +0 -1
  35. package/dist/static-bearer-auth.d.ts +0 -34
  36. package/dist/static-bearer-auth.d.ts.map +0 -1
  37. package/dist/static-bearer-auth.js +0 -146
  38. package/dist/static-bearer-auth.js.map +0 -1
  39. package/dist/types.d.ts +0 -193
  40. package/dist/types.d.ts.map +0 -1
  41. package/dist/types.js +0 -7
  42. package/dist/types.js.map +0 -1
package/dist/index.d.ts CHANGED
@@ -1,9 +1,403 @@
1
- export * from './entra-id-auth';
2
- export * from './static-bearer-auth';
3
- export * from './auth-utils';
4
- export * from './express-builders';
5
- export * from './types';
6
- export { sendErrorResponse, jsonErrorHandler } from './express-utils';
7
- export * from './functions';
8
- export * from './openid-connect-rp';
9
- //# sourceMappingURL=index.d.ts.map
1
+ import { Enforcer } from 'casbin';
2
+ import express, { Express, RequestHandler, NextFunction, Router } from 'express';
3
+ import { ParamsDictionary, ApplicationRequestHandler, Application } from 'express-serve-static-core';
4
+ import http from 'http';
5
+ import { HttpTerminator } from 'http-terminator';
6
+ import { AuthenticateCallback, Strategy, InitializeOptions } from 'passport';
7
+ import { ParsedQs } from 'qs';
8
+ import session from 'express-session';
9
+ import morgan from 'morgan';
10
+ import { TAgent } from '@veramo/core';
11
+ import { Issuer, BaseClient, ClientMetadata, ClientOptions } from 'openid-client';
12
+ import { JsonWebKey } from '@sphereon/ssi-types';
13
+
14
+ interface IExpressServerOpts {
15
+ port?: number;
16
+ cookieSigningKey?: string;
17
+ hostname?: string;
18
+ basePath?: string;
19
+ existingExpress?: Express;
20
+ listenCallback?: () => void;
21
+ startListening?: boolean;
22
+ }
23
+ declare function hasEndpointOpts(opts: any): any;
24
+ type HasEndpointOpts = {
25
+ endpointOpts?: IEndpointOpts & SingleEndpoints;
26
+ } & Record<string, any>;
27
+ type SingleEndpoints = Record<string, ISingleEndpointOpts | any>;
28
+ interface IEndpointOpts {
29
+ basePath?: string;
30
+ baseUrl?: string | URL;
31
+ globalAuth?: GenericAuthArgs;
32
+ }
33
+ interface ExpressSupport {
34
+ express: Express;
35
+ port: number;
36
+ hostname: string;
37
+ userIsInRole?: string | string[];
38
+ startListening: boolean;
39
+ server?: http.Server;
40
+ enforcer?: Enforcer;
41
+ start: (opts?: {
42
+ disableErrorHandler?: boolean;
43
+ doNotStartListening?: boolean;
44
+ }) => {
45
+ server: http.Server;
46
+ terminator: HttpTerminator;
47
+ };
48
+ stop: (terminator?: HttpTerminator) => Promise<boolean>;
49
+ }
50
+ interface ISingleEndpointOpts extends GenericAuthArgs {
51
+ endpoint?: EndpointArgs;
52
+ enabled?: boolean;
53
+ path?: string;
54
+ disableGlobalAuth?: boolean;
55
+ }
56
+ interface GenericAuthArgs {
57
+ authentication?: {
58
+ callback?: AuthenticateCallback | ((...args: any[]) => any);
59
+ useDefaultCallback?: boolean;
60
+ enabled?: boolean;
61
+ strategy?: string | string[] | Strategy;
62
+ strategyOptions?: Record<string, any> | any;
63
+ authInfo?: boolean;
64
+ session?: boolean;
65
+ };
66
+ authorization?: {
67
+ enabled?: boolean;
68
+ requireUserInRoles?: string | string[];
69
+ enforcer?: Enforcer;
70
+ };
71
+ }
72
+ interface EndpointArgs extends GenericAuthArgs {
73
+ resource?: string;
74
+ operation?: string;
75
+ handlers?: RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>[];
76
+ }
77
+ interface BearerUser extends Express.User {
78
+ id: string | number;
79
+ name?: string;
80
+ token: string;
81
+ }
82
+ interface IStaticBearerVerifyOptions {
83
+ message?: string | undefined;
84
+ scope: string | Array<string>;
85
+ }
86
+ interface ITokenPayload {
87
+ /** An App ID URI. Identifies the intended recipient of the token. */
88
+ aud?: string | undefined;
89
+ /** A security token service(STS) URI. Identifies the STS that constructs and returns the token,
90
+ * and the Azure AD tenant in which the user was authenticated.*/
91
+ iss?: string | undefined;
92
+ /** The identity provider that authenticated the subject of the token*/
93
+ idp?: string | undefined;
94
+ /** "Issued At" indicates when the authentication for this token occurred. */
95
+ iat?: number | undefined;
96
+ /** The "nbf" (not before) claim identifies the time before which the JWT must not be accepted for processing. */
97
+ nbf?: number | undefined;
98
+ /** The "exp" (expiration time) claim identifies the expiration time on or after which the JWT must not be accepted for processing. */
99
+ exp?: number | undefined;
100
+ /** An internal claim used by Azure AD to record data for token reuse. */
101
+ aio?: string | undefined;
102
+ /** Only present in v1.0 tokens. The "Authentication context class" claim. A value of "0" indicates the end-user authentication did not meet the requirements of ISO/IEC 29115. */
103
+ acr?: '0' | '1' | undefined;
104
+ /** Only present in v1.0 tokens. Identifies how the subject of the token was authenticated. */
105
+ amr?: string[] | undefined;
106
+ /** Only present in v1.0 tokens. GUID represents the application ID of the client using the token. */
107
+ appid?: string | undefined;
108
+ /** Only present in v2.0 tokens. The application ID of the client using the token. */
109
+ azp?: string | undefined;
110
+ /** Only present in v1.0 tokens. Indicates how the client was authenticated. For a public client, the value is "0".
111
+ * If client ID and client secret are used, the value is "1". If a client certificate was used for authentication, the value is "2". */
112
+ appidacr?: '0' | '1' | '2' | undefined;
113
+ /** Only present in v2.0 tokens. Indicates how the client was authenticated.
114
+ * For a public client, the value is "0". If client ID and client secret are used, the value is "1". If a client certificate was used for authentication, the value is "2". */
115
+ azpacr?: '0' | '1' | '2' | undefined;
116
+ /** Only present in v2.0 tokens. The primary username that represents the user. It could be an email address, phone number, or a generic username without a specified format */
117
+ preferred_username?: string | undefined;
118
+ /** Provides a human-readable value that identifies the subject of the token.
119
+ * The value is not guaranteed to be unique, it is mutable, and it's designed to be used only for display purposes. The profile scope is required in order to receive this claim. */
120
+ name?: string | undefined;
121
+ /** The set of scopes exposed by your application for which the client application has requested (and received) consent. */
122
+ scp?: string | undefined;
123
+ /** The set of permissions exposed by your application that the requesting application has been given permission to call. */
124
+ roles?: string[] | undefined;
125
+ /** Provides object IDs that represent the subject's group memberships. */
126
+ groups?: string | string[] | undefined;
127
+ /** Denoting the user is in at least one group. */
128
+ hasgroups?: true | undefined;
129
+ /** The principal about which the token asserts information, such as the user of an app. This value is immutable and cannot be reassigned or reused.
130
+ * It can be used to perform authorization checks safely, such as when the token is used to access a resource,
131
+ * and can be used as a key in database tables. Because the subject is always present in the tokens that Azure AD issues,
132
+ * we recommend using this value in a general-purpose authorization system. The subject is, however, a pairwise identifier - it is unique to a particular application ID. */
133
+ sub?: string | undefined;
134
+ /** GUID represents a user. This ID uniquely identifies the user across applications. */
135
+ oid?: string | undefined;
136
+ /** Represents the Azure AD tenant that the user is from. */
137
+ tid?: string | undefined;
138
+ /** Only present in v1.0 tokens. Provides a human readable value that identifies the subject of the token. */
139
+ unique_name?: string | undefined;
140
+ /** An internal claim used by Azure to revalidate tokens. */
141
+ uti?: string | undefined;
142
+ /** An internal claim used by Azure to revalidate tokens. */
143
+ rh?: string | undefined;
144
+ /** Indicates the version of the access token. */
145
+ ver?: '1.0' | '2.0' | undefined;
146
+ /** v1.0 basic claims */
147
+ /** The IP address the user authenticated from. */
148
+ ipaddr?: string | undefined;
149
+ /** In cases where the user has an on-premises authentication, this claim provides their SID. */
150
+ onprem_sid?: string | undefined;
151
+ /** Indicates when the user's password expires. */
152
+ pwd_exp?: number | undefined;
153
+ /** A URL where users can be sent to reset their password. */
154
+ pwd_url?: string | undefined;
155
+ /** Signals if the client is logging in from the corporate network. If they aren't, the claim isn't included. */
156
+ in_corp?: string | undefined;
157
+ /** An additional name for the user, separate from first or last name */
158
+ nickname?: string | undefined;
159
+ /** Provides the last name, surname, or family name of the user as defined on the user object. */
160
+ family_name?: string | undefined;
161
+ /** Provides the first or given name of the user, as set on the user object. */
162
+ given_name?: string | undefined;
163
+ /** The username of the user. May be a phone number, email address, or unformatted string. */
164
+ upn?: string | undefined;
165
+ }
166
+ interface IBaseStrategyOption {
167
+ identityMetadata: string;
168
+ clientID: string;
169
+ isB2C?: boolean | undefined;
170
+ validateIssuer?: boolean | undefined;
171
+ issuer?: string | string[] | undefined;
172
+ loggingLevel?: 'info' | 'warn' | 'error' | undefined;
173
+ loggingNoPII?: boolean | undefined;
174
+ clockSkew?: number | undefined;
175
+ }
176
+ interface IBaseStrategyOption {
177
+ identityMetadata: string;
178
+ clientID: string;
179
+ isB2C?: boolean | undefined;
180
+ validateIssuer?: boolean | undefined;
181
+ issuer?: string | string[] | undefined;
182
+ loggingLevel?: 'info' | 'warn' | 'error' | undefined;
183
+ loggingNoPII?: boolean | undefined;
184
+ clockSkew?: number | undefined;
185
+ }
186
+ interface IBearerStrategyOption extends IBaseStrategyOption {
187
+ audience?: string | string[] | undefined;
188
+ policyName?: String | undefined;
189
+ allowMultiAudiencesInToken?: boolean | undefined;
190
+ scope?: string[] | undefined;
191
+ }
192
+ interface IBearerStrategyOptionWithRequest extends IBearerStrategyOption {
193
+ passReqToCallback: boolean;
194
+ }
195
+ type VerifyBearerFunction = (token: ITokenPayload, done: VerifyCallback) => void;
196
+ interface VerifyCallback {
197
+ (error: any, user?: any, info?: any): void;
198
+ }
199
+
200
+ declare class EntraIDAuth {
201
+ private readonly strategy;
202
+ private options?;
203
+ static init(strategy: string): EntraIDAuth;
204
+ private constructor();
205
+ withOptions(options: IBearerStrategyOption | IBearerStrategyOptionWithRequest): this;
206
+ connectPassport(): void;
207
+ }
208
+
209
+ declare class StaticBearerAuth {
210
+ private readonly strategy;
211
+ private static providers;
212
+ private static verifyOptions;
213
+ private hashTokens?;
214
+ static init(strategy: string, provider?: StaticBearerUserProvider): StaticBearerAuth;
215
+ private constructor();
216
+ get provider(): StaticBearerUserProvider;
217
+ withHashTokens(hashTokens: boolean): this;
218
+ withUsers(users: BearerUser[] | BearerUser): this;
219
+ addUser(user: BearerUser[] | BearerUser): this;
220
+ withVerifyOptions(options: IStaticBearerVerifyOptions | string): this;
221
+ connectPassport(): void;
222
+ }
223
+ interface StaticBearerUserProvider {
224
+ strategy: string;
225
+ addUser(user: BearerUser | BearerUser[], hashToken?: boolean): void;
226
+ getUser(token: string): BearerUser | undefined;
227
+ hashedTokens?: boolean;
228
+ }
229
+ declare class MapBasedStaticBearerUserProvider implements StaticBearerUserProvider {
230
+ private readonly _strategy;
231
+ private readonly _users;
232
+ private readonly _hashedTokens;
233
+ constructor(strategy: string, hashedTokens?: boolean);
234
+ get users(): BearerUser[];
235
+ get hashedTokens(): boolean;
236
+ get strategy(): string;
237
+ getUser(token: string): BearerUser | undefined;
238
+ addUser(user: BearerUser | BearerUser[], hashToken?: boolean): void;
239
+ getUsers(): BearerUser[];
240
+ }
241
+
242
+ declare const checkUserIsInRole: (opts: {
243
+ roles: string | string[];
244
+ }) => (req: express.Request, res: express.Response, next: NextFunction) => void | express.Response<any, Record<string, any>>;
245
+ declare const checkAuthenticationOnly: (opts?: EndpointArgs) => (req: express.Request, res: express.Response, next: express.NextFunction) => void | express.Response<any, Record<string, any>>;
246
+ declare const checkAuthorizationOnly: (opts?: EndpointArgs) => (req: express.Request, res: express.Response, next: express.NextFunction) => void | express.Response<any, Record<string, any>>;
247
+ declare const isUserNotAuthenticated: (req: express.Request, res: express.Response, next: express.NextFunction) => void;
248
+ declare const isUserAuthenticated: (req: express.Request, res: express.Response, next: express.NextFunction) => void | express.Response<any, Record<string, any>>;
249
+ declare const checkAuth: (opts?: EndpointArgs) => RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>[];
250
+ declare function copyGlobalAuthToEndpoint(args?: {
251
+ opts?: HasEndpointOpts;
252
+ key: string;
253
+ }): void;
254
+ declare function copyGlobalAuthToEndpoints(args?: {
255
+ opts?: HasEndpointOpts;
256
+ keys: string[];
257
+ }): void;
258
+
259
+ type Handler<Request extends http.IncomingMessage, Response extends http.ServerResponse> = (req: Request, res: Response, callback: (err?: Error) => void) => void;
260
+ declare class ExpressBuilder {
261
+ private existingExpress?;
262
+ private hostnameOrIP?;
263
+ private port?;
264
+ private _handlers?;
265
+ private listenCallback?;
266
+ private _startListen?;
267
+ private readonly envVarPrefix?;
268
+ private _corsConfigurer?;
269
+ private _sessionOpts?;
270
+ private _usePassportAuth?;
271
+ private _passportInitOpts?;
272
+ private _userIsInRole?;
273
+ private _enforcer?;
274
+ private _server?;
275
+ private _terminator?;
276
+ private _morgan?;
277
+ private constructor();
278
+ static fromExistingExpress(opts?: {
279
+ existingExpress?: Express;
280
+ envVarPrefix?: string;
281
+ }): ExpressBuilder;
282
+ static fromServerOpts(opts: IExpressServerOpts & {
283
+ envVarPrefix?: string;
284
+ }): ExpressBuilder;
285
+ enableListen(startOnBuild?: boolean): this;
286
+ withMorganLogging(opts?: {
287
+ existingMorgan?: Handler<any, any>;
288
+ format?: string;
289
+ options?: morgan.Options<any, any>;
290
+ }): this;
291
+ withEnableListenOpts({ port, hostnameOrIP, callback, startOnBuild, }: {
292
+ port?: number;
293
+ hostnameOrIP?: string;
294
+ startOnBuild?: boolean;
295
+ callback?: () => void;
296
+ }): this;
297
+ withPort(port: number): this;
298
+ withHostname(hostnameOrIP: string): this;
299
+ withListenCallback(callback: () => void): this;
300
+ withExpress(existingExpress: Express): this;
301
+ withCorsConfigurer(configurer: ExpressCorsConfigurer): this;
302
+ withPassportAuth(usePassport: boolean, initializeOptions?: InitializeOptions): this;
303
+ withGlobalUserIsInRole(userIsInRole: string | string[]): this;
304
+ withEnforcer(enforcer: Enforcer): this;
305
+ startListening(express: Express): {
306
+ server: http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
307
+ terminator: HttpTerminator;
308
+ };
309
+ getHostname(): string;
310
+ getPort(): number;
311
+ setHandlers(handlers: ApplicationRequestHandler<any> | ApplicationRequestHandler<any>[]): this;
312
+ addHandler(handler: ApplicationRequestHandler<any>): this;
313
+ withSessionOptions(sessionOpts: session.SessionOptions): this;
314
+ build<T extends Application>(opts?: {
315
+ express?: Express;
316
+ startListening?: boolean;
317
+ handlers?: ApplicationRequestHandler<T> | ApplicationRequestHandler<T>[];
318
+ }): ExpressSupport;
319
+ protected buildExpress<T extends Application>(opts?: {
320
+ express?: Express;
321
+ startListening?: boolean;
322
+ handlers?: ApplicationRequestHandler<T> | ApplicationRequestHandler<T>[];
323
+ }): express.Express;
324
+ }
325
+ declare class ExpressCorsConfigurer {
326
+ private _disableCors?;
327
+ private _enablePreflightOptions?;
328
+ private _allowOrigin?;
329
+ private _allowMethods?;
330
+ private _allowedHeaders?;
331
+ private _allowCredentials?;
332
+ private readonly _express?;
333
+ private readonly _envVarPrefix?;
334
+ constructor(args?: {
335
+ existingExpress?: Express;
336
+ envVarPrefix?: string;
337
+ });
338
+ allowOrigin(value: string | boolean | RegExp | Array<string | boolean | RegExp>): this;
339
+ disableCors(value: boolean): this;
340
+ allowMethods(value: string | string[]): this;
341
+ allowedHeaders(value: string | string[]): this;
342
+ allowCredentials(value: boolean): this;
343
+ configure({ existingExpress }: {
344
+ existingExpress?: Express;
345
+ }): void;
346
+ }
347
+
348
+ declare function sendErrorResponse(response: express.Response, statusCode: number, message: string | object, error?: any): express.Response<any, Record<string, any>>;
349
+ declare const jsonErrorHandler: (err: any, req: express.Request, res: express.Response, next: NextFunction) => void | express.Response<any, Record<string, any>>;
350
+
351
+ declare function env(key?: string, prefix?: string): string | undefined;
352
+
353
+ declare function oidcDiscoverIssuer(opts?: {
354
+ issuerUrl?: string;
355
+ }): Promise<{
356
+ issuer: Issuer<BaseClient>;
357
+ issuerUrl: string;
358
+ }>;
359
+ declare function oidcGetClient(issuer: Issuer<BaseClient>, metadata: ClientMetadata, opts?: {
360
+ jwks?: {
361
+ keys: JsonWebKey[];
362
+ };
363
+ options?: ClientOptions;
364
+ }): Promise<BaseClient>;
365
+ declare function getLoginEndpoint(router: Router, opts?: ISingleEndpointOpts & {
366
+ redirectUrl?: string;
367
+ }): void;
368
+ declare function getLoginCallbackEndpoint(router: Router, opts?: ISingleEndpointOpts): void;
369
+ declare function getLogoutEndpoint(router: Router, client: BaseClient, opts?: ISingleEndpointOpts): void;
370
+ declare function getLogoutCallbackEndpoint(router: Router, opts?: ISingleEndpointOpts): void;
371
+ declare function getIdTokenEndpoint(router: Router, client: BaseClient, opts: ISingleEndpointOpts): void;
372
+ declare function getAuthenticatedUserEndpoint(router: Router, opts?: ISingleEndpointOpts): void;
373
+ interface IAuthenticationOpts {
374
+ enabledFeatures?: AuthenticationApiFeatures;
375
+ endpointOpts?: IAuthenticationEndpointOpts;
376
+ }
377
+ interface IAuthenticationEndpointOpts {
378
+ basePath?: string;
379
+ globalAuth?: GenericAuthArgs;
380
+ getAuthenticatedUser?: ISingleEndpointOpts;
381
+ getLogin?: ISingleEndpointOpts;
382
+ getLogout?: ISingleEndpointOpts;
383
+ getIdToken?: ISingleEndpointOpts;
384
+ }
385
+ type AuthenticationApiFeatures = 'login' | 'logout' | 'id-token' | 'authenticated-user';
386
+ declare class OpenIDConnectAuthApi {
387
+ get router(): express.Router;
388
+ private readonly _express;
389
+ private readonly _agent?;
390
+ private readonly _opts?;
391
+ private readonly _router;
392
+ constructor(args: {
393
+ agent?: TAgent<any>;
394
+ expressSupport: ExpressSupport;
395
+ client: BaseClient;
396
+ opts: IAuthenticationOpts;
397
+ });
398
+ get agent(): TAgent<any> | undefined;
399
+ get opts(): IAuthenticationOpts | undefined;
400
+ get express(): Express;
401
+ }
402
+
403
+ export { type AuthenticationApiFeatures, type BearerUser, type EndpointArgs, EntraIDAuth, ExpressBuilder, ExpressCorsConfigurer, type ExpressSupport, type GenericAuthArgs, type HasEndpointOpts, type IAuthenticationEndpointOpts, type IAuthenticationOpts, type IBaseStrategyOption, type IBearerStrategyOption, type IBearerStrategyOptionWithRequest, type IEndpointOpts, type IExpressServerOpts, type ISingleEndpointOpts, type IStaticBearerVerifyOptions, type ITokenPayload, MapBasedStaticBearerUserProvider, OpenIDConnectAuthApi, type SingleEndpoints, StaticBearerAuth, type StaticBearerUserProvider, type VerifyBearerFunction, type VerifyCallback, checkAuth, checkAuthenticationOnly, checkAuthorizationOnly, checkUserIsInRole, copyGlobalAuthToEndpoint, copyGlobalAuthToEndpoints, env, getAuthenticatedUserEndpoint, getIdTokenEndpoint, getLoginCallbackEndpoint, getLoginEndpoint, getLogoutCallbackEndpoint, getLogoutEndpoint, hasEndpointOpts, isUserAuthenticated, isUserNotAuthenticated, jsonErrorHandler, oidcDiscoverIssuer, oidcGetClient, sendErrorResponse };