@travetto/auth 8.0.0-alpha.9 → 8.0.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/README.md +8 -8
- package/__index__.ts +4 -4
- package/package.json +13 -13
- package/src/config.ts +1 -2
- package/src/context.ts +4 -5
- package/src/service.ts +13 -12
- package/src/types/authenticator.ts +2 -1
- package/src/types/error.ts +2 -3
- package/src/types/principal.ts +1 -1
- package/src/types/token.ts +1 -1
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
<!-- Please modify https://github.com/travetto/travetto/tree/main/module/auth/DOC.tsx and execute "npx trv doc" to rebuild -->
|
|
3
3
|
# Authentication
|
|
4
4
|
|
|
5
|
-
## Authentication
|
|
5
|
+
## Authentication support for the Travetto framework
|
|
6
6
|
|
|
7
7
|
**Install: @travetto/auth**
|
|
8
8
|
```bash
|
|
@@ -13,7 +13,7 @@ npm install @travetto/auth
|
|
|
13
13
|
yarn add @travetto/auth
|
|
14
14
|
```
|
|
15
15
|
|
|
16
|
-
This module provides the high-level backdrop for managing security principals.
|
|
16
|
+
This module provides the high-level backdrop for managing security principals. The goal of this module is to be a centralized location for various security frameworks to plug into. The primary contributions are:
|
|
17
17
|
* Standard Types
|
|
18
18
|
* Authentication Contract
|
|
19
19
|
* Authorization Contract
|
|
@@ -21,7 +21,7 @@ This module provides the high-level backdrop for managing security principals.
|
|
|
21
21
|
* Authorization Context
|
|
22
22
|
|
|
23
23
|
## Standard Types
|
|
24
|
-
The module's goal is to be as flexible as possible.
|
|
24
|
+
The module's goal is to be as flexible as possible. To that end, the primary contract that this module defines, is that of the [Principal](https://github.com/travetto/travetto/tree/main/module/auth/src/types/principal.ts#L7).
|
|
25
25
|
|
|
26
26
|
**Code: Principal**
|
|
27
27
|
```typescript
|
|
@@ -80,7 +80,7 @@ export interface Authenticator<T = unknown, C = unknown, P extends Principal = P
|
|
|
80
80
|
}
|
|
81
81
|
```
|
|
82
82
|
|
|
83
|
-
The [Authenticator](https://github.com/travetto/travetto/tree/main/module/auth/src/types/authenticator.ts#
|
|
83
|
+
The [Authenticator](https://github.com/travetto/travetto/tree/main/module/auth/src/types/authenticator.ts#L10) only requires one method to be defined, and that is `authenticate`. This method receives a generic payload, and a supplemental context as an input. The interface is responsible for converting that to an authenticated principal.
|
|
84
84
|
|
|
85
85
|
## Authorization Contract
|
|
86
86
|
|
|
@@ -134,14 +134,14 @@ export class AuthService {
|
|
|
134
134
|
}
|
|
135
135
|
```
|
|
136
136
|
|
|
137
|
-
The [AuthService](https://github.com/travetto/travetto/tree/main/module/auth/src/service.ts#
|
|
137
|
+
The [AuthService](https://github.com/travetto/travetto/tree/main/module/auth/src/service.ts#L12) operates as the owner of the current auth state for a given "request". "Request" here implies a set of operations over a period of time, with the http request/response model being an easy point of reference. This could also tie to a CLI operation, or any other invocation that requires some concept of authentication and authorization.
|
|
138
138
|
|
|
139
|
-
The service allows for storing and retrieving the active [Principal](https://github.com/travetto/travetto/tree/main/module/auth/src/types/principal.ts#L7), and/or the actively persisted auth token.
|
|
139
|
+
The service allows for storing and retrieving the active [Principal](https://github.com/travetto/travetto/tree/main/module/auth/src/types/principal.ts#L7), and/or the actively persisted auth token. This is extremely useful for other parts of the framework that may request authenticated information (if available). [Web Auth](https://github.com/travetto/travetto/tree/main/module/auth-web#readme "Web authentication integration support for the Travetto framework") makes heavy use of this state for enforcing endpoints when authentication is required.
|
|
140
140
|
|
|
141
141
|
### Login
|
|
142
|
-
"Logging in" can be thought of going through the action of finding a single source that can authenticate the identity for the request credentials.
|
|
142
|
+
"Logging in" can be thought of going through the action of finding a single source that can authenticate the identity for the request credentials. Some times there may be more than one valid source of authentication that you want to leverage, and the first one to authenticate wins. The [AuthService](https://github.com/travetto/travetto/tree/main/module/auth/src/service.ts#L12) operates in this fashion, in which a set of credentials and potential [Authenticator](https://github.com/travetto/travetto/tree/main/module/auth/src/types/authenticator.ts#L10)s are submitted, and the service will attempt to authenticate.
|
|
143
143
|
|
|
144
|
-
Upon successful authentication, an optional [Authorizer](https://github.com/travetto/travetto/tree/main/module/auth/src/types/authorizer.ts#L8) may be invoked to authorize the authenticated user.
|
|
144
|
+
Upon successful authentication, an optional [Authorizer](https://github.com/travetto/travetto/tree/main/module/auth/src/types/authorizer.ts#L8) may be invoked to authorize the authenticated user. The [Authenticator](https://github.com/travetto/travetto/tree/main/module/auth/src/types/authenticator.ts#L10) is assumed to be only one within the system, and should be tied to the specific product you are building for. The [Authorizer](https://github.com/travetto/travetto/tree/main/module/auth/src/types/authorizer.ts#L8) should be assumed to have multiple sources, and are generally specific to external third parties. All of these values are collected via the [Dependency Injection](https://github.com/travetto/travetto/tree/main/module/di#readme "Dependency registration/management and injection support.") module and will be auto-registered on startup.
|
|
145
145
|
|
|
146
146
|
If this process is too cumbersome or restrictive, manually authenticating and authorizing is still more than permissible, and setting the principal within the service is a logical equivalent to login.
|
|
147
147
|
|
package/__index__.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
export * from './src/service.ts';
|
|
2
|
-
export * from './src/context.ts';
|
|
3
1
|
export * from './src/config.ts';
|
|
2
|
+
export * from './src/context.ts';
|
|
3
|
+
export * from './src/service.ts';
|
|
4
4
|
export * from './src/types/authenticator.ts';
|
|
5
5
|
export * from './src/types/authorizer.ts';
|
|
6
|
-
export * from './src/types/principal.ts';
|
|
7
6
|
export * from './src/types/error.ts';
|
|
8
|
-
export * from './src/types/
|
|
7
|
+
export * from './src/types/principal.ts';
|
|
8
|
+
export * from './src/types/token.ts';
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/auth",
|
|
3
|
-
"version": "8.0.0
|
|
4
|
-
"
|
|
5
|
-
"description": "Authentication
|
|
3
|
+
"version": "8.0.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "Authentication support for the Travetto framework",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"authentication",
|
|
8
8
|
"travetto",
|
|
@@ -11,26 +11,26 @@
|
|
|
11
11
|
"homepage": "https://travetto.io",
|
|
12
12
|
"license": "MIT",
|
|
13
13
|
"author": {
|
|
14
|
-
"
|
|
15
|
-
"
|
|
14
|
+
"name": "Travetto Framework",
|
|
15
|
+
"email": "travetto.framework@gmail.com"
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"url": "git+https://github.com/travetto/travetto.git",
|
|
19
|
+
"directory": "module/auth"
|
|
16
20
|
},
|
|
17
21
|
"files": [
|
|
18
22
|
"__index__.ts",
|
|
19
23
|
"src"
|
|
20
24
|
],
|
|
25
|
+
"type": "module",
|
|
21
26
|
"main": "__index__.ts",
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
"directory": "module/auth"
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
25
29
|
},
|
|
26
30
|
"dependencies": {
|
|
27
|
-
"@travetto/context": "^8.0.0
|
|
31
|
+
"@travetto/context": "^8.0.0"
|
|
28
32
|
},
|
|
29
33
|
"travetto": {
|
|
30
34
|
"displayName": "Authentication"
|
|
31
|
-
},
|
|
32
|
-
"private": false,
|
|
33
|
-
"publishConfig": {
|
|
34
|
-
"access": "public"
|
|
35
35
|
}
|
|
36
36
|
}
|
package/src/config.ts
CHANGED
|
@@ -5,7 +5,6 @@ import { Ignore } from '@travetto/schema';
|
|
|
5
5
|
|
|
6
6
|
@Config('auth')
|
|
7
7
|
export class AuthConfig {
|
|
8
|
-
|
|
9
8
|
maxAge: TimeSpan | number = '1h';
|
|
10
9
|
rollingRenew: boolean = true;
|
|
11
10
|
|
|
@@ -16,4 +15,4 @@ export class AuthConfig {
|
|
|
16
15
|
finalize(): void {
|
|
17
16
|
this.maxAgeMs = TimeUtil.duration(this.maxAge, 'ms');
|
|
18
17
|
}
|
|
19
|
-
}
|
|
18
|
+
}
|
package/src/context.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { Inject, Injectable } from '@travetto/di';
|
|
2
1
|
import { type AsyncContext, AsyncContextValue } from '@travetto/context';
|
|
2
|
+
import { Inject, Injectable } from '@travetto/di';
|
|
3
3
|
|
|
4
|
-
import type { AuthToken } from './types/token.ts';
|
|
5
|
-
import type { Principal } from './types/principal.ts';
|
|
6
4
|
import type { AuthenticatorState } from './types/authenticator.ts';
|
|
5
|
+
import type { Principal } from './types/principal.ts';
|
|
6
|
+
import type { AuthToken } from './types/token.ts';
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Provides the primary context for the authenticated state
|
|
@@ -12,7 +12,6 @@ import type { AuthenticatorState } from './types/authenticator.ts';
|
|
|
12
12
|
*/
|
|
13
13
|
@Injectable()
|
|
14
14
|
export class AuthContext {
|
|
15
|
-
|
|
16
15
|
#principal = new AsyncContextValue<Principal>(this, { failIfUnbound: { write: true } });
|
|
17
16
|
#authToken = new AsyncContextValue<AuthToken>(this, { failIfUnbound: { write: true } });
|
|
18
17
|
#authState = new AsyncContextValue<AuthenticatorState>(this, { failIfUnbound: { write: true } });
|
|
@@ -71,4 +70,4 @@ export class AuthContext {
|
|
|
71
70
|
this.#authToken.set(undefined);
|
|
72
71
|
this.#authState.set(undefined);
|
|
73
72
|
}
|
|
74
|
-
}
|
|
73
|
+
}
|
package/src/service.ts
CHANGED
|
@@ -1,17 +1,15 @@
|
|
|
1
|
-
|
|
2
1
|
import { DependencyRegistryIndex, getDefaultQualifier, Inject, Injectable, PostConstruct } from '@travetto/di';
|
|
3
|
-
import {
|
|
2
|
+
import { TimeUtil, toConcrete } from '@travetto/runtime';
|
|
4
3
|
|
|
5
|
-
import type {
|
|
4
|
+
import type { AuthConfig } from './config.ts';
|
|
5
|
+
import type { AuthContext } from './context.ts';
|
|
6
6
|
import type { Authenticator } from './types/authenticator.ts';
|
|
7
7
|
import type { Authorizer } from './types/authorizer.ts';
|
|
8
8
|
import { AuthenticationError } from './types/error.ts';
|
|
9
|
-
import type {
|
|
10
|
-
import type { AuthConfig } from './config.ts';
|
|
9
|
+
import type { Principal } from './types/principal.ts';
|
|
11
10
|
|
|
12
11
|
@Injectable()
|
|
13
12
|
export class AuthService {
|
|
14
|
-
|
|
15
13
|
@Inject()
|
|
16
14
|
authContext: AuthContext;
|
|
17
15
|
|
|
@@ -60,10 +58,11 @@ export class AuthService {
|
|
|
60
58
|
this.authContext.authenticatorState = await authenticator.getState(context);
|
|
61
59
|
}
|
|
62
60
|
|
|
63
|
-
if (!principal) {
|
|
61
|
+
if (!principal) {
|
|
62
|
+
// Multi-step login process
|
|
64
63
|
return;
|
|
65
64
|
}
|
|
66
|
-
return this.authContext.principal = (await this.authorizer?.authorize(principal)) ?? principal;
|
|
65
|
+
return (this.authContext.principal = (await this.authorizer?.authorize(principal)) ?? principal);
|
|
67
66
|
} catch (error) {
|
|
68
67
|
if (!(error instanceof Error)) {
|
|
69
68
|
throw error;
|
|
@@ -94,10 +93,12 @@ export class AuthService {
|
|
|
94
93
|
|
|
95
94
|
principal.issuedAt ??= new Date();
|
|
96
95
|
|
|
97
|
-
if (principal.expiresAt && this.config.maxAgeMs && this.config.rollingRenew) {
|
|
96
|
+
if (principal.expiresAt && this.config.maxAgeMs && this.config.rollingRenew) {
|
|
97
|
+
// Session behavior
|
|
98
98
|
const end = principal.expiresAt.getTime();
|
|
99
99
|
const midPoint = end - this.config.maxAgeMs / 2;
|
|
100
|
-
if (Date.now() > midPoint) {
|
|
100
|
+
if (Date.now() > midPoint) {
|
|
101
|
+
// If we are past the half way mark, renew the token
|
|
101
102
|
principal.issuedAt = new Date();
|
|
102
103
|
principal.expiresAt = TimeUtil.fromNow(this.config.maxAgeMs); // This will trigger a re-send
|
|
103
104
|
}
|
|
@@ -108,9 +109,9 @@ export class AuthService {
|
|
|
108
109
|
* Enforce expiry, invalidating the principal if expired
|
|
109
110
|
*/
|
|
110
111
|
enforceExpiry(principal?: Principal): Principal | undefined {
|
|
111
|
-
if (principal
|
|
112
|
+
if (principal?.expiresAt && principal.expiresAt.getTime() < Date.now()) {
|
|
112
113
|
return undefined;
|
|
113
114
|
}
|
|
114
115
|
return principal;
|
|
115
116
|
}
|
|
116
|
-
}
|
|
117
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { AnyMap } from '@travetto/runtime';
|
|
2
|
+
|
|
2
3
|
import type { Principal } from './principal.ts';
|
|
3
4
|
|
|
4
5
|
/**
|
|
@@ -6,7 +7,7 @@ import type { Principal } from './principal.ts';
|
|
|
6
7
|
*
|
|
7
8
|
* @concrete
|
|
8
9
|
*/
|
|
9
|
-
export interface AuthenticatorState extends AnyMap {
|
|
10
|
+
export interface AuthenticatorState extends AnyMap {}
|
|
10
11
|
|
|
11
12
|
/**
|
|
12
13
|
* Supports validation payload of type T into an authenticated principal
|
package/src/types/error.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { type ErrorCategory, RuntimeError } from '@travetto/runtime';
|
|
2
2
|
|
|
3
3
|
export class AuthenticationError<T = Record<string, unknown> | undefined> extends RuntimeError<T> {
|
|
4
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
5
4
|
static defaultCategory = 'authentication' as ErrorCategory;
|
|
6
|
-
}
|
|
5
|
+
}
|
package/src/types/principal.ts
CHANGED
package/src/types/token.ts
CHANGED