@travetto/auth 7.0.0-rc.0 → 7.0.0-rc.2
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 +3 -3
- package/package.json +2 -2
- package/src/context.ts +2 -2
- package/src/service.ts +23 -23
package/README.md
CHANGED
|
@@ -126,11 +126,11 @@ export class AuthService {
|
|
|
126
126
|
/**
|
|
127
127
|
* Manage expiry state, renewing if allowed
|
|
128
128
|
*/
|
|
129
|
-
manageExpiry(
|
|
129
|
+
manageExpiry(principal?: Principal): void;
|
|
130
130
|
/**
|
|
131
131
|
* Enforce expiry, invalidating the principal if expired
|
|
132
132
|
*/
|
|
133
|
-
enforceExpiry(
|
|
133
|
+
enforceExpiry(principal?: Principal): Principal | undefined;
|
|
134
134
|
}
|
|
135
135
|
```
|
|
136
136
|
|
|
@@ -155,7 +155,7 @@ export class AuthContext {
|
|
|
155
155
|
/**
|
|
156
156
|
* Set principal
|
|
157
157
|
*/
|
|
158
|
-
set principal(
|
|
158
|
+
set principal(value: Principal | undefined);
|
|
159
159
|
/**
|
|
160
160
|
* Get the authentication token, if it exists
|
|
161
161
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/auth",
|
|
3
|
-
"version": "7.0.0-rc.
|
|
3
|
+
"version": "7.0.0-rc.2",
|
|
4
4
|
"description": "Authentication scaffolding for the Travetto framework",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"authentication",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"directory": "module/auth"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@travetto/context": "^7.0.0-rc.
|
|
26
|
+
"@travetto/context": "^7.0.0-rc.2"
|
|
27
27
|
},
|
|
28
28
|
"travetto": {
|
|
29
29
|
"displayName": "Authentication"
|
package/src/context.ts
CHANGED
package/src/service.ts
CHANGED
|
@@ -27,9 +27,9 @@ export class AuthService {
|
|
|
27
27
|
// Find all authenticators
|
|
28
28
|
const AuthenticatorTarget = toConcrete<Authenticator>();
|
|
29
29
|
for (const source of DependencyRegistryIndex.getCandidates(AuthenticatorTarget)) {
|
|
30
|
-
const
|
|
31
|
-
const
|
|
32
|
-
this.#authenticators.set(
|
|
30
|
+
const qualifier = source.qualifier || getDefaultQualifier(source.class);
|
|
31
|
+
const instance = DependencyRegistryIndex.getInstance(AuthenticatorTarget, qualifier);
|
|
32
|
+
this.#authenticators.set(qualifier, instance);
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
35
|
|
|
@@ -51,28 +51,28 @@ export class AuthService {
|
|
|
51
51
|
/**
|
|
52
52
|
* Attempt to authenticate, checking with multiple authentication sources
|
|
53
53
|
*/
|
|
54
|
-
for (const
|
|
54
|
+
for (const authenticator of await this.getAuthenticators<T, C>(authenticators)) {
|
|
55
55
|
try {
|
|
56
|
-
const principal = await
|
|
56
|
+
const principal = await authenticator.authenticate(payload, context);
|
|
57
57
|
|
|
58
|
-
if (
|
|
59
|
-
this.authContext.authenticatorState = await
|
|
58
|
+
if (authenticator.getState) {
|
|
59
|
+
this.authContext.authenticatorState = await authenticator.getState(context);
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
if (!principal) { // Multi-step login process
|
|
63
63
|
return;
|
|
64
64
|
}
|
|
65
65
|
return this.authContext.principal = (await this.authorizer?.authorize(principal)) ?? principal;
|
|
66
|
-
} catch (
|
|
67
|
-
if (!(
|
|
68
|
-
throw
|
|
66
|
+
} catch (error) {
|
|
67
|
+
if (!(error instanceof Error)) {
|
|
68
|
+
throw error;
|
|
69
69
|
}
|
|
70
|
-
lastError =
|
|
70
|
+
lastError = error;
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
if (lastError) {
|
|
75
|
-
console.warn('Failed to authenticate', { error: lastError, sources: authenticators.map(
|
|
75
|
+
console.warn('Failed to authenticate', { error: lastError, sources: authenticators.map(symbol => symbol.toString()) });
|
|
76
76
|
}
|
|
77
77
|
|
|
78
78
|
// Take the last error and return
|
|
@@ -82,23 +82,23 @@ export class AuthService {
|
|
|
82
82
|
/**
|
|
83
83
|
* Manage expiry state, renewing if allowed
|
|
84
84
|
*/
|
|
85
|
-
manageExpiry(
|
|
86
|
-
if (!
|
|
85
|
+
manageExpiry(principal?: Principal): void {
|
|
86
|
+
if (!principal) {
|
|
87
87
|
return;
|
|
88
88
|
}
|
|
89
89
|
|
|
90
90
|
if (this.config.maxAgeMs) {
|
|
91
|
-
|
|
91
|
+
principal.expiresAt ??= TimeUtil.fromNow(this.config.maxAgeMs);
|
|
92
92
|
}
|
|
93
93
|
|
|
94
|
-
|
|
94
|
+
principal.issuedAt ??= new Date();
|
|
95
95
|
|
|
96
|
-
if (
|
|
97
|
-
const end =
|
|
96
|
+
if (principal.expiresAt && this.config.maxAgeMs && this.config.rollingRenew) { // Session behavior
|
|
97
|
+
const end = principal.expiresAt.getTime();
|
|
98
98
|
const midPoint = end - this.config.maxAgeMs / 2;
|
|
99
99
|
if (Date.now() > midPoint) { // If we are past the half way mark, renew the token
|
|
100
|
-
|
|
101
|
-
|
|
100
|
+
principal.issuedAt = new Date();
|
|
101
|
+
principal.expiresAt = TimeUtil.fromNow(this.config.maxAgeMs); // This will trigger a re-send
|
|
102
102
|
}
|
|
103
103
|
}
|
|
104
104
|
}
|
|
@@ -106,10 +106,10 @@ export class AuthService {
|
|
|
106
106
|
/**
|
|
107
107
|
* Enforce expiry, invalidating the principal if expired
|
|
108
108
|
*/
|
|
109
|
-
enforceExpiry(
|
|
110
|
-
if (
|
|
109
|
+
enforceExpiry(principal?: Principal): Principal | undefined {
|
|
110
|
+
if (principal && principal.expiresAt && principal.expiresAt.getTime() < Date.now()) {
|
|
111
111
|
return undefined;
|
|
112
112
|
}
|
|
113
|
-
return
|
|
113
|
+
return principal;
|
|
114
114
|
}
|
|
115
115
|
}
|