@crossauth/fastify 0.0.14 → 0.0.16

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.
@@ -182,6 +182,10 @@ export interface FastifyOAuthClientOptions extends OAuthClientOptions {
182
182
  * Defaults to empty.
183
183
  */
184
184
  validFlows?: string[];
185
+ /**
186
+ * These token types will be treated as JWT. Default all of them
187
+ */
188
+ jwtTokens?: ("access" | "id" | "refresh")[];
185
189
  }
186
190
  /**
187
191
  * Query type for the `authorize` Fastify request.
@@ -270,6 +274,13 @@ export interface PasswordOobType {
270
274
  challenge_type?: string;
271
275
  name?: string;
272
276
  }
277
+ /**
278
+ * Query type for the refresh token flow Fastify request.
279
+ */
280
+ export interface TokensBodyType {
281
+ decode?: boolean;
282
+ csrfToken?: string;
283
+ }
273
284
  /**
274
285
  * The Fastify implementation of the OAuth client.
275
286
  *
@@ -331,7 +342,19 @@ export interface PasswordOobType {
331
342
  *
332
343
  * This pattern avoids you having to store the access token in the frontend.
333
344
  *
334
- * **Endpoints provided by this class**
345
+ * **Middleware**
346
+ *
347
+ * This class provides middleware that works with the BFF method.
348
+ *
349
+ * If an ID token is saved in the session and it is valid, the following
350
+ * state attributes are set in the request object:
351
+ *
352
+ * - `idPayload` the payload from the ID token
353
+ * - `user` a :class:`crossauth_backend.User` object created from the ID
354
+ * token
355
+ * - `authType` set to `oidc`
356
+ *
357
+ * **Endpoints provided by this class**
335
358
  *
336
359
  * In addition to the BFF endpoints above, this class provides the following
337
360
  * endpoints. The ENDPOINT column values can be overridden in
@@ -359,12 +382,13 @@ export interface PasswordOobType {
359
382
  * | POST | `api/devicecodeflow` | Initiates the device code flow | See {@link DeviceCodeBodyType} | See {@link DeviceCodeFlowResponse} | |
360
383
  * | POST | `devicecodepoll` | Initiates the device code flow | See {@link DeviceCodePollBodyType} | Authorization complete: See docs for`tokenResponseType`. Other cases, {@link @crossauth/common!OAuthTokenResponse} | |
361
384
  * | POST | `api/devicecodepoll` | Initiates the device code flow | See {@link DeviceCodePollBodyType} | {@link @crossauth/common!OAuthTokenResponse} | |
362
- * | POST | `access_token` | For BFF mode, returns the saved access token | | *Access token payload* | |
363
- * | POST | `refresh_token` | For BFF mode, returns the saved refresh token | | `token` containing the refresh token | |
364
- * | POST | `id_token ` | For BFF mode, returns the saved ID token | | *ID token payload* | |
385
+ * | POST | `access_token` | For BFF mode, returns the saved access token | `decode`, default `true` | *Access token payload* | |
386
+ * | POST | `refresh_token` | For BFF mode, returns the saved refresh token | `decode`, default `true` | `token` containing the refresh token | |
387
+ * | POST | `id_token ` | For BFF mode, returns the saved ID token | `decode`, default `true` | *ID token payload* | |
365
388
  * | POST | `have_access_token` | For BFF mode, returns whether an acccess token is saved | | `ok` | |
366
389
  * | POST | `have_refresh_token`| For BFF mode, returns whether a refresh token is saved | | `ok` | |
367
390
  * | POST | `have_id_token` | For BFF mode, returns whether an ID token is saved | | `ok` | |
391
+ * | POST | `tokens` | For BFF mode, returns all the saved tokens | `decode`, default `true` | *Token payloads * | |
368
392
  * | POST | `deletetokens` | Deletes all BFF tokens and displays a page | None | `ok` | `deleteTokensPage` |
369
393
  * | POST | `api/deletetokens` | Delertes all tokens and returns JSON | None | `ok` | |
370
394
  */
@@ -388,6 +412,9 @@ export declare class FastifyOAuthClient extends OAuthClientBackend {
388
412
  private errorFn;
389
413
  private loginUrl;
390
414
  private validFlows;
415
+ readonly jwtTokens: string[];
416
+ private testMiddleware;
417
+ private requestObj;
391
418
  /**
392
419
  * See {@link FastifyOAuthClientOptions}
393
420
  */
@@ -423,6 +450,7 @@ export declare class FastifyOAuthClient extends OAuthClientBackend {
423
450
  expires_at?: number;
424
451
  error?: string;
425
452
  error_description?: string;
453
+ id_token?: string;
426
454
  } | FastifyReply | undefined>;
427
455
  private refreshTokens;
428
456
  private deleteTokens;
@@ -1,9 +1,27 @@
1
1
  import { FastifyInstance } from 'fastify';
2
2
  import { Server, IncomingMessage, ServerResponse } from 'http';
3
3
  import { OAuthClientStorage, KeyStorage, OAuthAuthorizationServer, Authenticator, OAuthAuthorizationServerOptions, DoubleSubmitCsrfTokenOptions } from '@crossauth/backend';
4
- import { OpenIdConfiguration } from '@crossauth/common';
4
+ import { OpenIdConfiguration, User } from '@crossauth/common';
5
5
  import { FastifyServer } from './fastifyserver';
6
6
 
7
+ export interface DevicePageData {
8
+ authorizationNeeded?: {
9
+ user: User;
10
+ client_id: string;
11
+ client_name: string;
12
+ scope?: string;
13
+ scopes?: string[];
14
+ csrfToken?: string;
15
+ };
16
+ completed: boolean;
17
+ retryAllowed: boolean;
18
+ user?: User;
19
+ csrfToken?: string;
20
+ ok: boolean;
21
+ error?: string;
22
+ error_description?: string;
23
+ user_code?: string;
24
+ }
7
25
  /**
8
26
  * Options for {@link FastifyAuthorizationServer}
9
27
  */
@@ -2,6 +2,7 @@ import { FastifyRequest, FastifyInstance } from 'fastify';
2
2
  import { Server, IncomingMessage, ServerResponse } from 'http';
3
3
  import { User } from '@crossauth/common';
4
4
  import { OAuthResourceServer, UserStorage, OAuthResourceServerOptions, OAuthTokenConsumer } from '@crossauth/backend';
5
+ import { FastifySessionAdapter } from './fastifysessionadapter';
5
6
 
6
7
  /**
7
8
  * Options for {@link FastifyOAuthResourceServer}
@@ -32,6 +33,24 @@ export interface FastifyOAuthResourceServerOptions extends OAuthResourceServerOp
32
33
  acceptSessionAuthorization?: boolean;
33
34
  };
34
35
  };
36
+ /**
37
+ * Where access tokens may be found (in this order).
38
+ *
39
+ * If this contains `session`, must also provide the session adapter
40
+ *
41
+ * Default `header`
42
+ */
43
+ tokenLocations?: ("beader" | "session")[];
44
+ /**
45
+ * If tokenLocations contains `session`, tokens are keyed on this name.
46
+ *
47
+ * Default `oauth`
48
+ */
49
+ sessionDataName?: string;
50
+ /**
51
+ * If `tokenLocations` contains `session`, must provide a session adapter
52
+ */
53
+ sessionAdapter?: FastifySessionAdapter;
35
54
  }
36
55
  /**
37
56
  * OAuth resource server.
@@ -47,11 +66,17 @@ export interface FastifyOAuthResourceServerOptions extends OAuthResourceServerOp
47
66
  *
48
67
  * If you do set `protectedEndpoints` in
49
68
  * {@link FastifyOAuthResourceServer.constructor}
50
- * then a `preHandler` iscreated.
69
+ * then a `preHandler` is created.
70
+ *
71
+ * **Middleware**
72
+ *
51
73
  * The preHandler
52
74
  * hook will set the `accessTokenPayload`, `user` and `scope` fields
53
75
  * on the Fastify request object based on the content
54
76
  * of the access token in the `Authorization` header if it is valid.
77
+ * If a user storage is provided,
78
+ * it will be used to look the user up. Otherwise a minimal user object
79
+ * is created.
55
80
  * If it is not valid it will set the `authError` and `authErrorDescription`.
56
81
  * If the access token is invalid, or there is an error, a 401 or 500
57
82
  * response is sent before executing your endpoint code. As per
@@ -62,10 +87,13 @@ export declare class FastifyOAuthResourceServer extends OAuthResourceServer {
62
87
  private userStorage?;
63
88
  private protectedEndpoints;
64
89
  private errorBody;
90
+ private sessionDataName;
91
+ private tokenLocations;
92
+ private sessionAdapter?;
65
93
  /**
66
94
  * Constructor
67
95
  * @param app the Fastify app
68
- * @param tokenConsumers the token consumers, one per issuer
96
+ * @param tokenConsumers the token consumers, one per issuer and audience
69
97
  * @param options See {@link FastifyOAuthResourceServerOptions}
70
98
  */
71
99
  constructor(app: FastifyInstance<Server, IncomingMessage, ServerResponse>, tokenConsumers: OAuthTokenConsumer[], options?: FastifyOAuthResourceServerOptions);
@@ -90,4 +118,6 @@ export declare class FastifyOAuthResourceServer extends OAuthResourceServer {
90
118
  error?: string;
91
119
  error_description?: string;
92
120
  } | undefined>;
121
+ private tokenFromHeader;
122
+ private tokenFromSession;
93
123
  }
@@ -29,6 +29,12 @@ export interface FastifyServerOptions extends FastifySessionServerOptions, Fasti
29
29
  /** If this is passed, it is registered as a Nunjucks view folder with autoscape on */
30
30
  views?: string;
31
31
  isAdminFn?: (user: User) => boolean;
32
+ /**
33
+ * Config for `@fastify/cors`
34
+ */
35
+ cors?: {
36
+ [key: string]: any;
37
+ };
32
38
  }
33
39
  /**
34
40
  * Type for the function that is called to pass an error back to the user
@@ -42,7 +48,7 @@ export type FastifyErrorFn = (server: FastifyServer, request: FastifyRequest, re
42
48
  * This class provides a complete (but without HTML files) auth backend server
43
49
  * for Fastify applications
44
50
  *
45
- * If you do not pass an Fastify app to this class, it will create one.
51
+ * If you do not pass a Fastify app to this class, it will create one.
46
52
  * By default, pages are rendered
47
53
  * with Nunjucks. If you prefer another renderer that is compatible with
48
54
  * Fastify, create your
@@ -64,7 +70,7 @@ export type FastifyErrorFn = (server: FastifyServer, request: FastifyRequest, re
64
70
  * - `sessionServer` Session cookie management server. Uses sesion ID
65
71
  * and CSRF cookies. See {@link FastifySessionServer}.
66
72
  * - `sessionAdapter` If you want an OAuth client but not want to use
67
- * Fastify's session server, you can provide your own
73
+ * Crossauth's session server, you can provide your own
68
74
  * with this. Won't work with auth server.
69
75
  * - `oAuthAuthServer` OAuth authorization server. See
70
76
  * {@link FastifyAuthorizationServer}
@@ -86,7 +92,7 @@ export type FastifyErrorFn = (server: FastifyServer, request: FastifyRequest, re
86
92
  * **Authenticators**
87
93
  *
88
94
  * One and two factor authentication is supported. Authentication is provided
89
- * by classes implementing {@link Authenticator}. They are passed as an
95
+ * by classes implementing {@link @crossauth/backend!Authenticator}. They are passed as an
90
96
  * object to this class, keyed on the name that appears in the user record
91
97
  * as `factor1` or `factor2`.
92
98
  *
@@ -122,6 +128,7 @@ export declare class FastifyServer {
122
128
  readonly oAuthResServer?: FastifyOAuthResourceServer;
123
129
  /** Config for `@fastify/cors` */
124
130
  private cors;
131
+ private audience;
125
132
  /**
126
133
  * Integrates fastify session, API key and OAuth servers
127
134
  * @param config object with entries as follow:
@@ -389,7 +389,7 @@ export interface AuthenticatorDetails {
389
389
  hasSecrets: boolean;
390
390
  }
391
391
  /**
392
- * This class adds user endpoints to the Fastidfy session server.
392
+ * This class adds user endpoints to the Fastify session server.
393
393
  *
394
394
  * You shouldn't have create create this directly - it is created by
395
395
  * {@link FastifyServer}.
@@ -460,7 +460,7 @@ export interface AuthenticatorDetails {
460
460
  * be redirected to the page for entering the second factor.
461
461
  *
462
462
  * For API endpoints., the response will be
463
- * `{"ok": true, "factor2Reqiored': true}`. The user should then make a POST
463
+ * `{"ok": true, "factor2Required': true}`. The user should then make a POST
464
464
  * request to the same endpoint but with the 2FA field in the body instead
465
465
  * of the original request body, eg `{"otp": 123456}`. If the factor is
466
466
  * valid, the JSON data from the original post will be submittd.
@@ -482,6 +482,16 @@ export interface AuthenticatorDetails {
482
482
  * If you are serving other endpoints, or you want to use something other than
483
483
  * Nunjucks, you can create
484
484
  * and pass in your own Fastify app.
485
+
486
+ * **Middleware**
487
+ *
488
+ * This class registers one middleware function to fill in the following
489
+ * fields in the request:
490
+ *
491
+ * - `user` a {@link @crossauth/common!User}` object
492
+ * - `authType`: set to `cookie` or undefined
493
+ * - `csrfToken`: a CSRF token that can be used in POST requests
494
+ * - `sessionId` a session ID if one is created
485
495
  */
486
496
  export declare class FastifySessionServer implements FastifySessionAdapter {
487
497
  /**