@cratis/arc 22.45.0 → 22.47.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/cjs/identity/IdentityProvider.d.ts +11 -2
- package/dist/cjs/identity/IdentityProvider.d.ts.map +1 -1
- package/dist/cjs/identity/IdentityProvider.js +118 -44
- package/dist/cjs/identity/IdentityProvider.js.map +1 -1
- package/dist/cjs/identity/deserializeIdentityDetails.d.ts.map +1 -1
- package/dist/cjs/identity/deserializeIdentityDetails.js +1 -2
- package/dist/cjs/identity/deserializeIdentityDetails.js.map +1 -1
- package/dist/esm/identity/IdentityProvider.d.ts +11 -2
- package/dist/esm/identity/IdentityProvider.d.ts.map +1 -1
- package/dist/esm/identity/IdentityProvider.js +118 -44
- package/dist/esm/identity/IdentityProvider.js.map +1 -1
- package/dist/esm/identity/deserializeIdentityDetails.d.ts.map +1 -1
- package/dist/esm/identity/deserializeIdentityDetails.js +1 -2
- package/dist/esm/identity/deserializeIdentityDetails.js.map +1 -1
- package/dist/esm/identity/for_IdentityProvider/given/a_browser_with_a_legacy_identity.d.ts +11 -0
- package/dist/esm/identity/for_IdentityProvider/given/a_browser_with_a_legacy_identity.d.ts.map +1 -0
- package/dist/esm/identity/for_IdentityProvider/given/a_browser_with_a_legacy_identity.js +42 -0
- package/dist/esm/identity/for_IdentityProvider/given/a_browser_with_a_legacy_identity.js.map +1 -0
- package/dist/esm/identity/for_IdentityProvider/given/a_cookie_first_backend.d.ts +7 -0
- package/dist/esm/identity/for_IdentityProvider/given/a_cookie_first_backend.d.ts.map +1 -0
- package/dist/esm/identity/for_IdentityProvider/given/a_cookie_first_backend.js +19 -0
- package/dist/esm/identity/for_IdentityProvider/given/a_cookie_first_backend.js.map +1 -0
- package/dist/esm/identity/for_IdentityProvider/given/a_secure_browser_with_a_legacy_identity.d.ts +5 -0
- package/dist/esm/identity/for_IdentityProvider/given/a_secure_browser_with_a_legacy_identity.d.ts.map +1 -0
- package/dist/esm/identity/for_IdentityProvider/given/a_secure_browser_with_a_legacy_identity.js +5 -0
- package/dist/esm/identity/for_IdentityProvider/given/a_secure_browser_with_a_legacy_identity.js.map +1 -0
- package/dist/esm/identity/for_IdentityProvider/given/an_identity_provider.d.ts +1 -0
- package/dist/esm/identity/for_IdentityProvider/given/an_identity_provider.d.ts.map +1 -1
- package/dist/esm/identity/for_IdentityProvider/given/an_identity_provider.js +11 -0
- package/dist/esm/identity/for_IdentityProvider/given/an_identity_provider.js.map +1 -1
- package/dist/esm/identity/for_IdentityProvider/when_clearing_cache/after_a_legacy_refresh.d.ts +2 -0
- package/dist/esm/identity/for_IdentityProvider/when_clearing_cache/after_a_legacy_refresh.d.ts.map +1 -0
- package/dist/esm/identity/for_IdentityProvider/when_clearing_cache/after_a_legacy_refresh.js +15 -0
- package/dist/esm/identity/for_IdentityProvider/when_clearing_cache/after_a_legacy_refresh.js.map +1 -0
- package/dist/esm/identity/for_IdentityProvider/when_clearing_cache.d.ts +2 -0
- package/dist/esm/identity/for_IdentityProvider/when_clearing_cache.d.ts.map +1 -0
- package/dist/esm/identity/for_IdentityProvider/when_clearing_cache.js +17 -0
- package/dist/esm/identity/for_IdentityProvider/when_clearing_cache.js.map +1 -0
- package/dist/esm/identity/for_IdentityProvider/when_getting_current/after_reloading_a_legacy_refresh.d.ts +2 -0
- package/dist/esm/identity/for_IdentityProvider/when_getting_current/after_reloading_a_legacy_refresh.d.ts.map +1 -0
- package/dist/esm/identity/for_IdentityProvider/when_getting_current/after_reloading_a_legacy_refresh.js +18 -0
- package/dist/esm/identity/for_IdentityProvider/when_getting_current/after_reloading_a_legacy_refresh.js.map +1 -0
- package/dist/esm/identity/for_IdentityProvider/when_getting_current/after_the_server_did_not_resolve_an_identity.d.ts +2 -0
- package/dist/esm/identity/for_IdentityProvider/when_getting_current/after_the_server_did_not_resolve_an_identity.d.ts.map +1 -0
- package/dist/esm/identity/for_IdentityProvider/when_getting_current/after_the_server_did_not_resolve_an_identity.js +20 -0
- package/dist/esm/identity/for_IdentityProvider/when_getting_current/after_the_server_did_not_resolve_an_identity.js.map +1 -0
- package/dist/esm/identity/for_IdentityProvider/when_getting_current/and_the_server_has_no_identity_endpoint.d.ts +2 -0
- package/dist/esm/identity/for_IdentityProvider/when_getting_current/and_the_server_has_no_identity_endpoint.d.ts.map +1 -0
- package/dist/esm/identity/for_IdentityProvider/when_getting_current/and_the_server_has_no_identity_endpoint.js +24 -0
- package/dist/esm/identity/for_IdentityProvider/when_getting_current/and_the_server_has_no_identity_endpoint.js.map +1 -0
- package/dist/esm/identity/for_IdentityProvider/when_getting_current/and_the_server_rejects_the_caller_with_a_forged_identity_cookie.d.ts +2 -0
- package/dist/esm/identity/for_IdentityProvider/when_getting_current/and_the_server_rejects_the_caller_with_a_forged_identity_cookie.d.ts.map +1 -0
- package/dist/esm/identity/for_IdentityProvider/when_getting_current/and_the_server_rejects_the_caller_with_a_forged_identity_cookie.js +16 -0
- package/dist/esm/identity/for_IdentityProvider/when_getting_current/and_the_server_rejects_the_caller_with_a_forged_identity_cookie.js.map +1 -0
- package/dist/esm/identity/for_IdentityProvider/when_getting_current/twice.d.ts +2 -0
- package/dist/esm/identity/for_IdentityProvider/when_getting_current/twice.d.ts.map +1 -0
- package/dist/esm/identity/for_IdentityProvider/when_getting_current/twice.js +15 -0
- package/dist/esm/identity/for_IdentityProvider/when_getting_current/twice.js.map +1 -0
- package/dist/esm/identity/for_IdentityProvider/when_getting_current/with_a_details_type_that_declares_no_fields.js +2 -3
- package/dist/esm/identity/for_IdentityProvider/when_getting_current/with_a_details_type_that_declares_no_fields.js.map +1 -1
- package/dist/esm/identity/for_IdentityProvider/when_getting_current/with_a_forged_identity_cookie.d.ts +2 -0
- package/dist/esm/identity/for_IdentityProvider/when_getting_current/with_a_forged_identity_cookie.d.ts.map +1 -0
- package/dist/esm/identity/for_IdentityProvider/when_getting_current/with_a_forged_identity_cookie.js +18 -0
- package/dist/esm/identity/for_IdentityProvider/when_getting_current/with_a_forged_identity_cookie.js.map +1 -0
- package/dist/esm/identity/for_IdentityProvider/when_getting_current/with_roles.js +2 -3
- package/dist/esm/identity/for_IdentityProvider/when_getting_current/with_roles.js.map +1 -1
- package/dist/esm/identity/for_IdentityProvider/when_getting_current/with_type_safe_details.js +2 -3
- package/dist/esm/identity/for_IdentityProvider/when_getting_current/with_type_safe_details.js.map +1 -1
- package/dist/esm/identity/for_IdentityProvider/when_getting_current/without_roles.js +2 -3
- package/dist/esm/identity/for_IdentityProvider/when_getting_current/without_roles.js.map +1 -1
- package/dist/esm/identity/for_IdentityProvider/when_refreshing/after_getting_current.d.ts +2 -0
- package/dist/esm/identity/for_IdentityProvider/when_refreshing/after_getting_current.d.ts.map +1 -0
- package/dist/esm/identity/for_IdentityProvider/when_refreshing/after_getting_current.js +18 -0
- package/dist/esm/identity/for_IdentityProvider/when_refreshing/after_getting_current.js.map +1 -0
- package/dist/esm/identity/for_IdentityProvider/when_refreshing/after_the_server_rejects_a_retained_transition_identity.d.ts +2 -0
- package/dist/esm/identity/for_IdentityProvider/when_refreshing/after_the_server_rejects_a_retained_transition_identity.d.ts.map +1 -0
- package/dist/esm/identity/for_IdentityProvider/when_refreshing/after_the_server_rejects_a_retained_transition_identity.js +17 -0
- package/dist/esm/identity/for_IdentityProvider/when_refreshing/after_the_server_rejects_a_retained_transition_identity.js.map +1 -0
- package/dist/esm/identity/for_IdentityProvider/when_refreshing/and_the_server_has_no_identity_endpoint.d.ts +2 -0
- package/dist/esm/identity/for_IdentityProvider/when_refreshing/and_the_server_has_no_identity_endpoint.d.ts.map +1 -0
- package/dist/esm/identity/for_IdentityProvider/when_refreshing/and_the_server_has_no_identity_endpoint.js +13 -0
- package/dist/esm/identity/for_IdentityProvider/when_refreshing/and_the_server_has_no_identity_endpoint.js.map +1 -0
- package/dist/esm/identity/for_IdentityProvider/when_refreshing/and_there_is_no_identity_endpoint_on_consecutive_requests.d.ts +2 -0
- package/dist/esm/identity/for_IdentityProvider/when_refreshing/and_there_is_no_identity_endpoint_on_consecutive_requests.d.ts.map +1 -0
- package/dist/esm/identity/for_IdentityProvider/when_refreshing/and_there_is_no_identity_endpoint_on_consecutive_requests.js +17 -0
- package/dist/esm/identity/for_IdentityProvider/when_refreshing/and_there_is_no_identity_endpoint_on_consecutive_requests.js.map +1 -0
- package/dist/esm/identity/for_IdentityProvider/when_refreshing/with_a_cookie_first_backend_and_changed_credentials.d.ts +2 -0
- package/dist/esm/identity/for_IdentityProvider/when_refreshing/with_a_cookie_first_backend_and_changed_credentials.d.ts.map +1 -0
- package/dist/esm/identity/for_IdentityProvider/when_refreshing/with_a_cookie_first_backend_and_changed_credentials.js +21 -0
- package/dist/esm/identity/for_IdentityProvider/when_refreshing/with_a_cookie_first_backend_and_changed_credentials.js.map +1 -0
- package/dist/esm/identity/for_IdentityProvider/when_refreshing/with_a_cookie_first_backend_and_expired_credentials.d.ts +2 -0
- package/dist/esm/identity/for_IdentityProvider/when_refreshing/with_a_cookie_first_backend_and_expired_credentials.d.ts.map +1 -0
- package/dist/esm/identity/for_IdentityProvider/when_refreshing/with_a_cookie_first_backend_and_expired_credentials.js +16 -0
- package/dist/esm/identity/for_IdentityProvider/when_refreshing/with_a_cookie_first_backend_and_expired_credentials.js.map +1 -0
- package/dist/esm/identity/for_IdentityProvider/when_refreshing/with_a_new_cookie_on_the_not_found_response.d.ts +2 -0
- package/dist/esm/identity/for_IdentityProvider/when_refreshing/with_a_new_cookie_on_the_not_found_response.d.ts.map +1 -0
- package/dist/esm/identity/for_IdentityProvider/when_refreshing/with_a_new_cookie_on_the_not_found_response.js +17 -0
- package/dist/esm/identity/for_IdentityProvider/when_refreshing/with_a_new_cookie_on_the_not_found_response.js.map +1 -0
- package/dist/esm/identity/for_IdentityProvider/when_refreshing/with_a_secure_legacy_cookie_and_no_identity_endpoint.d.ts +2 -0
- package/dist/esm/identity/for_IdentityProvider/when_refreshing/with_a_secure_legacy_cookie_and_no_identity_endpoint.d.ts.map +1 -0
- package/dist/esm/identity/for_IdentityProvider/when_refreshing/with_a_secure_legacy_cookie_and_no_identity_endpoint.js +15 -0
- package/dist/esm/identity/for_IdentityProvider/when_refreshing/with_a_secure_legacy_cookie_and_no_identity_endpoint.js.map +1 -0
- package/dist/esm/tsconfig.tsbuildinfo +1 -1
- package/identity/IdentityProvider.ts +137 -43
- package/identity/deserializeIdentityDetails.ts +1 -2
- package/identity/for_IdentityProvider/given/a_browser_with_a_legacy_identity.ts +46 -0
- package/identity/for_IdentityProvider/given/a_cookie_first_backend.ts +25 -0
- package/identity/for_IdentityProvider/given/a_secure_browser_with_a_legacy_identity.ts +8 -0
- package/identity/for_IdentityProvider/given/an_identity_provider.ts +19 -0
- package/identity/for_IdentityProvider/when_clearing_cache/after_a_legacy_refresh.ts +21 -0
- package/identity/for_IdentityProvider/when_clearing_cache.ts +24 -0
- package/identity/for_IdentityProvider/when_getting_current/after_reloading_a_legacy_refresh.ts +24 -0
- package/identity/for_IdentityProvider/when_getting_current/after_the_server_did_not_resolve_an_identity.ts +31 -0
- package/identity/for_IdentityProvider/when_getting_current/and_the_server_has_no_identity_endpoint.ts +38 -0
- package/identity/for_IdentityProvider/when_getting_current/and_the_server_rejects_the_caller_with_a_forged_identity_cookie.ts +23 -0
- package/identity/for_IdentityProvider/when_getting_current/twice.ts +22 -0
- package/identity/for_IdentityProvider/when_getting_current/with_a_details_type_that_declares_no_fields.ts +2 -3
- package/identity/for_IdentityProvider/when_getting_current/with_a_forged_identity_cookie.ts +29 -0
- package/identity/for_IdentityProvider/when_getting_current/with_roles.ts +2 -3
- package/identity/for_IdentityProvider/when_getting_current/with_type_safe_details.ts +2 -4
- package/identity/for_IdentityProvider/when_getting_current/without_roles.ts +2 -3
- package/identity/for_IdentityProvider/when_refreshing/after_getting_current.ts +25 -0
- package/identity/for_IdentityProvider/when_refreshing/after_the_server_rejects_a_retained_transition_identity.ts +23 -0
- package/identity/for_IdentityProvider/when_refreshing/and_the_server_has_no_identity_endpoint.ts +19 -0
- package/identity/for_IdentityProvider/when_refreshing/and_there_is_no_identity_endpoint_on_consecutive_requests.ts +24 -0
- package/identity/for_IdentityProvider/when_refreshing/with_a_cookie_first_backend_and_changed_credentials.ts +27 -0
- package/identity/for_IdentityProvider/when_refreshing/with_a_cookie_first_backend_and_expired_credentials.ts +22 -0
- package/identity/for_IdentityProvider/when_refreshing/with_a_new_cookie_on_the_not_found_response.ts +23 -0
- package/identity/for_IdentityProvider/when_refreshing/with_a_secure_legacy_cookie_and_no_identity_endpoint.ts +21 -0
- package/package.json +1 -1
|
@@ -13,14 +13,31 @@ import { joinPaths } from '../joinPaths.js';
|
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* Represents an implementation of {@link IIdentityProvider}.
|
|
16
|
+
*
|
|
17
|
+
* The identity comes from the `/.cratis/me` endpoint, which derives it from the authenticated request. The
|
|
18
|
+
* result is kept in memory for the page, so repeated calls to {@link IdentityProvider.getCurrent} do not each go to
|
|
19
|
+
* the server; {@link IdentityProvider.refresh} always asks the server again.
|
|
20
|
+
*
|
|
21
|
+
* Only when the server has no `/.cratis/me` endpoint (it answers 404) does it fall back to the readable
|
|
22
|
+
* `.cratis-identity` cookie earlier versions relied on, and it warns when it does. That fallback is for the transition
|
|
23
|
+
* and will be removed.
|
|
16
24
|
*/
|
|
17
25
|
export class IdentityProvider extends IIdentityProvider {
|
|
18
26
|
|
|
27
|
+
/**
|
|
28
|
+
* The name of the identity cookie earlier versions of Arc wrote and read.
|
|
29
|
+
* @deprecated The identity comes from the `/.cratis/me` endpoint. The cookie is only read when that endpoint does
|
|
30
|
+
* not exist, and that fallback will be removed.
|
|
31
|
+
*/
|
|
19
32
|
static readonly CookieName = '.cratis-identity';
|
|
20
33
|
static httpHeadersCallback: GetHttpHeaders | undefined;
|
|
21
34
|
static apiBasePath: string = '';
|
|
22
35
|
static origin: string = '';
|
|
23
36
|
|
|
37
|
+
private static cachedResult: Promise<IdentityProviderResult | undefined> | undefined;
|
|
38
|
+
private static hasWarnedAboutLegacyCookie = false;
|
|
39
|
+
private static legacyCookie: string | undefined;
|
|
40
|
+
|
|
24
41
|
/**
|
|
25
42
|
* Sets the HTTP headers callback.
|
|
26
43
|
* @param callback Callback to set.
|
|
@@ -34,6 +51,10 @@ export class IdentityProvider extends IIdentityProvider {
|
|
|
34
51
|
* @param apiBasePath API base path to set.
|
|
35
52
|
*/
|
|
36
53
|
static setApiBasePath(apiBasePath: string): void {
|
|
54
|
+
if (IdentityProvider.apiBasePath !== apiBasePath) {
|
|
55
|
+
IdentityProvider.cachedResult = undefined;
|
|
56
|
+
IdentityProvider.legacyCookie = undefined;
|
|
57
|
+
}
|
|
37
58
|
IdentityProvider.apiBasePath = apiBasePath;
|
|
38
59
|
}
|
|
39
60
|
|
|
@@ -42,6 +63,10 @@ export class IdentityProvider extends IIdentityProvider {
|
|
|
42
63
|
* @param origin Origin to set.
|
|
43
64
|
*/
|
|
44
65
|
static setOrigin(origin: string): void {
|
|
66
|
+
if (IdentityProvider.origin !== origin) {
|
|
67
|
+
IdentityProvider.cachedResult = undefined;
|
|
68
|
+
IdentityProvider.legacyCookie = undefined;
|
|
69
|
+
}
|
|
45
70
|
IdentityProvider.origin = origin;
|
|
46
71
|
}
|
|
47
72
|
|
|
@@ -49,27 +74,17 @@ export class IdentityProvider extends IIdentityProvider {
|
|
|
49
74
|
* Gets the current identity by optionally specifying the details type.
|
|
50
75
|
* @param type Optional constructor for the details type to enable type-safe deserialization.
|
|
51
76
|
* @returns The current identity as {@link IIdentity}.
|
|
52
|
-
* @remarks The
|
|
77
|
+
* @remarks The identity is fetched from `/.cratis/me` the first time and kept in memory until
|
|
78
|
+
* {@link IdentityProvider.refresh} or {@link IdentityProvider.clearCache} is called. An identity that could not be
|
|
79
|
+
* resolved is not kept, so the next call asks the server again.
|
|
80
|
+
* The `extends object` constraint is required for compatibility with JsonSerializer.deserializeFromInstance().
|
|
53
81
|
*/
|
|
54
82
|
static async getCurrent<TDetails extends object = object>(type?: Constructor<TDetails>): Promise<IIdentity<TDetails>> {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
const json = atob(cookie[1]);
|
|
58
|
-
const result = JSON.parse(json) as IdentityProviderResult;
|
|
59
|
-
const details = deserializeIdentityDetails(type, result.details);
|
|
60
|
-
return {
|
|
61
|
-
id: result.id,
|
|
62
|
-
name: result.name,
|
|
63
|
-
roles: result.roles || [],
|
|
64
|
-
details: details as TDetails,
|
|
65
|
-
isSet: true,
|
|
66
|
-
isInRole: (role: string) => (result.roles || []).includes(role),
|
|
67
|
-
refresh: () => IdentityProvider.refresh(type)
|
|
68
|
-
} as IIdentity<TDetails>;
|
|
69
|
-
} else {
|
|
70
|
-
const identity = await this.refresh<TDetails>(type);
|
|
71
|
-
return identity;
|
|
83
|
+
if (!IdentityProvider.cachedResult) {
|
|
84
|
+
IdentityProvider.fetchAndCache();
|
|
72
85
|
}
|
|
86
|
+
const result = await IdentityProvider.cachedResult;
|
|
87
|
+
return IdentityProvider.toIdentity(result, type);
|
|
73
88
|
}
|
|
74
89
|
|
|
75
90
|
/** @inheritdoc */
|
|
@@ -77,8 +92,69 @@ export class IdentityProvider extends IIdentityProvider {
|
|
|
77
92
|
return IdentityProvider.getCurrent<TDetails>(type);
|
|
78
93
|
}
|
|
79
94
|
|
|
95
|
+
/**
|
|
96
|
+
* Fetches the current identity from `/.cratis/me`, replacing what is kept in memory.
|
|
97
|
+
* @param type Optional constructor for the details type to enable type-safe deserialization.
|
|
98
|
+
* @returns The current identity as {@link IIdentity}.
|
|
99
|
+
*/
|
|
80
100
|
static async refresh<TDetails extends object = object>(type?: Constructor<TDetails>): Promise<IIdentity<TDetails>> {
|
|
81
|
-
|
|
101
|
+
// Earlier backends resolve the cookie before consulting the current credentials. Retain it during the
|
|
102
|
+
// request for a possible 404 transition fallback, but do not send it with an explicit refresh request.
|
|
103
|
+
IdentityProvider.legacyCookie = IdentityProvider.readLegacyCookie() ?? IdentityProvider.legacyCookie;
|
|
104
|
+
IdentityProvider.cachedResult = undefined;
|
|
105
|
+
IdentityProvider.expireLegacyCookie();
|
|
106
|
+
const result = await IdentityProvider.fetchAndCache();
|
|
107
|
+
return IdentityProvider.toIdentity(result, type);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Forgets the identity kept in memory, so the next {@link IdentityProvider.getCurrent} asks the server again.
|
|
112
|
+
* It also expires a readable `.cratis-identity` cookie left by an earlier version. Call this when the user logs out.
|
|
113
|
+
*/
|
|
114
|
+
static clearCache(): void {
|
|
115
|
+
IdentityProvider.cachedResult = undefined;
|
|
116
|
+
IdentityProvider.legacyCookie = undefined;
|
|
117
|
+
IdentityProvider.expireLegacyCookie();
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Forgets the identity kept in memory.
|
|
122
|
+
* @deprecated Arc no longer keeps the identity in a cookie. Use {@link IdentityProvider.clearCache} instead.
|
|
123
|
+
*/
|
|
124
|
+
static clearIdentityCookie(): void {
|
|
125
|
+
IdentityProvider.clearCache();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
private static expireLegacyCookie(): void {
|
|
129
|
+
if (typeof document === 'undefined') return;
|
|
130
|
+
document.cookie = `${IdentityProvider.CookieName}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/`;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
private static readLegacyCookie(): string | undefined {
|
|
134
|
+
if (typeof document === 'undefined') return undefined;
|
|
135
|
+
const prefix = `${IdentityProvider.CookieName}=`;
|
|
136
|
+
return document.cookie.split(';').map(_ => _.trim()).find(_ => _.startsWith(prefix));
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
private static fetchAndCache(): Promise<IdentityProviderResult | undefined> {
|
|
140
|
+
const pending = IdentityProvider.fetchResult();
|
|
141
|
+
IdentityProvider.cachedResult = pending;
|
|
142
|
+
|
|
143
|
+
// Only a resolved identity is worth keeping: an unset one, or a failed request, has to be asked for
|
|
144
|
+
// again - otherwise a page that loaded before sign-in would never see the user who signed in.
|
|
145
|
+
const forget = () => {
|
|
146
|
+
if (IdentityProvider.cachedResult === pending) {
|
|
147
|
+
IdentityProvider.cachedResult = undefined;
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
pending.then(result => {
|
|
151
|
+
if (!result) forget();
|
|
152
|
+
}, forget);
|
|
153
|
+
|
|
154
|
+
return pending;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
private static async fetchResult(): Promise<IdentityProviderResult | undefined> {
|
|
82
158
|
const origin = IdentityProvider.origin || Globals.origin || '';
|
|
83
159
|
const apiBasePath = IdentityProvider.apiBasePath || Globals.apiBasePath || '';
|
|
84
160
|
const route = joinPaths(apiBasePath, '/.cratis/me');
|
|
@@ -89,13 +165,53 @@ export class IdentityProvider extends IIdentityProvider {
|
|
|
89
165
|
headers: IdentityProvider.httpHeadersCallback?.() ?? {}
|
|
90
166
|
});
|
|
91
167
|
|
|
168
|
+
// No identity endpoint at all - an application that has not mapped one, or one whose identity comes from a
|
|
169
|
+
// proxy in front of it. Anything else, a 401 or 403 above all, is the server's answer and a cookie the
|
|
170
|
+
// browser holds must never overrule it.
|
|
171
|
+
if (response.status === 404) {
|
|
172
|
+
return IdentityProvider.fromLegacyCookie();
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
IdentityProvider.legacyCookie = undefined;
|
|
92
176
|
if (!response.ok) {
|
|
177
|
+
return undefined;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return await response.json() as IdentityProviderResult;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
private static fromLegacyCookie(): IdentityProviderResult | undefined {
|
|
184
|
+
if (typeof document === 'undefined') return undefined;
|
|
185
|
+
// A proxy may issue a newer cookie on the response. Prefer it over the one expired before the request.
|
|
186
|
+
const responseCookie = IdentityProvider.readLegacyCookie();
|
|
187
|
+
const cookie = responseCookie ?? IdentityProvider.legacyCookie;
|
|
188
|
+
if (!cookie) return undefined;
|
|
189
|
+
IdentityProvider.legacyCookie = cookie;
|
|
190
|
+
|
|
191
|
+
try {
|
|
192
|
+
const result = JSON.parse(atob(decodeURIComponent(cookie.substring(`${IdentityProvider.CookieName}=`.length)))) as IdentityProviderResult;
|
|
193
|
+
// The proxy may not reissue its cookie while its own authorization record remains valid. Restore the
|
|
194
|
+
// transition fallback after a 404 so it also survives a reload, without changing a response cookie.
|
|
195
|
+
if (!responseCookie) document.cookie = `${cookie};path=/;samesite=lax${location.protocol === 'https:' ? ';secure' : ''}`;
|
|
196
|
+
if (!IdentityProvider.hasWarnedAboutLegacyCookie) {
|
|
197
|
+
IdentityProvider.hasWarnedAboutLegacyCookie = true;
|
|
198
|
+
console.warn(
|
|
199
|
+
`The server has no '/.cratis/me' endpoint, so the identity was read from the '${IdentityProvider.CookieName}' cookie. ` +
|
|
200
|
+
'That cookie is not protected and the fallback will be removed in a future major version. ' +
|
|
201
|
+
'Expose /.cratis/me to the frontend: https://github.com/Cratis/Arc/blob/main/Documentation/backend/csharp/identity/migrating-from-the-identity-cookie.md');
|
|
202
|
+
}
|
|
203
|
+
return result;
|
|
204
|
+
} catch {
|
|
205
|
+
return undefined;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
private static toIdentity<TDetails extends object = object>(result: IdentityProviderResult | undefined, type?: Constructor<TDetails>): IIdentity<TDetails> {
|
|
210
|
+
if (!result) {
|
|
93
211
|
return IdentityProvider.notSet(type);
|
|
94
212
|
}
|
|
95
213
|
|
|
96
|
-
const result = await response.json() as IdentityProviderResult;
|
|
97
214
|
const details = deserializeIdentityDetails(type, result.details);
|
|
98
|
-
|
|
99
215
|
return {
|
|
100
216
|
id: result.id,
|
|
101
217
|
name: result.name,
|
|
@@ -118,26 +234,4 @@ export class IdentityProvider extends IIdentityProvider {
|
|
|
118
234
|
refresh: () => IdentityProvider.refresh(type)
|
|
119
235
|
};
|
|
120
236
|
}
|
|
121
|
-
|
|
122
|
-
private static getCookie() {
|
|
123
|
-
if (typeof document === 'undefined') return [];
|
|
124
|
-
const decoded = decodeURIComponent(document.cookie);
|
|
125
|
-
const cookies = decoded.split(';').map(_ => _.trim());
|
|
126
|
-
const cookie = cookies.find(_ => _.indexOf(`${IdentityProvider.CookieName}=`) == 0);
|
|
127
|
-
if (cookie) {
|
|
128
|
-
const keyValue = cookie.split('=');
|
|
129
|
-
return [keyValue[0].trim(), keyValue[1].trim()];
|
|
130
|
-
}
|
|
131
|
-
return [];
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* Clears the identity cookie used by Arc to cache the current identity.
|
|
136
|
-
* Call this when the user logs out to ensure subsequent requests and WebSocket
|
|
137
|
-
* connections do not carry stale credentials.
|
|
138
|
-
*/
|
|
139
|
-
static clearIdentityCookie(): void {
|
|
140
|
-
if (typeof document === 'undefined') return;
|
|
141
|
-
document.cookie = `${IdentityProvider.CookieName}=;expires=Thu, 01 Jan 1970 00:00:00 GMT`;
|
|
142
|
-
}
|
|
143
237
|
}
|
|
@@ -33,8 +33,7 @@ function isPrimitiveDetailsType(type: Constructor): boolean {
|
|
|
33
33
|
* would otherwise crash or silently destroy the payload.
|
|
34
34
|
* @param {Constructor | undefined} type The details type to deserialize into, or `undefined` to leave
|
|
35
35
|
* the payload untouched.
|
|
36
|
-
* @param {unknown} details The raw details payload - parsed JSON from the
|
|
37
|
-
* `/.cratis/me` endpoint.
|
|
36
|
+
* @param {unknown} details The raw details payload - parsed JSON from the `/.cratis/me` endpoint.
|
|
38
37
|
* @returns {unknown} The deserialized instance, or `details` unchanged when deserializing it would be
|
|
39
38
|
* unsafe, pointless, or has already been done.
|
|
40
39
|
* @remarks
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { JSDOM } from 'jsdom';
|
|
5
|
+
import { an_identity_provider } from './an_identity_provider.js';
|
|
6
|
+
import { IdentityProvider } from '../../IdentityProvider.js';
|
|
7
|
+
|
|
8
|
+
export class a_browser_with_a_legacy_identity extends an_identity_provider {
|
|
9
|
+
private browser!: JSDOM;
|
|
10
|
+
private originalDocument!: Document;
|
|
11
|
+
private originalLocation: Location | undefined;
|
|
12
|
+
protected browserUrl = 'http://localhost/nested/page';
|
|
13
|
+
|
|
14
|
+
get cookieJar() {
|
|
15
|
+
return this.browser.cookieJar;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
constructor() {
|
|
19
|
+
super();
|
|
20
|
+
beforeEach(() => {
|
|
21
|
+
this.originalDocument = document;
|
|
22
|
+
this.originalLocation = global.location;
|
|
23
|
+
this.browser = new JSDOM('', { url: this.browserUrl });
|
|
24
|
+
global.document = this.browser.window.document;
|
|
25
|
+
global.location = this.browser.window.location;
|
|
26
|
+
document.cookie = `.cratis-identity=${btoa(JSON.stringify({ id: 'user-123', name: 'Original User', roles: ['Reader'], details: {} }))};path=/;samesite=lax${location.protocol === 'https:' ? ';secure' : ''}`;
|
|
27
|
+
this.fetchStub.resolves({ ok: false, status: 404 } as Response);
|
|
28
|
+
});
|
|
29
|
+
afterEach(() => {
|
|
30
|
+
IdentityProvider.clearCache();
|
|
31
|
+
this.browser.window.close();
|
|
32
|
+
global.document = this.originalDocument;
|
|
33
|
+
if (this.originalLocation) global.location = this.originalLocation;
|
|
34
|
+
else delete (global as { location?: Location }).location;
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
reload(): void {
|
|
39
|
+
const cookieJar = this.browser.cookieJar;
|
|
40
|
+
const url = this.browser.window.location.href;
|
|
41
|
+
this.browser.window.close();
|
|
42
|
+
this.browser = new JSDOM('', { url, cookieJar });
|
|
43
|
+
global.document = this.browser.window.document;
|
|
44
|
+
global.location = this.browser.window.location;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { an_identity_provider } from './an_identity_provider.js';
|
|
5
|
+
import { IdentityProviderResult } from '../../IdentityProviderResult.js';
|
|
6
|
+
|
|
7
|
+
export class a_cookie_first_backend extends an_identity_provider {
|
|
8
|
+
currentIdentity: IdentityProviderResult | undefined;
|
|
9
|
+
|
|
10
|
+
constructor() {
|
|
11
|
+
super();
|
|
12
|
+
beforeEach(() => {
|
|
13
|
+
this.currentIdentity = { id: 'user-123', name: 'Original User', roles: ['Reader'], details: { department: 'Original' } };
|
|
14
|
+
document.cookie = `.cratis-identity=${btoa(JSON.stringify(this.currentIdentity))}`;
|
|
15
|
+
this.fetchStub.callsFake(async () => {
|
|
16
|
+
// Earlier backends return the readable cookie before checking credentials or recomputing details.
|
|
17
|
+
const encodedCookie = document.cookie.split(';').map(_ => _.trim()).find(_ => _.startsWith('.cratis-identity='))?.substring('.cratis-identity='.length);
|
|
18
|
+
const identity = encodedCookie ? JSON.parse(atob(encodedCookie)) : this.currentIdentity;
|
|
19
|
+
return identity
|
|
20
|
+
? { ok: true, status: 200, json: async () => identity } as Response
|
|
21
|
+
: { ok: false, status: 401 } as Response;
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { a_browser_with_a_legacy_identity } from './a_browser_with_a_legacy_identity.js';
|
|
5
|
+
|
|
6
|
+
export class a_secure_browser_with_a_legacy_identity extends a_browser_with_a_legacy_identity {
|
|
7
|
+
protected browserUrl = 'https://identity.example/nested/page';
|
|
8
|
+
}
|
|
@@ -27,5 +27,24 @@ export class an_identity_provider {
|
|
|
27
27
|
|
|
28
28
|
this.fetchHelper = createFetchHelper();
|
|
29
29
|
this.fetchStub = this.fetchHelper.stubFetch();
|
|
30
|
+
|
|
31
|
+
// The context is created once per suite, but every spec has to start from a page that has not asked
|
|
32
|
+
// for the identity yet.
|
|
33
|
+
beforeEach(() => {
|
|
34
|
+
(global as { document?: { cookie: string } }).document!.cookie = '';
|
|
35
|
+
IdentityProvider.clearCache();
|
|
36
|
+
this.fetchStub.resetHistory();
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Answers every request to `/.cratis/me` with the given identity.
|
|
42
|
+
* @param identity The identity the server reports.
|
|
43
|
+
*/
|
|
44
|
+
serverReports(identity: object) {
|
|
45
|
+
this.fetchStub.resolves({
|
|
46
|
+
ok: true,
|
|
47
|
+
json: async () => identity
|
|
48
|
+
} as Response);
|
|
30
49
|
}
|
|
31
50
|
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { IdentityProvider } from '../../IdentityProvider.js';
|
|
5
|
+
import { IIdentity } from '../../IIdentity.js';
|
|
6
|
+
import { a_browser_with_a_legacy_identity } from '../given/a_browser_with_a_legacy_identity.js';
|
|
7
|
+
import { given } from '../../../given.js';
|
|
8
|
+
|
|
9
|
+
describe('when clearing cache after a legacy refresh', given(a_browser_with_a_legacy_identity, context => {
|
|
10
|
+
let identity: IIdentity;
|
|
11
|
+
|
|
12
|
+
beforeEach(async () => {
|
|
13
|
+
await IdentityProvider.refresh();
|
|
14
|
+
IdentityProvider.clearCache();
|
|
15
|
+
identity = await IdentityProvider.getCurrent();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('should ask the server again', () => context.fetchStub.calledTwice.should.be.true);
|
|
19
|
+
it('should discard the retained transition identity', () => identity.isSet.should.be.false);
|
|
20
|
+
it('should leave the legacy cookie expired', () => document.cookie.should.equal(''));
|
|
21
|
+
}));
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { IdentityProvider } from '../IdentityProvider.js';
|
|
5
|
+
import { IIdentity } from '../IIdentity.js';
|
|
6
|
+
import { an_identity_provider } from './given/an_identity_provider.js';
|
|
7
|
+
import { given } from '../../given.js';
|
|
8
|
+
|
|
9
|
+
describe('when clearing cache', given(an_identity_provider, context => {
|
|
10
|
+
let identity: IIdentity;
|
|
11
|
+
|
|
12
|
+
beforeEach(async () => {
|
|
13
|
+
context.serverReports({ id: 'user-123', name: 'Test User', roles: [], details: {} });
|
|
14
|
+
await IdentityProvider.getCurrent();
|
|
15
|
+
|
|
16
|
+
IdentityProvider.clearCache();
|
|
17
|
+
context.fetchStub.resolves({ ok: false, status: 401 } as Response);
|
|
18
|
+
identity = await IdentityProvider.getCurrent();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('should ask the server again', () => context.fetchStub.calledTwice.should.be.true);
|
|
22
|
+
it('should report what the server reports now', () => identity.isSet.should.be.false);
|
|
23
|
+
it('should expire a legacy identity cookie', () => (global as { document?: { cookie: string } }).document!.cookie.should.contain('.cratis-identity=;expires=Thu, 01 Jan 1970'));
|
|
24
|
+
}));
|
package/identity/for_IdentityProvider/when_getting_current/after_reloading_a_legacy_refresh.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { vi } from 'vitest';
|
|
5
|
+
import { IdentityProvider } from '../../IdentityProvider.js';
|
|
6
|
+
import { IIdentity } from '../../IIdentity.js';
|
|
7
|
+
import { a_browser_with_a_legacy_identity } from '../given/a_browser_with_a_legacy_identity.js';
|
|
8
|
+
import { given } from '../../../given.js';
|
|
9
|
+
|
|
10
|
+
describe('when getting current after reloading a legacy refresh', given(a_browser_with_a_legacy_identity, context => {
|
|
11
|
+
let identity: IIdentity;
|
|
12
|
+
|
|
13
|
+
beforeEach(async () => {
|
|
14
|
+
await IdentityProvider.refresh();
|
|
15
|
+
context.reload();
|
|
16
|
+
vi.resetModules();
|
|
17
|
+
const { IdentityProvider: reloadedProvider } = await import('../../IdentityProvider.js');
|
|
18
|
+
identity = await reloadedProvider.getCurrent();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('should ask the server again after the reload', () => context.fetchStub.calledTwice.should.be.true);
|
|
22
|
+
it('should preserve the transition identity across the reload', () => identity.id.should.equal('user-123'));
|
|
23
|
+
it('should preserve the transition roles across the reload', () => identity.isInRole('Reader').should.be.true);
|
|
24
|
+
}));
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { IdentityProvider } from '../../IdentityProvider.js';
|
|
5
|
+
import { IIdentity } from '../../IIdentity.js';
|
|
6
|
+
import { an_identity_provider } from '../given/an_identity_provider.js';
|
|
7
|
+
import { given } from '../../../given.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* An identity the server could not resolve is not kept, so a page that loaded before the user signed in sees
|
|
11
|
+
* them once they have.
|
|
12
|
+
*/
|
|
13
|
+
describe('when getting current after the server did not resolve an identity', given(an_identity_provider, context => {
|
|
14
|
+
let first: IIdentity;
|
|
15
|
+
let second: IIdentity;
|
|
16
|
+
|
|
17
|
+
beforeEach(async () => {
|
|
18
|
+
context.fetchStub.onFirstCall().resolves({ ok: false, status: 401 } as Response);
|
|
19
|
+
context.fetchStub.onSecondCall().resolves({
|
|
20
|
+
ok: true,
|
|
21
|
+
json: async () => ({ id: 'user-123', name: 'Test User', roles: [], details: {} })
|
|
22
|
+
} as Response);
|
|
23
|
+
|
|
24
|
+
first = await IdentityProvider.getCurrent();
|
|
25
|
+
second = await IdentityProvider.getCurrent();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('should report the first identity as not set', () => first.isSet.should.be.false);
|
|
29
|
+
it('should ask the server again', () => context.fetchStub.calledTwice.should.be.true);
|
|
30
|
+
it('should report the identity the server resolved', () => second.id.should.equal('user-123'));
|
|
31
|
+
}));
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { IdentityProvider } from '../../IdentityProvider.js';
|
|
5
|
+
import { IIdentity } from '../../IIdentity.js';
|
|
6
|
+
import { an_identity_provider } from '../given/an_identity_provider.js';
|
|
7
|
+
import { given } from '../../../given.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* An application whose identity comes from a proxy in front of it may have no `/.cratis/me` of its own, and earlier
|
|
11
|
+
* versions read the identity from the cookie that proxy writes. Until the proxy serves the endpoint, the cookie is
|
|
12
|
+
* still read in that one case - loudly, so the dependency does not go unnoticed.
|
|
13
|
+
*/
|
|
14
|
+
describe('when getting current and the server has no identity endpoint', given(an_identity_provider, context => {
|
|
15
|
+
let identity: IIdentity;
|
|
16
|
+
let warnings: unknown[][];
|
|
17
|
+
let originalConsoleWarn: typeof console.warn;
|
|
18
|
+
|
|
19
|
+
beforeEach(async () => {
|
|
20
|
+
warnings = [];
|
|
21
|
+
originalConsoleWarn = console.warn;
|
|
22
|
+
console.warn = (...args: unknown[]) => { warnings.push(args); };
|
|
23
|
+
(IdentityProvider as unknown as { hasWarnedAboutLegacyCookie: boolean }).hasWarnedAboutLegacyCookie = false;
|
|
24
|
+
|
|
25
|
+
const cookie = btoa(JSON.stringify({ id: 'user-123', name: 'Test User', roles: ['Reader'], details: {} }));
|
|
26
|
+
(global as { document?: { cookie: string } }).document!.cookie = `.cratis-identity=${cookie}`;
|
|
27
|
+
context.fetchStub.resolves({ ok: false, status: 404 } as Response);
|
|
28
|
+
|
|
29
|
+
identity = await IdentityProvider.getCurrent();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
afterEach(() => { console.warn = originalConsoleWarn; });
|
|
33
|
+
|
|
34
|
+
it('should ask the server first', () => context.fetchStub.calledOnce.should.be.true);
|
|
35
|
+
it('should report the identity from the cookie', () => identity.id.should.equal('user-123'));
|
|
36
|
+
it('should report the roles from the cookie', () => identity.isInRole('Reader').should.be.true);
|
|
37
|
+
it('should warn that the cookie was read', () => warnings.length.should.equal(1));
|
|
38
|
+
}));
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { IdentityProvider } from '../../IdentityProvider.js';
|
|
5
|
+
import { IIdentity } from '../../IIdentity.js';
|
|
6
|
+
import { an_identity_provider } from '../given/an_identity_provider.js';
|
|
7
|
+
import { given } from '../../../given.js';
|
|
8
|
+
|
|
9
|
+
describe('when getting current and the server rejects the caller with a forged identity cookie', given(an_identity_provider, context => {
|
|
10
|
+
let identity: IIdentity;
|
|
11
|
+
|
|
12
|
+
beforeEach(async () => {
|
|
13
|
+
const forged = btoa(JSON.stringify({ id: 'admin', name: 'Forged Administrator', roles: ['admin'], details: {} }));
|
|
14
|
+
(global as { document?: { cookie: string } }).document!.cookie = `.cratis-identity=${forged}`;
|
|
15
|
+
context.fetchStub.resolves({ ok: false, status: 401 } as Response);
|
|
16
|
+
|
|
17
|
+
identity = await IdentityProvider.getCurrent();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('should not be set', () => identity.isSet.should.be.false);
|
|
21
|
+
it('should not report the forged identity', () => identity.id.should.equal(''));
|
|
22
|
+
it('should not report the forged role', () => identity.isInRole('admin').should.be.false);
|
|
23
|
+
}));
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { IdentityProvider } from '../../IdentityProvider.js';
|
|
5
|
+
import { IIdentity } from '../../IIdentity.js';
|
|
6
|
+
import { an_identity_provider } from '../given/an_identity_provider.js';
|
|
7
|
+
import { given } from '../../../given.js';
|
|
8
|
+
|
|
9
|
+
describe('when getting current twice', given(an_identity_provider, context => {
|
|
10
|
+
let first: IIdentity;
|
|
11
|
+
let second: IIdentity;
|
|
12
|
+
|
|
13
|
+
beforeEach(async () => {
|
|
14
|
+
context.serverReports({ id: 'user-123', name: 'Test User', roles: [], details: {} });
|
|
15
|
+
|
|
16
|
+
first = await IdentityProvider.getCurrent();
|
|
17
|
+
second = await IdentityProvider.getCurrent();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('should ask the server only once', () => context.fetchStub.calledOnce.should.be.true);
|
|
21
|
+
it('should report the same identity both times', () => second.id.should.equal(first.id));
|
|
22
|
+
}));
|
|
@@ -12,7 +12,7 @@ class UndecoratedDetails {
|
|
|
12
12
|
role!: string;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
describe('when getting current with a details type that declares no fields', given(an_identity_provider,
|
|
15
|
+
describe('when getting current with a details type that declares no fields', given(an_identity_provider, context => {
|
|
16
16
|
let originalConsoleWarn: typeof console.warn;
|
|
17
17
|
let result: { details: UndecoratedDetails };
|
|
18
18
|
|
|
@@ -28,8 +28,7 @@ describe('when getting current with a details type that declares no fields', giv
|
|
|
28
28
|
role: 'admin'
|
|
29
29
|
}
|
|
30
30
|
};
|
|
31
|
-
|
|
32
|
-
(global as { document?: { cookie: string } }).document!.cookie = `.cratis-identity=${encodedData}`;
|
|
31
|
+
context.serverReports(identityData);
|
|
33
32
|
|
|
34
33
|
const identity = await IdentityProvider.getCurrent(UndecoratedDetails);
|
|
35
34
|
result = { details: identity.details };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { IdentityProvider } from '../../IdentityProvider.js';
|
|
5
|
+
import { IIdentity } from '../../IIdentity.js';
|
|
6
|
+
import { an_identity_provider } from '../given/an_identity_provider.js';
|
|
7
|
+
import { given } from '../../../given.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A script on the page, or on a sibling subdomain, can write a `.cratis-identity` cookie. Earlier versions read
|
|
11
|
+
* the identity from it, so whoever wrote it decided who the page thought the user was.
|
|
12
|
+
*/
|
|
13
|
+
describe('when getting current with a forged identity cookie', given(an_identity_provider, context => {
|
|
14
|
+
let identity: IIdentity;
|
|
15
|
+
|
|
16
|
+
beforeEach(async () => {
|
|
17
|
+
const forged = btoa(JSON.stringify({ id: 'admin', name: 'Forged Administrator', roles: ['admin'], details: {} }));
|
|
18
|
+
(global as { document?: { cookie: string } }).document!.cookie = `.cratis-identity=${forged}`;
|
|
19
|
+
context.serverReports({ id: 'user-123', name: 'Test User', roles: ['Reader'], details: {} });
|
|
20
|
+
|
|
21
|
+
identity = await IdentityProvider.getCurrent();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('should ask the server', () => context.fetchStub.calledOnce.should.be.true);
|
|
25
|
+
it('should ask the identity endpoint', () => String(context.fetchStub.firstCall.args[0]).should.contain('/.cratis/me'));
|
|
26
|
+
it('should report the identity the server reported', () => identity.id.should.equal('user-123'));
|
|
27
|
+
it('should report the roles the server reported', () => identity.roles.should.deep.equal(['Reader']));
|
|
28
|
+
it('should not report the forged role', () => identity.isInRole('admin').should.be.false);
|
|
29
|
+
}));
|
|
@@ -5,7 +5,7 @@ import { IdentityProvider } from '../../IdentityProvider.js';
|
|
|
5
5
|
import { an_identity_provider } from '../given/an_identity_provider.js';
|
|
6
6
|
import { given } from '../../../given.js';
|
|
7
7
|
|
|
8
|
-
describe('when getting current with roles', given(an_identity_provider,
|
|
8
|
+
describe('when getting current with roles', given(an_identity_provider, context => {
|
|
9
9
|
let identity: { id: string; name: string; roles: string[]; isInRole: (role: string) => boolean };
|
|
10
10
|
|
|
11
11
|
beforeEach(async () => {
|
|
@@ -15,8 +15,7 @@ describe('when getting current with roles', given(an_identity_provider, () => {
|
|
|
15
15
|
roles: ['Admin', 'User'],
|
|
16
16
|
details: {}
|
|
17
17
|
};
|
|
18
|
-
|
|
19
|
-
(global as { document?: { cookie: string } }).document!.cookie = `.cratis-identity=${encodedData}`;
|
|
18
|
+
context.serverReports(identityData);
|
|
20
19
|
|
|
21
20
|
const result = await IdentityProvider.getCurrent();
|
|
22
21
|
identity = {
|
|
@@ -14,14 +14,13 @@ class TestDetails {
|
|
|
14
14
|
role!: string;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
describe('when getting current with type safe details', given(an_identity_provider,
|
|
17
|
+
describe('when getting current with type safe details', given(an_identity_provider, context => {
|
|
18
18
|
let result: { id: string; name: string; details: TestDetails };
|
|
19
19
|
let testGuid: Guid;
|
|
20
20
|
|
|
21
21
|
beforeEach(async () => {
|
|
22
22
|
testGuid = Guid.create();
|
|
23
23
|
|
|
24
|
-
// Mock document.cookie with base64-encoded identity data
|
|
25
24
|
const identityData = {
|
|
26
25
|
id: 'test-user-id',
|
|
27
26
|
name: 'Test User',
|
|
@@ -30,8 +29,7 @@ describe('when getting current with type safe details', given(an_identity_provid
|
|
|
30
29
|
role: 'admin'
|
|
31
30
|
}
|
|
32
31
|
};
|
|
33
|
-
|
|
34
|
-
(global as { document?: { cookie: string } }).document!.cookie = `.cratis-identity=${encodedData}`;
|
|
32
|
+
context.serverReports(identityData);
|
|
35
33
|
|
|
36
34
|
const identity = await IdentityProvider.getCurrent(TestDetails);
|
|
37
35
|
result = {
|