@asgardeo/auth-spa 0.2.19 → 0.2.22
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/.eslintrc.js +1 -2
- package/README.md +53 -24
- package/babel.config.js +1 -2
- package/dist/asgardeo-spa.production.esm.js +105 -44
- package/dist/asgardeo-spa.production.esm.js.map +1 -1
- package/dist/asgardeo-spa.production.js +106 -45
- package/dist/asgardeo-spa.production.js.map +1 -1
- package/dist/asgardeo-spa.production.min.js +1 -1
- package/dist/asgardeo-spa.production.min.js.map +1 -1
- package/dist/polyfilled/asgardeo-spa.production.esm.js +103 -42
- package/dist/polyfilled/asgardeo-spa.production.esm.js.map +1 -1
- package/dist/polyfilled/asgardeo-spa.production.js +103 -42
- package/dist/polyfilled/asgardeo-spa.production.js.map +1 -1
- package/dist/polyfilled/asgardeo-spa.production.min.js +1 -1
- package/dist/polyfilled/asgardeo-spa.production.min.js.map +1 -1
- package/dist/src/client.d.ts +10 -6
- package/dist/src/client.d.ts.map +1 -1
- package/dist/src/client.js +26 -14
- package/dist/src/client.js.map +1 -1
- package/dist/src/clients/main-thread-client.d.ts.map +1 -1
- package/dist/src/clients/main-thread-client.js +61 -48
- package/dist/src/clients/main-thread-client.js.map +1 -1
- package/dist/src/clients/web-worker-client.d.ts +1 -1
- package/dist/src/clients/web-worker-client.d.ts.map +1 -1
- package/dist/src/clients/web-worker-client.js +51 -32
- package/dist/src/clients/web-worker-client.js.map +1 -1
- package/dist/src/{exception/exception.d.ts → constants/errors.d.ts} +3 -11
- package/dist/src/constants/errors.d.ts.map +1 -0
- package/dist/src/{exception/exception.js → constants/errors.js} +3 -14
- package/dist/src/constants/errors.js.map +1 -0
- package/dist/src/constants/hooks.d.ts +2 -1
- package/dist/src/constants/hooks.d.ts.map +1 -1
- package/dist/src/constants/hooks.js +1 -0
- package/dist/src/constants/hooks.js.map +1 -1
- package/dist/src/constants/index.d.ts +1 -0
- package/dist/src/constants/index.d.ts.map +1 -1
- package/dist/src/constants/index.js +1 -0
- package/dist/src/constants/index.js.map +1 -1
- package/dist/src/constants/parameters.d.ts +1 -0
- package/dist/src/constants/parameters.d.ts.map +1 -1
- package/dist/src/constants/parameters.js +1 -0
- package/dist/src/constants/parameters.js.map +1 -1
- package/dist/src/helpers/session-management-helper.d.ts +1 -1
- package/dist/src/helpers/session-management-helper.d.ts.map +1 -1
- package/dist/src/helpers/session-management-helper.js +33 -43
- package/dist/src/helpers/session-management-helper.js.map +1 -1
- package/dist/src/index-polyfill.d.ts +0 -1
- package/dist/src/index-polyfill.d.ts.map +1 -1
- package/dist/src/index-polyfill.js +0 -1
- package/dist/src/index-polyfill.js.map +1 -1
- package/dist/src/models/client.d.ts +4 -4
- package/dist/src/models/client.d.ts.map +1 -1
- package/dist/src/models/http-client.d.ts +1 -1
- package/dist/src/models/http-client.d.ts.map +1 -1
- package/dist/src/models/index.d.ts +1 -0
- package/dist/src/models/index.d.ts.map +1 -1
- package/dist/src/models/index.js +1 -0
- package/dist/src/models/index.js.map +1 -1
- package/dist/src/models/message.d.ts +1 -0
- package/dist/src/models/message.d.ts.map +1 -1
- package/dist/src/models/session-management-helper.d.ts +2 -1
- package/dist/src/models/session-management-helper.d.ts.map +1 -1
- package/dist/src/{exception/index.js → models/sign-out-error.d.ts} +6 -3
- package/dist/src/models/sign-out-error.d.ts.map +1 -0
- package/{src/exception/index.ts → dist/src/models/sign-out-error.js} +3 -3
- package/dist/src/models/sign-out-error.js.map +1 -0
- package/dist/src/models/web-worker.d.ts +3 -3
- package/dist/src/models/web-worker.d.ts.map +1 -1
- package/dist/src/models/web-worker.js.map +1 -1
- package/dist/src/public-api.d.ts +1 -1
- package/dist/src/public-api.d.ts.map +1 -1
- package/dist/src/public-api.js +1 -1
- package/dist/src/public-api.js.map +1 -1
- package/dist/src/utils/crypto-utils.d.ts +33 -0
- package/dist/src/utils/crypto-utils.d.ts.map +1 -0
- package/dist/src/utils/crypto-utils.js +55 -0
- package/dist/src/utils/crypto-utils.js.map +1 -0
- package/dist/src/utils/spa-utils.d.ts +5 -3
- package/dist/src/utils/spa-utils.d.ts.map +1 -1
- package/dist/src/utils/spa-utils.js +26 -10
- package/dist/src/utils/spa-utils.js.map +1 -1
- package/dist/src/worker/client.worker.d.ts.map +1 -1
- package/dist/src/worker/client.worker.js +5 -5
- package/dist/src/worker/client.worker.js.map +1 -1
- package/dist/src/worker/worker-core.d.ts.map +1 -1
- package/dist/src/worker/worker-core.js +57 -49
- package/dist/src/worker/worker-core.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +31 -30
- package/rollup.config.js +3 -5
- package/src/client.ts +45 -50
- package/src/clients/main-thread-client.ts +96 -89
- package/src/clients/web-worker-client.ts +73 -48
- package/src/constants/errors.ts +19 -0
- package/src/constants/hooks.ts +2 -1
- package/src/constants/index.ts +1 -0
- package/src/constants/parameters.ts +1 -0
- package/src/helpers/session-management-helper.ts +31 -40
- package/src/index-polyfill.ts +0 -1
- package/src/models/client.ts +5 -5
- package/src/models/http-client.ts +3 -2
- package/src/models/index.ts +1 -0
- package/src/models/message.ts +1 -0
- package/src/models/session-management-helper.ts +3 -2
- package/{dist/src/exception/index.d.ts → src/models/sign-out-error.ts} +6 -3
- package/src/models/web-worker.ts +8 -2
- package/src/public-api.ts +2 -1
- package/src/utils/crypto-utils.ts +74 -0
- package/src/utils/spa-utils.ts +33 -10
- package/src/worker/client.worker.ts +6 -11
- package/src/worker/worker-core.ts +98 -101
- package/dist/src/exception/exception.d.ts.map +0 -1
- package/dist/src/exception/exception.js.map +0 -1
- package/dist/src/exception/index.d.ts.map +0 -1
- package/dist/src/exception/index.js.map +0 -1
- package/src/exception/exception.ts +0 -44
|
@@ -18,15 +18,18 @@
|
|
|
18
18
|
|
|
19
19
|
import {
|
|
20
20
|
AUTHORIZATION_CODE,
|
|
21
|
+
AsgardeoAuthException,
|
|
21
22
|
AuthClientConfig,
|
|
23
|
+
AuthenticationUtils,
|
|
22
24
|
BasicUserInfo,
|
|
23
25
|
CustomGrantConfig,
|
|
24
26
|
DecodedIDTokenPayload,
|
|
27
|
+
FetchResponse,
|
|
25
28
|
GetAuthURLConfig,
|
|
26
29
|
OIDCEndpoints,
|
|
27
|
-
OIDCProviderMetaData,
|
|
28
30
|
ResponseMode,
|
|
29
|
-
SESSION_STATE
|
|
31
|
+
SESSION_STATE,
|
|
32
|
+
STATE
|
|
30
33
|
} from "@asgardeo/auth-js";
|
|
31
34
|
import WorkerFile from "web-worker:../worker/client.worker.ts";
|
|
32
35
|
import {
|
|
@@ -62,7 +65,6 @@ import {
|
|
|
62
65
|
START_AUTO_REFRESH_TOKEN,
|
|
63
66
|
UPDATE_CONFIG
|
|
64
67
|
} from "../constants";
|
|
65
|
-
import { AsgardeoSPAException } from "../exception";
|
|
66
68
|
import { SessionManagementHelper } from "../helpers";
|
|
67
69
|
import {
|
|
68
70
|
AuthorizationInfo,
|
|
@@ -79,7 +81,9 @@ import {
|
|
|
79
81
|
import { SPACustomGrantConfig } from "../models/request-custom-grant";
|
|
80
82
|
import { SPAUtils } from "../utils";
|
|
81
83
|
|
|
82
|
-
export const WebWorkerClient = (
|
|
84
|
+
export const WebWorkerClient = async (
|
|
85
|
+
config: AuthClientConfig<WebWorkerClientConfig>
|
|
86
|
+
): Promise<WebWorkerClientInterface> => {
|
|
83
87
|
/**
|
|
84
88
|
* HttpClient handlers
|
|
85
89
|
*/
|
|
@@ -91,7 +95,7 @@ export const WebWorkerClient = (config: AuthClientConfig<WebWorkerClientConfig>)
|
|
|
91
95
|
let _isHttpHandlerEnabled: boolean = true;
|
|
92
96
|
let _getSignOutURLFromSessionStorage: boolean = false;
|
|
93
97
|
|
|
94
|
-
const _sessionManagementHelper = SessionManagementHelper(
|
|
98
|
+
const _sessionManagementHelper = await SessionManagementHelper(
|
|
95
99
|
async () => {
|
|
96
100
|
const message: Message<string> = {
|
|
97
101
|
type: SIGN_OUT
|
|
@@ -114,24 +118,22 @@ export const WebWorkerClient = (config: AuthClientConfig<WebWorkerClientConfig>)
|
|
|
114
118
|
const communicate = <T, R>(message: Message<T>): Promise<R> => {
|
|
115
119
|
const channel = new MessageChannel();
|
|
116
120
|
|
|
117
|
-
worker.postMessage(message, [channel.port2]);
|
|
121
|
+
worker.postMessage(message, [ channel.port2 ]);
|
|
118
122
|
|
|
119
123
|
return new Promise((resolve, reject) => {
|
|
120
124
|
const timer = setTimeout(() => {
|
|
121
125
|
reject(
|
|
122
|
-
new
|
|
123
|
-
"WEB_WORKER_CLIENT-COM-
|
|
124
|
-
"web-worker-client",
|
|
125
|
-
"communicate",
|
|
126
|
+
new AsgardeoAuthException(
|
|
127
|
+
"SPA-WEB_WORKER_CLIENT-COM-TO01",
|
|
126
128
|
"Operation timed out.",
|
|
127
129
|
"No response was received from the web worker for " +
|
|
128
|
-
|
|
129
|
-
|
|
130
|
+
_requestTimeout / 1000 +
|
|
131
|
+
" since dispatching the request"
|
|
130
132
|
)
|
|
131
133
|
);
|
|
132
134
|
}, _requestTimeout);
|
|
133
135
|
|
|
134
|
-
return (channel.port1.onmessage = ({ data }: { data: ResponseMessage<string
|
|
136
|
+
return (channel.port1.onmessage = ({ data }: { data: ResponseMessage<string>; }) => {
|
|
135
137
|
clearTimeout(timer);
|
|
136
138
|
|
|
137
139
|
if (data?.success) {
|
|
@@ -156,13 +158,13 @@ export const WebWorkerClient = (config: AuthClientConfig<WebWorkerClientConfig>)
|
|
|
156
158
|
* @returns {Promise<HttpResponse|boolean>} A promise that resolves with a boolean value or the request
|
|
157
159
|
* response if the the `returnResponse` attribute in the `requestParams` object is set to `true`.
|
|
158
160
|
*/
|
|
159
|
-
const requestCustomGrant = (requestParams: SPACustomGrantConfig): Promise<
|
|
161
|
+
const requestCustomGrant = (requestParams: SPACustomGrantConfig): Promise<FetchResponse | BasicUserInfo> => {
|
|
160
162
|
const message: Message<CustomGrantConfig> = {
|
|
161
163
|
data: requestParams,
|
|
162
164
|
type: REQUEST_CUSTOM_GRANT
|
|
163
165
|
};
|
|
164
166
|
|
|
165
|
-
return communicate<CustomGrantConfig,
|
|
167
|
+
return communicate<CustomGrantConfig, FetchResponse | BasicUserInfo>(message)
|
|
166
168
|
.then((response) => {
|
|
167
169
|
if (requestParams.preventSignOutURLUpdate) {
|
|
168
170
|
_getSignOutURLFromSessionStorage = true;
|
|
@@ -345,8 +347,7 @@ export const WebWorkerClient = (config: AuthClientConfig<WebWorkerClientConfig>)
|
|
|
345
347
|
config.checkSessionInterval ?? 3,
|
|
346
348
|
config.sessionRefreshInterval ?? 300,
|
|
347
349
|
config.signInRedirectURL,
|
|
348
|
-
|
|
349
|
-
config.enablePKCE
|
|
350
|
+
async (params?: GetAuthURLConfig): Promise<string> => (await getAuthorizationURL(params)).authorizationURL
|
|
350
351
|
);
|
|
351
352
|
};
|
|
352
353
|
|
|
@@ -393,7 +394,11 @@ export const WebWorkerClient = (config: AuthClientConfig<WebWorkerClientConfig>)
|
|
|
393
394
|
try {
|
|
394
395
|
const response: AuthorizationResponse = await communicate<GetAuthURLConfig, AuthorizationResponse>(message);
|
|
395
396
|
|
|
396
|
-
|
|
397
|
+
const pkceKey: string = AuthenticationUtils.extractPKCEKeyFromStateParam(
|
|
398
|
+
new URL(response.authorizationURL).searchParams.get(STATE) ?? ""
|
|
399
|
+
);
|
|
400
|
+
|
|
401
|
+
response.pkce && config.enablePKCE && SPAUtils.setPKCE(pkceKey, response.pkce);
|
|
397
402
|
|
|
398
403
|
const urlString: string = response.authorizationURL;
|
|
399
404
|
|
|
@@ -422,7 +427,7 @@ export const WebWorkerClient = (config: AuthClientConfig<WebWorkerClientConfig>)
|
|
|
422
427
|
}
|
|
423
428
|
|
|
424
429
|
if (data?.type == CHECK_SESSION_SIGNED_IN && data?.data?.code) {
|
|
425
|
-
requestAccessToken(data?.data?.code, data?.data?.sessionState)
|
|
430
|
+
requestAccessToken(data?.data?.code, data?.data?.sessionState, data?.data?.state)
|
|
426
431
|
.then((response: BasicUserInfo) => {
|
|
427
432
|
window.removeEventListener("message", listenToPromptNoneIFrame);
|
|
428
433
|
resolve(response);
|
|
@@ -441,22 +446,54 @@ export const WebWorkerClient = (config: AuthClientConfig<WebWorkerClientConfig>)
|
|
|
441
446
|
});
|
|
442
447
|
};
|
|
443
448
|
|
|
449
|
+
/**
|
|
450
|
+
* Generates an authorization URL.
|
|
451
|
+
*
|
|
452
|
+
* @param {GetAuthURLConfig} params Authorization URL params.
|
|
453
|
+
* @returns {Promise<string>} Authorization URL.
|
|
454
|
+
*/
|
|
455
|
+
const getAuthorizationURL = async (params?: GetAuthURLConfig): Promise<AuthorizationResponse> => {
|
|
456
|
+
const config: AuthClientConfig<WebWorkerClientConfig> = await getConfigData();
|
|
457
|
+
|
|
458
|
+
const message: Message<GetAuthURLConfig> = {
|
|
459
|
+
data: params,
|
|
460
|
+
type: GET_AUTH_URL
|
|
461
|
+
};
|
|
462
|
+
|
|
463
|
+
return communicate<GetAuthURLConfig, AuthorizationResponse>(message).then(
|
|
464
|
+
async (response: AuthorizationResponse) => {
|
|
465
|
+
if (response.pkce && config.enablePKCE) {
|
|
466
|
+
const pkceKey: string = AuthenticationUtils.extractPKCEKeyFromStateParam(
|
|
467
|
+
new URL(response.authorizationURL).searchParams.get(STATE) ?? ""
|
|
468
|
+
);
|
|
469
|
+
|
|
470
|
+
SPAUtils.setPKCE(pkceKey, response.pkce);
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
return Promise.resolve(response);
|
|
474
|
+
}
|
|
475
|
+
);
|
|
476
|
+
};
|
|
477
|
+
|
|
444
478
|
const requestAccessToken = async (
|
|
445
479
|
resolvedAuthorizationCode: string,
|
|
446
|
-
resolvedSessionState: string
|
|
480
|
+
resolvedSessionState: string,
|
|
481
|
+
resolvedState: string
|
|
447
482
|
): Promise<BasicUserInfo> => {
|
|
448
483
|
const config: AuthClientConfig<WebWorkerClientConfig> = await getConfigData();
|
|
484
|
+
const pkceKey: string = AuthenticationUtils.extractPKCEKeyFromStateParam(resolvedState);
|
|
449
485
|
|
|
450
486
|
const message: Message<AuthorizationInfo> = {
|
|
451
487
|
data: {
|
|
452
488
|
code: resolvedAuthorizationCode,
|
|
453
|
-
pkce: config.enablePKCE ? SPAUtils.getPKCE() : undefined,
|
|
454
|
-
sessionState: resolvedSessionState
|
|
489
|
+
pkce: config.enablePKCE ? SPAUtils.getPKCE(pkceKey) : undefined,
|
|
490
|
+
sessionState: resolvedSessionState,
|
|
491
|
+
state: resolvedState
|
|
455
492
|
},
|
|
456
493
|
type: REQUEST_ACCESS_TOKEN
|
|
457
494
|
};
|
|
458
495
|
|
|
459
|
-
config.enablePKCE && SPAUtils.removePKCE();
|
|
496
|
+
config.enablePKCE && SPAUtils.removePKCE(pkceKey);
|
|
460
497
|
|
|
461
498
|
return communicate<AuthorizationInfo, BasicUserInfo>(message)
|
|
462
499
|
.then((response) => {
|
|
@@ -494,7 +531,8 @@ export const WebWorkerClient = (config: AuthClientConfig<WebWorkerClientConfig>)
|
|
|
494
531
|
const signIn = async (
|
|
495
532
|
params?: GetAuthURLConfig,
|
|
496
533
|
authorizationCode?: string,
|
|
497
|
-
sessionState?: string
|
|
534
|
+
sessionState?: string,
|
|
535
|
+
state?: string
|
|
498
536
|
): Promise<BasicUserInfo> => {
|
|
499
537
|
const config: AuthClientConfig<WebWorkerClientConfig> = await getConfigData();
|
|
500
538
|
|
|
@@ -526,15 +564,7 @@ export const WebWorkerClient = (config: AuthClientConfig<WebWorkerClientConfig>)
|
|
|
526
564
|
|
|
527
565
|
history.pushState(null, document.title, url.toString());
|
|
528
566
|
|
|
529
|
-
|
|
530
|
-
new AsgardeoSPAException(
|
|
531
|
-
"WEB_WORKER_CLIENT-SI-BE",
|
|
532
|
-
"web-worker-client",
|
|
533
|
-
"signIn",
|
|
534
|
-
error,
|
|
535
|
-
errorDescription ?? ""
|
|
536
|
-
)
|
|
537
|
-
);
|
|
567
|
+
throw new AsgardeoAuthException("SPA-WEB_WORKER_CLIENT-SI-SE01", error, errorDescription ?? "");
|
|
538
568
|
}
|
|
539
569
|
|
|
540
570
|
if (await isAuthenticated()) {
|
|
@@ -550,31 +580,26 @@ export const WebWorkerClient = (config: AuthClientConfig<WebWorkerClientConfig>)
|
|
|
550
580
|
|
|
551
581
|
let resolvedAuthorizationCode: string;
|
|
552
582
|
let resolvedSessionState: string;
|
|
583
|
+
let resolvedState: string;
|
|
553
584
|
|
|
554
585
|
if (config?.responseMode === ResponseMode.formPost && authorizationCode) {
|
|
555
586
|
resolvedAuthorizationCode = authorizationCode;
|
|
556
587
|
resolvedSessionState = sessionState ?? "";
|
|
588
|
+
resolvedState = state ?? "";
|
|
557
589
|
} else {
|
|
558
590
|
resolvedAuthorizationCode = new URL(window.location.href).searchParams.get(AUTHORIZATION_CODE) ?? "";
|
|
559
591
|
resolvedSessionState = new URL(window.location.href).searchParams.get(SESSION_STATE) ?? "";
|
|
592
|
+
resolvedState = new URL(window.location.href).searchParams.get(STATE) ?? "";
|
|
593
|
+
|
|
560
594
|
SPAUtils.removeAuthorizationCode();
|
|
561
595
|
}
|
|
562
596
|
|
|
563
|
-
if (resolvedAuthorizationCode) {
|
|
564
|
-
return requestAccessToken(resolvedAuthorizationCode, resolvedSessionState);
|
|
597
|
+
if (resolvedAuthorizationCode && resolvedState) {
|
|
598
|
+
return requestAccessToken(resolvedAuthorizationCode, resolvedSessionState, resolvedState);
|
|
565
599
|
}
|
|
566
600
|
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
type: GET_AUTH_URL
|
|
570
|
-
};
|
|
571
|
-
|
|
572
|
-
return communicate<GetAuthURLConfig, AuthorizationResponse>(message)
|
|
573
|
-
.then(async (response) => {
|
|
574
|
-
if (response.pkce && config.enablePKCE) {
|
|
575
|
-
SPAUtils.setPKCE(response.pkce);
|
|
576
|
-
}
|
|
577
|
-
|
|
601
|
+
return getAuthorizationURL(params)
|
|
602
|
+
.then(async (response: AuthorizationResponse) => {
|
|
578
603
|
location.href = response.authorizationURL;
|
|
579
604
|
|
|
580
605
|
await SPAUtils.waitTillPageRedirect();
|
|
@@ -651,12 +676,12 @@ export const WebWorkerClient = (config: AuthClientConfig<WebWorkerClientConfig>)
|
|
|
651
676
|
});
|
|
652
677
|
};
|
|
653
678
|
|
|
654
|
-
const getOIDCServiceEndpoints = (): Promise<
|
|
679
|
+
const getOIDCServiceEndpoints = (): Promise<OIDCEndpoints> => {
|
|
655
680
|
const message: Message<null> = {
|
|
656
681
|
type: GET_OIDC_SERVICE_ENDPOINTS
|
|
657
682
|
};
|
|
658
683
|
|
|
659
|
-
return communicate<null,
|
|
684
|
+
return communicate<null, OIDCEndpoints>(message)
|
|
660
685
|
.then((response) => {
|
|
661
686
|
return Promise.resolve(response);
|
|
662
687
|
})
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2021, WSO2 Inc. (http://www.wso2.com) All Rights Reserved.
|
|
3
|
+
*
|
|
4
|
+
* WSO2 Inc. licenses this file to you under the Apache License,
|
|
5
|
+
* Version 2.0 (the "License"); you may not use this file except
|
|
6
|
+
* in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing,
|
|
12
|
+
* software distributed under the License is distributed on an
|
|
13
|
+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
14
|
+
* KIND, either express or implied. See the License for the
|
|
15
|
+
* specific language governing permissions and limitations
|
|
16
|
+
* under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
export const ACCESS_TOKEN_INVALID = "Access token is invalid";
|
package/src/constants/hooks.ts
CHANGED
|
@@ -25,5 +25,6 @@ export enum Hooks {
|
|
|
25
25
|
HttpRequestError = "http-request-error",
|
|
26
26
|
HttpRequestSuccess = "http-request-success",
|
|
27
27
|
RevokeAccessToken = "revoke-access-token",
|
|
28
|
-
CustomGrant = "custom-grant"
|
|
28
|
+
CustomGrant = "custom-grant",
|
|
29
|
+
SignOutFailed = "sign-out-failed"
|
|
29
30
|
}
|
package/src/constants/index.ts
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
* under the License.
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
-
import { SESSION_STATE } from "@asgardeo/auth-js";
|
|
19
|
+
import { GetAuthURLConfig, SESSION_STATE } from "@asgardeo/auth-js";
|
|
20
20
|
import {
|
|
21
21
|
CHECK_SESSION_SIGNED_IN,
|
|
22
22
|
CHECK_SESSION_SIGNED_OUT,
|
|
@@ -27,6 +27,7 @@ import {
|
|
|
27
27
|
SET_SESSION_STATE_FROM_IFRAME,
|
|
28
28
|
SILENT_SIGN_IN_STATE,
|
|
29
29
|
STATE,
|
|
30
|
+
STATE_QUERY,
|
|
30
31
|
Storage
|
|
31
32
|
} from "../constants";
|
|
32
33
|
import { AuthorizationInfo, Message, SessionManagementHelperInterface } from "../models";
|
|
@@ -38,14 +39,13 @@ export const SessionManagementHelper = (() => {
|
|
|
38
39
|
let _sessionState: () => Promise<string>;
|
|
39
40
|
let _interval: number;
|
|
40
41
|
let _redirectURL: string;
|
|
41
|
-
let _authorizationEndpoint: string;
|
|
42
42
|
let _sessionRefreshInterval: number;
|
|
43
43
|
let _signOut: () => Promise<string>;
|
|
44
44
|
let _sessionRefreshIntervalTimeout: number;
|
|
45
45
|
let _checkSessionIntervalTimeout: number;
|
|
46
46
|
let _storage: Storage;
|
|
47
47
|
let _setSessionState: (sessionState: string) => void;
|
|
48
|
-
let
|
|
48
|
+
let _getAuthorizationURL: (params?: GetAuthURLConfig) => Promise<string>;
|
|
49
49
|
|
|
50
50
|
const initialize = (
|
|
51
51
|
clientID: string,
|
|
@@ -54,17 +54,15 @@ export const SessionManagementHelper = (() => {
|
|
|
54
54
|
interval: number,
|
|
55
55
|
sessionRefreshInterval: number,
|
|
56
56
|
redirectURL: string,
|
|
57
|
-
|
|
58
|
-
isPKCEEnabled: boolean
|
|
57
|
+
getAuthorizationURL: (params?: GetAuthURLConfig) => Promise<string>
|
|
59
58
|
): void => {
|
|
60
59
|
_clientID = clientID;
|
|
61
60
|
_checkSessionEndpoint = checkSessionEndpoint;
|
|
62
61
|
_sessionState = getSessionState;
|
|
63
62
|
_interval = interval;
|
|
64
63
|
_redirectURL = redirectURL;
|
|
65
|
-
_authorizationEndpoint = authorizationEndpoint;
|
|
66
64
|
_sessionRefreshInterval = sessionRefreshInterval;
|
|
67
|
-
|
|
65
|
+
_getAuthorizationURL = getAuthorizationURL;
|
|
68
66
|
|
|
69
67
|
if (_interval > -1) {
|
|
70
68
|
initiateCheckSession();
|
|
@@ -115,17 +113,6 @@ export const SessionManagementHelper = (() => {
|
|
|
115
113
|
clearInterval(_sessionRefreshIntervalTimeout);
|
|
116
114
|
}
|
|
117
115
|
|
|
118
|
-
const getRandomPKCEChallenge = (): string => {
|
|
119
|
-
const chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz-_";
|
|
120
|
-
const stringLength = 43;
|
|
121
|
-
let randomString = "";
|
|
122
|
-
for (let i = 0; i < stringLength; i++) {
|
|
123
|
-
const rnum = Math.floor(Math.random() * chars.length);
|
|
124
|
-
randomString += chars.substring(rnum, rnum + 1);
|
|
125
|
-
}
|
|
126
|
-
return randomString;
|
|
127
|
-
};
|
|
128
|
-
|
|
129
116
|
const listenToResponseFromOPIFrame = (): void => {
|
|
130
117
|
async function receiveMessage(e: MessageEvent) {
|
|
131
118
|
const targetOrigin = _checkSessionEndpoint;
|
|
@@ -149,7 +136,7 @@ export const SessionManagementHelper = (() => {
|
|
|
149
136
|
window?.addEventListener("message", receiveMessage, false);
|
|
150
137
|
};
|
|
151
138
|
|
|
152
|
-
const sendPromptNoneRequest = () => {
|
|
139
|
+
const sendPromptNoneRequest = async () => {
|
|
153
140
|
const rpIFrame = document.getElementById(RP_IFRAME) as HTMLIFrameElement;
|
|
154
141
|
|
|
155
142
|
const promptNoneIFrame: HTMLIFrameElement = rpIFrame?.contentDocument?.getElementById(
|
|
@@ -170,20 +157,12 @@ export const SessionManagementHelper = (() => {
|
|
|
170
157
|
window?.addEventListener("message", receiveMessageListener);
|
|
171
158
|
}
|
|
172
159
|
|
|
173
|
-
const promptNoneURL =
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
promptNoneURL.searchParams.set("redirect_uri", _redirectURL);
|
|
178
|
-
promptNoneURL.searchParams.set("state", STATE);
|
|
179
|
-
promptNoneURL.searchParams.set("prompt", "none");
|
|
180
|
-
|
|
181
|
-
if(_isPKCEEnabled){
|
|
182
|
-
promptNoneURL.searchParams.set("code_challenge_method", "S256");
|
|
183
|
-
promptNoneURL.searchParams.set("code_challenge", getRandomPKCEChallenge());
|
|
184
|
-
}
|
|
160
|
+
const promptNoneURL: string = await _getAuthorizationURL({
|
|
161
|
+
prompt: "none",
|
|
162
|
+
state: STATE
|
|
163
|
+
});
|
|
185
164
|
|
|
186
|
-
promptNoneIFrame.src = promptNoneURL
|
|
165
|
+
promptNoneIFrame.src = promptNoneURL;
|
|
187
166
|
}
|
|
188
167
|
};
|
|
189
168
|
|
|
@@ -196,20 +175,21 @@ export const SessionManagementHelper = (() => {
|
|
|
196
175
|
const receivePromptNoneResponse = async (
|
|
197
176
|
setSessionState?: (sessionState: string | null) => Promise<void>
|
|
198
177
|
): Promise<boolean> => {
|
|
199
|
-
const state = new URL(window.location.href).searchParams.get(
|
|
178
|
+
const state = new URL(window.location.href).searchParams.get(STATE_QUERY);
|
|
200
179
|
const sessionState = new URL(window.location.href).searchParams.get(SESSION_STATE);
|
|
201
180
|
const parent = window.parent.parent;
|
|
202
181
|
|
|
203
|
-
if (state !== null && (state
|
|
182
|
+
if (state !== null && (state.includes(STATE) || state.includes(SILENT_SIGN_IN_STATE))) {
|
|
204
183
|
// Prompt none response.
|
|
205
184
|
const code = new URL(window.location.href).searchParams.get("code");
|
|
206
185
|
|
|
207
186
|
if (code !== null && code.length !== 0) {
|
|
208
|
-
if (state
|
|
187
|
+
if (state.includes(SILENT_SIGN_IN_STATE)) {
|
|
209
188
|
const message: Message<AuthorizationInfo> = {
|
|
210
189
|
data: {
|
|
211
190
|
code,
|
|
212
|
-
sessionState: sessionState ?? ""
|
|
191
|
+
sessionState: sessionState ?? "",
|
|
192
|
+
state
|
|
213
193
|
},
|
|
214
194
|
type: CHECK_SESSION_SIGNED_IN
|
|
215
195
|
};
|
|
@@ -228,7 +208,7 @@ export const SessionManagementHelper = (() => {
|
|
|
228
208
|
const newSessionState = new URL(window.location.href).searchParams.get("session_state");
|
|
229
209
|
|
|
230
210
|
if (_storage === Storage.LocalStorage || _storage === Storage.SessionStorage) {
|
|
231
|
-
setSessionState && await setSessionState(newSessionState);
|
|
211
|
+
setSessionState && (await setSessionState(newSessionState));
|
|
232
212
|
} else {
|
|
233
213
|
const message: Message<string> = {
|
|
234
214
|
data: newSessionState ?? "",
|
|
@@ -246,7 +226,7 @@ export const SessionManagementHelper = (() => {
|
|
|
246
226
|
|
|
247
227
|
return true;
|
|
248
228
|
} else {
|
|
249
|
-
if (state
|
|
229
|
+
if (state.includes(SILENT_SIGN_IN_STATE)) {
|
|
250
230
|
const message: Message<null> = {
|
|
251
231
|
type: CHECK_SESSION_SIGNED_OUT
|
|
252
232
|
};
|
|
@@ -275,15 +255,16 @@ export const SessionManagementHelper = (() => {
|
|
|
275
255
|
return false;
|
|
276
256
|
};
|
|
277
257
|
|
|
278
|
-
return (
|
|
258
|
+
return async (
|
|
279
259
|
signOut: () => Promise<string>,
|
|
280
260
|
storage: Storage,
|
|
281
261
|
setSessionState: (sessionState: string) => void
|
|
282
|
-
): SessionManagementHelperInterface => {
|
|
262
|
+
): Promise<SessionManagementHelperInterface> => {
|
|
283
263
|
let rpIFrame = document.createElement("iframe");
|
|
284
264
|
rpIFrame.setAttribute("id", RP_IFRAME);
|
|
285
265
|
rpIFrame.style.display = "none";
|
|
286
266
|
|
|
267
|
+
let rpIframeLoaded: boolean = false;
|
|
287
268
|
rpIFrame.onload = () => {
|
|
288
269
|
rpIFrame = document.getElementById(RP_IFRAME) as HTMLIFrameElement;
|
|
289
270
|
|
|
@@ -303,6 +284,8 @@ export const SessionManagementHelper = (() => {
|
|
|
303
284
|
|
|
304
285
|
opIFrame && rpIFrame?.contentDocument?.body?.appendChild(opIFrame);
|
|
305
286
|
promptNoneIFrame && rpIFrame?.contentDocument?.body?.appendChild(promptNoneIFrame);
|
|
287
|
+
|
|
288
|
+
rpIframeLoaded = true;
|
|
306
289
|
}
|
|
307
290
|
|
|
308
291
|
document?.body?.appendChild(rpIFrame);
|
|
@@ -312,6 +295,14 @@ export const SessionManagementHelper = (() => {
|
|
|
312
295
|
_storage = storage;
|
|
313
296
|
_setSessionState = setSessionState;
|
|
314
297
|
|
|
298
|
+
const sleep = (): Promise<any> => {
|
|
299
|
+
return new Promise((resolve) => setTimeout(resolve, 1));
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
while (rpIframeLoaded === false) {
|
|
303
|
+
await sleep();
|
|
304
|
+
}
|
|
305
|
+
|
|
315
306
|
return {
|
|
316
307
|
initialize,
|
|
317
308
|
receivePromptNoneResponse,
|
package/src/index-polyfill.ts
CHANGED
package/src/models/client.ts
CHANGED
|
@@ -21,8 +21,8 @@ import {
|
|
|
21
21
|
BasicUserInfo,
|
|
22
22
|
CustomGrantConfig,
|
|
23
23
|
DecodedIDTokenPayload,
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
FetchResponse,
|
|
25
|
+
OIDCEndpoints
|
|
26
26
|
} from "@asgardeo/auth-js";
|
|
27
27
|
import {
|
|
28
28
|
HttpError,
|
|
@@ -51,7 +51,7 @@ export interface MainThreadClientInterface {
|
|
|
51
51
|
signInRedirectURL?: string
|
|
52
52
|
): Promise<BasicUserInfo>;
|
|
53
53
|
signOut(signOutRedirectURL?: string): Promise<boolean>;
|
|
54
|
-
requestCustomGrant(config: CustomGrantConfig): Promise<BasicUserInfo |
|
|
54
|
+
requestCustomGrant(config: CustomGrantConfig): Promise<BasicUserInfo | FetchResponse>;
|
|
55
55
|
refreshAccessToken(): Promise<BasicUserInfo>;
|
|
56
56
|
revokeAccessToken(): Promise<boolean>;
|
|
57
57
|
getBasicUserInfo(): Promise<BasicUserInfo>;
|
|
@@ -65,7 +65,7 @@ export interface MainThreadClientInterface {
|
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
export interface WebWorkerClientInterface {
|
|
68
|
-
requestCustomGrant(requestParams: CustomGrantConfig): Promise<
|
|
68
|
+
requestCustomGrant(requestParams: CustomGrantConfig): Promise<FetchResponse | BasicUserInfo>;
|
|
69
69
|
httpRequest<T = any>(config: HttpRequestConfig): Promise<HttpResponse<T>>;
|
|
70
70
|
httpRequestAll<T = any>(configs: HttpRequestConfig[]): Promise<HttpResponse<T>[]>;
|
|
71
71
|
enableHttpHandler(): Promise<boolean>;
|
|
@@ -79,7 +79,7 @@ export interface WebWorkerClientInterface {
|
|
|
79
79
|
): Promise<BasicUserInfo>;
|
|
80
80
|
signOut(signOutRedirectURL?: string): Promise<boolean>;
|
|
81
81
|
revokeAccessToken(): Promise<boolean>;
|
|
82
|
-
getOIDCServiceEndpoints(): Promise<
|
|
82
|
+
getOIDCServiceEndpoints(): Promise<OIDCEndpoints>;
|
|
83
83
|
getBasicUserInfo(): Promise<BasicUserInfo>;
|
|
84
84
|
getDecodedIDToken(): Promise<DecodedIDTokenPayload>;
|
|
85
85
|
getIDToken(): Promise<string>;
|
|
@@ -33,8 +33,9 @@ export interface HttpRequestConfig extends AxiosRequestConfig {
|
|
|
33
33
|
|
|
34
34
|
export {
|
|
35
35
|
AxiosResponse as HttpResponse,
|
|
36
|
-
Method,
|
|
37
|
-
|
|
36
|
+
Method as HttpMethod,
|
|
37
|
+
AxiosRequestTransformer as HttpRequestTransformer,
|
|
38
|
+
AxiosResponseTransformer as HttpResponseTransformer,
|
|
38
39
|
AxiosAdapter as HttpAdapter,
|
|
39
40
|
AxiosBasicCredentials as HttpBasicCredentials,
|
|
40
41
|
ResponseType,
|
package/src/models/index.ts
CHANGED
package/src/models/message.ts
CHANGED
|
@@ -16,6 +16,8 @@
|
|
|
16
16
|
* under the License.
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
+
import { GetAuthURLConfig } from "..";
|
|
20
|
+
|
|
19
21
|
export interface SessionManagementHelperInterface {
|
|
20
22
|
initialize(
|
|
21
23
|
clientID: string,
|
|
@@ -24,8 +26,7 @@ export interface SessionManagementHelperInterface {
|
|
|
24
26
|
interval: number,
|
|
25
27
|
sessionRefreshInterval: number,
|
|
26
28
|
redirectURL: string,
|
|
27
|
-
|
|
28
|
-
isPKCEEnabled?: boolean
|
|
29
|
+
getAuthorizationURL: (params?: GetAuthURLConfig) => Promise<string>
|
|
29
30
|
): void;
|
|
30
31
|
receivePromptNoneResponse(
|
|
31
32
|
setSessionState?: (sessionState: string | null) => Promise<void>
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright (c)
|
|
2
|
+
* Copyright (c) 2022, WSO2 Inc. (http://www.wso2.com) All Rights Reserved.
|
|
3
3
|
*
|
|
4
4
|
* WSO2 Inc. licenses this file to you under the Apache License,
|
|
5
5
|
* Version 2.0 (the "License"); you may not use this file except
|
|
@@ -15,5 +15,8 @@
|
|
|
15
15
|
* specific language governing permissions and limitations
|
|
16
16
|
* under the License.
|
|
17
17
|
*/
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
|
|
19
|
+
export interface SignOutError {
|
|
20
|
+
error: string;
|
|
21
|
+
description: string;
|
|
22
|
+
}
|
package/src/models/web-worker.ts
CHANGED
|
@@ -22,6 +22,7 @@ import {
|
|
|
22
22
|
BasicUserInfo,
|
|
23
23
|
CustomGrantConfig,
|
|
24
24
|
DecodedIDTokenPayload,
|
|
25
|
+
FetchResponse,
|
|
25
26
|
OIDCEndpoints
|
|
26
27
|
} from "@asgardeo/auth-js";
|
|
27
28
|
import { HttpRequestConfig, HttpResponse, Message } from ".";
|
|
@@ -44,10 +45,15 @@ export interface WebWorkerCoreInterface {
|
|
|
44
45
|
enableHttpHandler(): void;
|
|
45
46
|
disableHttpHandler(): void;
|
|
46
47
|
getAuthorizationURL(params?: AuthorizationURLParams, signInRedirectURL?: string): Promise<AuthorizationResponse>;
|
|
47
|
-
requestAccessToken(
|
|
48
|
+
requestAccessToken(
|
|
49
|
+
authorizationCode?: string,
|
|
50
|
+
sessionState?: string,
|
|
51
|
+
pkce?: string,
|
|
52
|
+
state?: string
|
|
53
|
+
): Promise<BasicUserInfo>;
|
|
48
54
|
signOut(signOutRedirectURL?: string): Promise<string>;
|
|
49
55
|
getSignOutURL(signOutRedirectURL?: string): Promise<string>;
|
|
50
|
-
requestCustomGrant(config: CustomGrantConfig): Promise<BasicUserInfo |
|
|
56
|
+
requestCustomGrant(config: CustomGrantConfig): Promise<BasicUserInfo | FetchResponse>;
|
|
51
57
|
refreshAccessToken(): Promise<BasicUserInfo>;
|
|
52
58
|
revokeAccessToken(): Promise<boolean>;
|
|
53
59
|
getBasicUserInfo(): Promise<BasicUserInfo>;
|
package/src/public-api.ts
CHANGED