@muze-nl/metro-oidc 0.5.0 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/browser.js CHANGED
@@ -877,19 +877,22 @@
877
877
  localState = {
878
878
  get: () => localStorage.getItem("metro/state:" + site),
879
879
  set: (value) => localStorage.setItem("metro/state:" + site, value),
880
- has: () => localStorage.getItem("metro/state:" + site) !== null
880
+ has: () => localStorage.getItem("metro/state:" + site) !== null,
881
+ delete: () => localStorage.remoteItem("metro/state:" + site)
881
882
  };
882
883
  localTokens = {
883
884
  get: (name) => JSON.parse(localStorage.getItem(site + ":" + name)),
884
885
  set: (name, value) => localStorage.setItem(site + ":" + name, JSON.stringify(value)),
885
- has: (name) => localStorage.getItem(site + ":" + name) !== null
886
+ has: (name) => localStorage.getItem(site + ":" + name) !== null,
887
+ delete: (name) => localStorage.removeItem(site + ":" + name)
886
888
  };
887
889
  } else {
888
890
  let stateMap = /* @__PURE__ */ new Map();
889
891
  localState = {
890
892
  get: () => stateMap.get("metro/state:" + site),
891
893
  set: (value) => stateMap.set("metro/state:" + site, value),
892
- has: () => stateMap.has("metro/state:" + site)
894
+ has: () => stateMap.has("metro/state:" + site),
895
+ delete: () => stateMap.delete("metro/state:" + site)
893
896
  };
894
897
  localTokens = /* @__PURE__ */ new Map();
895
898
  }
@@ -981,8 +984,10 @@
981
984
  };
982
985
  async function oauth2authorized(req, next) {
983
986
  getTokensFromLocation();
984
- let accessToken = options.tokens.get("access_token");
985
- if (!accessToken) {
987
+ const accessToken = options.tokens.get("access_token");
988
+ const refreshToken = options.tokens.get("refresh_token");
989
+ const tokenIsExpired = isExpired(accessToken);
990
+ if (!accessToken || tokenIsExpired && !refreshToken) {
986
991
  try {
987
992
  let token = await fetchAccessToken();
988
993
  if (!token) {
@@ -992,9 +997,9 @@
992
997
  throw e;
993
998
  }
994
999
  return oauth2authorized(req, next);
995
- } else if (isExpired(accessToken)) {
1000
+ } else if (tokenIsExpired && refreshToken) {
996
1001
  try {
997
- let token = await fetchRefreshToken();
1002
+ let token = await refreshAccessToken();
998
1003
  if (!token) {
999
1004
  return response("false");
1000
1005
  }
@@ -1070,9 +1075,10 @@
1070
1075
  };
1071
1076
  options.tokens.set("refresh_token", token);
1072
1077
  }
1078
+ options.tokens.delete("authorization_code");
1073
1079
  return data;
1074
1080
  }
1075
- async function fetchRefreshToken() {
1081
+ async function refreshAccessToken() {
1076
1082
  let refreshTokenReq = getAccessTokenRequest("refresh_token");
1077
1083
  let response2 = await options.client.post(refreshTokenReq);
1078
1084
  if (!response2.ok) {
@@ -1149,20 +1155,20 @@
1149
1155
  grant_type: grant_type || oauth2.grant_type,
1150
1156
  client_id: oauth2.client_id
1151
1157
  };
1152
- const code_verifier = options.tokens.get("code_verifier");
1153
- if (code_verifier) {
1154
- params2.code_verifier = code_verifier;
1155
- }
1156
1158
  if (oauth2.client_secret) {
1157
1159
  params2.client_secret = oauth2.client_secret;
1158
1160
  }
1159
1161
  if (oauth2.scope) {
1160
1162
  params2.scope = oauth2.scope;
1161
1163
  }
1162
- switch (oauth2.grant_type) {
1164
+ switch (params2.grant_type) {
1163
1165
  case "authorization_code":
1164
1166
  params2.redirect_uri = oauth2.redirect_uri;
1165
1167
  params2.code = options.tokens.get("authorization_code");
1168
+ const code_verifier = options.tokens.get("code_verifier");
1169
+ if (code_verifier) {
1170
+ params2.code_verifier = code_verifier;
1171
+ }
1166
1172
  break;
1167
1173
  case "client_credentials":
1168
1174
  break;
@@ -1675,9 +1681,18 @@
1675
1681
  client_info: openid_client_metadata
1676
1682
  });
1677
1683
  const defaultOptions = {
1678
- client: everything_default.client().with(throwermw()).with(jsonmw())
1684
+ client: everything_default.client().with(throwermw()).with(jsonmw()),
1685
+ client_info: {
1686
+ redirect_uris: [globalThis.document?.location.href]
1687
+ }
1679
1688
  };
1680
1689
  options = Object.assign({}, defaultOptions, options);
1690
+ if (!options.client_info) {
1691
+ options.client_info = {};
1692
+ }
1693
+ if (!options.client_info.redirect_uris) {
1694
+ options.client_info.redirect_uris = [globalThis.document?.location.href];
1695
+ }
1681
1696
  let response2 = await options.client.post(options.registration_endpoint, {
1682
1697
  body: options.client_info
1683
1698
  });
@@ -1737,7 +1752,7 @@
1737
1752
  if (!options.openid_configuration && options.store.has("openid_configuration")) {
1738
1753
  options.openid_configuration = options.store.get("openid_configuration");
1739
1754
  }
1740
- if (!options.client_info.client_id && options.store.has("client_info")) {
1755
+ if (!options.client_info?.client_id && options.store.has("client_info")) {
1741
1756
  options.client_info = options.store.get("client_info");
1742
1757
  }
1743
1758
  return async (req, next) => {
@@ -1761,7 +1776,6 @@
1761
1776
  options.store.set("openid_configuration", options.openid_configuration);
1762
1777
  }
1763
1778
  if (!options.client_info?.client_id) {
1764
- assert(options.client_info?.client_name, Required());
1765
1779
  if (!options.openid_configuration.registration_endpoint) {
1766
1780
  throw metroError("metro.oidcmw: Error: issuer " + options.issuer + " does not support dynamic client registration, but you haven't specified a client_id");
1767
1781
  }
@@ -1779,8 +1793,8 @@
1779
1793
  force_authorization: true,
1780
1794
  authorize_callback: options.authorize_callback,
1781
1795
  oauth2_configuration: {
1782
- client_id: options.client_info.client_id,
1783
- client_secret: options.client_info.client_secret,
1796
+ client_id: options.client_info?.client_id,
1797
+ client_secret: options.client_info?.client_secret,
1784
1798
  grant_type: "authorization_code",
1785
1799
  response_type: "code",
1786
1800
  response_mode: "query",