@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
|
@@ -7,14 +7,23 @@ export declare class IdentityProvider extends IIdentityProvider {
|
|
|
7
7
|
static httpHeadersCallback: GetHttpHeaders | undefined;
|
|
8
8
|
static apiBasePath: string;
|
|
9
9
|
static origin: string;
|
|
10
|
+
private static cachedResult;
|
|
11
|
+
private static hasWarnedAboutLegacyCookie;
|
|
12
|
+
private static legacyCookie;
|
|
10
13
|
static setHttpHeadersCallback(callback: GetHttpHeaders): void;
|
|
11
14
|
static setApiBasePath(apiBasePath: string): void;
|
|
12
15
|
static setOrigin(origin: string): void;
|
|
13
16
|
static getCurrent<TDetails extends object = object>(type?: Constructor<TDetails>): Promise<IIdentity<TDetails>>;
|
|
14
17
|
getCurrent<TDetails extends object = object>(type?: Constructor<TDetails>): Promise<IIdentity<TDetails>>;
|
|
15
18
|
static refresh<TDetails extends object = object>(type?: Constructor<TDetails>): Promise<IIdentity<TDetails>>;
|
|
16
|
-
|
|
17
|
-
private static getCookie;
|
|
19
|
+
static clearCache(): void;
|
|
18
20
|
static clearIdentityCookie(): void;
|
|
21
|
+
private static expireLegacyCookie;
|
|
22
|
+
private static readLegacyCookie;
|
|
23
|
+
private static fetchAndCache;
|
|
24
|
+
private static fetchResult;
|
|
25
|
+
private static fromLegacyCookie;
|
|
26
|
+
private static toIdentity;
|
|
27
|
+
private static notSet;
|
|
19
28
|
}
|
|
20
29
|
//# sourceMappingURL=IdentityProvider.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IdentityProvider.d.ts","sourceRoot":"","sources":["../../../identity/IdentityProvider.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAC3D,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAG3C,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;
|
|
1
|
+
{"version":3,"file":"IdentityProvider.d.ts","sourceRoot":"","sources":["../../../identity/IdentityProvider.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAC3D,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAG3C,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAgBtD,qBAAa,gBAAiB,SAAQ,iBAAiB;IAOnD,MAAM,CAAC,QAAQ,CAAC,UAAU,sBAAsB;IAChD,MAAM,CAAC,mBAAmB,EAAE,cAAc,GAAG,SAAS,CAAC;IACvD,MAAM,CAAC,WAAW,EAAE,MAAM,CAAM;IAChC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAM;IAE3B,OAAO,CAAC,MAAM,CAAC,YAAY,CAA0D;IACrF,OAAO,CAAC,MAAM,CAAC,0BAA0B,CAAS;IAClD,OAAO,CAAC,MAAM,CAAC,YAAY,CAAqB;IAMhD,MAAM,CAAC,sBAAsB,CAAC,QAAQ,EAAE,cAAc,GAAG,IAAI,CAE5D;IAMD,MAAM,CAAC,cAAc,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI,CAM/C;IAMD,MAAM,CAAC,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAMrC;IAWD,OAAa,UAAU,CAAC,QAAQ,SAAS,MAAM,GAAG,MAAM,EAAE,IAAI,CAAC,EAAE,WAAW,CAAC,QAAQ,CAAC,GAAG,OAAO,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,CAMpH;IAGK,UAAU,CAAC,QAAQ,SAAS,MAAM,GAAG,MAAM,EAAE,IAAI,CAAC,EAAE,WAAW,CAAC,QAAQ,CAAC,GAAG,OAAO,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,CAE7G;IAOD,OAAa,OAAO,CAAC,QAAQ,SAAS,MAAM,GAAG,MAAM,EAAE,IAAI,CAAC,EAAE,WAAW,CAAC,QAAQ,CAAC,GAAG,OAAO,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,CAQjH;IAMD,MAAM,CAAC,UAAU,IAAI,IAAI,CAIxB;IAMD,MAAM,CAAC,mBAAmB,IAAI,IAAI,CAEjC;IAED,OAAO,CAAC,MAAM,CAAC,kBAAkB;IAKjC,OAAO,CAAC,MAAM,CAAC,gBAAgB;IAM/B,OAAO,CAAC,MAAM,CAAC,aAAa;mBAkBP,WAAW;IA0BhC,OAAO,CAAC,MAAM,CAAC,gBAAgB;IA0B/B,OAAO,CAAC,MAAM,CAAC,UAAU;IAiBzB,OAAO,CAAC,MAAM,CAAC,MAAM;CAWxB"}
|
|
@@ -10,11 +10,26 @@ var joinPaths = require('../joinPaths.js');
|
|
|
10
10
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
11
11
|
/**
|
|
12
12
|
* Represents an implementation of {@link IIdentityProvider}.
|
|
13
|
+
*
|
|
14
|
+
* The identity comes from the `/.cratis/me` endpoint, which derives it from the authenticated request. The
|
|
15
|
+
* result is kept in memory for the page, so repeated calls to {@link IdentityProvider.getCurrent} do not each go to
|
|
16
|
+
* the server; {@link IdentityProvider.refresh} always asks the server again.
|
|
17
|
+
*
|
|
18
|
+
* Only when the server has no `/.cratis/me` endpoint (it answers 404) does it fall back to the readable
|
|
19
|
+
* `.cratis-identity` cookie earlier versions relied on, and it warns when it does. That fallback is for the transition
|
|
20
|
+
* and will be removed.
|
|
13
21
|
*/ class IdentityProvider extends IIdentityProvider.IIdentityProvider {
|
|
14
|
-
|
|
22
|
+
/**
|
|
23
|
+
* The name of the identity cookie earlier versions of Arc wrote and read.
|
|
24
|
+
* @deprecated The identity comes from the `/.cratis/me` endpoint. The cookie is only read when that endpoint does
|
|
25
|
+
* not exist, and that fallback will be removed.
|
|
26
|
+
*/ static CookieName = '.cratis-identity';
|
|
15
27
|
static httpHeadersCallback;
|
|
16
28
|
static apiBasePath = '';
|
|
17
29
|
static origin = '';
|
|
30
|
+
static cachedResult;
|
|
31
|
+
static hasWarnedAboutLegacyCookie = false;
|
|
32
|
+
static legacyCookie;
|
|
18
33
|
/**
|
|
19
34
|
* Sets the HTTP headers callback.
|
|
20
35
|
* @param callback Callback to set.
|
|
@@ -25,44 +40,92 @@ var joinPaths = require('../joinPaths.js');
|
|
|
25
40
|
* Sets the API base path.
|
|
26
41
|
* @param apiBasePath API base path to set.
|
|
27
42
|
*/ static setApiBasePath(apiBasePath) {
|
|
43
|
+
if (IdentityProvider.apiBasePath !== apiBasePath) {
|
|
44
|
+
IdentityProvider.cachedResult = undefined;
|
|
45
|
+
IdentityProvider.legacyCookie = undefined;
|
|
46
|
+
}
|
|
28
47
|
IdentityProvider.apiBasePath = apiBasePath;
|
|
29
48
|
}
|
|
30
49
|
/**
|
|
31
50
|
* Sets the origin.
|
|
32
51
|
* @param origin Origin to set.
|
|
33
52
|
*/ static setOrigin(origin) {
|
|
53
|
+
if (IdentityProvider.origin !== origin) {
|
|
54
|
+
IdentityProvider.cachedResult = undefined;
|
|
55
|
+
IdentityProvider.legacyCookie = undefined;
|
|
56
|
+
}
|
|
34
57
|
IdentityProvider.origin = origin;
|
|
35
58
|
}
|
|
36
59
|
/**
|
|
37
60
|
* Gets the current identity by optionally specifying the details type.
|
|
38
61
|
* @param type Optional constructor for the details type to enable type-safe deserialization.
|
|
39
62
|
* @returns The current identity as {@link IIdentity}.
|
|
40
|
-
* @remarks The
|
|
63
|
+
* @remarks The identity is fetched from `/.cratis/me` the first time and kept in memory until
|
|
64
|
+
* {@link IdentityProvider.refresh} or {@link IdentityProvider.clearCache} is called. An identity that could not be
|
|
65
|
+
* resolved is not kept, so the next call asks the server again.
|
|
66
|
+
* The `extends object` constraint is required for compatibility with JsonSerializer.deserializeFromInstance().
|
|
41
67
|
*/ static async getCurrent(type) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const json = atob(cookie[1]);
|
|
45
|
-
const result = JSON.parse(json);
|
|
46
|
-
const details = deserializeIdentityDetails.deserializeIdentityDetails(type, result.details);
|
|
47
|
-
return {
|
|
48
|
-
id: result.id,
|
|
49
|
-
name: result.name,
|
|
50
|
-
roles: result.roles || [],
|
|
51
|
-
details: details,
|
|
52
|
-
isSet: true,
|
|
53
|
-
isInRole: (role)=>(result.roles || []).includes(role),
|
|
54
|
-
refresh: ()=>IdentityProvider.refresh(type)
|
|
55
|
-
};
|
|
56
|
-
} else {
|
|
57
|
-
const identity = await this.refresh(type);
|
|
58
|
-
return identity;
|
|
68
|
+
if (!IdentityProvider.cachedResult) {
|
|
69
|
+
IdentityProvider.fetchAndCache();
|
|
59
70
|
}
|
|
71
|
+
const result = await IdentityProvider.cachedResult;
|
|
72
|
+
return IdentityProvider.toIdentity(result, type);
|
|
60
73
|
}
|
|
61
74
|
/** @inheritdoc */ async getCurrent(type) {
|
|
62
75
|
return IdentityProvider.getCurrent(type);
|
|
63
76
|
}
|
|
64
|
-
|
|
65
|
-
|
|
77
|
+
/**
|
|
78
|
+
* Fetches the current identity from `/.cratis/me`, replacing what is kept in memory.
|
|
79
|
+
* @param type Optional constructor for the details type to enable type-safe deserialization.
|
|
80
|
+
* @returns The current identity as {@link IIdentity}.
|
|
81
|
+
*/ static async refresh(type) {
|
|
82
|
+
// Earlier backends resolve the cookie before consulting the current credentials. Retain it during the
|
|
83
|
+
// request for a possible 404 transition fallback, but do not send it with an explicit refresh request.
|
|
84
|
+
IdentityProvider.legacyCookie = IdentityProvider.readLegacyCookie() ?? IdentityProvider.legacyCookie;
|
|
85
|
+
IdentityProvider.cachedResult = undefined;
|
|
86
|
+
IdentityProvider.expireLegacyCookie();
|
|
87
|
+
const result = await IdentityProvider.fetchAndCache();
|
|
88
|
+
return IdentityProvider.toIdentity(result, type);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Forgets the identity kept in memory, so the next {@link IdentityProvider.getCurrent} asks the server again.
|
|
92
|
+
* It also expires a readable `.cratis-identity` cookie left by an earlier version. Call this when the user logs out.
|
|
93
|
+
*/ static clearCache() {
|
|
94
|
+
IdentityProvider.cachedResult = undefined;
|
|
95
|
+
IdentityProvider.legacyCookie = undefined;
|
|
96
|
+
IdentityProvider.expireLegacyCookie();
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Forgets the identity kept in memory.
|
|
100
|
+
* @deprecated Arc no longer keeps the identity in a cookie. Use {@link IdentityProvider.clearCache} instead.
|
|
101
|
+
*/ static clearIdentityCookie() {
|
|
102
|
+
IdentityProvider.clearCache();
|
|
103
|
+
}
|
|
104
|
+
static expireLegacyCookie() {
|
|
105
|
+
if (typeof document === 'undefined') return;
|
|
106
|
+
document.cookie = `${IdentityProvider.CookieName}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/`;
|
|
107
|
+
}
|
|
108
|
+
static readLegacyCookie() {
|
|
109
|
+
if (typeof document === 'undefined') return undefined;
|
|
110
|
+
const prefix = `${IdentityProvider.CookieName}=`;
|
|
111
|
+
return document.cookie.split(';').map((_)=>_.trim()).find((_)=>_.startsWith(prefix));
|
|
112
|
+
}
|
|
113
|
+
static fetchAndCache() {
|
|
114
|
+
const pending = IdentityProvider.fetchResult();
|
|
115
|
+
IdentityProvider.cachedResult = pending;
|
|
116
|
+
// Only a resolved identity is worth keeping: an unset one, or a failed request, has to be asked for
|
|
117
|
+
// again - otherwise a page that loaded before sign-in would never see the user who signed in.
|
|
118
|
+
const forget = ()=>{
|
|
119
|
+
if (IdentityProvider.cachedResult === pending) {
|
|
120
|
+
IdentityProvider.cachedResult = undefined;
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
pending.then((result)=>{
|
|
124
|
+
if (!result) forget();
|
|
125
|
+
}, forget);
|
|
126
|
+
return pending;
|
|
127
|
+
}
|
|
128
|
+
static async fetchResult() {
|
|
66
129
|
const origin = IdentityProvider.origin || Globals.Globals.origin || '';
|
|
67
130
|
const apiBasePath = IdentityProvider.apiBasePath || Globals.Globals.apiBasePath || '';
|
|
68
131
|
const route = joinPaths.joinPaths(apiBasePath, '/.cratis/me');
|
|
@@ -71,10 +134,43 @@ var joinPaths = require('../joinPaths.js');
|
|
|
71
134
|
method: 'GET',
|
|
72
135
|
headers: IdentityProvider.httpHeadersCallback?.() ?? {}
|
|
73
136
|
});
|
|
137
|
+
// No identity endpoint at all - an application that has not mapped one, or one whose identity comes from a
|
|
138
|
+
// proxy in front of it. Anything else, a 401 or 403 above all, is the server's answer and a cookie the
|
|
139
|
+
// browser holds must never overrule it.
|
|
140
|
+
if (response.status === 404) {
|
|
141
|
+
return IdentityProvider.fromLegacyCookie();
|
|
142
|
+
}
|
|
143
|
+
IdentityProvider.legacyCookie = undefined;
|
|
74
144
|
if (!response.ok) {
|
|
145
|
+
return undefined;
|
|
146
|
+
}
|
|
147
|
+
return await response.json();
|
|
148
|
+
}
|
|
149
|
+
static fromLegacyCookie() {
|
|
150
|
+
if (typeof document === 'undefined') return undefined;
|
|
151
|
+
// A proxy may issue a newer cookie on the response. Prefer it over the one expired before the request.
|
|
152
|
+
const responseCookie = IdentityProvider.readLegacyCookie();
|
|
153
|
+
const cookie = responseCookie ?? IdentityProvider.legacyCookie;
|
|
154
|
+
if (!cookie) return undefined;
|
|
155
|
+
IdentityProvider.legacyCookie = cookie;
|
|
156
|
+
try {
|
|
157
|
+
const result = JSON.parse(atob(decodeURIComponent(cookie.substring(`${IdentityProvider.CookieName}=`.length))));
|
|
158
|
+
// The proxy may not reissue its cookie while its own authorization record remains valid. Restore the
|
|
159
|
+
// transition fallback after a 404 so it also survives a reload, without changing a response cookie.
|
|
160
|
+
if (!responseCookie) document.cookie = `${cookie};path=/;samesite=lax${location.protocol === 'https:' ? ';secure' : ''}`;
|
|
161
|
+
if (!IdentityProvider.hasWarnedAboutLegacyCookie) {
|
|
162
|
+
IdentityProvider.hasWarnedAboutLegacyCookie = true;
|
|
163
|
+
console.warn(`The server has no '/.cratis/me' endpoint, so the identity was read from the '${IdentityProvider.CookieName}' cookie. ` + 'That cookie is not protected and the fallback will be removed in a future major version. ' + 'Expose /.cratis/me to the frontend: https://github.com/Cratis/Arc/blob/main/Documentation/backend/csharp/identity/migrating-from-the-identity-cookie.md');
|
|
164
|
+
}
|
|
165
|
+
return result;
|
|
166
|
+
} catch {
|
|
167
|
+
return undefined;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
static toIdentity(result, type) {
|
|
171
|
+
if (!result) {
|
|
75
172
|
return IdentityProvider.notSet(type);
|
|
76
173
|
}
|
|
77
|
-
const result = await response.json();
|
|
78
174
|
const details = deserializeIdentityDetails.deserializeIdentityDetails(type, result.details);
|
|
79
175
|
return {
|
|
80
176
|
id: result.id,
|
|
@@ -97,28 +193,6 @@ var joinPaths = require('../joinPaths.js');
|
|
|
97
193
|
refresh: ()=>IdentityProvider.refresh(type)
|
|
98
194
|
};
|
|
99
195
|
}
|
|
100
|
-
static getCookie() {
|
|
101
|
-
if (typeof document === 'undefined') return [];
|
|
102
|
-
const decoded = decodeURIComponent(document.cookie);
|
|
103
|
-
const cookies = decoded.split(';').map((_)=>_.trim());
|
|
104
|
-
const cookie = cookies.find((_)=>_.indexOf(`${IdentityProvider.CookieName}=`) == 0);
|
|
105
|
-
if (cookie) {
|
|
106
|
-
const keyValue = cookie.split('=');
|
|
107
|
-
return [
|
|
108
|
-
keyValue[0].trim(),
|
|
109
|
-
keyValue[1].trim()
|
|
110
|
-
];
|
|
111
|
-
}
|
|
112
|
-
return [];
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* Clears the identity cookie used by Arc to cache the current identity.
|
|
116
|
-
* Call this when the user logs out to ensure subsequent requests and WebSocket
|
|
117
|
-
* connections do not carry stale credentials.
|
|
118
|
-
*/ static clearIdentityCookie() {
|
|
119
|
-
if (typeof document === 'undefined') return;
|
|
120
|
-
document.cookie = `${IdentityProvider.CookieName}=;expires=Thu, 01 Jan 1970 00:00:00 GMT`;
|
|
121
|
-
}
|
|
122
196
|
}
|
|
123
197
|
|
|
124
198
|
exports.IdentityProvider = IdentityProvider;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IdentityProvider.js","sources":["../../../identity/IdentityProvider.ts"],"sourcesContent":["// Copyright (c) Cratis. All rights reserved.\n// Licensed under the MIT license. See LICENSE file in the project root for full license information.\n\nimport { Constructor } from '@cratis/fundamentals';\nimport { IIdentityProvider } from './IIdentityProvider.js';\nimport { IIdentity } from './IIdentity.js';\nimport { IdentityProviderResult } from './IdentityProviderResult.js';\nimport { deserializeIdentityDetails } from './deserializeIdentityDetails.js';\nimport { GetHttpHeaders } from '../GetHttpHeaders.js';\nimport { Globals } from '../Globals.js';\nimport { UrlHelpers } from '../UrlHelpers.js';\nimport { joinPaths } from '../joinPaths.js';\n\n/**\n * Represents an implementation of {@link IIdentityProvider}.\n*/\nexport class IdentityProvider extends IIdentityProvider {\n\n static readonly CookieName = '.cratis-identity';\n static httpHeadersCallback: GetHttpHeaders | undefined;\n static apiBasePath: string = '';\n static origin: string = '';\n\n /**\n * Sets the HTTP headers callback.\n * @param callback Callback to set.\n */\n static setHttpHeadersCallback(callback: GetHttpHeaders): void {\n IdentityProvider.httpHeadersCallback = callback;\n }\n\n /**\n * Sets the API base path.\n * @param apiBasePath API base path to set.\n */\n static setApiBasePath(apiBasePath: string): void {\n IdentityProvider.apiBasePath = apiBasePath;\n }\n\n /**\n * Sets the origin.\n * @param origin Origin to set.\n */\n static setOrigin(origin: string): void {\n IdentityProvider.origin = origin;\n }\n\n /**\n * Gets the current identity by optionally specifying the details type.\n * @param type Optional constructor for the details type to enable type-safe deserialization.\n * @returns The current identity as {@link IIdentity}.\n * @remarks The `extends object` constraint is required for compatibility with JsonSerializer.deserializeFromInstance().\n */\n static async getCurrent<TDetails extends object = object>(type?: Constructor<TDetails>): Promise<IIdentity<TDetails>> {\n const cookie = this.getCookie();\n if (cookie.length == 2) {\n const json = atob(cookie[1]);\n const result = JSON.parse(json) as IdentityProviderResult;\n const details = deserializeIdentityDetails(type, result.details);\n return {\n id: result.id,\n name: result.name,\n roles: result.roles || [],\n details: details as TDetails,\n isSet: true,\n isInRole: (role: string) => (result.roles || []).includes(role),\n refresh: () => IdentityProvider.refresh(type)\n } as IIdentity<TDetails>;\n } else {\n const identity = await this.refresh<TDetails>(type);\n return identity;\n }\n }\n\n /** @inheritdoc */\n async getCurrent<TDetails extends object = object>(type?: Constructor<TDetails>): Promise<IIdentity<TDetails>> {\n return IdentityProvider.getCurrent<TDetails>(type);\n }\n\n static async refresh<TDetails extends object = object>(type?: Constructor<TDetails>): Promise<IIdentity<TDetails>> {\n IdentityProvider.clearIdentityCookie();\n const origin = IdentityProvider.origin || Globals.origin || '';\n const apiBasePath = IdentityProvider.apiBasePath || Globals.apiBasePath || '';\n const route = joinPaths(apiBasePath, '/.cratis/me');\n const url = UrlHelpers.createUrlFrom(origin, apiBasePath, route);\n const response = await fetch(\n url, {\n method: 'GET',\n headers: IdentityProvider.httpHeadersCallback?.() ?? {}\n });\n\n if (!response.ok) {\n return IdentityProvider.notSet(type);\n }\n\n const result = await response.json() as IdentityProviderResult;\n const details = deserializeIdentityDetails(type, result.details);\n\n return {\n id: result.id,\n name: result.name,\n roles: result.roles || [],\n details: details as TDetails,\n isSet: true,\n isInRole: (role: string) => (result.roles || []).includes(role),\n refresh: () => IdentityProvider.refresh(type)\n };\n }\n\n private static notSet<TDetails extends object = object>(type?: Constructor<TDetails>): IIdentity<TDetails> {\n return {\n id: '',\n name: '',\n roles: [],\n details: {} as TDetails,\n isSet: false,\n isInRole: () => false,\n refresh: () => IdentityProvider.refresh(type)\n };\n }\n\n private static getCookie() {\n if (typeof document === 'undefined') return [];\n const decoded = decodeURIComponent(document.cookie);\n const cookies = decoded.split(';').map(_ => _.trim());\n const cookie = cookies.find(_ => _.indexOf(`${IdentityProvider.CookieName}=`) == 0);\n if (cookie) {\n const keyValue = cookie.split('=');\n return [keyValue[0].trim(), keyValue[1].trim()];\n }\n return [];\n }\n\n /**\n * Clears the identity cookie used by Arc to cache the current identity.\n * Call this when the user logs out to ensure subsequent requests and WebSocket\n * connections do not carry stale credentials.\n */\n static clearIdentityCookie(): void {\n if (typeof document === 'undefined') return;\n document.cookie = `${IdentityProvider.CookieName}=;expires=Thu, 01 Jan 1970 00:00:00 GMT`;\n }\n}\n"],"names":["IdentityProvider","IIdentityProvider","CookieName","httpHeadersCallback","apiBasePath","origin","setHttpHeadersCallback","callback","setApiBasePath","setOrigin","getCurrent","type","cookie","getCookie","length","json","atob","result","JSON","parse","details","deserializeIdentityDetails","id","name","roles","isSet","isInRole","role","includes","refresh","identity","clearIdentityCookie","Globals","route","joinPaths","url","UrlHelpers","createUrlFrom","response","fetch","method","headers","ok","notSet","document","decoded","decodeURIComponent","cookies","split","map","_","trim","find","indexOf","keyValue"],"mappings":";;;;;;;;AAAA;AACA;AAYA;;AAEA,GACO,MAAMA,gBAAAA,SAAyBC,mCAAAA,CAAAA;AAElC,IAAA,OAAgBC,aAAa,kBAAA;AAC7B,IAAA,OAAOC,mBAAAA;AACP,IAAA,OAAOC,cAAsB,EAAA;AAC7B,IAAA,OAAOC,SAAiB,EAAA;AAExB;;;QAIA,OAAOC,sBAAAA,CAAuBC,QAAwB,EAAQ;AAC1DP,QAAAA,gBAAAA,CAAiBG,mBAAmB,GAAGI,QAAAA;AAC3C,IAAA;AAEA;;;QAIA,OAAOC,cAAAA,CAAeJ,WAAmB,EAAQ;AAC7CJ,QAAAA,gBAAAA,CAAiBI,WAAW,GAAGA,WAAAA;AACnC,IAAA;AAEA;;;QA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Z,iBAAiBE,UAAU,CAAC,uCAAuC,CAAC;AAC7F,IAAA;AACJ;;"}
|
|
1
|
+
{"version":3,"file":"IdentityProvider.js","sources":["../../../identity/IdentityProvider.ts"],"sourcesContent":["// Copyright (c) Cratis. All rights reserved.\n// Licensed under the MIT license. See LICENSE file in the project root for full license information.\n\nimport { Constructor } from '@cratis/fundamentals';\nimport { IIdentityProvider } from './IIdentityProvider.js';\nimport { IIdentity } from './IIdentity.js';\nimport { IdentityProviderResult } from './IdentityProviderResult.js';\nimport { deserializeIdentityDetails } from './deserializeIdentityDetails.js';\nimport { GetHttpHeaders } from '../GetHttpHeaders.js';\nimport { Globals } from '../Globals.js';\nimport { UrlHelpers } from '../UrlHelpers.js';\nimport { joinPaths } from '../joinPaths.js';\n\n/**\n * Represents an implementation of {@link IIdentityProvider}.\n *\n * The identity comes from the `/.cratis/me` endpoint, which derives it from the authenticated request. The\n * result is kept in memory for the page, so repeated calls to {@link IdentityProvider.getCurrent} do not each go to\n * the server; {@link IdentityProvider.refresh} always asks the server again.\n *\n * Only when the server has no `/.cratis/me` endpoint (it answers 404) does it fall back to the readable\n * `.cratis-identity` cookie earlier versions relied on, and it warns when it does. That fallback is for the transition\n * and will be removed.\n*/\nexport class IdentityProvider extends IIdentityProvider {\n\n /**\n * The name of the identity cookie earlier versions of Arc wrote and read.\n * @deprecated The identity comes from the `/.cratis/me` endpoint. The cookie is only read when that endpoint does\n * not exist, and that fallback will be removed.\n */\n static readonly CookieName = '.cratis-identity';\n static httpHeadersCallback: GetHttpHeaders | undefined;\n static apiBasePath: string = '';\n static origin: string = '';\n\n private static cachedResult: Promise<IdentityProviderResult | undefined> | undefined;\n private static hasWarnedAboutLegacyCookie = false;\n private static legacyCookie: string | undefined;\n\n /**\n * Sets the HTTP headers callback.\n * @param callback Callback to set.\n */\n static setHttpHeadersCallback(callback: GetHttpHeaders): void {\n IdentityProvider.httpHeadersCallback = callback;\n }\n\n /**\n * Sets the API base path.\n * @param apiBasePath API base path to set.\n */\n static setApiBasePath(apiBasePath: string): void {\n if (IdentityProvider.apiBasePath !== apiBasePath) {\n IdentityProvider.cachedResult = undefined;\n IdentityProvider.legacyCookie = undefined;\n }\n IdentityProvider.apiBasePath = apiBasePath;\n }\n\n /**\n * Sets the origin.\n * @param origin Origin to set.\n */\n static setOrigin(origin: string): void {\n if (IdentityProvider.origin !== origin) {\n IdentityProvider.cachedResult = undefined;\n IdentityProvider.legacyCookie = undefined;\n }\n IdentityProvider.origin = origin;\n }\n\n /**\n * Gets the current identity by optionally specifying the details type.\n * @param type Optional constructor for the details type to enable type-safe deserialization.\n * @returns The current identity as {@link IIdentity}.\n * @remarks The identity is fetched from `/.cratis/me` the first time and kept in memory until\n * {@link IdentityProvider.refresh} or {@link IdentityProvider.clearCache} is called. An identity that could not be\n * resolved is not kept, so the next call asks the server again.\n * The `extends object` constraint is required for compatibility with JsonSerializer.deserializeFromInstance().\n */\n static async getCurrent<TDetails extends object = object>(type?: Constructor<TDetails>): Promise<IIdentity<TDetails>> {\n if (!IdentityProvider.cachedResult) {\n IdentityProvider.fetchAndCache();\n }\n const result = await IdentityProvider.cachedResult;\n return IdentityProvider.toIdentity(result, type);\n }\n\n /** @inheritdoc */\n async getCurrent<TDetails extends object = object>(type?: Constructor<TDetails>): Promise<IIdentity<TDetails>> {\n return IdentityProvider.getCurrent<TDetails>(type);\n }\n\n /**\n * Fetches the current identity from `/.cratis/me`, replacing what is kept in memory.\n * @param type Optional constructor for the details type to enable type-safe deserialization.\n * @returns The current identity as {@link IIdentity}.\n */\n static async refresh<TDetails extends object = object>(type?: Constructor<TDetails>): Promise<IIdentity<TDetails>> {\n // Earlier backends resolve the cookie before consulting the current credentials. Retain it during the\n // request for a possible 404 transition fallback, but do not send it with an explicit refresh request.\n IdentityProvider.legacyCookie = IdentityProvider.readLegacyCookie() ?? IdentityProvider.legacyCookie;\n IdentityProvider.cachedResult = undefined;\n IdentityProvider.expireLegacyCookie();\n const result = await IdentityProvider.fetchAndCache();\n return IdentityProvider.toIdentity(result, type);\n }\n\n /**\n * Forgets the identity kept in memory, so the next {@link IdentityProvider.getCurrent} asks the server again.\n * It also expires a readable `.cratis-identity` cookie left by an earlier version. Call this when the user logs out.\n */\n static clearCache(): void {\n IdentityProvider.cachedResult = undefined;\n IdentityProvider.legacyCookie = undefined;\n IdentityProvider.expireLegacyCookie();\n }\n\n /**\n * Forgets the identity kept in memory.\n * @deprecated Arc no longer keeps the identity in a cookie. Use {@link IdentityProvider.clearCache} instead.\n */\n static clearIdentityCookie(): void {\n IdentityProvider.clearCache();\n }\n\n private static expireLegacyCookie(): void {\n if (typeof document === 'undefined') return;\n document.cookie = `${IdentityProvider.CookieName}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/`;\n }\n\n private static readLegacyCookie(): string | undefined {\n if (typeof document === 'undefined') return undefined;\n const prefix = `${IdentityProvider.CookieName}=`;\n return document.cookie.split(';').map(_ => _.trim()).find(_ => _.startsWith(prefix));\n }\n\n private static fetchAndCache(): Promise<IdentityProviderResult | undefined> {\n const pending = IdentityProvider.fetchResult();\n IdentityProvider.cachedResult = pending;\n\n // Only a resolved identity is worth keeping: an unset one, or a failed request, has to be asked for\n // again - otherwise a page that loaded before sign-in would never see the user who signed in.\n const forget = () => {\n if (IdentityProvider.cachedResult === pending) {\n IdentityProvider.cachedResult = undefined;\n }\n };\n pending.then(result => {\n if (!result) forget();\n }, forget);\n\n return pending;\n }\n\n private static async fetchResult(): Promise<IdentityProviderResult | undefined> {\n const origin = IdentityProvider.origin || Globals.origin || '';\n const apiBasePath = IdentityProvider.apiBasePath || Globals.apiBasePath || '';\n const route = joinPaths(apiBasePath, '/.cratis/me');\n const url = UrlHelpers.createUrlFrom(origin, apiBasePath, route);\n const response = await fetch(\n url, {\n method: 'GET',\n headers: IdentityProvider.httpHeadersCallback?.() ?? {}\n });\n\n // No identity endpoint at all - an application that has not mapped one, or one whose identity comes from a\n // proxy in front of it. Anything else, a 401 or 403 above all, is the server's answer and a cookie the\n // browser holds must never overrule it.\n if (response.status === 404) {\n return IdentityProvider.fromLegacyCookie();\n }\n\n IdentityProvider.legacyCookie = undefined;\n if (!response.ok) {\n return undefined;\n }\n\n return await response.json() as IdentityProviderResult;\n }\n\n private static fromLegacyCookie(): IdentityProviderResult | undefined {\n if (typeof document === 'undefined') return undefined;\n // A proxy may issue a newer cookie on the response. Prefer it over the one expired before the request.\n const responseCookie = IdentityProvider.readLegacyCookie();\n const cookie = responseCookie ?? IdentityProvider.legacyCookie;\n if (!cookie) return undefined;\n IdentityProvider.legacyCookie = cookie;\n\n try {\n const result = JSON.parse(atob(decodeURIComponent(cookie.substring(`${IdentityProvider.CookieName}=`.length)))) as IdentityProviderResult;\n // The proxy may not reissue its cookie while its own authorization record remains valid. Restore the\n // transition fallback after a 404 so it also survives a reload, without changing a response cookie.\n if (!responseCookie) document.cookie = `${cookie};path=/;samesite=lax${location.protocol === 'https:' ? ';secure' : ''}`;\n if (!IdentityProvider.hasWarnedAboutLegacyCookie) {\n IdentityProvider.hasWarnedAboutLegacyCookie = true;\n console.warn(\n `The server has no '/.cratis/me' endpoint, so the identity was read from the '${IdentityProvider.CookieName}' cookie. ` +\n 'That cookie is not protected and the fallback will be removed in a future major version. ' +\n 'Expose /.cratis/me to the frontend: https://github.com/Cratis/Arc/blob/main/Documentation/backend/csharp/identity/migrating-from-the-identity-cookie.md');\n }\n return result;\n } catch {\n return undefined;\n }\n }\n\n private static toIdentity<TDetails extends object = object>(result: IdentityProviderResult | undefined, type?: Constructor<TDetails>): IIdentity<TDetails> {\n if (!result) {\n return IdentityProvider.notSet(type);\n }\n\n const details = deserializeIdentityDetails(type, result.details);\n return {\n id: result.id,\n name: result.name,\n roles: result.roles || [],\n details: details as TDetails,\n isSet: true,\n isInRole: (role: string) => (result.roles || []).includes(role),\n refresh: () => IdentityProvider.refresh(type)\n };\n }\n\n private static notSet<TDetails extends object = object>(type?: Constructor<TDetails>): IIdentity<TDetails> {\n return {\n id: '',\n name: '',\n roles: [],\n details: {} as TDetails,\n isSet: false,\n isInRole: () => false,\n refresh: () => IdentityProvider.refresh(type)\n };\n }\n}\n"],"names":["IdentityProvider","IIdentityProvider","CookieName","httpHeadersCallback","apiBasePath","origin","cachedResult","hasWarnedAboutLegacyCookie","legacyCookie","setHttpHeadersCallback","callback","setApiBasePath","undefined","setOrigin","getCurrent","type","fetchAndCache","result","toIdentity","refresh","readLegacyCookie","expireLegacyCookie","clearCache","clearIdentityCookie","document","cookie","prefix","split","map","_","trim","find","startsWith","pending","fetchResult","forget","then","Globals","route","joinPaths","url","UrlHelpers","createUrlFrom","response","fetch","method","headers","status","fromLegacyCookie","ok","json","responseCookie","JSON","parse","atob","decodeURIComponent","substring","length","location","protocol","console","warn","notSet","details","deserializeIdentityDetails","id","name","roles","isSet","isInRole","role","includes"],"mappings":";;;;;;;;AAAA;AACA;AAYA;;;;;;;;;;AAUA,GACO,MAAMA,gBAAAA,SAAyBC,mCAAAA,CAAAA;AAElC;;;;QAKA,OAAgBC,aAAa,kBAAA;AAC7B,IAAA,OAAOC,mBAAAA;AACP,IAAA,OAAOC,cAAsB,EAAA;AAC7B,IAAA,OAAOC,SAAiB,EAAA;AAExB,IAAA,OAAeC,YAAAA;AACf,IAAA,OAAeC,6BAA6B,KAAA;AAC5C,IAAA,OAAeC,YAAAA;AAEf;;;QAIA,OAAOC,sBAAAA,CAAuBC,QAAwB,EAAQ;AAC1DV,QAAAA,gBAAAA,CAAiBG,mBAAmB,GAAGO,QAAAA;AAC3C,IAAA;AAEA;;;QAIA,OAAOC,cAAAA,CAAeP,WAAmB,EAAQ;QAC7C,IAAIJ,gBAAAA,CAAiBI,WAAW,KAAKA,WAAAA,EAAa;AAC9CJ,YAAAA,gBAAAA,CAAiBM,YAAY,GAAGM,SAAAA;AAChCZ,YAAAA,gBAAAA,CAAiBQ,YAAY,GAAGI,SAAAA;AACpC,QAAA;AACAZ,QAAAA,gBAAAA,CAAiBI,WAAW,GAAGA,WAAAA;AACnC,IAAA;AAE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A,CAAOkD,KAAK,IAAI,EAAC,EAAGI,QAAQ,CAACD,IAAAA,CAAAA;YAC1DnD,OAAAA,EAAS,IAAMnB,gBAAAA,CAAiBmB,OAAO,CAACJ,IAAAA;AAC5C,SAAA;AACJ,IAAA;IAEA,OAAe+C,MAAAA,CAAyC/C,IAA4B,EAAuB;QACvG,OAAO;YACHkD,EAAAA,EAAI,EAAA;YACJC,IAAAA,EAAM,EAAA;AACNC,YAAAA,KAAAA,EAAO,EAAE;AACTJ,YAAAA,OAAAA,EAAS,EAAC;YACVK,KAAAA,EAAO,KAAA;AACPC,YAAAA,QAAAA,EAAU,IAAM,KAAA;YAChBlD,OAAAA,EAAS,IAAMnB,gBAAAA,CAAiBmB,OAAO,CAACJ,IAAAA;AAC5C,SAAA;AACJ,IAAA;AACJ;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deserializeIdentityDetails.d.ts","sourceRoot":"","sources":["../../../identity/deserializeIdentityDetails.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,WAAW,EAA0B,MAAM,sBAAsB,CAAC;
|
|
1
|
+
{"version":3,"file":"deserializeIdentityDetails.d.ts","sourceRoot":"","sources":["../../../identity/deserializeIdentityDetails.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,WAAW,EAA0B,MAAM,sBAAsB,CAAC;AAkD3E,wBAAgB,0BAA0B,CAAC,IAAI,EAAE,WAAW,GAAG,SAAS,EAAE,OAAO,EAAE,OAAO,GAAG,OAAO,CAwBnG"}
|
|
@@ -30,8 +30,7 @@ var fundamentals = require('@cratis/fundamentals');
|
|
|
30
30
|
* would otherwise crash or silently destroy the payload.
|
|
31
31
|
* @param {Constructor | undefined} type The details type to deserialize into, or `undefined` to leave
|
|
32
32
|
* the payload untouched.
|
|
33
|
-
* @param {unknown} details The raw details payload - parsed JSON from the
|
|
34
|
-
* `/.cratis/me` endpoint.
|
|
33
|
+
* @param {unknown} details The raw details payload - parsed JSON from the `/.cratis/me` endpoint.
|
|
35
34
|
* @returns {unknown} The deserialized instance, or `details` unchanged when deserializing it would be
|
|
36
35
|
* unsafe, pointless, or has already been done.
|
|
37
36
|
* @remarks
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deserializeIdentityDetails.js","sources":["../../../identity/deserializeIdentityDetails.ts"],"sourcesContent":["// Copyright (c) Cratis. All rights reserved.\n// Licensed under the MIT license. See LICENSE file in the project root for full license information.\n\nimport { Constructor, Fields, JsonSerializer } from '@cratis/fundamentals';\n\n/*\n * ⚠️ deserializeIdentityDetails is exported deliberately, unlike deserializeQueryModel in\n * ../queries/deserializeQueryModel.ts.\n *\n * JsonSerializer's converter registry is module state, and a consumer package can easily resolve a\n * different physical copy of @cratis/fundamentals than this one - which is why deserializeQueryModel\n * stays package-private. Identity details are different: @cratis/arc.react's useIdentity() needs to\n * apply the exact same deserialization as this package's own IdentityProvider, for a payload that may\n * already be flowing through this package's IdentityProvider/IIdentity. Exporting the function itself\n * - rather than having @cratis/arc.react re-implement the same logic against its own copy of\n * @cratis/fundamentals - guarantees every identity details value is deserialized through this one\n * physical JsonSerializer/Fields, regardless of which package's copy of @cratis/fundamentals the\n * caller resolved.\n */\n\n/**\n * Determines whether a details type is a JavaScript primitive wrapper rather than a real,\n * `@field`-decorated model.\n * @param {Constructor} type The details type to check.\n * @returns {boolean} True when the type is a primitive wrapper.\n */\nfunction isPrimitiveDetailsType(type: Constructor): boolean {\n return type === String || type === Number || type === Boolean;\n}\n\n/**\n * Deserializes identity details into their strongly-typed shape, guarding every way that doing so\n * would otherwise crash or silently destroy the payload.\n * @param {Constructor | undefined} type The details type to deserialize into, or `undefined` to leave\n * the payload untouched.\n * @param {unknown} details The raw details payload - parsed JSON from the
|
|
1
|
+
{"version":3,"file":"deserializeIdentityDetails.js","sources":["../../../identity/deserializeIdentityDetails.ts"],"sourcesContent":["// Copyright (c) Cratis. All rights reserved.\n// Licensed under the MIT license. See LICENSE file in the project root for full license information.\n\nimport { Constructor, Fields, JsonSerializer } from '@cratis/fundamentals';\n\n/*\n * ⚠️ deserializeIdentityDetails is exported deliberately, unlike deserializeQueryModel in\n * ../queries/deserializeQueryModel.ts.\n *\n * JsonSerializer's converter registry is module state, and a consumer package can easily resolve a\n * different physical copy of @cratis/fundamentals than this one - which is why deserializeQueryModel\n * stays package-private. Identity details are different: @cratis/arc.react's useIdentity() needs to\n * apply the exact same deserialization as this package's own IdentityProvider, for a payload that may\n * already be flowing through this package's IdentityProvider/IIdentity. Exporting the function itself\n * - rather than having @cratis/arc.react re-implement the same logic against its own copy of\n * @cratis/fundamentals - guarantees every identity details value is deserialized through this one\n * physical JsonSerializer/Fields, regardless of which package's copy of @cratis/fundamentals the\n * caller resolved.\n */\n\n/**\n * Determines whether a details type is a JavaScript primitive wrapper rather than a real,\n * `@field`-decorated model.\n * @param {Constructor} type The details type to check.\n * @returns {boolean} True when the type is a primitive wrapper.\n */\nfunction isPrimitiveDetailsType(type: Constructor): boolean {\n return type === String || type === Number || type === Boolean;\n}\n\n/**\n * Deserializes identity details into their strongly-typed shape, guarding every way that doing so\n * would otherwise crash or silently destroy the payload.\n * @param {Constructor | undefined} type The details type to deserialize into, or `undefined` to leave\n * the payload untouched.\n * @param {unknown} details The raw details payload - parsed JSON from the `/.cratis/me` endpoint.\n * @returns {unknown} The deserialized instance, or `details` unchanged when deserializing it would be\n * unsafe, pointless, or has already been done.\n * @remarks\n * {@link JsonSerializer.deserializeFromInstance} assumes a well-formed object and a target type\n * carrying `@field` metadata - neither is guaranteed here:\n * - No type means there is nothing to deserialize into.\n * - A `null`/`undefined`/non-object payload would make `deserializeFromInstance`'s internal\n * `instance[field.name]` lookup throw.\n * - A payload that is already an instance of the target type has already been deserialized -\n * deserializing it again is destructive, not merely wasteful: it can throw on a nested temporal\n * value and double-wraps a concept (`{ value: { value: '...' } }`).\n * - `Object` and the primitive wrapper types (`String`, `Number`, `Boolean`) are not real,\n * `@field`-decorated models; deserializing into them would discard the payload.\n * - A type with no `@field`-decorated members deserializes to an empty instance, silently discarding\n * the payload. That is worse than leaving the payload alone, so this warns and passes it through\n * instead - preserving data beats a hard crash for anyone upgrading with an undecorated type.\n */\nexport function deserializeIdentityDetails(type: Constructor | undefined, details: unknown): unknown {\n if (!type || type === Object || isPrimitiveDetailsType(type)) {\n return details;\n }\n\n if (details === null || details === undefined || typeof details !== 'object') {\n return details;\n }\n\n if (details instanceof type) {\n return details;\n }\n\n const fields = Fields.getFieldsForType(type);\n if (fields.length === 0) {\n console.warn(\n `Identity details type '${type.name}' has no @field-decorated members. Deserializing into ` +\n 'it would discard the payload and return an empty instance, so the raw payload is being ' +\n 'passed through instead. Add @field decorators for every property that should be ' +\n 'populated, or omit the details type to receive the raw payload as-is.');\n return details;\n }\n\n return JsonSerializer.deserializeFromInstance(type as Constructor<object>, details);\n}\n"],"names":["isPrimitiveDetailsType","type","String","Number","Boolean","deserializeIdentityDetails","details","Object","undefined","fields","Fields","getFieldsForType","length","console","warn","name","JsonSerializer","deserializeFromInstance"],"mappings":";;;;AAAA;AACA;AAIA;;;;;;;;;;;;;;;;;;IAqBA,SAASA,uBAAuBC,IAAiB,EAAA;AAC7C,IAAA,OAAOA,IAAAA,KAASC,MAAAA,IAAUD,IAAAA,KAASE,MAAAA,IAAUF,IAAAA,KAASG,OAAAA;AAC1D;AAEA;;;;;;;;;;;;;;;;;;;;;;AAsBC,IACM,SAASC,0BAAAA,CAA2BJ,IAA6B,EAAEK,OAAgB,EAAA;AACtF,IAAA,IAAI,CAACL,IAAAA,IAAQA,IAAAA,KAASM,MAAAA,IAAUP,uBAAuBC,IAAAA,CAAAA,EAAO;QAC1D,OAAOK,OAAAA;AACX,IAAA;AAEA,IAAA,IAAIA,YAAY,IAAA,IAAQA,OAAAA,KAAYE,SAAAA,IAAa,OAAOF,YAAY,QAAA,EAAU;QAC1E,OAAOA,OAAAA;AACX,IAAA;AAEA,IAAA,IAAIA,mBAAmBL,IAAAA,EAAM;QACzB,OAAOK,OAAAA;AACX,IAAA;IAEA,MAAMG,MAAAA,GAASC,mBAAAA,CAAOC,gBAAgB,CAACV,IAAAA,CAAAA;IACvC,IAAIQ,MAAAA,CAAOG,MAAM,KAAK,CAAA,EAAG;AACrBC,QAAAA,OAAAA,CAAQC,IAAI,CACR,CAAC,uBAAuB,EAAEb,IAAAA,CAAKc,IAAI,CAAC,sDAAsD,CAAC,GAC3F,yFAAA,GACA,kFAAA,GACA,uEAAA,CAAA;QACJ,OAAOT,OAAAA;AACX,IAAA;IAEA,OAAOU,2BAAAA,CAAeC,uBAAuB,CAAChB,IAAAA,EAA6BK,OAAAA,CAAAA;AAC/E;;"}
|
|
@@ -7,14 +7,23 @@ export declare class IdentityProvider extends IIdentityProvider {
|
|
|
7
7
|
static httpHeadersCallback: GetHttpHeaders | undefined;
|
|
8
8
|
static apiBasePath: string;
|
|
9
9
|
static origin: string;
|
|
10
|
+
private static cachedResult;
|
|
11
|
+
private static hasWarnedAboutLegacyCookie;
|
|
12
|
+
private static legacyCookie;
|
|
10
13
|
static setHttpHeadersCallback(callback: GetHttpHeaders): void;
|
|
11
14
|
static setApiBasePath(apiBasePath: string): void;
|
|
12
15
|
static setOrigin(origin: string): void;
|
|
13
16
|
static getCurrent<TDetails extends object = object>(type?: Constructor<TDetails>): Promise<IIdentity<TDetails>>;
|
|
14
17
|
getCurrent<TDetails extends object = object>(type?: Constructor<TDetails>): Promise<IIdentity<TDetails>>;
|
|
15
18
|
static refresh<TDetails extends object = object>(type?: Constructor<TDetails>): Promise<IIdentity<TDetails>>;
|
|
16
|
-
|
|
17
|
-
private static getCookie;
|
|
19
|
+
static clearCache(): void;
|
|
18
20
|
static clearIdentityCookie(): void;
|
|
21
|
+
private static expireLegacyCookie;
|
|
22
|
+
private static readLegacyCookie;
|
|
23
|
+
private static fetchAndCache;
|
|
24
|
+
private static fetchResult;
|
|
25
|
+
private static fromLegacyCookie;
|
|
26
|
+
private static toIdentity;
|
|
27
|
+
private static notSet;
|
|
19
28
|
}
|
|
20
29
|
//# sourceMappingURL=IdentityProvider.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IdentityProvider.d.ts","sourceRoot":"","sources":["../../../identity/IdentityProvider.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAC3D,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAG3C,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;
|
|
1
|
+
{"version":3,"file":"IdentityProvider.d.ts","sourceRoot":"","sources":["../../../identity/IdentityProvider.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAC3D,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAG3C,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAgBtD,qBAAa,gBAAiB,SAAQ,iBAAiB;IAOnD,MAAM,CAAC,QAAQ,CAAC,UAAU,sBAAsB;IAChD,MAAM,CAAC,mBAAmB,EAAE,cAAc,GAAG,SAAS,CAAC;IACvD,MAAM,CAAC,WAAW,EAAE,MAAM,CAAM;IAChC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAM;IAE3B,OAAO,CAAC,MAAM,CAAC,YAAY,CAA0D;IACrF,OAAO,CAAC,MAAM,CAAC,0BAA0B,CAAS;IAClD,OAAO,CAAC,MAAM,CAAC,YAAY,CAAqB;IAMhD,MAAM,CAAC,sBAAsB,CAAC,QAAQ,EAAE,cAAc,GAAG,IAAI,CAE5D;IAMD,MAAM,CAAC,cAAc,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI,CAM/C;IAMD,MAAM,CAAC,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAMrC;IAWD,OAAa,UAAU,CAAC,QAAQ,SAAS,MAAM,GAAG,MAAM,EAAE,IAAI,CAAC,EAAE,WAAW,CAAC,QAAQ,CAAC,GAAG,OAAO,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,CAMpH;IAGK,UAAU,CAAC,QAAQ,SAAS,MAAM,GAAG,MAAM,EAAE,IAAI,CAAC,EAAE,WAAW,CAAC,QAAQ,CAAC,GAAG,OAAO,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,CAE7G;IAOD,OAAa,OAAO,CAAC,QAAQ,SAAS,MAAM,GAAG,MAAM,EAAE,IAAI,CAAC,EAAE,WAAW,CAAC,QAAQ,CAAC,GAAG,OAAO,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,CAQjH;IAMD,MAAM,CAAC,UAAU,IAAI,IAAI,CAIxB;IAMD,MAAM,CAAC,mBAAmB,IAAI,IAAI,CAEjC;IAED,OAAO,CAAC,MAAM,CAAC,kBAAkB;IAKjC,OAAO,CAAC,MAAM,CAAC,gBAAgB;IAM/B,OAAO,CAAC,MAAM,CAAC,aAAa;mBAkBP,WAAW;IA0BhC,OAAO,CAAC,MAAM,CAAC,gBAAgB;IA0B/B,OAAO,CAAC,MAAM,CAAC,UAAU;IAiBzB,OAAO,CAAC,MAAM,CAAC,MAAM;CAWxB"}
|
|
@@ -8,11 +8,26 @@ import { joinPaths } from '../joinPaths.js';
|
|
|
8
8
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
9
9
|
/**
|
|
10
10
|
* Represents an implementation of {@link IIdentityProvider}.
|
|
11
|
+
*
|
|
12
|
+
* The identity comes from the `/.cratis/me` endpoint, which derives it from the authenticated request. The
|
|
13
|
+
* result is kept in memory for the page, so repeated calls to {@link IdentityProvider.getCurrent} do not each go to
|
|
14
|
+
* the server; {@link IdentityProvider.refresh} always asks the server again.
|
|
15
|
+
*
|
|
16
|
+
* Only when the server has no `/.cratis/me` endpoint (it answers 404) does it fall back to the readable
|
|
17
|
+
* `.cratis-identity` cookie earlier versions relied on, and it warns when it does. That fallback is for the transition
|
|
18
|
+
* and will be removed.
|
|
11
19
|
*/ class IdentityProvider extends IIdentityProvider {
|
|
12
|
-
|
|
20
|
+
/**
|
|
21
|
+
* The name of the identity cookie earlier versions of Arc wrote and read.
|
|
22
|
+
* @deprecated The identity comes from the `/.cratis/me` endpoint. The cookie is only read when that endpoint does
|
|
23
|
+
* not exist, and that fallback will be removed.
|
|
24
|
+
*/ static CookieName = '.cratis-identity';
|
|
13
25
|
static httpHeadersCallback;
|
|
14
26
|
static apiBasePath = '';
|
|
15
27
|
static origin = '';
|
|
28
|
+
static cachedResult;
|
|
29
|
+
static hasWarnedAboutLegacyCookie = false;
|
|
30
|
+
static legacyCookie;
|
|
16
31
|
/**
|
|
17
32
|
* Sets the HTTP headers callback.
|
|
18
33
|
* @param callback Callback to set.
|
|
@@ -23,44 +38,92 @@ import { joinPaths } from '../joinPaths.js';
|
|
|
23
38
|
* Sets the API base path.
|
|
24
39
|
* @param apiBasePath API base path to set.
|
|
25
40
|
*/ static setApiBasePath(apiBasePath) {
|
|
41
|
+
if (IdentityProvider.apiBasePath !== apiBasePath) {
|
|
42
|
+
IdentityProvider.cachedResult = undefined;
|
|
43
|
+
IdentityProvider.legacyCookie = undefined;
|
|
44
|
+
}
|
|
26
45
|
IdentityProvider.apiBasePath = apiBasePath;
|
|
27
46
|
}
|
|
28
47
|
/**
|
|
29
48
|
* Sets the origin.
|
|
30
49
|
* @param origin Origin to set.
|
|
31
50
|
*/ static setOrigin(origin) {
|
|
51
|
+
if (IdentityProvider.origin !== origin) {
|
|
52
|
+
IdentityProvider.cachedResult = undefined;
|
|
53
|
+
IdentityProvider.legacyCookie = undefined;
|
|
54
|
+
}
|
|
32
55
|
IdentityProvider.origin = origin;
|
|
33
56
|
}
|
|
34
57
|
/**
|
|
35
58
|
* Gets the current identity by optionally specifying the details type.
|
|
36
59
|
* @param type Optional constructor for the details type to enable type-safe deserialization.
|
|
37
60
|
* @returns The current identity as {@link IIdentity}.
|
|
38
|
-
* @remarks The
|
|
61
|
+
* @remarks The identity is fetched from `/.cratis/me` the first time and kept in memory until
|
|
62
|
+
* {@link IdentityProvider.refresh} or {@link IdentityProvider.clearCache} is called. An identity that could not be
|
|
63
|
+
* resolved is not kept, so the next call asks the server again.
|
|
64
|
+
* The `extends object` constraint is required for compatibility with JsonSerializer.deserializeFromInstance().
|
|
39
65
|
*/ static async getCurrent(type) {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
const json = atob(cookie[1]);
|
|
43
|
-
const result = JSON.parse(json);
|
|
44
|
-
const details = deserializeIdentityDetails(type, result.details);
|
|
45
|
-
return {
|
|
46
|
-
id: result.id,
|
|
47
|
-
name: result.name,
|
|
48
|
-
roles: result.roles || [],
|
|
49
|
-
details: details,
|
|
50
|
-
isSet: true,
|
|
51
|
-
isInRole: (role)=>(result.roles || []).includes(role),
|
|
52
|
-
refresh: ()=>IdentityProvider.refresh(type)
|
|
53
|
-
};
|
|
54
|
-
} else {
|
|
55
|
-
const identity = await this.refresh(type);
|
|
56
|
-
return identity;
|
|
66
|
+
if (!IdentityProvider.cachedResult) {
|
|
67
|
+
IdentityProvider.fetchAndCache();
|
|
57
68
|
}
|
|
69
|
+
const result = await IdentityProvider.cachedResult;
|
|
70
|
+
return IdentityProvider.toIdentity(result, type);
|
|
58
71
|
}
|
|
59
72
|
/** @inheritdoc */ async getCurrent(type) {
|
|
60
73
|
return IdentityProvider.getCurrent(type);
|
|
61
74
|
}
|
|
62
|
-
|
|
63
|
-
|
|
75
|
+
/**
|
|
76
|
+
* Fetches the current identity from `/.cratis/me`, replacing what is kept in memory.
|
|
77
|
+
* @param type Optional constructor for the details type to enable type-safe deserialization.
|
|
78
|
+
* @returns The current identity as {@link IIdentity}.
|
|
79
|
+
*/ static async refresh(type) {
|
|
80
|
+
// Earlier backends resolve the cookie before consulting the current credentials. Retain it during the
|
|
81
|
+
// request for a possible 404 transition fallback, but do not send it with an explicit refresh request.
|
|
82
|
+
IdentityProvider.legacyCookie = IdentityProvider.readLegacyCookie() ?? IdentityProvider.legacyCookie;
|
|
83
|
+
IdentityProvider.cachedResult = undefined;
|
|
84
|
+
IdentityProvider.expireLegacyCookie();
|
|
85
|
+
const result = await IdentityProvider.fetchAndCache();
|
|
86
|
+
return IdentityProvider.toIdentity(result, type);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Forgets the identity kept in memory, so the next {@link IdentityProvider.getCurrent} asks the server again.
|
|
90
|
+
* It also expires a readable `.cratis-identity` cookie left by an earlier version. Call this when the user logs out.
|
|
91
|
+
*/ static clearCache() {
|
|
92
|
+
IdentityProvider.cachedResult = undefined;
|
|
93
|
+
IdentityProvider.legacyCookie = undefined;
|
|
94
|
+
IdentityProvider.expireLegacyCookie();
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Forgets the identity kept in memory.
|
|
98
|
+
* @deprecated Arc no longer keeps the identity in a cookie. Use {@link IdentityProvider.clearCache} instead.
|
|
99
|
+
*/ static clearIdentityCookie() {
|
|
100
|
+
IdentityProvider.clearCache();
|
|
101
|
+
}
|
|
102
|
+
static expireLegacyCookie() {
|
|
103
|
+
if (typeof document === 'undefined') return;
|
|
104
|
+
document.cookie = `${IdentityProvider.CookieName}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/`;
|
|
105
|
+
}
|
|
106
|
+
static readLegacyCookie() {
|
|
107
|
+
if (typeof document === 'undefined') return undefined;
|
|
108
|
+
const prefix = `${IdentityProvider.CookieName}=`;
|
|
109
|
+
return document.cookie.split(';').map((_)=>_.trim()).find((_)=>_.startsWith(prefix));
|
|
110
|
+
}
|
|
111
|
+
static fetchAndCache() {
|
|
112
|
+
const pending = IdentityProvider.fetchResult();
|
|
113
|
+
IdentityProvider.cachedResult = pending;
|
|
114
|
+
// Only a resolved identity is worth keeping: an unset one, or a failed request, has to be asked for
|
|
115
|
+
// again - otherwise a page that loaded before sign-in would never see the user who signed in.
|
|
116
|
+
const forget = ()=>{
|
|
117
|
+
if (IdentityProvider.cachedResult === pending) {
|
|
118
|
+
IdentityProvider.cachedResult = undefined;
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
pending.then((result)=>{
|
|
122
|
+
if (!result) forget();
|
|
123
|
+
}, forget);
|
|
124
|
+
return pending;
|
|
125
|
+
}
|
|
126
|
+
static async fetchResult() {
|
|
64
127
|
const origin = IdentityProvider.origin || Globals.origin || '';
|
|
65
128
|
const apiBasePath = IdentityProvider.apiBasePath || Globals.apiBasePath || '';
|
|
66
129
|
const route = joinPaths(apiBasePath, '/.cratis/me');
|
|
@@ -69,10 +132,43 @@ import { joinPaths } from '../joinPaths.js';
|
|
|
69
132
|
method: 'GET',
|
|
70
133
|
headers: IdentityProvider.httpHeadersCallback?.() ?? {}
|
|
71
134
|
});
|
|
135
|
+
// No identity endpoint at all - an application that has not mapped one, or one whose identity comes from a
|
|
136
|
+
// proxy in front of it. Anything else, a 401 or 403 above all, is the server's answer and a cookie the
|
|
137
|
+
// browser holds must never overrule it.
|
|
138
|
+
if (response.status === 404) {
|
|
139
|
+
return IdentityProvider.fromLegacyCookie();
|
|
140
|
+
}
|
|
141
|
+
IdentityProvider.legacyCookie = undefined;
|
|
72
142
|
if (!response.ok) {
|
|
143
|
+
return undefined;
|
|
144
|
+
}
|
|
145
|
+
return await response.json();
|
|
146
|
+
}
|
|
147
|
+
static fromLegacyCookie() {
|
|
148
|
+
if (typeof document === 'undefined') return undefined;
|
|
149
|
+
// A proxy may issue a newer cookie on the response. Prefer it over the one expired before the request.
|
|
150
|
+
const responseCookie = IdentityProvider.readLegacyCookie();
|
|
151
|
+
const cookie = responseCookie ?? IdentityProvider.legacyCookie;
|
|
152
|
+
if (!cookie) return undefined;
|
|
153
|
+
IdentityProvider.legacyCookie = cookie;
|
|
154
|
+
try {
|
|
155
|
+
const result = JSON.parse(atob(decodeURIComponent(cookie.substring(`${IdentityProvider.CookieName}=`.length))));
|
|
156
|
+
// The proxy may not reissue its cookie while its own authorization record remains valid. Restore the
|
|
157
|
+
// transition fallback after a 404 so it also survives a reload, without changing a response cookie.
|
|
158
|
+
if (!responseCookie) document.cookie = `${cookie};path=/;samesite=lax${location.protocol === 'https:' ? ';secure' : ''}`;
|
|
159
|
+
if (!IdentityProvider.hasWarnedAboutLegacyCookie) {
|
|
160
|
+
IdentityProvider.hasWarnedAboutLegacyCookie = true;
|
|
161
|
+
console.warn(`The server has no '/.cratis/me' endpoint, so the identity was read from the '${IdentityProvider.CookieName}' cookie. ` + 'That cookie is not protected and the fallback will be removed in a future major version. ' + 'Expose /.cratis/me to the frontend: https://github.com/Cratis/Arc/blob/main/Documentation/backend/csharp/identity/migrating-from-the-identity-cookie.md');
|
|
162
|
+
}
|
|
163
|
+
return result;
|
|
164
|
+
} catch {
|
|
165
|
+
return undefined;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
static toIdentity(result, type) {
|
|
169
|
+
if (!result) {
|
|
73
170
|
return IdentityProvider.notSet(type);
|
|
74
171
|
}
|
|
75
|
-
const result = await response.json();
|
|
76
172
|
const details = deserializeIdentityDetails(type, result.details);
|
|
77
173
|
return {
|
|
78
174
|
id: result.id,
|
|
@@ -95,28 +191,6 @@ import { joinPaths } from '../joinPaths.js';
|
|
|
95
191
|
refresh: ()=>IdentityProvider.refresh(type)
|
|
96
192
|
};
|
|
97
193
|
}
|
|
98
|
-
static getCookie() {
|
|
99
|
-
if (typeof document === 'undefined') return [];
|
|
100
|
-
const decoded = decodeURIComponent(document.cookie);
|
|
101
|
-
const cookies = decoded.split(';').map((_)=>_.trim());
|
|
102
|
-
const cookie = cookies.find((_)=>_.indexOf(`${IdentityProvider.CookieName}=`) == 0);
|
|
103
|
-
if (cookie) {
|
|
104
|
-
const keyValue = cookie.split('=');
|
|
105
|
-
return [
|
|
106
|
-
keyValue[0].trim(),
|
|
107
|
-
keyValue[1].trim()
|
|
108
|
-
];
|
|
109
|
-
}
|
|
110
|
-
return [];
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Clears the identity cookie used by Arc to cache the current identity.
|
|
114
|
-
* Call this when the user logs out to ensure subsequent requests and WebSocket
|
|
115
|
-
* connections do not carry stale credentials.
|
|
116
|
-
*/ static clearIdentityCookie() {
|
|
117
|
-
if (typeof document === 'undefined') return;
|
|
118
|
-
document.cookie = `${IdentityProvider.CookieName}=;expires=Thu, 01 Jan 1970 00:00:00 GMT`;
|
|
119
|
-
}
|
|
120
194
|
}
|
|
121
195
|
|
|
122
196
|
export { IdentityProvider };
|