@axinom/mosaic-user-auth 0.3.6-rc.1 → 0.3.6

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/index.es.js CHANGED
@@ -1,4 +1,4 @@
1
- import { createContext, createElement, useContext } from 'react';
1
+ import { createContext, useState, createElement, useContext } from 'react';
2
2
 
3
3
  var TokenRenewalMethod;
4
4
  (function (TokenRenewalMethod) {
@@ -56,7 +56,7 @@ function commonjsRequire () {
56
56
 
57
57
  var enums = createCommonjsModule(function (module, exports) {
58
58
  Object.defineProperty(exports, "__esModule", { value: true });
59
- exports.IdpProtocol = exports.CheckOtpResponseCode = exports.CompleteUserSignUpResponseCode = exports.UserSignUpResponseCode = exports.ROPCPasswordResponseCode = exports.ROPCSignInResponseCode = exports.IdpConfigurationResponseCode = exports.SignOutResponseCode = exports.TokenResponseCode = void 0;
59
+ exports.DecryptNativeCookieResponseCode = exports.IdpProtocol = exports.CheckOtpResponseCode = exports.CompleteUserSignUpResponseCode = exports.UserSignUpResponseCode = exports.ROPCPasswordResponseCode = exports.ROPCSignInResponseCode = exports.IdpConfigurationResponseCode = exports.SignOutResponseCode = exports.TokenResponseCode = void 0;
60
60
  /**
61
61
  * Token response code from User Service Auth API
62
62
  */
@@ -138,12 +138,23 @@ var CheckOtpResponseCode;
138
138
  CheckOtpResponseCode["ERROR"] = "ERROR";
139
139
  CheckOtpResponseCode["SERVICE_CONFIGURATION_ERROR"] = "SERVICE_CONFIGURATION_ERROR";
140
140
  })(CheckOtpResponseCode = exports.CheckOtpResponseCode || (exports.CheckOtpResponseCode = {}));
141
+ /**
142
+ * IDP Protocol categories
143
+ */
141
144
  var IdpProtocol;
142
145
  (function (IdpProtocol) {
143
146
  IdpProtocol["OIDC"] = "OIDC";
144
147
  IdpProtocol["OAUTH2"] = "OAUTH2";
145
148
  IdpProtocol["DELEGATED"] = "DELEGATED";
146
149
  })(IdpProtocol = exports.IdpProtocol || (exports.IdpProtocol = {}));
150
+ /**
151
+ * Response codes specific to Native Cookie Decryption.
152
+ */
153
+ var DecryptNativeCookieResponseCode;
154
+ (function (DecryptNativeCookieResponseCode) {
155
+ DecryptNativeCookieResponseCode["SUCCESS"] = "SUCCESS";
156
+ DecryptNativeCookieResponseCode["COOKIE_DECRYPTION_FAILED"] = "COOKIE_DECRYPTION_FAILED";
157
+ })(DecryptNativeCookieResponseCode = exports.DecryptNativeCookieResponseCode || (exports.DecryptNativeCookieResponseCode = {}));
147
158
 
148
159
  });
149
160
 
@@ -1286,6 +1297,8 @@ var IdpConnectionsOrderBy;
1286
1297
  IdpConnectionsOrderBy["NATURAL"] = "NATURAL";
1287
1298
  IdpConnectionsOrderBy["PRIMARY_KEY_ASC"] = "PRIMARY_KEY_ASC";
1288
1299
  IdpConnectionsOrderBy["PRIMARY_KEY_DESC"] = "PRIMARY_KEY_DESC";
1300
+ IdpConnectionsOrderBy["PROTOCOL_ASC"] = "PROTOCOL_ASC";
1301
+ IdpConnectionsOrderBy["PROTOCOL_DESC"] = "PROTOCOL_DESC";
1289
1302
  IdpConnectionsOrderBy["PROVIDER_ICON_URL_ASC"] = "PROVIDER_ICON_URL_ASC";
1290
1303
  IdpConnectionsOrderBy["PROVIDER_ICON_URL_DESC"] = "PROVIDER_ICON_URL_DESC";
1291
1304
  IdpConnectionsOrderBy["PROVIDER_ID_ASC"] = "PROVIDER_ID_ASC";
@@ -1311,6 +1324,15 @@ var IdpConnectionsOrderBy;
1311
1324
  IdpConnectionsOrderBy["USER_INFO_RETRIEVAL_WEBHOOK_ASC"] = "USER_INFO_RETRIEVAL_WEBHOOK_ASC";
1312
1325
  IdpConnectionsOrderBy["USER_INFO_RETRIEVAL_WEBHOOK_DESC"] = "USER_INFO_RETRIEVAL_WEBHOOK_DESC";
1313
1326
  })(IdpConnectionsOrderBy || (IdpConnectionsOrderBy = {}));
1327
+ var IdpProtocol;
1328
+ (function (IdpProtocol) {
1329
+ /** Delegated */
1330
+ IdpProtocol["DELEGATED"] = "DELEGATED";
1331
+ /** OAuth 2.0 */
1332
+ IdpProtocol["OAUTH2"] = "OAUTH2";
1333
+ /** OIDC */
1334
+ IdpProtocol["OIDC"] = "OIDC";
1335
+ })(IdpProtocol || (IdpProtocol = {}));
1314
1336
  /** Methods to use when ordering `IdProvider`. */
1315
1337
  var IdProvidersOrderBy;
1316
1338
  (function (IdProvidersOrderBy) {
@@ -1353,6 +1375,29 @@ var ServiceConfigurationsOrderBy;
1353
1375
  ServiceConfigurationsOrderBy["WEBHOOK_TIMEOUT_ASC"] = "WEBHOOK_TIMEOUT_ASC";
1354
1376
  ServiceConfigurationsOrderBy["WEBHOOK_TIMEOUT_DESC"] = "WEBHOOK_TIMEOUT_DESC";
1355
1377
  })(ServiceConfigurationsOrderBy || (ServiceConfigurationsOrderBy = {}));
1378
+ /** Methods to use when ordering `UserIdpDatum`. */
1379
+ var UserIdpDataOrderBy;
1380
+ (function (UserIdpDataOrderBy) {
1381
+ UserIdpDataOrderBy["CREATED_DATE_ASC"] = "CREATED_DATE_ASC";
1382
+ UserIdpDataOrderBy["CREATED_DATE_DESC"] = "CREATED_DATE_DESC";
1383
+ UserIdpDataOrderBy["IDP_CONNECTION_ID_ASC"] = "IDP_CONNECTION_ID_ASC";
1384
+ UserIdpDataOrderBy["IDP_CONNECTION_ID_DESC"] = "IDP_CONNECTION_ID_DESC";
1385
+ UserIdpDataOrderBy["IDP_SUBJECT_ID_ASC"] = "IDP_SUBJECT_ID_ASC";
1386
+ UserIdpDataOrderBy["IDP_SUBJECT_ID_DESC"] = "IDP_SUBJECT_ID_DESC";
1387
+ UserIdpDataOrderBy["INITIAL_IDP_REFRESH_TOKEN_ASC"] = "INITIAL_IDP_REFRESH_TOKEN_ASC";
1388
+ UserIdpDataOrderBy["INITIAL_IDP_REFRESH_TOKEN_DESC"] = "INITIAL_IDP_REFRESH_TOKEN_DESC";
1389
+ UserIdpDataOrderBy["NATURAL"] = "NATURAL";
1390
+ UserIdpDataOrderBy["PRIMARY_KEY_ASC"] = "PRIMARY_KEY_ASC";
1391
+ UserIdpDataOrderBy["PRIMARY_KEY_DESC"] = "PRIMARY_KEY_DESC";
1392
+ UserIdpDataOrderBy["PROVIDER_ID_ASC"] = "PROVIDER_ID_ASC";
1393
+ UserIdpDataOrderBy["PROVIDER_ID_DESC"] = "PROVIDER_ID_DESC";
1394
+ UserIdpDataOrderBy["RAW_PAYLOAD_ASC"] = "RAW_PAYLOAD_ASC";
1395
+ UserIdpDataOrderBy["RAW_PAYLOAD_DESC"] = "RAW_PAYLOAD_DESC";
1396
+ UserIdpDataOrderBy["UPDATED_DATE_ASC"] = "UPDATED_DATE_ASC";
1397
+ UserIdpDataOrderBy["UPDATED_DATE_DESC"] = "UPDATED_DATE_DESC";
1398
+ UserIdpDataOrderBy["USER_ID_ASC"] = "USER_ID_ASC";
1399
+ UserIdpDataOrderBy["USER_ID_DESC"] = "USER_ID_DESC";
1400
+ })(UserIdpDataOrderBy || (UserIdpDataOrderBy = {}));
1356
1401
  /** Methods to use when ordering `UserProfile`. */
1357
1402
  var UserProfilesOrderBy;
1358
1403
  (function (UserProfilesOrderBy) {
@@ -1417,7 +1462,8 @@ var UsersOrderBy;
1417
1462
  UsersOrderBy["UPDATED_USER_ASC"] = "UPDATED_USER_ASC";
1418
1463
  UsersOrderBy["UPDATED_USER_DESC"] = "UPDATED_USER_DESC";
1419
1464
  })(UsersOrderBy || (UsersOrderBy = {}));
1420
- const AuthenticateConsumerApplicationDocument = { "kind": "Document", "definitions": [{ "kind": "OperationDefinition", "operation": "mutation", "name": { "kind": "Name", "value": "AuthenticateConsumerApplication" }, "variableDefinitions": [{ "kind": "VariableDefinition", "variable": { "kind": "Variable", "name": { "kind": "Name", "value": "input" } }, "type": { "kind": "NonNullType", "type": { "kind": "NamedType", "name": { "kind": "Name", "value": "AuthenticateConsumerApplicationInput" } } }, "directives": [] }], "directives": [], "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "authenticateConsumerApplication" }, "arguments": [{ "kind": "Argument", "name": { "kind": "Name", "value": "input" }, "value": { "kind": "Variable", "name": { "kind": "Name", "value": "input" } } }], "directives": [], "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "accessToken" }, "arguments": [], "directives": [] }, { "kind": "Field", "name": { "kind": "Name", "value": "expiresInSeconds" }, "arguments": [], "directives": [] }, { "kind": "Field", "name": { "kind": "Name", "value": "tokenType" }, "arguments": [], "directives": [] }] } }] } }] };
1465
+ const AuthenticateConsumerApplicationDocument = { "kind": "Document", "definitions": [{ "kind": "OperationDefinition", "operation": "mutation", "name": { "kind": "Name", "value": "AuthenticateConsumerApplication" }, "variableDefinitions": [{ "kind": "VariableDefinition", "variable": { "kind": "Variable", "name": { "kind": "Name", "value": "input" } }, "type": { "kind": "NonNullType", "type": { "kind": "NamedType", "name": { "kind": "Name", "value": "AuthenticateConsumerApplicationInput" } } }, "directives": [] }], "directives": [], "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "authenticateConsumerApplication" }, "arguments": [{ "kind": "Argument", "name": { "kind": "Name", "value": "input" }, "value": { "kind": "Variable", "name": { "kind": "Name", "value": "input" } } }], "directives": [], "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "accessToken" }, "arguments": [], "directives": [] }, { "kind": "Field", "name": { "kind": "Name", "value": "expiresInSeconds" }, "arguments": [], "directives": [] }, { "kind": "Field", "name": { "kind": "Name", "value": "tokenType" }, "arguments": [], "directives": [] }] } }] } }] };
1466
+ const DecryptNativeCookieDocument = { "kind": "Document", "definitions": [{ "kind": "OperationDefinition", "operation": "mutation", "name": { "kind": "Name", "value": "DecryptNativeCookie" }, "variableDefinitions": [{ "kind": "VariableDefinition", "variable": { "kind": "Variable", "name": { "kind": "Name", "value": "input" } }, "type": { "kind": "NonNullType", "type": { "kind": "NamedType", "name": { "kind": "Name", "value": "DecryptWithKeyAesInput" } } }, "directives": [] }], "directives": [], "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "decryptWithKeyAes" }, "arguments": [{ "kind": "Argument", "name": { "kind": "Name", "value": "input" }, "value": { "kind": "Variable", "name": { "kind": "Name", "value": "input" } } }], "directives": [], "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "decryptedValue" }, "arguments": [], "directives": [] }] } }] } }] };
1421
1467
 
1422
1468
  /**
1423
1469
  * Authenticate a Consumer Application and receive a token.
@@ -1834,7 +1880,7 @@ const completePasswordReset = (completePasswordResetRequest, axAuthManagementEnd
1834
1880
  code: dist.ROPCPasswordResponseCode.SUCCESS,
1835
1881
  };
1836
1882
  });
1837
- const getIdpConfigurations = (userAuthConfig, originUrl) => __awaiter(void 0, void 0, void 0, function* () {
1883
+ const getIdpConfigurations = (userAuthConfig) => __awaiter(void 0, void 0, void 0, function* () {
1838
1884
  var _a, _b;
1839
1885
  const configResponse = yield invokeUserAuthMethod('get-user-auth-idp-config', userAuthConfig);
1840
1886
  if (configResponse.code === dist.IdpConfigurationResponseCode.SUCCESS) {
@@ -1846,7 +1892,6 @@ const getIdpConfigurations = (userAuthConfig, originUrl) => __awaiter(void 0, vo
1846
1892
  providerIconUrl: idp.providerIconUrl,
1847
1893
  title: idp.title,
1848
1894
  sortOrder: idp.sortOrder,
1849
- authUrl: `${userAuthConfig.userAuthBaseUrl}/oauth?tenantId=${userAuthConfig.tenantId}&environmentId=${userAuthConfig.environmentId}&applicationId=${userAuthConfig.applicationId}&idpConnectionId=${idp.idpConnectionId}&originUrl=${originUrl}&userAuthProxyUrl=${userAuthConfig.userAuthBaseUrl}`,
1850
1895
  };
1851
1896
  })) !== null && _b !== void 0 ? _b : []);
1852
1897
  }
@@ -1854,11 +1899,39 @@ const getIdpConfigurations = (userAuthConfig, originUrl) => __awaiter(void 0, vo
1854
1899
  return [];
1855
1900
  }
1856
1901
  });
1902
+ /**
1903
+ * Get the Authentication URL for a given IDP for web based applications.
1904
+ *
1905
+ * @param userAuthConfig The UserAuthConfig object.
1906
+ * @param idpConnectionId IDP Connection ID that should be authenticated with.
1907
+ * @param returnUrl The return URL which the User Service will redirect after completing the authentication flow.
1908
+ * @returns a URL object
1909
+ */
1910
+ const getAuthUrl = (userAuthConfig, idpConnectionId, returnUrl) => {
1911
+ const authUrl = new URL('oauth', userAuthConfig.userAuthBaseUrl);
1912
+ authUrl.searchParams.set('tenantId', userAuthConfig.tenantId);
1913
+ authUrl.searchParams.set('environmentId', userAuthConfig.environmentId);
1914
+ authUrl.searchParams.set('applicationId', userAuthConfig.applicationId);
1915
+ authUrl.searchParams.set('idpConnectionId', idpConnectionId);
1916
+ authUrl.searchParams.set('originUrl', returnUrl);
1917
+ authUrl.searchParams.set('userAuthProxyUrl', userAuthConfig.userAuthBaseUrl);
1918
+ return authUrl;
1919
+ };
1920
+ /**
1921
+ * TODO: Move this to the react-native-lib.
1922
+ * react-native does not support URL.searchParams.set() method.
1923
+ * We need to explicitly install react-native-url-polyfill package.
1924
+ * Best if we do that in react-native specific user-auth-lib. *
1925
+ */
1926
+ const getNativeAuthUrl = (userAuthConfig, idpConnectionId, returnUrl, encryptionKey) => {
1927
+ const authUrl = new URL(`oauth?tenantId=${userAuthConfig.tenantId}&environmentId=${userAuthConfig.environmentId}&applicationId=${userAuthConfig.applicationId}&idpConnectionId=${idpConnectionId}&originUrl=${returnUrl}&userAuthProxyUrl=${userAuthConfig.userAuthBaseUrl}&encryptionKey=${encryptionKey}`, userAuthConfig.userAuthBaseUrl);
1928
+ return authUrl;
1929
+ };
1857
1930
  /**
1858
1931
  * Returns a new user token by invoking the User Auth API
1859
1932
  */
1860
1933
  const fetchUserToken = (userAuthConfig) => __awaiter(void 0, void 0, void 0, function* () {
1861
- var _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
1934
+ var _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t;
1862
1935
  const tokenResponse = yield invokeUserAuthMethod('token', userAuthConfig);
1863
1936
  if (tokenResponse.code !== dist.TokenResponseCode.SUCCESS) {
1864
1937
  throw Error(tokenResponse.message);
@@ -1875,6 +1948,7 @@ const fetchUserToken = (userAuthConfig) => __awaiter(void 0, void 0, void 0, fun
1875
1948
  extensions: tokenResponse.extensions,
1876
1949
  accessToken: (_q = (_p = tokenResponse.user) === null || _p === void 0 ? void 0 : _p.token.accessToken) !== null && _q !== void 0 ? _q : '',
1877
1950
  expiresInSeconds: (_s = (_r = tokenResponse.user) === null || _r === void 0 ? void 0 : _r.token.expiresInSeconds) !== null && _s !== void 0 ? _s : 0,
1951
+ expiresAt: (_t = tokenResponse.user) === null || _t === void 0 ? void 0 : _t.token.expiresAt,
1878
1952
  };
1879
1953
  }
1880
1954
  });
@@ -1882,20 +1956,56 @@ const fetchUserToken = (userAuthConfig) => __awaiter(void 0, void 0, void 0, fun
1882
1956
  * Returns a user token. The method will ensure to avoid unnecessary API calls by caching valid user tokens.
1883
1957
  */
1884
1958
  const getUserToken = (userAuthConfig) => __awaiter(void 0, void 0, void 0, function* () {
1959
+ if ((currentUserToken === null || currentUserToken === void 0 ? void 0 : currentUserToken.expiresAt) === undefined ||
1960
+ currentUserToken.expiresAt <= new Date(Date.now())) {
1961
+ currentUserToken = null;
1962
+ }
1885
1963
  if (currentUserToken !== null) {
1886
1964
  // A valid user token is already available
1887
1965
  return Promise.resolve(currentUserToken);
1888
1966
  }
1889
1967
  currentUserToken = yield fetchUserToken(userAuthConfig);
1890
- // Clear currentUserToken when the token expires, so the next call will retrieve a new one
1891
- setTimeout(() => __awaiter(void 0, void 0, void 0, function* () {
1892
- currentUserToken = null;
1893
- }), (currentUserToken.expiresInSeconds - 60) * 1000);
1894
1968
  return currentUserToken;
1895
1969
  });
1896
1970
  const logoutUser = (userAuthConfig) => __awaiter(void 0, void 0, void 0, function* () {
1897
1971
  const logoutResponse = yield invokeUserAuthMethod('sign-out', userAuthConfig);
1898
1972
  return logoutResponse.code === dist.SignOutResponseCode.SUCCESS;
1973
+ });
1974
+ /**
1975
+ * Decrypts an encrypted User Token Cookie
1976
+ *
1977
+ * @param encryptedCookie
1978
+ * @param key
1979
+ * @param userServiceManagementEndpoint
1980
+ * @returns
1981
+ */
1982
+ const decryptNativeCookie = (encryptedCookie, key, userServiceManagementEndpoint) => __awaiter(void 0, void 0, void 0, function* () {
1983
+ const variables = {
1984
+ input: {
1985
+ encryptedValue: encryptedCookie,
1986
+ key,
1987
+ },
1988
+ };
1989
+ const decryptNativeCookieResponse = yield (yield fetch(`${userServiceManagementEndpoint}`, {
1990
+ method: 'POST',
1991
+ cache: 'no-cache',
1992
+ redirect: 'follow',
1993
+ referrerPolicy: 'origin',
1994
+ headers: {
1995
+ 'content-type': 'application/json',
1996
+ },
1997
+ body: stringifyGqlQuery(DecryptNativeCookieDocument, variables),
1998
+ })).json();
1999
+ if (decryptNativeCookieResponse.errors !== undefined) {
2000
+ return {
2001
+ code: dist.DecryptNativeCookieResponseCode.COOKIE_DECRYPTION_FAILED,
2002
+ message: decryptNativeCookieResponse.errors[0].message,
2003
+ };
2004
+ }
2005
+ return {
2006
+ code: dist.DecryptNativeCookieResponseCode.SUCCESS,
2007
+ decryptedCookie: decryptNativeCookieResponse.data.decryptWithKeyAes.decryptedValue,
2008
+ };
1899
2009
  });
1900
2010
 
1901
2011
  /** Methods to use when ordering `UserProfile`. */
@@ -2099,9 +2209,10 @@ class UserServiceClient {
2099
2209
  * @param userServiceConfig Configuration values for the user service API
2100
2210
  * @param tokenRenewalMethod Token renewal method to use
2101
2211
  */
2102
- constructor(userAuthConfig, userServiceConfig, tokenRenewalMethod = TokenRenewalMethod.ON_DEMAND) {
2212
+ constructor(userAuthConfig, userServiceConfig, setIsSignedIn, tokenRenewalMethod = TokenRenewalMethod.ON_DEMAND) {
2103
2213
  this.userAuthConfig = userAuthConfig;
2104
2214
  this.userServiceConfig = userServiceConfig;
2215
+ this.setIsSignedIn = setIsSignedIn;
2105
2216
  this.tokenRenewalMethod = tokenRenewalMethod;
2106
2217
  this._tokenChangedHandlers = [];
2107
2218
  this._tokenResponse = null;
@@ -2116,6 +2227,7 @@ class UserServiceClient {
2116
2227
  try {
2117
2228
  const userToken = yield fetchUserToken(this.userAuthConfig);
2118
2229
  const userProfile = yield getUserProfile(this.userServiceConfig.userServiceBaseUrl, userToken.accessToken, userToken.profileId);
2230
+ this.setIsSignedIn(true);
2119
2231
  return {
2120
2232
  code: 'SUCCESS',
2121
2233
  userToken,
@@ -2127,6 +2239,7 @@ class UserServiceClient {
2127
2239
  };
2128
2240
  }
2129
2241
  catch (error) {
2242
+ this.setIsSignedIn(false);
2130
2243
  assertError(error);
2131
2244
  return {
2132
2245
  code: 'ERROR',
@@ -2186,7 +2299,7 @@ class UserServiceClient {
2186
2299
  * @returns a boolean indicating if the Mosaic application has an AxAuth IDP provider configured and it is enabled.
2187
2300
  */
2188
2301
  this.isDirectSignInConfigured = () => __awaiter(this, void 0, void 0, function* () {
2189
- const idpConfigurations = yield getIdpConfigurations(this.userAuthConfig, 'https://non-existent-url');
2302
+ const idpConfigurations = yield getIdpConfigurations(this.userAuthConfig);
2190
2303
  this._axAuthIdpConfig = idpConfigurations.find((idpConfig) => idpConfig.providerId === 'AX_AUTH');
2191
2304
  return this._axAuthIdpConfig !== undefined;
2192
2305
  });
@@ -2212,15 +2325,14 @@ class UserServiceClient {
2212
2325
  /**
2213
2326
  * Registers a new user in the AX_AUTH IDP configured for the application.
2214
2327
  *
2215
- * @param originUrl
2216
2328
  * @param userSignUpRequest
2217
2329
  * @returns `UserSignUpResponse`
2218
2330
  */
2219
- this.initiateUserSignUp = (originUrl, userSignUpRequest) => __awaiter(this, void 0, void 0, function* () {
2331
+ this.initiateUserSignUp = (userSignUpRequest) => __awaiter(this, void 0, void 0, function* () {
2220
2332
  var _d;
2221
2333
  // If _axAuthIdpConfig is not fetched previously, retrieve it.
2222
2334
  if (this._axAuthIdpConfig === undefined) {
2223
- yield this.getIdpConfigurations(originUrl);
2335
+ yield this.getIdpConfigurations();
2224
2336
  }
2225
2337
  if (this._axAuthIdpConfig !== undefined &&
2226
2338
  this._axAuthIdpConfig.clientId !== null &&
@@ -2239,15 +2351,14 @@ class UserServiceClient {
2239
2351
  /**
2240
2352
  * Checks if a given User Sign-Up OTP Code is valid
2241
2353
  *
2242
- * @param originUrl
2243
2354
  * @param checkUserSignUpOtpRequest
2244
2355
  * @returns `CheckUserSignUpOtpResponse`
2245
2356
  */
2246
- this.checkUserSignUpOTP = (originUrl, checkUserSignUpOtpRequest) => __awaiter(this, void 0, void 0, function* () {
2357
+ this.checkUserSignUpOTP = (checkUserSignUpOtpRequest) => __awaiter(this, void 0, void 0, function* () {
2247
2358
  var _e;
2248
2359
  // If _axAuthIdpConfig is not fetched previously, retrieve it.
2249
2360
  if (this._axAuthIdpConfig === undefined) {
2250
- yield this.getIdpConfigurations(originUrl);
2361
+ yield this.getIdpConfigurations();
2251
2362
  }
2252
2363
  if (this._axAuthIdpConfig !== undefined &&
2253
2364
  this._axAuthIdpConfig.clientId !== null &&
@@ -2288,15 +2399,14 @@ class UserServiceClient {
2288
2399
  * This will initiate the call to the webhook configured in AX_AUTH to send the generated OTP
2289
2400
  * to the user.
2290
2401
  *
2291
- * @param originUrl
2292
2402
  * @param email
2293
2403
  * @returns `InitiatePasswordResetResponse`
2294
2404
  */
2295
- this.initiateResetPassword = (originUrl, email) => __awaiter(this, void 0, void 0, function* () {
2405
+ this.initiateResetPassword = (email) => __awaiter(this, void 0, void 0, function* () {
2296
2406
  var _g;
2297
2407
  // If _axAuthIdpConfig is not fetched previously, retrieve it.
2298
2408
  if (this._axAuthIdpConfig === undefined) {
2299
- yield this.getIdpConfigurations(originUrl);
2409
+ yield this.getIdpConfigurations();
2300
2410
  }
2301
2411
  if (this._axAuthIdpConfig !== undefined &&
2302
2412
  this._axAuthIdpConfig.clientId !== null &&
@@ -2314,15 +2424,14 @@ class UserServiceClient {
2314
2424
  /**
2315
2425
  * Checks if a given Reset Password OTP Code is valid
2316
2426
  *
2317
- * @param originUrl
2318
2427
  * @param checkPasswordResetOtpRequest
2319
2428
  * @returns `CheckPasswordResetOtpResponse`
2320
2429
  */
2321
- this.checkResetPasswordOTP = (originUrl, checkPasswordResetOtpRequest) => __awaiter(this, void 0, void 0, function* () {
2430
+ this.checkResetPasswordOTP = (checkPasswordResetOtpRequest) => __awaiter(this, void 0, void 0, function* () {
2322
2431
  var _h;
2323
2432
  // If _axAuthIdpConfig is not fetched previously, retrieve it.
2324
2433
  if (this._axAuthIdpConfig === undefined) {
2325
- yield this.getIdpConfigurations(originUrl);
2434
+ yield this.getIdpConfigurations();
2326
2435
  }
2327
2436
  if (this._axAuthIdpConfig !== undefined &&
2328
2437
  this._axAuthIdpConfig.clientId !== null &&
@@ -2341,7 +2450,6 @@ class UserServiceClient {
2341
2450
  * Completes the password reset flow for a user registered using the AX_AUTH IDP.
2342
2451
  * The user needs to input the OTP along with a new password to finish the process.
2343
2452
  *
2344
- * @param originUrl
2345
2453
  * @param completePasswordResetRequest
2346
2454
  * @returns
2347
2455
  */
@@ -2365,19 +2473,21 @@ class UserServiceClient {
2365
2473
  * When in need of a token, please call this method to get a new token.
2366
2474
  */
2367
2475
  this.getToken = () => __awaiter(this, void 0, void 0, function* () {
2476
+ var _k;
2477
+ // Set token response to null if the token has expired.
2478
+ if (this.tokenResponse !== null &&
2479
+ (((_k = this.tokenResponse.userToken) === null || _k === void 0 ? void 0 : _k.expiresAt) === undefined ||
2480
+ this.tokenResponse.userToken.expiresAt <= new Date(Date.now()))) {
2481
+ this.tokenResponse = null;
2482
+ this.setIsSignedIn(false);
2483
+ }
2368
2484
  if (this.tokenResponse !== null && this.tokenResponse.code === 'SUCCESS') {
2369
2485
  // We already have a valid token
2370
2486
  return Promise.resolve(this.tokenResponse);
2371
2487
  }
2372
2488
  this.tokenResponse = yield this.fetchToken();
2373
2489
  if (this.tokenResponse.userToken !== undefined) {
2374
- if (this.tokenRenewalMethod === TokenRenewalMethod.ON_DEMAND) {
2375
- setTimeout(() => __awaiter(this, void 0, void 0, function* () {
2376
- // Clear tokenResponse when the token expires, so the next call will retrieve a new one
2377
- this.tokenResponse = null;
2378
- }), (this.tokenResponse.userToken.expiresInSeconds - inAdvanceTokenRenewalDurationInSeconds) * 1000);
2379
- }
2380
- else if (this.tokenRenewalMethod === TokenRenewalMethod.PRE_EMPTIVE) {
2490
+ if (this.tokenRenewalMethod === TokenRenewalMethod.PRE_EMPTIVE) {
2381
2491
  // Renew the token pre-emptively
2382
2492
  yield this.recursivelyRefreshToken();
2383
2493
  }
@@ -2401,11 +2511,10 @@ class UserServiceClient {
2401
2511
  * Returns an array of IDP Configurations that are configured for the application
2402
2512
  * This list will exclude connections for provider id AX_AUTH as it is used for a user sign-up and ROPC flows.
2403
2513
  *
2404
- * @param originUrl URL to redirect to once the Oauth 2.0 flow is complete
2405
2514
  */
2406
- this.getIdpConfigurations = (originUrl) => __awaiter(this, void 0, void 0, function* () {
2515
+ this.getIdpConfigurations = () => __awaiter(this, void 0, void 0, function* () {
2407
2516
  // Extract the IDP Configuration for AX_AUTH and remove it from the ID Providers list.
2408
- const idpConfigurations = (yield getIdpConfigurations(this.userAuthConfig, originUrl)).filter((idpConfig) => {
2517
+ const idpConfigurations = (yield getIdpConfigurations(this.userAuthConfig)).filter((idpConfig) => {
2409
2518
  if (idpConfig.providerId === 'AX_AUTH') {
2410
2519
  this._axAuthIdpConfig = idpConfig;
2411
2520
  return;
@@ -2421,7 +2530,12 @@ class UserServiceClient {
2421
2530
  this.tokenResponse = null;
2422
2531
  // We want the user to be considered logged out, so we emit the TokenChanged here using 'null'
2423
2532
  this.emitTokenChanged(null);
2424
- return logoutUser(this.userAuthConfig);
2533
+ const logoutStatus = yield logoutUser(this.userAuthConfig);
2534
+ if (logoutStatus) {
2535
+ // Set the sign in status as false.
2536
+ this.setSignedInStatus(false);
2537
+ }
2538
+ return logoutStatus;
2425
2539
  });
2426
2540
  /**
2427
2541
  * Sets a given Profile ID as the active profile
@@ -2478,6 +2592,46 @@ class UserServiceClient {
2478
2592
  this.authenticateConsumerApplication = (authenticateConsumerApplicationRequest) => __awaiter(this, void 0, void 0, function* () {
2479
2593
  return authenticateConsumerApplication(authenticateConsumerApplicationRequest, this.userServiceConfig.userServiceBaseUrl);
2480
2594
  });
2595
+ /**
2596
+ * Decrypt an User Token Cookie used in Native Apps.
2597
+ * @param encryptedCookie
2598
+ * @param key
2599
+ * @returns
2600
+ */
2601
+ this.decryptNativeCookie = (encryptedCookie, key) => __awaiter(this, void 0, void 0, function* () {
2602
+ return decryptNativeCookie(encryptedCookie, key, `${this.userServiceConfig.userServiceBaseUrl}/graphql-management`);
2603
+ });
2604
+ /**
2605
+ * Returns the authentication URL for web based applications.
2606
+ *
2607
+ * @param idpConnectionId The ID of the User Service IDP Connection used for authentication.
2608
+ * @param returnUrl Redirect URL which the IDP will redirect to after the authentication flow is finished.
2609
+ * @returns a URL object with the Authentication URL
2610
+ */
2611
+ this.getAuthUrl = (idpConnectionId, returnUrl) => {
2612
+ return getAuthUrl(this.userAuthConfig, idpConnectionId, returnUrl);
2613
+ };
2614
+ /**
2615
+ * Returns the authentication URL for native applications.
2616
+ *
2617
+ * @param idpConnectionId The ID of the User Service IDP Connection used for authentication.
2618
+ * @param returnUrl Redirect URL which the IDP will redirect to after the authentication flow is finished.
2619
+ * @param encryptionKey A hex string with the length of 32 bytes that is used as the encryption key to encrypt the authorization Cookie.
2620
+ * @returns a URL object with the Authentication URL
2621
+ */
2622
+ this.getNativeAuthUrl = (idpConnectionId, returnUrl, encryptionKey) => {
2623
+ const authUrl = getNativeAuthUrl(this.userAuthConfig, idpConnectionId, returnUrl, encryptionKey);
2624
+ return authUrl;
2625
+ };
2626
+ /**
2627
+ * Sets the sign in status explicitly, allowing to re-render any
2628
+ * React components with the UserServiceProvider.
2629
+ *
2630
+ * @param signInStatus
2631
+ */
2632
+ this.setSignedInStatus = (signInStatus) => {
2633
+ this.setIsSignedIn(signInStatus);
2634
+ };
2481
2635
  // asynchronously get the ROPC endpoints at client initialization
2482
2636
  this.getROPCEndpoints();
2483
2637
  }
@@ -2499,8 +2653,9 @@ const UserServiceContext = createContext(null);
2499
2653
  * Provides the User Service Client that can be retrieved using the `useUserService` hook
2500
2654
  */
2501
2655
  const UserServiceProvider = ({ children, userAuthConfig, userServiceConfig, tokenRenewalMethod = TokenRenewalMethod.ON_DEMAND, }) => {
2502
- const client = new UserServiceClient(userAuthConfig, userServiceConfig, tokenRenewalMethod);
2503
- return (createElement(UserServiceContext.Provider, { value: client }, children));
2656
+ const [isSignedIn, setIsSignedIn] = useState(false);
2657
+ const client = new UserServiceClient(userAuthConfig, userServiceConfig, setIsSignedIn, tokenRenewalMethod);
2658
+ return (createElement(UserServiceContext.Provider, { value: Object.assign({ isSignedIn }, client) }, children));
2504
2659
  };
2505
2660
  /**
2506
2661
  * Retrieves the User Service Client