@opengeoweb/authentication 9.26.0 → 9.27.1

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/index.esm.js CHANGED
@@ -6855,9 +6855,8 @@ $({ global: true, bind: true, enumerable: true, forced: FORCED }, {
6855
6855
 
6856
6856
  const API_NAMESPACE$1 = 'api';
6857
6857
  const DEFAULT_TIMEOUT = 15000;
6858
- const KEEP_ALIVE_IN_SECONDS = 60; // Number of seconds in between intervals to check with the token request if connection is stil intact
6859
- const KEEP_ALIVE_POLLER_IN_SECONDS = 10; // Number of milliseconds to check if connection is restored or to check if KEEP_ALIVE_IN_SECONDS has passed
6860
- const REFRESH_TOKEN_WHEN_PCT_EXPIRED = 75; // Refresh token when 75% expired. Set to (10 / 3600) * 100 = 0.2777778% to test with 10 second interval.
6858
+ const KEEP_ALIVE_POLLER_IN_SECONDS = 60; // Number of seconds between the checks if the token should be refreshed.
6859
+ const REFRESH_TOKEN_WHEN_PCT_EXPIRED = 75; // Refresh token when 75% expired. Set to (10 / 3600) * 100 = 0.2777778% to test with 10 second interval (assuming 1 hour token expiration).
6861
6860
  const DEFAULT_TOKEN_EXPIRES_IN = 3600; // Number of seconds a token expires by default
6862
6861
  const MILLISECOND_TO_SECOND = 1 / 1000;
6863
6862
  const ns$1 = API_NAMESPACE$1;
@@ -6910,7 +6909,6 @@ const makeCredentialsFromTokenResponse = (tokenResponse, authConfig) => {
6910
6909
  token: access_token,
6911
6910
  refresh_token: refresh_token || '',
6912
6911
  expires_at: epochTimeTokenExpirationInSeconds,
6913
- keep_session_alive_at: getCurrentTimeInSeconds() + KEEP_ALIVE_IN_SECONDS,
6914
6912
  has_connection_issue: false
6915
6913
  };
6916
6914
  return newAuth;
@@ -6951,7 +6949,8 @@ const refreshAccessTokenAndSetAuthContext = ({
6951
6949
  auth,
6952
6950
  onSetAuth,
6953
6951
  config,
6954
- timeout: _timeout2 = DEFAULT_TIMEOUT
6952
+ timeout: _timeout2 = DEFAULT_TIMEOUT,
6953
+ configURLS
6955
6954
  }) => __awaiter(void 0, void 0, void 0, function* () {
6956
6955
  try {
6957
6956
  const refreshedToken = yield refreshAccessToken({
@@ -6959,11 +6958,15 @@ const refreshAccessTokenAndSetAuthContext = ({
6959
6958
  config,
6960
6959
  timeout: _timeout2
6961
6960
  });
6962
- const newAuth = makeCredentialsFromTokenResponse(refreshedToken);
6961
+ const newAuth = makeCredentialsFromTokenResponse(refreshedToken, configURLS);
6963
6962
  // Cognito does not send a new refresh token, but gitlab does. Set it here into the auth context.
6964
6963
  if (!newAuth.refresh_token || newAuth.refresh_token.length === 0) {
6965
6964
  newAuth.refresh_token = auth.refresh_token;
6966
6965
  }
6966
+ // If the prop for the role config is not set, keep the roles of the current auth context.
6967
+ if (!configURLS) {
6968
+ newAuth.roles = auth.roles;
6969
+ }
6967
6970
  onSetAuth(newAuth);
6968
6971
  } catch (e) {
6969
6972
  onSetAuth(Object.assign(Object.assign({}, auth), {
@@ -6973,12 +6976,8 @@ const refreshAccessTokenAndSetAuthContext = ({
6973
6976
  });
6974
6977
  const createApiInstance = ({
6975
6978
  auth,
6976
- onSetAuth,
6977
6979
  config: {
6978
- baseURL,
6979
- authTokenURL,
6980
- authClientId,
6981
- appURL
6980
+ baseURL
6982
6981
  } = {},
6983
6982
  timeout: _timeout3 = DEFAULT_TIMEOUT
6984
6983
  }) => {
@@ -6989,20 +6988,7 @@ const createApiInstance = ({
6989
6988
  });
6990
6989
  // Request interceptor for API calls done BEFORE the request is made.
6991
6990
  axiosInstance.interceptors.request.use(axiosConfig => __awaiter(void 0, void 0, void 0, function* () {
6992
- const timeInSecondsLeftBeforeExpiration = auth && auth.expires_at ? auth.expires_at - getCurrentTimeInSeconds() : 0; // If expires_at is not set, don't do anything. (set timeInSecondsLeftBeforeExpiration = 0 will skip refresh)
6993
- if (timeInSecondsLeftBeforeExpiration < 0) {
6994
- yield refreshAccessTokenAndSetAuthContext({
6995
- auth,
6996
- onSetAuth,
6997
- config: {
6998
- baseURL,
6999
- authTokenURL,
7000
- authClientId,
7001
- appURL
7002
- },
7003
- timeout: _timeout3
7004
- });
7005
- }
6991
+ // Add the access token to the headers of the request.
7006
6992
  const newConfig = Object.assign(Object.assign({}, axiosConfig), {
7007
6993
  headers: Object.assign({
7008
6994
  'Content-Type': 'application/json',
@@ -7018,20 +7004,10 @@ const createApiInstance = ({
7018
7004
  axiosInstance.interceptors.response.use(response => response, error => __awaiter(void 0, void 0, void 0, function* () {
7019
7005
  var _a;
7020
7006
  const originalRequest = error.config;
7007
+ // If request fails with 401, retry the request once.
7021
7008
  if (error.response && error.response.status && error.response.status === 401 && !originalRequest.inRetry) {
7022
7009
  originalRequest.inRetry = true;
7023
- yield refreshAccessTokenAndSetAuthContext({
7024
- auth,
7025
- onSetAuth,
7026
- config: {
7027
- baseURL,
7028
- authTokenURL,
7029
- authClientId,
7030
- appURL
7031
- },
7032
- timeout: _timeout3
7033
- });
7034
- // Update the headers of the original request with the refreshed access token
7010
+ // Update the headers of the original request with the token from the current auth context.
7035
7011
  if ((_a = originalRequest.headers) === null || _a === void 0 ? void 0 : _a.Authorization) {
7036
7012
  originalRequest.headers.Authorization = `Bearer ${auth === null || auth === void 0 ? void 0 : auth.token}`;
7037
7013
  }
@@ -7158,7 +7134,8 @@ const AuthenticationContext = /*#__PURE__*/React__default.createContext({
7158
7134
  auth: null,
7159
7135
  onSetAuth: null,
7160
7136
  authConfig: null,
7161
- sessionStorageProvider: null
7137
+ sessionStorageProvider: null,
7138
+ currentRole: null
7162
7139
  });
7163
7140
  const useAuthenticationDefaultProps = () => {
7164
7141
  const [isLoggedIn, onLogin] = React__default.useState(false);
@@ -7169,7 +7146,6 @@ const useAuthenticationDefaultProps = () => {
7169
7146
  token: '',
7170
7147
  refresh_token: '',
7171
7148
  expires_at: 0,
7172
- keep_session_alive_at: 0,
7173
7149
  has_connection_issue: false
7174
7150
  };
7175
7151
  const auth = React__default.useRef(Object.assign({}, emptyCredentials)).current;
@@ -7188,12 +7164,15 @@ const useAuthenticationDefaultProps = () => {
7188
7164
  }
7189
7165
  };
7190
7166
  const sessionStorageProvider = getSessionStorageProvider();
7167
+ const [currentRole, setCurrentRole] = React__default.useState(auth.roles && auth.roles[0] || GEOWEB_ROLE_USER);
7191
7168
  return {
7192
7169
  isLoggedIn,
7193
7170
  onLogin,
7194
7171
  auth: isLoggedIn ? auth : null,
7195
7172
  onSetAuth,
7196
- sessionStorageProvider
7173
+ sessionStorageProvider,
7174
+ currentRole,
7175
+ setCurrentRole
7197
7176
  };
7198
7177
  };
7199
7178
  const AuthenticationProvider = ({
@@ -7207,15 +7186,19 @@ const AuthenticationProvider = ({
7207
7186
  onLogin,
7208
7187
  auth,
7209
7188
  onSetAuth,
7210
- sessionStorageProvider
7189
+ sessionStorageProvider,
7190
+ currentRole,
7191
+ setCurrentRole
7211
7192
  } = value || defaultValues;
7212
7193
  const authConfig = _configURLS;
7194
+ // Checks the token expiration time regularly and renews it before it expires.
7213
7195
  const interval = useRef();
7214
7196
  React__default.useEffect(() => {
7215
7197
  interval.current = setInterval(() => __awaiter(void 0, void 0, void 0, function* () {
7216
7198
  if (auth) {
7217
7199
  const currentTime = getCurrentTimeInSeconds();
7218
- if (auth.keep_session_alive_at && currentTime > auth.keep_session_alive_at) {
7200
+ const timeInSecondsLeftBeforeExpiration = auth.expires_at ? auth.expires_at - currentTime : 0;
7201
+ if (timeInSecondsLeftBeforeExpiration < 0) {
7219
7202
  yield refreshAccessTokenAndSetAuthContext({
7220
7203
  auth,
7221
7204
  onSetAuth,
@@ -7224,7 +7207,8 @@ const AuthenticationProvider = ({
7224
7207
  authTokenURL: _configURLS.GW_AUTH_TOKEN_URL,
7225
7208
  appURL: _configURLS.GW_APP_URL,
7226
7209
  authClientId: _configURLS.GW_AUTH_CLIENT_ID
7227
- }
7210
+ },
7211
+ configURLS: _configURLS
7228
7212
  });
7229
7213
  }
7230
7214
  }
@@ -7232,15 +7216,17 @@ const AuthenticationProvider = ({
7232
7216
  return () => {
7233
7217
  clearInterval(interval.current);
7234
7218
  };
7235
- }, [auth, _configURLS.GW_APP_URL, _configURLS.GW_AUTH_CLIENT_ID, _configURLS.GW_AUTH_TOKEN_URL, onSetAuth]);
7219
+ }, [auth, _configURLS.GW_APP_URL, _configURLS.GW_AUTH_CLIENT_ID, _configURLS.GW_AUTH_TOKEN_URL, onSetAuth, _configURLS]);
7236
7220
  const contextValue = React__default.useMemo(() => ({
7237
7221
  isLoggedIn,
7238
7222
  onLogin,
7239
7223
  auth,
7240
7224
  onSetAuth,
7241
7225
  authConfig,
7242
- sessionStorageProvider
7243
- }), [isLoggedIn, onLogin, auth, onSetAuth, authConfig, sessionStorageProvider]);
7226
+ sessionStorageProvider,
7227
+ currentRole,
7228
+ setCurrentRole
7229
+ }), [isLoggedIn, onLogin, auth, onSetAuth, authConfig, sessionStorageProvider, currentRole, setCurrentRole]);
7244
7230
  return jsx(AuthenticationContext.Provider, Object.assign({
7245
7231
  value: contextValue
7246
7232
  }, {
@@ -7655,4 +7641,4 @@ const useApi = (apiCall, params, callbacks) => {
7655
7641
  };
7656
7642
  };
7657
7643
 
7658
- export { AUTH_NAMESPACE, ApiProvider, AuthenticationContext, AuthenticationProvider, HandleOAuth2Code as Code, GEOWEB_ROLE_PRESETS_ADMIN, GEOWEB_ROLE_USER, KEEP_ALIVE_IN_SECONDS, KEEP_ALIVE_POLLER_IN_SECONDS, OAuth2Login as Login, OAuth2Logout as Logout, MILLISECOND_TO_SECOND, REFRESH_TOKEN_WHEN_PCT_EXPIRED, RequireAuth, SessionStorageKey, UserMenuRoles, UserMenuRolesConnect, apiTranslations, authTranslations, createApiInstance, createFakeApiInstance, createNonAuthApiInstance, fakeApiRequest, getApi, getAuthConfig, getCodeChallenge, getConfig, getCurrentTimeInSeconds, getCurrentUrlLocation, getRandomString, getSessionStorageProvider, groupsToRoles, makeCredentialsFromTokenResponse, refreshAccessToken, refreshAccessTokenAndSetAuthContext, useApi, useApiContext, useAuthenticationContext, useAuthenticationDefaultProps };
7644
+ export { AUTH_NAMESPACE, ApiProvider, AuthenticationContext, AuthenticationProvider, HandleOAuth2Code as Code, GEOWEB_ROLE_PRESETS_ADMIN, GEOWEB_ROLE_USER, KEEP_ALIVE_POLLER_IN_SECONDS, OAuth2Login as Login, OAuth2Logout as Logout, MILLISECOND_TO_SECOND, REFRESH_TOKEN_WHEN_PCT_EXPIRED, RequireAuth, SessionStorageKey, UserMenuRoles, UserMenuRolesConnect, apiTranslations, authTranslations, createApiInstance, createFakeApiInstance, createNonAuthApiInstance, fakeApiRequest, getApi, getAuthConfig, getCodeChallenge, getConfig, getCurrentTimeInSeconds, getCurrentUrlLocation, getRandomString, getSessionStorageProvider, groupsToRoles, makeCredentialsFromTokenResponse, refreshAccessToken, refreshAccessTokenAndSetAuthContext, useApi, useApiContext, useAuthenticationContext, useAuthenticationDefaultProps };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengeoweb/authentication",
3
- "version": "9.26.0",
3
+ "version": "9.27.1",
4
4
  "description": "GeoWeb authentication library for the opengeoweb project",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -14,13 +14,15 @@
14
14
  "@opengeoweb/theme": "*",
15
15
  "i18next": "^23.11.5",
16
16
  "react-i18next": "^14.1.2",
17
- "@opengeoweb/snackbar": "9.26.0",
17
+ "@opengeoweb/snackbar": "9.27.1",
18
18
  "react-redux": "^8.1.3",
19
19
  "@reduxjs/toolkit": "^1.9.7",
20
20
  "@mui/material": "^5.16.0"
21
21
  },
22
22
  "peerDependencies": {
23
- "react": "18"
23
+ "react": "18",
24
+ "@emotion/react": "*",
25
+ "@emotion/styled": "*"
24
26
  },
25
27
  "module": "./index.esm.js",
26
28
  "type": "module",
@@ -11,7 +11,6 @@ export interface Credentials {
11
11
  token: string;
12
12
  refresh_token: string;
13
13
  expires_at?: number;
14
- keep_session_alive_at?: number;
15
14
  has_connection_issue?: boolean;
16
15
  }
17
16
  export interface ApiUrls {
@@ -1,8 +1,7 @@
1
1
  import { AxiosInstance, AxiosResponse } from 'axios';
2
2
  import { ConfigType } from '@opengeoweb/shared';
3
3
  import { CreateApiProps, Credentials, Role } from './types';
4
- export declare const KEEP_ALIVE_IN_SECONDS = 60;
5
- export declare const KEEP_ALIVE_POLLER_IN_SECONDS = 10;
4
+ export declare const KEEP_ALIVE_POLLER_IN_SECONDS = 60;
6
5
  export declare const REFRESH_TOKEN_WHEN_PCT_EXPIRED = 75;
7
6
  export declare const MILLISECOND_TO_SECOND: number;
8
7
  export declare const GEOWEB_ROLE_PRESETS_ADMIN: Role;
@@ -17,8 +16,10 @@ export declare const GEOWEB_ROLE_USER: Role;
17
16
  */
18
17
  export declare const makeCredentialsFromTokenResponse: (tokenResponse: AxiosResponse, authConfig?: ConfigType) => Credentials;
19
18
  export declare const refreshAccessToken: ({ auth, config: { authTokenURL, authClientId, appURL }, timeout, }: CreateApiProps) => Promise<AxiosResponse>;
20
- export declare const refreshAccessTokenAndSetAuthContext: ({ auth, onSetAuth, config, timeout, }: CreateApiProps) => Promise<void>;
21
- export declare const createApiInstance: ({ auth, onSetAuth, config: { baseURL, authTokenURL, authClientId, appURL }, timeout, }: CreateApiProps) => AxiosInstance;
19
+ export declare const refreshAccessTokenAndSetAuthContext: ({ auth, onSetAuth, config, timeout, configURLS, }: CreateApiProps & {
20
+ configURLS?: ConfigType | undefined;
21
+ }) => Promise<void>;
22
+ export declare const createApiInstance: ({ auth, config: { baseURL }, timeout, }: CreateApiProps) => AxiosInstance;
22
23
  export declare const createNonAuthApiInstance: ({ config: { baseURL }, timeout, }: CreateApiProps) => AxiosInstance;
23
24
  export declare const fakeApiRequest: (signal?: AbortController) => Promise<void>;
24
25
  export declare const createFakeApiInstance: () => AxiosInstance;
@@ -1,5 +1,5 @@
1
1
  import { SessionStorageProvider } from '../../utils/session';
2
- import { Credentials } from '../ApiContext/types';
2
+ import { Credentials, Role } from '../ApiContext/types';
3
3
  export interface AuthenticationConfig {
4
4
  GW_AUTH_LOGIN_URL: string;
5
5
  GW_AUTH_LOGOUT_URL: string;
@@ -16,6 +16,8 @@ export interface AuthenticationDefaultStateProps {
16
16
  auth: Credentials | null;
17
17
  onSetAuth: (auth: Credentials) => void;
18
18
  sessionStorageProvider: SessionStorageProvider;
19
+ currentRole?: Role;
20
+ setCurrentRole?: (newRole: Role) => void;
19
21
  }
20
22
  export interface AuthenticationContextProps extends AuthenticationDefaultStateProps {
21
23
  authConfig: AuthenticationConfig;