@axa-fr/react-oidc 6.6.7 → 6.6.9-alpha0

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.
Files changed (68) hide show
  1. package/dist/FetchToken.d.ts.map +1 -1
  2. package/dist/FetchToken.js +4 -9
  3. package/dist/FetchToken.js.map +1 -1
  4. package/dist/OidcProvider.d.ts.map +1 -1
  5. package/dist/OidcProvider.js +19 -19
  6. package/dist/OidcProvider.js.map +1 -1
  7. package/dist/OidcSecure.d.ts +1 -2
  8. package/dist/OidcSecure.d.ts.map +1 -1
  9. package/dist/OidcSecure.js +4 -7
  10. package/dist/OidcSecure.js.map +1 -1
  11. package/dist/OidcServiceWorker.js +40 -11
  12. package/dist/ReactOidc.d.ts +3 -3
  13. package/dist/ReactOidc.d.ts.map +1 -1
  14. package/dist/ReactOidc.js +19 -17
  15. package/dist/ReactOidc.js.map +1 -1
  16. package/dist/User.d.ts.map +1 -1
  17. package/dist/User.js +2 -5
  18. package/dist/User.js.map +1 -1
  19. package/dist/core/default-component/Callback.component.js +3 -3
  20. package/dist/core/default-component/Callback.component.js.map +1 -1
  21. package/dist/core/default-component/SilentCallback.component.js +3 -6
  22. package/dist/core/default-component/SilentCallback.component.js.map +1 -1
  23. package/dist/core/default-component/SilentLogin.component.js +2 -5
  24. package/dist/core/default-component/SilentLogin.component.js.map +1 -1
  25. package/dist/vanilla/index.d.ts +1 -1
  26. package/dist/vanilla/index.d.ts.map +1 -1
  27. package/dist/vanilla/index.js +3 -3
  28. package/dist/vanilla/index.js.map +1 -1
  29. package/dist/vanilla/initWorker.d.ts +1 -1
  30. package/dist/vanilla/initWorker.d.ts.map +1 -1
  31. package/dist/vanilla/initWorker.js +4 -0
  32. package/dist/vanilla/initWorker.js.map +1 -1
  33. package/dist/vanilla/noHashQueryStringUtils.d.ts.map +1 -1
  34. package/dist/vanilla/noHashQueryStringUtils.js +1 -2
  35. package/dist/vanilla/noHashQueryStringUtils.js.map +1 -1
  36. package/dist/vanilla/oidc.d.ts +16 -12
  37. package/dist/vanilla/oidc.d.ts.map +1 -1
  38. package/dist/vanilla/oidc.js +83 -38
  39. package/dist/vanilla/oidc.js.map +1 -1
  40. package/dist/vanilla/parseTokens.d.ts +11 -2
  41. package/dist/vanilla/parseTokens.d.ts.map +1 -1
  42. package/dist/vanilla/parseTokens.js +2 -2
  43. package/dist/vanilla/parseTokens.js.map +1 -1
  44. package/dist/vanilla/timer.js +3 -3
  45. package/dist/vanilla/timer.js.map +1 -1
  46. package/dist/vanilla/vanillaOidc.d.ts +56 -0
  47. package/dist/vanilla/vanillaOidc.d.ts.map +1 -0
  48. package/dist/vanilla/vanillaOidc.js +84 -0
  49. package/dist/vanilla/vanillaOidc.js.map +1 -0
  50. package/package.json +1 -1
  51. package/src/Home.tsx +0 -1
  52. package/src/configurations.ts +3 -3
  53. package/src/oidc/FetchToken.tsx +6 -11
  54. package/src/oidc/OidcProvider.tsx +21 -20
  55. package/src/oidc/OidcSecure.tsx +5 -6
  56. package/src/oidc/ReactOidc.tsx +20 -14
  57. package/src/oidc/User.ts +2 -4
  58. package/src/oidc/core/default-component/Callback.component.tsx +3 -3
  59. package/src/oidc/core/default-component/SilentCallback.component.tsx +3 -3
  60. package/src/oidc/core/default-component/SilentLogin.component.tsx +2 -2
  61. package/src/oidc/vanilla/OidcServiceWorker.js +40 -11
  62. package/src/oidc/vanilla/index.ts +1 -1
  63. package/src/oidc/vanilla/initWorker.ts +5 -0
  64. package/src/oidc/vanilla/noHashQueryStringUtils.ts +1 -2
  65. package/src/oidc/vanilla/oidc.ts +108 -65
  66. package/src/oidc/vanilla/parseTokens.ts +13 -4
  67. package/src/oidc/vanilla/timer.ts +3 -3
  68. package/src/oidc/vanilla/vanillaOidc.ts +74 -0
@@ -19,10 +19,44 @@ import timer from './timer';
19
19
  import {CheckSessionIFrame} from "./checkSessionIFrame"
20
20
  import {getParseQueryStringFromLocation} from "./route-utils";
21
21
  import {AuthorizationServiceConfigurationJson} from "@openid/appauth/src/authorization_service_configuration";
22
- import {computeTimeLeft, isTokensOidcValid, isTokensValid, parseOriginalTokens, setTokens} from "./parseTokens";
22
+ import {computeTimeLeft, isTokensOidcValid, isTokensValid, parseOriginalTokens, setTokens, Tokens} from "./parseTokens";
23
+
24
+ const TOKEN_TYPE ={
25
+ refresh_token:"refresh_token",
26
+ access_token:"access_token"
27
+ }
28
+
29
+ const performRevocationRequestAsync= async (url, token, token_type=TOKEN_TYPE.refresh_token, client_id) => {
30
+ const details = {
31
+ token:token,
32
+ token_type_hint:token_type,
33
+ client_id: client_id
34
+ }
35
+
36
+ let formBody = [];
37
+ for (const property in details) {
38
+ const encodedKey = encodeURIComponent(property);
39
+ const encodedValue = encodeURIComponent(details[property]);
40
+ formBody.push(`${encodedKey}=${encodedValue}`);
41
+ }
42
+ const formBodyString = formBody.join("&");
43
+
44
+ const response = await internalFetch(url, {
45
+ method: 'POST',
46
+ headers: {
47
+ 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
48
+ },
49
+ body: formBodyString,
50
+ });
51
+ if(response.status !== 200){
52
+ return { success:false };
53
+ }
54
+ return {
55
+ success : true
56
+ };
57
+ }
23
58
 
24
59
  const performTokenRequestAsync= async (url, details, extras, oldTokens) => {
25
-
26
60
  for (let [key, value] of Object.entries(extras)) {
27
61
  if (details[key] === undefined) {
28
62
  details[key] = value;
@@ -77,6 +111,15 @@ const internalFetch = async (url, headers, numberRetry=0) => {
77
111
  return response;
78
112
  }
79
113
 
114
+ const randomString = function(length) {
115
+ let text = "";
116
+ const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
117
+ for(let i = 0; i < length; i++) {
118
+ text += possible.charAt(Math.floor(Math.random() * possible.length));
119
+ }
120
+ return text;
121
+ }
122
+
80
123
  export interface OidcAuthorizationServiceConfigurationJson extends AuthorizationServiceConfigurationJson{
81
124
  check_session_iframe?: string;
82
125
  issuer:string;
@@ -103,10 +146,6 @@ export interface StringMap {
103
146
  [key: string]: string;
104
147
  }
105
148
 
106
- export interface loginCallbackResult {
107
- state: string,
108
- callbackPath: string,
109
- }
110
149
 
111
150
  export interface AuthorityConfiguration {
112
151
  authorization_endpoint: string;
@@ -146,16 +185,25 @@ const oidcFactory = (configuration: OidcConfiguration, name="default") => {
146
185
  oidcDatabase[name] = new Oidc(configuration, name)
147
186
  return oidcDatabase[name];
148
187
  }
188
+ export type LoginCallback = {
189
+ callbackPath:string
190
+ }
191
+
192
+ export type InternalLoginCallback = {
193
+ callbackPath:string
194
+ parsedTokens:Tokens,
195
+ }
149
196
 
150
- const loginCallbackWithAutoTokensRenewAsync = async (oidc) => {
151
- const { parsedTokens, state, callbackPath } = await oidc.loginCallbackAsync();
197
+ const loginCallbackWithAutoTokensRenewAsync = async (oidc) : Promise<LoginCallback> => {
198
+ const { parsedTokens, callbackPath } = await oidc.loginCallbackAsync();
152
199
  oidc.timeoutId = autoRenewTokens(oidc, parsedTokens.refreshToken, parsedTokens.expiresAt)
153
- return { state, callbackPath };
200
+ return { callbackPath };
154
201
  }
155
202
 
156
203
  async function renewTokensAndStartTimerAsync(oidc, refreshToken, forceRefresh =false, extras:StringMap=null) {
157
- const {tokens, status} = await oidc.synchroniseTokensAsync(refreshToken, 0, forceRefresh, extras);
158
- oidc.tokens = tokens;
204
+ const updateTokens = (tokens) => oidc.tokens = tokens;
205
+ const {tokens, status} = await oidc.synchroniseTokensAsync(refreshToken, 0, forceRefresh, extras, updateTokens);
206
+
159
207
  const serviceWorker = await initWorkerAsync(oidc.configuration.service_worker_relative_url, oidc.configurationName);
160
208
  if (!serviceWorker) {
161
209
  const session = initSession(oidc.configurationName, oidc.configuration.redirect_uri, oidc.configuration.storage);
@@ -306,24 +354,10 @@ const fetchFromIssuer = async (openIdIssuerUrl: string, timeCacheSecond = oneHou
306
354
  return new OidcAuthorizationServiceConfiguration(result);
307
355
  }
308
356
 
309
- const buildQueries = (extras:StringMap) => {
310
- let queries = '';
311
- if(extras != null){
312
- for (let [key, value] of Object.entries(extras)) {
313
- if (queries === ""){
314
- queries = `?${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
315
- } else {
316
- queries+= `&${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
317
- }
318
- }
319
- }
320
- return queries;
321
- }
322
-
323
357
  export class Oidc {
324
358
  public configuration: OidcConfiguration;
325
359
  public userInfo: null;
326
- public tokens: null;
360
+ public tokens?: Tokens;
327
361
  public events: Array<any>;
328
362
  private timeoutId: NodeJS.Timeout;
329
363
  private configurationName: string;
@@ -360,13 +394,13 @@ export class Oidc {
360
394
  this.initAsync(this.configuration.authority, this.configuration.authority_configuration);
361
395
  }
362
396
 
363
- subscriveEvents(func){
397
+ subscriveEvents(func):string{
364
398
  const id = getRandomInt(9999999999999).toString();
365
399
  this.events.push({id, func});
366
400
  return id;
367
401
  }
368
402
 
369
- removeEventSubscription(id){
403
+ removeEventSubscription(id) :void{
370
404
  const newEvents = this.events.filter(e => e.id !== id);
371
405
  this.events = newEvents;
372
406
  }
@@ -402,7 +436,7 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
402
436
  }
403
437
  }
404
438
 
405
- async silentLoginCallBackAsync() {
439
+ async silentLoginCallbackAsync() {
406
440
  try {
407
441
  await this.loginCallbackAsync(true);
408
442
  this._silentLoginCallbackFromIFrame();
@@ -536,11 +570,10 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
536
570
  }
537
571
 
538
572
  tryKeepExistingSessionPromise = null;
539
- async tryKeepExistingSessionAsync() {
573
+ async tryKeepExistingSessionAsync() :Promise<boolean> {
540
574
  if(this.tryKeepExistingSessionPromise !== null){
541
575
  return this.tryKeepExistingSessionPromise;
542
576
  }
543
-
544
577
  const funcAsync =async () => {
545
578
  let serviceWorker
546
579
  if (this.tokens != null) {
@@ -617,25 +650,20 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
617
650
  });
618
651
  }
619
652
 
620
- loginPromise: Promise<any>=null;
653
+ loginPromise: Promise<void>=null;
621
654
  async loginAsync(callbackPath:string=undefined, extras:StringMap=null, isSilentSignin:boolean=false, scope:string=undefined, silentLoginOnly = false) {
622
655
  if(this.loginPromise !== null){
623
656
  return this.loginPromise;
624
657
  }
625
-
626
658
  const loginLocalAsync=async () => {
627
-
628
659
  const location = window.location;
629
660
  const url = callbackPath || location.pathname + (location.search || '') + (location.hash || '');
630
-
631
661
  const configuration = this.configuration;
632
662
  let state = undefined;
633
663
  if(extras && "state" in extras){
634
664
  state = extras["state"];
635
665
  delete extras["state"];
636
666
  }
637
-
638
-
639
667
  if(silentLoginOnly){
640
668
  try {
641
669
  const extraFinal = extras ?? configuration.extras ?? {};
@@ -662,15 +690,6 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
662
690
  scope = configuration.scope;
663
691
  }
664
692
 
665
- const randomString = function(length) {
666
- let text = "";
667
- const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
668
- for(let i = 0; i < length; i++) {
669
- text += possible.charAt(Math.floor(Math.random() * possible.length));
670
- }
671
- return text;
672
- }
673
-
674
693
  setLoginParams(this.configurationName, redirectUri, {callbackPath: url, extras, state});
675
694
  const extraFinal = extras ?? configuration.extras ?? {};
676
695
  if(!extraFinal.nonce) {
@@ -693,7 +712,6 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
693
712
  storage = new MemoryStorageBackend(session.saveItemsAsync, {});
694
713
  }
695
714
 
696
-
697
715
  // @ts-ignore
698
716
  const queryStringUtil = redirectUri.includes("#") ? new HashQueryStringUtils() : new NoHashQueryStringUtils();
699
717
  const authorizationHandler = new RedirectRequestHandler(storage, queryStringUtil, window.location, new DefaultCrypto());
@@ -781,24 +799,22 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
781
799
  return this.loginCallbackPromise;
782
800
  }
783
801
 
784
- const loginCallbackLocalAsync= async( ) =>{
802
+ const loginCallbackLocalAsync= async():Promise<InternalLoginCallback> =>{
785
803
  const response = await this._loginCallbackAsync(isSilenSignin);
786
804
  // @ts-ignore
787
- const tokens = response.tokens;
788
- const parsedTokens = setTokens(tokens);
789
- this.tokens = parsedTokens;
805
+ const parsedTokens = response.tokens;
806
+ // @ts-ignore
807
+ this.tokens = response.tokens;
790
808
  const oidc = this;
791
809
  const serviceWorker = await initWorkerAsync(oidc.configuration.service_worker_relative_url, oidc.configurationName);
792
810
  if (!serviceWorker) {
793
811
  const session = initSession(this.configurationName, oidc.configuration.redirect_uri, oidc.configuration.storage);
794
812
  await session.setTokens(parsedTokens);
795
813
  }
796
-
797
814
  this.publishEvent(Oidc.eventNames.token_aquired, parsedTokens);
798
815
  // @ts-ignore
799
816
  return { parsedTokens, state:response.state, callbackPath : response.callbackPath};
800
817
  }
801
-
802
818
  this.loginCallbackPromise = loginCallbackLocalAsync();
803
819
  return this.loginCallbackPromise.then(result =>{
804
820
  this.loginCallbackPromise = null;
@@ -891,16 +907,19 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
891
907
  try {
892
908
  const tokenHandler = new BaseTokenRequestHandler(new FetchRequestor());
893
909
  tokenHandler.performTokenRequest(oidcServerConfiguration, tokenRequest).then(async (tokenResponse) => {
910
+
894
911
  if (timeoutId) {
895
912
  clearTimeout(timeoutId);
896
913
  this.timeoutId = null;
897
914
  const loginParams = getLoginParams(this.configurationName, redirectUri);
898
-
915
+ let formattedTokens = null;
899
916
  if (serviceWorker) {
900
917
  const {tokens} = await serviceWorker.initAsync(oidcServerConfiguration, "syncTokensAsync");
901
- tokenResponse = tokens;
918
+ formattedTokens = tokens;
919
+ } else{
920
+ formattedTokens = setTokens(tokenResponse);
902
921
  }
903
- if(!isTokensOidcValid(tokenResponse, nonceData.nonce, oidcServerConfiguration)){
922
+ if(!isTokensOidcValid(formattedTokens, nonceData.nonce, oidcServerConfiguration)){
904
923
  const exception = new Error("Tokens are not OpenID valid");
905
924
  if(timeoutId) {
906
925
  clearTimeout(timeoutId);
@@ -915,7 +934,7 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
915
934
  this.startCheckSessionAsync(oidcServerConfiguration.check_session_iframe, clientId, sessionState, isSilentSignin).then(() => {
916
935
  this.publishEvent(eventNames.loginCallbackAsync_end, {});
917
936
  resolve({
918
- tokens: tokenResponse,
937
+ tokens: formattedTokens,
919
938
  state: request.state,
920
939
  callbackPath: loginParams.callbackPath,
921
940
  });
@@ -941,12 +960,11 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
941
960
  }
942
961
  }
943
962
 
944
- async synchroniseTokensAsync(refreshToken, index=0, forceRefresh =false, extras:StringMap=null) {
945
-
963
+ async synchroniseTokensAsync(refreshToken, index=0, forceRefresh =false, extras:StringMap=null, updateTokens) {
946
964
  if (document.hidden) {
947
965
  await sleepAsync(1000);
948
966
  this.publishEvent(eventNames.refreshTokensAsync, {message: "wait because document is hidden"});
949
- return await this.synchroniseTokensAsync(refreshToken, index, forceRefresh);
967
+ return await this.synchroniseTokensAsync(refreshToken, index, forceRefresh, extras, updateTokens);
950
968
  }
951
969
  let numberTryOnline = 6;
952
970
  while (!navigator.onLine && numberTryOnline > 0) {
@@ -968,6 +986,7 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
968
986
  prompt: "none"
969
987
  }, loginParams.state);
970
988
  if (silent_token_response) {
989
+ updateTokens(silent_token_response.tokens);
971
990
  this.publishEvent(Oidc.eventNames.token_renewed, {});
972
991
  return {tokens:silent_token_response.tokens, status:"LOGGED"};
973
992
  }
@@ -975,12 +994,14 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
975
994
  console.error(exceptionSilent);
976
995
  this.publishEvent(eventNames.refreshTokensAsync_silent_error, {message: "exceptionSilent" ,exception: exceptionSilent.message});
977
996
  if(exceptionSilent && exceptionSilent.message && exceptionSilent.message.startsWith("oidc")){
997
+ updateTokens(null);
978
998
  this.publishEvent(eventNames.refreshTokensAsync_error, {message: `refresh token silent` });
979
999
  return {tokens:null, status:"SESSION_LOST"};
980
1000
  }
981
1001
  await sleepAsync(1000);
982
1002
  throw exceptionSilent;
983
1003
  }
1004
+ updateTokens(null);
984
1005
  this.publishEvent(eventNames.refreshTokensAsync_error, {message: `refresh token silent return` });
985
1006
  return {tokens:null, status:"SESSION_LOST"};
986
1007
  }
@@ -990,16 +1011,21 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
990
1011
  const { status, tokens, nonce } = await this.syncTokensInfoAsync(configuration, this.configurationName, this.tokens, forceRefresh);
991
1012
  switch (status) {
992
1013
  case "SESSION_LOST":
1014
+ updateTokens(null);
993
1015
  this.publishEvent(eventNames.refreshTokensAsync_error, {message: `refresh token session lost` });
994
1016
  return {tokens:null, status:"SESSION_LOST"};
995
1017
  case "NOT_CONNECTED":
1018
+ updateTokens(null);
996
1019
  return {tokens:null, status:null};
997
1020
  case "TOKENS_VALID":
1021
+ updateTokens(tokens);
998
1022
  return {tokens, status:"LOGGED_IN"};
999
1023
  case "TOKEN_UPDATED_BY_ANOTHER_TAB_TOKENS_VALID":
1024
+ updateTokens(tokens);
1000
1025
  this.publishEvent(Oidc.eventNames.token_renewed, {});
1001
1026
  return {tokens, status:"LOGGED_IN"};
1002
1027
  case "LOGOUT_FROM_ANOTHER_TAB":
1028
+ updateTokens(null);
1003
1029
  this.publishEvent(eventNames.logout_from_another_tab, {"status": "session syncTokensAsync"});
1004
1030
  return {tokens:null, status:"LOGGED_OUT"};
1005
1031
  case "REQUIRE_SYNC_TOKENS":
@@ -1008,7 +1034,6 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
1008
1034
  default:
1009
1035
  if(!refreshToken)
1010
1036
  {
1011
- this.publishEvent(eventNames.refreshTokensAsync_begin, {refreshToken:refreshToken, tryNumber: index});
1012
1037
  return await localsilentLoginAsync();
1013
1038
  }
1014
1039
  this.publishEvent(eventNames.refreshTokensAsync_begin, {refreshToken:refreshToken, status, tryNumber: index});
@@ -1028,9 +1053,11 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
1028
1053
  const tokenResponse = await performTokenRequestAsync(oidcServerConfiguration.tokenEndpoint, details, finalExtras, tokens);
1029
1054
  if (tokenResponse.success) {
1030
1055
  if(!isTokensOidcValid(tokenResponse.data, nonce.nonce, oidcServerConfiguration)){
1056
+ updateTokens(null);
1031
1057
  this.publishEvent(eventNames.refreshTokensAsync_error, {message: `refresh token return not valid tokens` });
1032
1058
  return {tokens:null, status:"SESSION_LOST"};
1033
1059
  }
1060
+ updateTokens(tokenResponse.data);
1034
1061
  this.publishEvent(eventNames.refreshTokensAsync_end, {success: tokenResponse.success});
1035
1062
  this.publishEvent(Oidc.eventNames.token_renewed, {});
1036
1063
  return {tokens: tokenResponse.data, status:"LOGGED_IN"};
@@ -1039,13 +1066,13 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
1039
1066
  message: "bad request",
1040
1067
  tokenResponse: tokenResponse
1041
1068
  });
1042
- return await this.synchroniseTokensAsync(null, index+1, forceRefresh);
1069
+ return await this.synchroniseTokensAsync(null, index+1, forceRefresh, extras, updateTokens);
1043
1070
  }
1044
1071
  }
1045
1072
  } catch (exception) {
1046
1073
  console.error(exception);
1047
1074
  this.publishEvent(eventNames.refreshTokensAsync_silent_error, {message: "exception" ,exception: exception.message});
1048
- return this.synchroniseTokensAsync(refreshToken, index+1, forceRefresh);
1075
+ return this.synchroniseTokensAsync(refreshToken, index+1, forceRefresh, extras, updateTokens);
1049
1076
  }
1050
1077
  }
1051
1078
 
@@ -1103,8 +1130,8 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
1103
1130
  return { tokens:currentTokens, status, nonce};
1104
1131
  }
1105
1132
 
1106
- loginCallbackWithAutoTokensRenewPromise:Promise<loginCallbackResult> = null;
1107
- loginCallbackWithAutoTokensRenewAsync():Promise<loginCallbackResult>{
1133
+ loginCallbackWithAutoTokensRenewPromise:Promise<LoginCallback> = null;
1134
+ loginCallbackWithAutoTokensRenewAsync():Promise<LoginCallback>{
1108
1135
  if(this.loginCallbackWithAutoTokensRenewPromise !== null){
1109
1136
  return this.loginCallbackWithAutoTokensRenewPromise;
1110
1137
  }
@@ -1187,6 +1214,21 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
1187
1214
  const url = isUri ? callbackPathOrUrl : window.location.origin + path;
1188
1215
  // @ts-ignore
1189
1216
  const idToken = this.tokens ? this.tokens.idToken : "";
1217
+ const revocationEndpoint = oidcServerConfiguration.revocationEndpoint;
1218
+ if(revocationEndpoint) {
1219
+ const promises = [];
1220
+ if(this.tokens.accessToken){
1221
+ const revokeAccessTokenPromise = performRevocationRequestAsync(revocationEndpoint, this.tokens.accessToken, TOKEN_TYPE.refresh_token, configuration.client_id);
1222
+ promises.push(revokeAccessTokenPromise);
1223
+ }
1224
+ if(this.tokens.refreshToken) {
1225
+ const revokeRefreshTokenPromise = performRevocationRequestAsync(revocationEndpoint, this.tokens.refreshToken, TOKEN_TYPE.refresh_token, configuration.client_id);
1226
+ promises.push(revokeRefreshTokenPromise);
1227
+ }
1228
+ if(promises.length > 0){
1229
+ await Promise.all(promises);
1230
+ }
1231
+ }
1190
1232
  // @ts-ignore
1191
1233
  const sub = this.tokens && this.tokens.idTokenPayload ? this.tokens.idTokenPayload.sub : null;
1192
1234
  await this.destroyAsync("LOGGED_OUT");
@@ -1196,6 +1238,7 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
1196
1238
  await oidc.logoutSameTabAsync(this.configuration.client_id, sub);
1197
1239
  }
1198
1240
  }
1241
+
1199
1242
 
1200
1243
  if(oidcServerConfiguration.endSessionEndpoint) {
1201
1244
  if(!extras){
@@ -24,8 +24,17 @@ const countLetter = (str, find)=> {
24
24
  return (str.split(find)).length - 1;
25
25
  }
26
26
 
27
-
28
- export const setTokens = (tokens, oldTokens=null) =>{
27
+ export type Tokens = {
28
+ refreshToken: string,
29
+ idTokenPayload:any,
30
+ idToken:string,
31
+ accessTokenPayload:any,
32
+ accessToken:string,
33
+ expiresAt: number,
34
+ issuedAt: number
35
+ };
36
+
37
+ export const setTokens = (tokens, oldTokens=null):Tokens =>{
29
38
 
30
39
  if(!tokens){
31
40
  return null;
@@ -45,9 +54,9 @@ export const setTokens = (tokens, oldTokens=null) =>{
45
54
  }
46
55
  const _idTokenPayload = tokens.idTokenPayload ? tokens.idTokenPayload : extractTokenPayload(tokens.idToken);
47
56
 
48
- const idTokenExipreAt =(_idTokenPayload && _idTokenPayload.exp) ? _idTokenPayload.exp: Number.MAX_VALUE;
57
+ const idTokenExpireAt =(_idTokenPayload && _idTokenPayload.exp) ? _idTokenPayload.exp: Number.MAX_VALUE;
49
58
  const accessTokenExpiresAt = (accessTokenPayload && accessTokenPayload.exp)? accessTokenPayload.exp : tokens.issuedAt + tokens.expiresIn;
50
- const expiresAt = idTokenExipreAt < accessTokenExpiresAt ? idTokenExipreAt : accessTokenExpiresAt;
59
+ const expiresAt = idTokenExpireAt < accessTokenExpiresAt ? idTokenExpireAt : accessTokenExpiresAt;
51
60
 
52
61
  const newTokens = {...tokens, idTokenPayload: _idTokenPayload, accessTokenPayload, expiresAt};
53
62
  // When refresh_token is not rotated we reuse ald refresh_token
@@ -32,9 +32,9 @@
32
32
  };
33
33
 
34
34
  function onMessage(port, event) {
35
- var method = event.data[0];
36
- var id = event.data[1];
37
- var option = event.data[2];
35
+ const method = event.data[0];
36
+ const id = event.data[1];
37
+ const option = event.data[2];
38
38
 
39
39
  if (methods[method]) {
40
40
  methods[method](port, id, option);
@@ -0,0 +1,74 @@
1
+ import {LoginCallback, Oidc, OidcConfiguration, StringMap} from "./oidc";
2
+ import {isTokensValid} from "./parseTokens";
3
+ import {sleepAsync} from "./initWorker";
4
+ import {Tokens} from "./parseTokens";
5
+
6
+ type ValidToken = {
7
+ isTokensValid: Boolean,
8
+ tokens: Tokens,
9
+ numberWaited: Number
10
+ }
11
+
12
+ export class VanillaOidc {
13
+ private _oidc: Oidc;
14
+ constructor(oidc: Oidc) {
15
+ this._oidc = oidc;
16
+ }
17
+ subscriveEvents(func:Function):string{
18
+ return this._oidc.subscriveEvents(func);
19
+ }
20
+ removeEventSubscription(id:string):void{
21
+ this._oidc.removeEventSubscription(id);
22
+ }
23
+ publishEvent(eventName:string, data:any) : void{
24
+ this._oidc.publishEvent(eventName, data);
25
+ }
26
+ static getOrCreate(configuration:OidcConfiguration, name:string="default"):VanillaOidc {
27
+ return new VanillaOidc(Oidc.getOrCreate(configuration, name));
28
+ }
29
+ static get(name:string="default"):VanillaOidc {
30
+ return new VanillaOidc(Oidc.get(name));
31
+ }
32
+ static eventNames = Oidc.eventNames;
33
+ tryKeepExistingSessionAsync():Promise<boolean>{
34
+ return this._oidc.tryKeepExistingSessionAsync();
35
+ }
36
+ loginAsync(callbackPath:string=undefined, extras:StringMap=null, isSilentSignin:boolean=false, scope:string=undefined, silentLoginOnly = false):Promise<void> {
37
+ return this._oidc.loginAsync(callbackPath, extras, isSilentSignin, scope, silentLoginOnly);
38
+ }
39
+ logoutAsync(callbackPathOrUrl: string | null | undefined = undefined, extras: StringMap = null):Promise<void> {
40
+ return this._oidc.logoutAsync(callbackPathOrUrl, extras);
41
+ }
42
+ silentLoginCallbackAsync():Promise<any>{
43
+ return this._oidc.silentLoginCallbackAsync();
44
+ };
45
+ renewTokensAsync(extras:StringMap=null):Promise<void> {
46
+ return this._oidc.renewTokensAsync(extras);
47
+ }
48
+ loginCallbackAsync():Promise<LoginCallback>{
49
+ return this._oidc.loginCallbackWithAutoTokensRenewAsync();
50
+ }
51
+ get tokens():Tokens {
52
+ return this._oidc.tokens;
53
+ }
54
+ get configuration():OidcConfiguration {
55
+ return this._oidc.configuration;
56
+ }
57
+ async getValidTokenAsync(waitMs=200, numberWait=50 ): Promise<ValidToken> {
58
+ const oidc = this._oidc;
59
+ let numberWaitTemp = numberWait;
60
+ while (oidc.tokens && !isTokensValid(oidc.tokens) && numberWaitTemp > 0) {
61
+ await sleepAsync(200);
62
+ numberWaitTemp=numberWaitTemp-1;
63
+ }
64
+ const isValid = !isTokensValid(oidc.tokens);
65
+ return {
66
+ isTokensValid: isValid,
67
+ tokens: oidc.tokens,
68
+ numberWaited: numberWaitTemp - numberWait
69
+ };
70
+ }
71
+ async userInfoAsync():Promise<any>{
72
+ return this._oidc.userInfoAsync();
73
+ }
74
+ }