@equinor/mad-auth 0.6.5 → 0.8.0

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/CHANGELOG.md CHANGED
@@ -1,5 +1,40 @@
1
1
  # @equinor/mad-auth
2
2
 
3
+ ## 0.8.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 6a0179e: Updating minor versions since we are upgrading to Expo 57
8
+
9
+ ### Patch Changes
10
+
11
+ - 030b1d0: jest plugin preset updated
12
+ - 030b1d0: Packages upgraded to support expo 57
13
+ - fd0806d: Root Modal component updated
14
+ - Updated dependencies [030b1d0]
15
+ - Updated dependencies [030b1d0]
16
+ - Updated dependencies [fd0806d]
17
+ - Updated dependencies [6a0179e]
18
+ - @equinor/mad-components@0.26.0
19
+
20
+ ## 0.7.0
21
+
22
+ ### Minor Changes
23
+
24
+ - d060502: Add a high-level `authenticate` function that falls back from silent to interactive
25
+ authentication.
26
+
27
+ `authenticateSilently` now remains silent and returns `null` when stored refresh credentials are
28
+ expired or invalid. Consumers that may prompt the user should use `authenticate`, exported by
29
+ both `@equinor/mad-auth` and `@equinor/mad-core`. Interactive resource authentication preserves
30
+ the OIDC scopes needed to replace invalid refresh credentials, and concurrent callers share a
31
+ single browser prompt.
32
+
33
+ Native consumers can be notified when the interactive fallback is cancelled or dismissed,
34
+ including when the authorization provider returns an explicit user-cancellation response. MAD
35
+ Core uses this signal to clear the unusable session and reset its navigator to the login screen
36
+ without retaining protected route history.
37
+
3
38
  ## 0.6.5
4
39
 
5
40
  ### Patch Changes
package/README.md CHANGED
@@ -38,7 +38,24 @@ you need some inspiration. Alternatively, you can create the login flow logic fr
38
38
  provided functions below.
39
39
 
40
40
  This package provides a few basic functions you can use to either make your own login button, or use
41
- throughout the app:
41
+ throughout the app.
42
+
43
+ For authenticated API requests that may prompt the user when silent authentication fails, use:
44
+
45
+ ```ts
46
+ authenticate(
47
+ scopes: string[],
48
+ options?: { onAuthenticationCancelled?: () => void | Promise<void> },
49
+ ): Promise<MadAuthenticationResult | null>
50
+ ```
51
+
52
+ Interactive fallback requests the supplied API scopes together with the OIDC scopes required to
53
+ maintain the authenticated session. On native platforms, the optional callback runs when the user
54
+ cancels or dismisses that interactive fallback, including through the authorization provider's
55
+ user-cancellation response. Authentication still resolves to `null`.
56
+
57
+ The lower-level functions below are intended for flows that explicitly require only one
58
+ authentication mode.
42
59
 
43
60
  If you are not using our login button, nor `useAuthenticate`, use this function to initate the
44
61
  authentication client:
@@ -53,13 +70,13 @@ To check if the authentication client exists, use this function:
53
70
  authenticationClientExists(): boolean
54
71
  ```
55
72
 
56
- To authenticate interactively, use this function:
73
+ To explicitly open interactive authentication, use this function:
57
74
 
58
75
  ```ts
59
76
  authenticateInteractively(scope: string[]): Promise<MadAuthenticationResult | null>
60
77
  ```
61
78
 
62
- To authenticate silently, use this function:
79
+ To authenticate without opening interactive UI, use this function:
63
80
 
64
81
  ```ts
65
82
  authenticateSilently(scope: string[]): Promise<MadAuthenticationResult | null>
@@ -1,5 +1,5 @@
1
1
  import { AuthRequestConfig, DiscoveryDocument } from "expo-auth-session";
2
- import { MadAccount, MadAuthenticationResult } from "../../types";
2
+ import { AuthenticateOptions, MadAccount, MadAuthenticationResult } from "../../types";
3
3
  import "core-js/stable/atob";
4
4
  /**
5
5
  * Initiates the auth client by setting the needed properties in the zustand store
@@ -13,7 +13,7 @@ export declare function authenticationClientExists(): boolean;
13
13
  * user has it installed
14
14
  * @returns {MadAuthenticationResult | null} an object containing the access token and the account, or null
15
15
  */
16
- export declare const authenticateInteractively: (scopes?: string[]) => Promise<MadAuthenticationResult | null>;
16
+ export declare const authenticateInteractively: (scopes?: string[], options?: AuthenticateOptions) => Promise<MadAuthenticationResult | null>;
17
17
  /**
18
18
  * Authenticate silently.
19
19
  * @returns {MadAuthenticationResult | null} an object containing the access token and the account, or null
@@ -6,9 +6,10 @@ import {
6
6
  initiateAuthenticationClient,
7
7
  reset,
8
8
  signOut
9
- } from "../../chunk-QOOJRSKO.js";
9
+ } from "../../chunk-4YDX5Z52.js";
10
10
  import "../../chunk-7NSJ6UAV.js";
11
- import "../../chunk-43BP7XC5.js";
11
+ import "../../chunk-ESTJUQRT.js";
12
+ import "../../chunk-LAMIO5KV.js";
12
13
  import "../../chunk-26ZZDCTT.js";
13
14
  import "../../chunk-AF7VKFEK.js";
14
15
  import "../../chunk-CWMXXUWU.js";
@@ -1,8 +1,10 @@
1
1
  export { getAccountWeb, MSALAccount, MSALResult } from "./web";
2
2
  import { AuthRequestConfig, DiscoveryDocument } from "expo-auth-session";
3
+ import { AuthenticateOptions } from "../../types";
3
4
  export declare const signOut: () => boolean | Promise<boolean>;
4
- export declare const authenticateSilently: (scopes?: string[]) => Promise<import("../..").MadAuthenticationResult | null>;
5
- export declare const authenticateInteractively: (scopes?: string[]) => Promise<import("../..").MadAuthenticationResult | null>;
5
+ export declare const authenticateSilently: (scopes?: string[]) => Promise<import("../../types").MadAuthenticationResult | null>;
6
+ export declare const authenticateInteractively: (scopes?: string[]) => Promise<import("../../types").MadAuthenticationResult | null>;
7
+ export declare const authenticate: (scopes?: string[], options?: AuthenticateOptions) => Promise<import("../../types").MadAuthenticationResult | null>;
6
8
  export declare const initiateAuthenticationClient: (config: AuthRequestConfig, discovery: DiscoveryDocument) => void;
7
9
  export declare const authenticationClientExists: () => boolean;
8
- export declare const getAccount: () => Promise<import("../..").MadAccount | null>;
10
+ export declare const getAccount: () => Promise<import("../../types").MadAccount | null>;
@@ -1,11 +1,12 @@
1
1
  import {
2
+ authenticate,
2
3
  authenticateInteractively,
3
4
  authenticateSilently,
4
5
  authenticationClientExists,
5
6
  getAccount,
6
7
  initiateAuthenticationClient,
7
8
  signOut
8
- } from "../../chunk-BHEEEJRS.js";
9
+ } from "../../chunk-NIB4SJNW.js";
9
10
  import "../../chunk-MVOBRSKE.js";
10
11
  import {
11
12
  getAccountWeb
@@ -13,13 +14,15 @@ import {
13
14
  import "../../chunk-PAED4QNA.js";
14
15
  import "../../chunk-MWXZHQN3.js";
15
16
  import "../../chunk-7SOSXNKP.js";
16
- import "../../chunk-QOOJRSKO.js";
17
+ import "../../chunk-4YDX5Z52.js";
17
18
  import "../../chunk-7NSJ6UAV.js";
18
- import "../../chunk-43BP7XC5.js";
19
+ import "../../chunk-ESTJUQRT.js";
20
+ import "../../chunk-LAMIO5KV.js";
19
21
  import "../../chunk-26ZZDCTT.js";
20
22
  import "../../chunk-AF7VKFEK.js";
21
23
  import "../../chunk-CWMXXUWU.js";
22
24
  export {
25
+ authenticate,
23
26
  authenticateInteractively,
24
27
  authenticateSilently,
25
28
  authenticationClientExists,
@@ -1,16 +1,17 @@
1
1
  import {
2
2
  LoginButton
3
- } from "../../chunk-4RQPL46Z.js";
4
- import "../../chunk-MWAUPYR3.js";
5
- import "../../chunk-BHEEEJRS.js";
3
+ } from "../../chunk-PXGI4IWV.js";
4
+ import "../../chunk-JRUTRXI6.js";
5
+ import "../../chunk-NIB4SJNW.js";
6
6
  import "../../chunk-MVOBRSKE.js";
7
7
  import "../../chunk-IGJODDKF.js";
8
8
  import "../../chunk-PAED4QNA.js";
9
9
  import "../../chunk-MWXZHQN3.js";
10
10
  import "../../chunk-7SOSXNKP.js";
11
- import "../../chunk-QOOJRSKO.js";
11
+ import "../../chunk-4YDX5Z52.js";
12
12
  import "../../chunk-7NSJ6UAV.js";
13
- import "../../chunk-43BP7XC5.js";
13
+ import "../../chunk-ESTJUQRT.js";
14
+ import "../../chunk-LAMIO5KV.js";
14
15
  import "../../chunk-26ZZDCTT.js";
15
16
  import "../../chunk-AF7VKFEK.js";
16
17
  import "../../chunk-CWMXXUWU.js";
@@ -1,15 +1,16 @@
1
1
  import {
2
2
  useAccount
3
3
  } from "../../chunk-CD2D2YCX.js";
4
- import "../../chunk-BHEEEJRS.js";
4
+ import "../../chunk-NIB4SJNW.js";
5
5
  import "../../chunk-MVOBRSKE.js";
6
6
  import "../../chunk-IGJODDKF.js";
7
7
  import "../../chunk-PAED4QNA.js";
8
8
  import "../../chunk-MWXZHQN3.js";
9
9
  import "../../chunk-7SOSXNKP.js";
10
- import "../../chunk-QOOJRSKO.js";
10
+ import "../../chunk-4YDX5Z52.js";
11
11
  import "../../chunk-7NSJ6UAV.js";
12
- import "../../chunk-43BP7XC5.js";
12
+ import "../../chunk-ESTJUQRT.js";
13
+ import "../../chunk-LAMIO5KV.js";
13
14
  import "../../chunk-26ZZDCTT.js";
14
15
  import "../../chunk-AF7VKFEK.js";
15
16
  import "../../chunk-CWMXXUWU.js";
@@ -1,15 +1,16 @@
1
1
  import {
2
2
  useAuthenticate
3
- } from "../../chunk-MWAUPYR3.js";
4
- import "../../chunk-BHEEEJRS.js";
3
+ } from "../../chunk-JRUTRXI6.js";
4
+ import "../../chunk-NIB4SJNW.js";
5
5
  import "../../chunk-MVOBRSKE.js";
6
6
  import "../../chunk-IGJODDKF.js";
7
7
  import "../../chunk-PAED4QNA.js";
8
8
  import "../../chunk-MWXZHQN3.js";
9
9
  import "../../chunk-7SOSXNKP.js";
10
- import "../../chunk-QOOJRSKO.js";
10
+ import "../../chunk-4YDX5Z52.js";
11
11
  import "../../chunk-7NSJ6UAV.js";
12
- import "../../chunk-43BP7XC5.js";
12
+ import "../../chunk-ESTJUQRT.js";
13
+ import "../../chunk-LAMIO5KV.js";
13
14
  import "../../chunk-26ZZDCTT.js";
14
15
  import "../../chunk-AF7VKFEK.js";
15
16
  import "../../chunk-CWMXXUWU.js";
@@ -1,22 +1,26 @@
1
- import "../chunk-LA2WB3PZ.js";
2
- import {
3
- LoginButton
4
- } from "../chunk-4RQPL46Z.js";
1
+ import "../chunk-DKMZBOMM.js";
5
2
  import {
6
3
  useAccount
7
4
  } from "../chunk-CD2D2YCX.js";
5
+ import "../chunk-NP4GJ5X5.js";
6
+ import {
7
+ CorruptRefreshToken
8
+ } from "../chunk-PNCY5HPP.js";
9
+ import {
10
+ LoginButton
11
+ } from "../chunk-PXGI4IWV.js";
8
12
  import {
9
13
  useAuthenticate
10
- } from "../chunk-MWAUPYR3.js";
11
- import "../chunk-NP4GJ5X5.js";
14
+ } from "../chunk-JRUTRXI6.js";
12
15
  import {
16
+ authenticate,
13
17
  authenticateInteractively,
14
18
  authenticateSilently,
15
19
  authenticationClientExists,
16
20
  getAccount,
17
21
  initiateAuthenticationClient,
18
22
  signOut
19
- } from "../chunk-BHEEEJRS.js";
23
+ } from "../chunk-NIB4SJNW.js";
20
24
  import "../chunk-MVOBRSKE.js";
21
25
  import {
22
26
  getAccountWeb
@@ -24,12 +28,10 @@ import {
24
28
  import "../chunk-PAED4QNA.js";
25
29
  import "../chunk-MWXZHQN3.js";
26
30
  import "../chunk-7SOSXNKP.js";
27
- import "../chunk-QOOJRSKO.js";
31
+ import "../chunk-4YDX5Z52.js";
28
32
  import "../chunk-7NSJ6UAV.js";
29
- import "../chunk-43BP7XC5.js";
30
- import {
31
- CorruptRefreshToken
32
- } from "../chunk-PNCY5HPP.js";
33
+ import "../chunk-ESTJUQRT.js";
34
+ import "../chunk-LAMIO5KV.js";
33
35
  import {
34
36
  getAllTokens,
35
37
  getConfig,
@@ -57,6 +59,7 @@ import "../chunk-CWMXXUWU.js";
57
59
  export {
58
60
  CorruptRefreshToken,
59
61
  LoginButton,
62
+ authenticate,
60
63
  authenticateInteractively,
61
64
  authenticateSilently,
62
65
  authenticationClientExists,
@@ -0,0 +1,2 @@
1
+ export declare const requiredAuthenticationScopes: string[];
2
+ export declare const withRequiredAuthenticationScopes: (scopes?: string[]) => string[];
@@ -0,0 +1,9 @@
1
+ import {
2
+ requiredAuthenticationScopes,
3
+ withRequiredAuthenticationScopes
4
+ } from "../../chunk-ESTJUQRT.js";
5
+ import "../../chunk-CWMXXUWU.js";
6
+ export {
7
+ requiredAuthenticationScopes,
8
+ withRequiredAuthenticationScopes
9
+ };
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  tokenRefresh
3
- } from "../../chunk-43BP7XC5.js";
3
+ } from "../../chunk-LAMIO5KV.js";
4
4
  import "../../chunk-26ZZDCTT.js";
5
5
  import "../../chunk-AF7VKFEK.js";
6
6
  import "../../chunk-CWMXXUWU.js";
@@ -0,0 +1,205 @@
1
+ import {
2
+ decodeToken
3
+ } from "./chunk-7NSJ6UAV.js";
4
+ import {
5
+ withRequiredAuthenticationScopes
6
+ } from "./chunk-ESTJUQRT.js";
7
+ import {
8
+ tokenRefresh
9
+ } from "./chunk-LAMIO5KV.js";
10
+ import {
11
+ getConfig,
12
+ getDiscovery,
13
+ getRefreshToken,
14
+ getToken,
15
+ getUserData,
16
+ resetConfig,
17
+ resetDiscovery,
18
+ resetRefreshToken,
19
+ resetToken,
20
+ resetUserData,
21
+ setConfig,
22
+ setDiscovery,
23
+ setRefreshToken,
24
+ setToken,
25
+ setUserData
26
+ } from "./chunk-26ZZDCTT.js";
27
+ import {
28
+ __async,
29
+ __spreadProps,
30
+ __spreadValues
31
+ } from "./chunk-CWMXXUWU.js";
32
+
33
+ // src/Expo-AuthSession/authenticationHandler/auth.ts
34
+ import {
35
+ AuthRequest,
36
+ exchangeCodeAsync,
37
+ refreshAsync,
38
+ TokenResponse
39
+ } from "expo-auth-session";
40
+ import "core-js/stable/atob";
41
+ function initiateAuthenticationClient(config, discovery) {
42
+ setConfig(config);
43
+ setDiscovery(discovery);
44
+ }
45
+ function authenticationClientExists() {
46
+ return !!getConfig() && !!getDiscovery();
47
+ }
48
+ var resetRefreshTokenIfCurrent = (failedRefreshToken) => {
49
+ if (failedRefreshToken && getRefreshToken() === failedRefreshToken) {
50
+ resetRefreshToken();
51
+ }
52
+ };
53
+ var authenticationCancellationErrors = /* @__PURE__ */ new Set(["access_denied"]);
54
+ var runInteractiveAuthentication = (scopes) => __async(null, null, function* () {
55
+ var _a, _b, _c;
56
+ const discovery = getDiscovery();
57
+ const config = getConfig();
58
+ if (!discovery || !config) return { type: "failed" };
59
+ const requestConfig = __spreadProps(__spreadValues({}, config), {
60
+ scopes: withRequiredAuthenticationScopes(scopes != null ? scopes : config.scopes)
61
+ });
62
+ const authRequest = new AuthRequest(requestConfig);
63
+ const codeResponse = yield authRequest.promptAsync(discovery);
64
+ if ((codeResponse == null ? void 0 : codeResponse.type) === "success") {
65
+ const accessTokenConfig = {
66
+ clientId: requestConfig.clientId,
67
+ code: codeResponse.params["code"],
68
+ extraParams: authRequest.codeVerifier ? { code_verifier: authRequest.codeVerifier } : void 0,
69
+ redirectUri: requestConfig.redirectUri
70
+ };
71
+ const tokenResponse = yield exchangeCodeAsync(accessTokenConfig, discovery);
72
+ const userData = decodeToken(tokenResponse.idToken);
73
+ if (!userData) return { type: "failed" };
74
+ setUserData(userData);
75
+ setToken(tokenResponse);
76
+ if (tokenResponse.refreshToken) {
77
+ setRefreshToken(tokenResponse.refreshToken);
78
+ } else {
79
+ resetRefreshToken();
80
+ }
81
+ return {
82
+ type: "success",
83
+ result: { account: userData, accessToken: tokenResponse.accessToken }
84
+ };
85
+ }
86
+ const authenticationError = (codeResponse == null ? void 0 : codeResponse.type) === "error" ? (_c = (_a = codeResponse.params) == null ? void 0 : _a["error"]) != null ? _c : (_b = codeResponse.error) == null ? void 0 : _b.code : void 0;
87
+ if ((codeResponse == null ? void 0 : codeResponse.type) === "cancel" || (codeResponse == null ? void 0 : codeResponse.type) === "dismiss" || authenticationError !== void 0 && authenticationCancellationErrors.has(authenticationError)) {
88
+ return { type: "cancelled" };
89
+ }
90
+ return { type: "failed" };
91
+ });
92
+ var interactiveAuthInFlight = null;
93
+ var interactiveAuthInFlightScopes;
94
+ var scopesMatch = (left, right) => (left == null ? void 0 : left.length) === (right == null ? void 0 : right.length) && (left == null ? void 0 : left.every((scope) => right == null ? void 0 : right.includes(scope)));
95
+ var authenticateInteractively = (scopes, options) => __async(null, null, function* () {
96
+ var _a, _b;
97
+ if (interactiveAuthInFlight) {
98
+ const inFlightAuthentication = interactiveAuthInFlight;
99
+ const inFlightScopes = interactiveAuthInFlightScopes;
100
+ const sharedOutcome = yield inFlightAuthentication;
101
+ if (sharedOutcome.type === "cancelled") {
102
+ yield (_a = options == null ? void 0 : options.onAuthenticationCancelled) == null ? void 0 : _a.call(options);
103
+ return null;
104
+ }
105
+ if (sharedOutcome.type === "failed") return null;
106
+ if (scopesMatch(scopes, inFlightScopes)) return sharedOutcome.result;
107
+ return authenticateSilently(scopes);
108
+ }
109
+ interactiveAuthInFlightScopes = scopes;
110
+ interactiveAuthInFlight = runInteractiveAuthentication(scopes);
111
+ try {
112
+ const outcome = yield interactiveAuthInFlight;
113
+ if (outcome.type === "cancelled") {
114
+ yield (_b = options == null ? void 0 : options.onAuthenticationCancelled) == null ? void 0 : _b.call(options);
115
+ return null;
116
+ }
117
+ return outcome.type === "success" ? outcome.result : null;
118
+ } finally {
119
+ interactiveAuthInFlight = null;
120
+ interactiveAuthInFlightScopes = void 0;
121
+ }
122
+ });
123
+ var authenticateSilently = (scopes) => __async(null, null, function* () {
124
+ const userData = getUserData();
125
+ const token = getToken(scopes);
126
+ if (!userData) return null;
127
+ if (token) {
128
+ if (TokenResponse.isTokenFresh(token)) {
129
+ return {
130
+ account: userData,
131
+ accessToken: token.accessToken
132
+ };
133
+ }
134
+ try {
135
+ const newToken = yield tokenRefresh(token, scopes);
136
+ if (newToken && TokenResponse.isTokenFresh(newToken)) {
137
+ setToken(newToken);
138
+ if (newToken.refreshToken) setRefreshToken(newToken.refreshToken);
139
+ return {
140
+ account: userData,
141
+ accessToken: newToken.accessToken
142
+ };
143
+ }
144
+ } catch (e) {
145
+ resetRefreshTokenIfCurrent(token.refreshToken);
146
+ }
147
+ return null;
148
+ } else {
149
+ const refreshToken = getRefreshToken();
150
+ const config = getConfig();
151
+ const discovery = getDiscovery();
152
+ if (!config || !discovery) return null;
153
+ if (refreshToken) {
154
+ try {
155
+ const refreshed = yield refreshAsync(
156
+ {
157
+ clientId: config.clientId,
158
+ refreshToken,
159
+ scopes
160
+ },
161
+ discovery
162
+ );
163
+ if (TokenResponse.isTokenFresh(refreshed)) {
164
+ setToken(refreshed);
165
+ if (refreshed.refreshToken) setRefreshToken(refreshed.refreshToken);
166
+ return {
167
+ account: userData,
168
+ accessToken: refreshed.accessToken
169
+ };
170
+ }
171
+ } catch (e) {
172
+ resetRefreshTokenIfCurrent(refreshToken);
173
+ return null;
174
+ }
175
+ }
176
+ return null;
177
+ }
178
+ });
179
+ function getAccount() {
180
+ var _a;
181
+ return (_a = getUserData()) != null ? _a : null;
182
+ }
183
+ function signOut() {
184
+ if (!authenticationClientExists()) {
185
+ throw new Error("Unable to authenticate, authentication client does not exist");
186
+ }
187
+ resetUserData();
188
+ resetToken();
189
+ resetRefreshToken();
190
+ return true;
191
+ }
192
+ function reset() {
193
+ resetDiscovery();
194
+ resetConfig();
195
+ }
196
+
197
+ export {
198
+ initiateAuthenticationClient,
199
+ authenticationClientExists,
200
+ authenticateInteractively,
201
+ authenticateSilently,
202
+ getAccount,
203
+ signOut,
204
+ reset
205
+ };
@@ -1,26 +1,27 @@
1
- import {
2
- LoginButton
3
- } from "./chunk-4RQPL46Z.js";
4
1
  import {
5
2
  useAccount
6
3
  } from "./chunk-CD2D2YCX.js";
4
+ import {
5
+ CorruptRefreshToken
6
+ } from "./chunk-PNCY5HPP.js";
7
+ import {
8
+ LoginButton
9
+ } from "./chunk-PXGI4IWV.js";
7
10
  import {
8
11
  useAuthenticate
9
- } from "./chunk-MWAUPYR3.js";
12
+ } from "./chunk-JRUTRXI6.js";
10
13
  import {
14
+ authenticate,
11
15
  authenticateInteractively,
12
16
  authenticateSilently,
13
17
  authenticationClientExists,
14
18
  getAccount,
15
19
  initiateAuthenticationClient,
16
20
  signOut
17
- } from "./chunk-BHEEEJRS.js";
21
+ } from "./chunk-NIB4SJNW.js";
18
22
  import {
19
23
  getAccountWeb
20
24
  } from "./chunk-IGJODDKF.js";
21
- import {
22
- CorruptRefreshToken
23
- } from "./chunk-PNCY5HPP.js";
24
25
  import {
25
26
  getAllTokens,
26
27
  getConfig,
@@ -52,6 +53,7 @@ var Expo_AuthSession_exports = {};
52
53
  __export(Expo_AuthSession_exports, {
53
54
  CorruptRefreshToken: () => CorruptRefreshToken,
54
55
  LoginButton: () => LoginButton,
56
+ authenticate: () => authenticate,
55
57
  authenticateInteractively: () => authenticateInteractively,
56
58
  authenticateSilently: () => authenticateSilently,
57
59
  authenticationClientExists: () => authenticationClientExists,
@@ -0,0 +1,10 @@
1
+ // src/Expo-AuthSession/utils/scopes.ts
2
+ var requiredAuthenticationScopes = ["openid", "profile", "offline_access"];
3
+ var withRequiredAuthenticationScopes = (scopes = []) => [
4
+ .../* @__PURE__ */ new Set([...scopes, ...requiredAuthenticationScopes])
5
+ ];
6
+
7
+ export {
8
+ requiredAuthenticationScopes,
9
+ withRequiredAuthenticationScopes
10
+ };
@@ -3,7 +3,7 @@ import {
3
3
  authenticateSilently,
4
4
  authenticationClientExists,
5
5
  initiateAuthenticationClient
6
- } from "./chunk-BHEEEJRS.js";
6
+ } from "./chunk-NIB4SJNW.js";
7
7
  import {
8
8
  __async
9
9
  } from "./chunk-CWMXXUWU.js";
@@ -3,7 +3,9 @@ import {
3
3
  getDiscovery
4
4
  } from "./chunk-26ZZDCTT.js";
5
5
  import {
6
- __async
6
+ __async,
7
+ __spreadProps,
8
+ __spreadValues
7
9
  } from "./chunk-CWMXXUWU.js";
8
10
 
9
11
  // src/Expo-AuthSession/utils/tokenRefresh.ts
@@ -13,10 +15,8 @@ var tokenRefresh = (token, scopes) => __async(null, null, function* () {
13
15
  if (!discovery || !config) {
14
16
  return;
15
17
  }
16
- if (scopes) {
17
- config.scopes = scopes;
18
- }
19
- return yield token.refreshAsync(config, discovery);
18
+ const requestConfig = scopes ? __spreadProps(__spreadValues({}, config), { scopes }) : config;
19
+ return yield token.refreshAsync(requestConfig, discovery);
20
20
  });
21
21
 
22
22
  export {
@@ -13,13 +13,21 @@ import {
13
13
  getAccount,
14
14
  initiateAuthenticationClient,
15
15
  signOut
16
- } from "./chunk-QOOJRSKO.js";
16
+ } from "./chunk-4YDX5Z52.js";
17
+ import {
18
+ __async
19
+ } from "./chunk-CWMXXUWU.js";
17
20
 
18
21
  // src/Expo-AuthSession/authenticationHandler/index.ts
19
22
  import { Platform } from "react-native";
20
23
  var signOut2 = () => Platform.OS === "web" ? signOutWeb() : signOut();
21
24
  var authenticateSilently2 = (scopes) => Platform.OS === "web" ? authenticateSilentlyWeb(scopes) : authenticateSilently(scopes);
22
25
  var authenticateInteractively2 = (scopes) => Platform.OS === "web" ? authenticateInteractivelyWeb(scopes) : authenticateInteractively(scopes);
26
+ var authenticate = (scopes, options) => __async(null, null, function* () {
27
+ const silentResult = yield authenticateSilently2(scopes);
28
+ if (silentResult) return silentResult;
29
+ return Platform.OS === "web" ? authenticateInteractivelyWeb(scopes) : authenticateInteractively(scopes, options);
30
+ });
23
31
  var initiateAuthenticationClient2 = (config, discovery) => Platform.OS === "web" ? initiateAuthenticationClientWeb({
24
32
  clientId: config.clientId,
25
33
  redirectUri: config.redirectUri
@@ -31,6 +39,7 @@ export {
31
39
  signOut2 as signOut,
32
40
  authenticateSilently2 as authenticateSilently,
33
41
  authenticateInteractively2 as authenticateInteractively,
42
+ authenticate,
34
43
  initiateAuthenticationClient2 as initiateAuthenticationClient,
35
44
  authenticationClientExists2 as authenticationClientExists,
36
45
  getAccount2 as getAccount
@@ -1,6 +1,9 @@
1
1
  import {
2
2
  useAuthenticate
3
- } from "./chunk-MWAUPYR3.js";
3
+ } from "./chunk-JRUTRXI6.js";
4
+ import {
5
+ withRequiredAuthenticationScopes
6
+ } from "./chunk-ESTJUQRT.js";
4
7
  import {
5
8
  __objRest,
6
9
  __spreadValues
@@ -34,7 +37,7 @@ var LoginButton = (_a) => {
34
37
  ]);
35
38
  const config = {
36
39
  clientId,
37
- scopes: [...scopes, "openid", "profile", "offline_access"],
40
+ scopes: withRequiredAuthenticationScopes(scopes),
38
41
  redirectUri,
39
42
  responseType: ResponseType.Code
40
43
  };
package/dist/index.js CHANGED
@@ -1,21 +1,22 @@
1
1
  import "./chunk-6F4PWJZI.js";
2
2
  import {
3
3
  Expo_AuthSession_exports
4
- } from "./chunk-LA2WB3PZ.js";
5
- import "./chunk-4RQPL46Z.js";
4
+ } from "./chunk-DKMZBOMM.js";
6
5
  import "./chunk-CD2D2YCX.js";
7
- import "./chunk-MWAUPYR3.js";
8
6
  import "./chunk-NP4GJ5X5.js";
9
- import "./chunk-BHEEEJRS.js";
7
+ import "./chunk-PNCY5HPP.js";
8
+ import "./chunk-PXGI4IWV.js";
9
+ import "./chunk-JRUTRXI6.js";
10
+ import "./chunk-NIB4SJNW.js";
10
11
  import "./chunk-MVOBRSKE.js";
11
12
  import "./chunk-IGJODDKF.js";
12
13
  import "./chunk-PAED4QNA.js";
13
14
  import "./chunk-MWXZHQN3.js";
14
15
  import "./chunk-7SOSXNKP.js";
15
- import "./chunk-QOOJRSKO.js";
16
+ import "./chunk-4YDX5Z52.js";
16
17
  import "./chunk-7NSJ6UAV.js";
17
- import "./chunk-43BP7XC5.js";
18
- import "./chunk-PNCY5HPP.js";
18
+ import "./chunk-ESTJUQRT.js";
19
+ import "./chunk-LAMIO5KV.js";
19
20
  import "./chunk-26ZZDCTT.js";
20
21
  import "./chunk-AF7VKFEK.js";
21
22
  import "./chunk-CWMXXUWU.js";
package/dist/types.d.ts CHANGED
@@ -7,3 +7,6 @@ export type MadAuthenticationResult = {
7
7
  account: MadAccount;
8
8
  accessToken: string;
9
9
  };
10
+ export type AuthenticateOptions = {
11
+ onAuthenticationCancelled?: () => void | Promise<void>;
12
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@equinor/mad-auth",
3
3
  "description": "An authentication library that uses expo-auth-session (mobile) and @azure/msal-browser (web) under the hood",
4
- "version": "0.6.5",
4
+ "version": "0.8.0",
5
5
  "author": {
6
6
  "name": "Equinor Mobile App Delivery team (MAD)"
7
7
  },
@@ -12,7 +12,7 @@
12
12
  "@types/aes-js": "^3.1.4",
13
13
  "aes-js": "^3.1.2",
14
14
  "core-js": "^3.37.0",
15
- "expo-secure-store": "~55.0.14",
15
+ "expo-secure-store": "~57.0.3",
16
16
  "jwt-decode": "^4.0.0",
17
17
  "react-native-get-random-values": "^1.11.0",
18
18
  "zustand": "^4.5.2"
@@ -21,14 +21,16 @@
21
21
  "@babel/core": "^7.25.2",
22
22
  "@equinor/eslint-config-mad": "*",
23
23
  "@equinor/mad-tsconfig": "*",
24
+ "@react-native/jest-preset": "~0.86.3",
24
25
  "@testing-library/jest-native": "^5.4.3",
25
26
  "@testing-library/react": "^14.0.0",
26
27
  "@testing-library/react-native": "^12.3.0",
27
28
  "@types/showdown": "^2.0.0",
28
29
  "eslint": "^8.46.0",
30
+ "expo-auth-session": "~57.0.11",
29
31
  "jest": "^29.7.0",
30
- "jest-expo": "~55.0.16",
31
- "react-test-renderer": "19.0.0",
32
+ "jest-expo": "~57.0.5",
33
+ "react-test-renderer": "19.2.3",
32
34
  "tsup": "^8.3.5"
33
35
  },
34
36
  "files": [
@@ -48,11 +50,11 @@
48
50
  "main": "./dist/index.js",
49
51
  "peerDependencies": {
50
52
  "@azure/msal-browser": "^3.30.0",
51
- "expo-auth-session": "~55.0.15",
52
- "expo-crypto": "~55.0.14",
53
+ "expo-auth-session": "~57.0.11",
54
+ "expo-crypto": "~57.0.2",
53
55
  "react": "^19.2.0",
54
56
  "react-dom": "^19.2.0",
55
- "react-native": "~0.83.6"
57
+ "react-native": "~0.86.3"
56
58
  },
57
59
  "scripts": {
58
60
  "build": "tsup-node && echo '\n ⚙️ Generating typescript declarations..' && tsc --project tsconfig.json --emitDeclarationOnly --declaration",
@@ -1,146 +0,0 @@
1
- import {
2
- decodeToken
3
- } from "./chunk-7NSJ6UAV.js";
4
- import {
5
- tokenRefresh
6
- } from "./chunk-43BP7XC5.js";
7
- import {
8
- getConfig,
9
- getDiscovery,
10
- getRefreshToken,
11
- getToken,
12
- getUserData,
13
- resetConfig,
14
- resetDiscovery,
15
- resetRefreshToken,
16
- resetToken,
17
- resetUserData,
18
- setConfig,
19
- setDiscovery,
20
- setRefreshToken,
21
- setToken,
22
- setUserData
23
- } from "./chunk-26ZZDCTT.js";
24
- import {
25
- __async
26
- } from "./chunk-CWMXXUWU.js";
27
-
28
- // src/Expo-AuthSession/authenticationHandler/auth.ts
29
- import {
30
- AuthRequest,
31
- exchangeCodeAsync,
32
- refreshAsync,
33
- TokenResponse
34
- } from "expo-auth-session";
35
- import "core-js/stable/atob";
36
- function initiateAuthenticationClient(config, discovery) {
37
- setConfig(config);
38
- setDiscovery(discovery);
39
- }
40
- function authenticationClientExists() {
41
- return !!getConfig() && !!getDiscovery();
42
- }
43
- var authenticateInteractively = (scopes) => __async(null, null, function* () {
44
- const discovery = getDiscovery();
45
- const config = getConfig();
46
- if (!discovery || !config) return null;
47
- if (scopes) {
48
- config.scopes = scopes;
49
- }
50
- const authRequest = new AuthRequest(config);
51
- const codeResponse = yield authRequest.promptAsync(discovery);
52
- if ((codeResponse == null ? void 0 : codeResponse.type) === "success") {
53
- const accessTokenConfig = {
54
- clientId: config.clientId,
55
- code: codeResponse.params["code"],
56
- extraParams: authRequest.codeVerifier ? { code_verifier: authRequest.codeVerifier } : void 0,
57
- redirectUri: config.redirectUri
58
- };
59
- const tokenResponse = yield exchangeCodeAsync(accessTokenConfig, discovery);
60
- const userData = decodeToken(tokenResponse.idToken);
61
- if (!userData) return null;
62
- setUserData(userData);
63
- setToken(tokenResponse);
64
- if (tokenResponse.refreshToken) setRefreshToken(tokenResponse.refreshToken);
65
- return { account: userData, accessToken: tokenResponse.accessToken };
66
- }
67
- return null;
68
- });
69
- var authenticateSilently = (scopes) => __async(null, null, function* () {
70
- const userData = getUserData();
71
- const token = getToken(scopes);
72
- if (!userData) return null;
73
- if (token) {
74
- if (TokenResponse.isTokenFresh(token)) {
75
- return {
76
- account: userData,
77
- accessToken: token.accessToken
78
- };
79
- }
80
- const newToken = yield tokenRefresh(token, scopes);
81
- if (newToken) {
82
- setToken(newToken);
83
- if (newToken.refreshToken) setRefreshToken(newToken.refreshToken);
84
- return {
85
- account: userData,
86
- accessToken: newToken.accessToken
87
- };
88
- }
89
- const interactiveToken = yield authenticateInteractively();
90
- return interactiveToken;
91
- } else {
92
- const refreshToken = getRefreshToken();
93
- const config = getConfig();
94
- const discovery = getDiscovery();
95
- if (!config || !discovery) return null;
96
- if (refreshToken) {
97
- try {
98
- const refreshed = yield refreshAsync(
99
- {
100
- clientId: config.clientId,
101
- refreshToken,
102
- scopes
103
- },
104
- discovery
105
- );
106
- setToken(refreshed);
107
- if (refreshed.refreshToken) setRefreshToken(refreshed.refreshToken);
108
- return {
109
- account: userData,
110
- accessToken: refreshed.accessToken
111
- };
112
- } catch (e) {
113
- const authResp = yield authenticateInteractively();
114
- return authResp;
115
- }
116
- }
117
- return null;
118
- }
119
- });
120
- function getAccount() {
121
- var _a;
122
- return (_a = getUserData()) != null ? _a : null;
123
- }
124
- function signOut() {
125
- if (!authenticationClientExists()) {
126
- throw new Error("Unable to authenticate, authentication client does not exist");
127
- }
128
- resetUserData();
129
- resetToken();
130
- resetRefreshToken();
131
- return true;
132
- }
133
- function reset() {
134
- resetDiscovery();
135
- resetConfig();
136
- }
137
-
138
- export {
139
- initiateAuthenticationClient,
140
- authenticationClientExists,
141
- authenticateInteractively,
142
- authenticateSilently,
143
- getAccount,
144
- signOut,
145
- reset
146
- };