@equinor/mad-auth 0.6.4 → 0.7.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,31 @@
1
1
  # @equinor/mad-auth
2
2
 
3
+ ## 0.7.0
4
+
5
+ ### Minor Changes
6
+
7
+ - d060502: Add a high-level `authenticate` function that falls back from silent to interactive
8
+ authentication.
9
+
10
+ `authenticateSilently` now remains silent and returns `null` when stored refresh credentials are
11
+ expired or invalid. Consumers that may prompt the user should use `authenticate`, exported by
12
+ both `@equinor/mad-auth` and `@equinor/mad-core`. Interactive resource authentication preserves
13
+ the OIDC scopes needed to replace invalid refresh credentials, and concurrent callers share a
14
+ single browser prompt.
15
+
16
+ Native consumers can be notified when the interactive fallback is cancelled or dismissed,
17
+ including when the authorization provider returns an explicit user-cancellation response. MAD
18
+ Core uses this signal to clear the unusable session and reset its navigator to the login screen
19
+ without retaining protected route history.
20
+
21
+ ## 0.6.5
22
+
23
+ ### Patch Changes
24
+
25
+ - bb939ed: Type support added back for navigation factories
26
+ - Updated dependencies [bb939ed]
27
+ - @equinor/mad-components@0.25.5
28
+
3
29
  ## 0.6.4
4
30
 
5
31
  ### 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,25 +1,23 @@
1
- import "../chunk-XP7PYJNY.js";
1
+ import "../chunk-2DUW4YVG.js";
2
+ import {
3
+ LoginButton
4
+ } from "../chunk-PXGI4IWV.js";
2
5
  import {
3
6
  useAccount
4
7
  } 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-4RQPL46Z.js";
12
8
  import {
13
9
  useAuthenticate
14
- } from "../chunk-MWAUPYR3.js";
10
+ } from "../chunk-JRUTRXI6.js";
11
+ import "../chunk-NP4GJ5X5.js";
15
12
  import {
13
+ authenticate,
16
14
  authenticateInteractively,
17
15
  authenticateSilently,
18
16
  authenticationClientExists,
19
17
  getAccount,
20
18
  initiateAuthenticationClient,
21
19
  signOut
22
- } from "../chunk-BHEEEJRS.js";
20
+ } from "../chunk-NIB4SJNW.js";
23
21
  import "../chunk-MVOBRSKE.js";
24
22
  import {
25
23
  getAccountWeb
@@ -27,9 +25,13 @@ import {
27
25
  import "../chunk-PAED4QNA.js";
28
26
  import "../chunk-MWXZHQN3.js";
29
27
  import "../chunk-7SOSXNKP.js";
30
- import "../chunk-QOOJRSKO.js";
28
+ import "../chunk-4YDX5Z52.js";
31
29
  import "../chunk-7NSJ6UAV.js";
32
- import "../chunk-43BP7XC5.js";
30
+ import "../chunk-ESTJUQRT.js";
31
+ import "../chunk-LAMIO5KV.js";
32
+ import {
33
+ CorruptRefreshToken
34
+ } from "../chunk-PNCY5HPP.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";
@@ -1,26 +1,27 @@
1
+ import {
2
+ LoginButton
3
+ } from "./chunk-PXGI4IWV.js";
1
4
  import {
2
5
  useAccount
3
6
  } from "./chunk-CD2D2YCX.js";
4
- import {
5
- CorruptRefreshToken
6
- } from "./chunk-PNCY5HPP.js";
7
- import {
8
- LoginButton
9
- } from "./chunk-4RQPL46Z.js";
10
7
  import {
11
8
  useAuthenticate
12
- } from "./chunk-MWAUPYR3.js";
9
+ } from "./chunk-JRUTRXI6.js";
13
10
  import {
11
+ authenticate,
14
12
  authenticateInteractively,
15
13
  authenticateSilently,
16
14
  authenticationClientExists,
17
15
  getAccount,
18
16
  initiateAuthenticationClient,
19
17
  signOut
20
- } from "./chunk-BHEEEJRS.js";
18
+ } from "./chunk-NIB4SJNW.js";
21
19
  import {
22
20
  getAccountWeb
23
21
  } from "./chunk-IGJODDKF.js";
22
+ import {
23
+ CorruptRefreshToken
24
+ } 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,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
+ };
@@ -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-XP7PYJNY.js";
4
+ } from "./chunk-2DUW4YVG.js";
5
+ import "./chunk-PXGI4IWV.js";
5
6
  import "./chunk-CD2D2YCX.js";
7
+ import "./chunk-JRUTRXI6.js";
6
8
  import "./chunk-NP4GJ5X5.js";
7
- import "./chunk-PNCY5HPP.js";
8
- import "./chunk-4RQPL46Z.js";
9
- import "./chunk-MWAUPYR3.js";
10
- import "./chunk-BHEEEJRS.js";
9
+ import "./chunk-NIB4SJNW.js";
11
10
  import "./chunk-MVOBRSKE.js";
12
11
  import "./chunk-IGJODDKF.js";
13
12
  import "./chunk-PAED4QNA.js";
14
13
  import "./chunk-MWXZHQN3.js";
15
14
  import "./chunk-7SOSXNKP.js";
16
- import "./chunk-QOOJRSKO.js";
15
+ import "./chunk-4YDX5Z52.js";
17
16
  import "./chunk-7NSJ6UAV.js";
18
- import "./chunk-43BP7XC5.js";
17
+ import "./chunk-ESTJUQRT.js";
18
+ import "./chunk-LAMIO5KV.js";
19
+ import "./chunk-PNCY5HPP.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.4",
4
+ "version": "0.7.0",
5
5
  "author": {
6
6
  "name": "Equinor Mobile App Delivery team (MAD)"
7
7
  },
@@ -26,6 +26,7 @@
26
26
  "@testing-library/react-native": "^12.3.0",
27
27
  "@types/showdown": "^2.0.0",
28
28
  "eslint": "^8.46.0",
29
+ "expo-auth-session": "~55.0.15",
29
30
  "jest": "^29.7.0",
30
31
  "jest-expo": "~55.0.16",
31
32
  "react-test-renderer": "19.0.0",
@@ -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
- };