@equinor/mad-auth 0.0.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/CHANGELOG.md +0 -0
- package/CODE_OF_CONDUCT.md +49 -0
- package/LICENSE +21 -0
- package/README.md +98 -0
- package/dist/_internal/translationLayer.d.ts +4 -0
- package/dist/_internal/translationLayer.js +8 -0
- package/dist/auth.d.ts +35 -0
- package/dist/auth.js +17 -0
- package/dist/chunk-6F4PWJZI.js +0 -0
- package/dist/chunk-GJUHWXAK.js +68 -0
- package/dist/chunk-GUKC6UGZ.js +0 -0
- package/dist/chunk-GZV7V4EL.js +113 -0
- package/dist/chunk-HAB34YS7.js +0 -0
- package/dist/chunk-O5YSBQA2.js +53 -0
- package/dist/chunk-VC6L6TBN.js +46 -0
- package/dist/chunk-YGQY3H47.js +28 -0
- package/dist/components/LoginButton.d.ts +10 -0
- package/dist/components/LoginButton.js +9 -0
- package/dist/components/index.d.ts +1 -0
- package/dist/components/index.js +10 -0
- package/dist/hooks/index.d.ts +2 -0
- package/dist/hooks/index.js +13 -0
- package/dist/hooks/useAccount.d.ts +5 -0
- package/dist/hooks/useAccount.js +8 -0
- package/dist/hooks/useAuthenticate.d.ts +19 -0
- package/dist/hooks/useAuthenticate.js +8 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +32 -0
- package/dist/types.d.ts +9 -0
- package/dist/types.js +1 -0
- package/package.json +46 -0
package/CHANGELOG.md
ADDED
|
File without changes
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# How we work
|
|
2
|
+
|
|
3
|
+
In Equinor, [how we deliver is as important as what we
|
|
4
|
+
deliver](https://www.equinor.com/en/careers/our-culture.html).
|
|
5
|
+
|
|
6
|
+
This set the expectations for how we collaborate in our open source projects,
|
|
7
|
+
and applies to all community members and participants in any Equinor maintained
|
|
8
|
+
project.
|
|
9
|
+
|
|
10
|
+
## Handling issues within the communities
|
|
11
|
+
|
|
12
|
+
We expect all to have a low threshold for raising issues, or in general discuss
|
|
13
|
+
how we live up to our
|
|
14
|
+
[values](https://www.equinor.com/en/about-us.html#our-values). Equally, we also
|
|
15
|
+
encourage all community members to appreciate when concerns are raised, and make
|
|
16
|
+
its best effort in solving them.
|
|
17
|
+
|
|
18
|
+
As well as responsibility for what is delivered the project maintainers are also
|
|
19
|
+
responsible creating an environment for proper handling of issues raised within
|
|
20
|
+
the communities.
|
|
21
|
+
|
|
22
|
+
## Call out for assistance
|
|
23
|
+
|
|
24
|
+
For any problem not directly resolvable within the community, we encourage you
|
|
25
|
+
to call out for assistance. Getting an outsiders perspective on the topic might
|
|
26
|
+
be just what is needed for you to proceed.
|
|
27
|
+
|
|
28
|
+
Send an e-mail to opensource_at_equinor.com and invite for a discussion. The
|
|
29
|
+
email will be handled by a team within the Equinor organization.
|
|
30
|
+
|
|
31
|
+
Your request will be kept confidential from the team or community in question,
|
|
32
|
+
unless you chose to disclose it yourself.
|
|
33
|
+
|
|
34
|
+
## Ethics helpline
|
|
35
|
+
|
|
36
|
+
In Equinor, we want you to speak up whenever you see unethical behaviour that
|
|
37
|
+
conflicts with our values or threatens our reputation.
|
|
38
|
+
|
|
39
|
+
To underline this, we continuously encourage and remind our employees and any
|
|
40
|
+
external third parties interacting with us to raise concerns or report any
|
|
41
|
+
suspected or potential breaches of law or company policies.
|
|
42
|
+
|
|
43
|
+
For any questions or issues that one suspect falls outside the scope of
|
|
44
|
+
behaviour regulated, and handled within the open source code of conduct, or you
|
|
45
|
+
wish to place an anonymous, confidential report we encourage to use the [Equinor
|
|
46
|
+
Ethics
|
|
47
|
+
helpline](https://secure.ethicspoint.eu/domain/media/en/gui/102166/index.html).
|
|
48
|
+
|
|
49
|
+
This helpline is hosted by a third party helpline provider.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Equinor ASA
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# MAD AUTH
|
|
2
|
+
|
|
3
|
+
A simple authentication package used by the mad-team. It uses MSAL under the hood, and requires you
|
|
4
|
+
to set up an app registration in azure.
|
|
5
|
+
|
|
6
|
+
# Usage
|
|
7
|
+
|
|
8
|
+
You have to install `react-native-msal` in your app in order to make this work. `react-native-msal`
|
|
9
|
+
is an archived package no longer maintained, but we have created a fork which you can find
|
|
10
|
+
[here](https://github.com/equinor/react-native-msal). Install the newest commit by putting this in
|
|
11
|
+
your `package.json`:
|
|
12
|
+
|
|
13
|
+
```json
|
|
14
|
+
{
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"react-native-msal": "git+https://github.com/equinor/react-native-msal.git#COMMIT_HASH"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
PS: you might need `yarn` installed on your computer for it to work.
|
|
22
|
+
|
|
23
|
+
After installing it, follow
|
|
24
|
+
[this guide if you use expo](https://github.com/equinor/react-native-msal/blob/main/docs/expo_setup.md),
|
|
25
|
+
or
|
|
26
|
+
[this guide for iOS setup in a bare react native project](https://github.com/equinor/react-native-msal/blob/main/docs/ios_setup.md).
|
|
27
|
+
Our fork of `react-native-msal` will not work for Android.
|
|
28
|
+
|
|
29
|
+
Next, add our provided `LoginButton` to your application:
|
|
30
|
+
|
|
31
|
+
```tsx
|
|
32
|
+
export const LoginScreen = () => {
|
|
33
|
+
const navigation = useNavigation<NavigationProp<RootStackParamList>>();
|
|
34
|
+
return (
|
|
35
|
+
<View
|
|
36
|
+
style={{
|
|
37
|
+
height: "100%",
|
|
38
|
+
alignItems: "center",
|
|
39
|
+
justifyContent: "center",
|
|
40
|
+
flexDirection: "row",
|
|
41
|
+
}}
|
|
42
|
+
>
|
|
43
|
+
<LoginButton
|
|
44
|
+
redirectUri="msauth.com.equinor.mad.chronicles://auth"
|
|
45
|
+
clientId="49222fe1-4e0a-4310-9e81-1a2c3eb9b2ed"
|
|
46
|
+
onAuthenticationSuccessful={() => navigation.navigate("Root")}
|
|
47
|
+
enableAutomaticAuthentication
|
|
48
|
+
/>
|
|
49
|
+
</View>
|
|
50
|
+
);
|
|
51
|
+
};
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
The `LoginButton` provided by this package uses `@equinor/mad-components`'s `Button` component. You
|
|
55
|
+
can style it however you want. However, if you want to create your own `LoginButton` from scratch,
|
|
56
|
+
you can do so by using the `useAuthenticate` hook. See the implementation of our `LoginButton` if
|
|
57
|
+
you need some inspiration. Alternatively, you can create the login flow logic from scratch using the
|
|
58
|
+
provided functions below.
|
|
59
|
+
|
|
60
|
+
This package provides a few basic functions you can use to either make your own login button, or use
|
|
61
|
+
throughout the app:
|
|
62
|
+
|
|
63
|
+
If you are not using our login button, nor `useAuthenticate`, use this function to initate the
|
|
64
|
+
authentication client:
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
initiateAuthenticationClient({clientId: string, redirectUri: string, authority?: string}): Promise<void>
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
To check if the authentication client exists, use this function:
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
authenticationClientExists(): boolean
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
To authenticate interactively, use this function:
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
authenticateInteractively(scope: string[]): Promise<MadAuthenticationResult | null>
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
To authenticate silently, use this function:
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
authenticateSilently(scope: string[]): Promise<MadAuthenticationResult | null>
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
To get account, use this function:
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
getAccount(): Promise<MadAccount | null>
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Alternatively, you can use this hook to get account:
|
|
95
|
+
|
|
96
|
+
```ts
|
|
97
|
+
useAccount(): MadAccount | null
|
|
98
|
+
```
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { MSALAccount, MSALResult } from "react-native-msal";
|
|
2
|
+
import { MadAccount, MadAuthenticationResult } from "../types";
|
|
3
|
+
export declare const getMadAccount: (account: MSALAccount) => MadAccount;
|
|
4
|
+
export declare const getMadAuthenticationResult: (result: MSALResult) => MadAuthenticationResult;
|
package/dist/auth.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { MadAccount, MadAuthenticationResult } from "./types";
|
|
2
|
+
export type InitiateAuthenticationClientConfig = {
|
|
3
|
+
clientId: string;
|
|
4
|
+
redirectUri: string;
|
|
5
|
+
authority?: string;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Initiate the authentication client
|
|
9
|
+
* @param {InitiateAuthenticationClientConfig} config
|
|
10
|
+
*/
|
|
11
|
+
export declare function initiateAuthenticationClient({ clientId, redirectUri, authority, }: InitiateAuthenticationClientConfig): Promise<void>;
|
|
12
|
+
/**
|
|
13
|
+
* Check if the authentication client exists
|
|
14
|
+
* @returns {boolean} whether the authentication client exists
|
|
15
|
+
*/
|
|
16
|
+
export declare function authenticationClientExists(): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Authenticate interactively. This will open an in-app browser, or the Microsoft Authenticator app if the
|
|
19
|
+
* user has it installed
|
|
20
|
+
* @param scopes an array of scopes
|
|
21
|
+
* @returns {MadAuthenticationResult | null} an object containing the access token and the account, or null
|
|
22
|
+
*/
|
|
23
|
+
export declare function authenticateInteractively(scopes: string[]): Promise<MadAuthenticationResult | null>;
|
|
24
|
+
export declare function getAccount(): Promise<MadAccount | null>;
|
|
25
|
+
/**
|
|
26
|
+
* Authenticate silently.
|
|
27
|
+
* @param scopes an array of scopes
|
|
28
|
+
* @returns {MadAuthenticationResult | null} an object containing the access token and the account, or null
|
|
29
|
+
*/
|
|
30
|
+
export declare function authenticateSilently(scopes: string[]): Promise<MadAuthenticationResult | null>;
|
|
31
|
+
/**
|
|
32
|
+
* Sign out the account.
|
|
33
|
+
* @returns {boolean} whether we successfully signed out.
|
|
34
|
+
*/
|
|
35
|
+
export declare function signOut(): Promise<boolean>;
|
package/dist/auth.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import {
|
|
2
|
+
authenticateInteractively,
|
|
3
|
+
authenticateSilently,
|
|
4
|
+
authenticationClientExists,
|
|
5
|
+
getAccount,
|
|
6
|
+
initiateAuthenticationClient,
|
|
7
|
+
signOut
|
|
8
|
+
} from "./chunk-GZV7V4EL.js";
|
|
9
|
+
import "./chunk-GJUHWXAK.js";
|
|
10
|
+
export {
|
|
11
|
+
authenticateInteractively,
|
|
12
|
+
authenticateSilently,
|
|
13
|
+
authenticationClientExists,
|
|
14
|
+
getAccount,
|
|
15
|
+
initiateAuthenticationClient,
|
|
16
|
+
signOut
|
|
17
|
+
};
|
|
File without changes
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
3
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
4
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __spreadValues = (a, b) => {
|
|
7
|
+
for (var prop in b || (b = {}))
|
|
8
|
+
if (__hasOwnProp.call(b, prop))
|
|
9
|
+
__defNormalProp(a, prop, b[prop]);
|
|
10
|
+
if (__getOwnPropSymbols)
|
|
11
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
12
|
+
if (__propIsEnum.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
}
|
|
15
|
+
return a;
|
|
16
|
+
};
|
|
17
|
+
var __objRest = (source, exclude) => {
|
|
18
|
+
var target = {};
|
|
19
|
+
for (var prop in source)
|
|
20
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
21
|
+
target[prop] = source[prop];
|
|
22
|
+
if (source != null && __getOwnPropSymbols)
|
|
23
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
24
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
25
|
+
target[prop] = source[prop];
|
|
26
|
+
}
|
|
27
|
+
return target;
|
|
28
|
+
};
|
|
29
|
+
var __async = (__this, __arguments, generator) => {
|
|
30
|
+
return new Promise((resolve, reject) => {
|
|
31
|
+
var fulfilled = (value) => {
|
|
32
|
+
try {
|
|
33
|
+
step(generator.next(value));
|
|
34
|
+
} catch (e) {
|
|
35
|
+
reject(e);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var rejected = (value) => {
|
|
39
|
+
try {
|
|
40
|
+
step(generator.throw(value));
|
|
41
|
+
} catch (e) {
|
|
42
|
+
reject(e);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
46
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
// src/_internal/translationLayer.ts
|
|
51
|
+
var getMadAccount = (account) => {
|
|
52
|
+
return {
|
|
53
|
+
username: account.username,
|
|
54
|
+
name: account.claims.name || void 0,
|
|
55
|
+
identifier: account.identifier
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
var getMadAuthenticationResult = (result) => {
|
|
59
|
+
return { account: getMadAccount(result.account), accessToken: result.accessToken };
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export {
|
|
63
|
+
__spreadValues,
|
|
64
|
+
__objRest,
|
|
65
|
+
__async,
|
|
66
|
+
getMadAccount,
|
|
67
|
+
getMadAuthenticationResult
|
|
68
|
+
};
|
|
File without changes
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__async,
|
|
3
|
+
getMadAccount,
|
|
4
|
+
getMadAuthenticationResult
|
|
5
|
+
} from "./chunk-GJUHWXAK.js";
|
|
6
|
+
|
|
7
|
+
// src/auth.ts
|
|
8
|
+
import PublicClientApplication from "react-native-msal";
|
|
9
|
+
var pca = null;
|
|
10
|
+
function _getMsalAccount() {
|
|
11
|
+
return __async(this, null, function* () {
|
|
12
|
+
if (!pca) {
|
|
13
|
+
throw new Error("Unable to authenticate, pca is null");
|
|
14
|
+
}
|
|
15
|
+
const accounts = yield pca.getAccounts();
|
|
16
|
+
if (accounts.length > 0) {
|
|
17
|
+
const account = accounts[0];
|
|
18
|
+
return account;
|
|
19
|
+
}
|
|
20
|
+
return null;
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
function _getMadAccountFromMSAL() {
|
|
24
|
+
return __async(this, null, function* () {
|
|
25
|
+
const msalAccount = yield _getMsalAccount();
|
|
26
|
+
if (msalAccount)
|
|
27
|
+
return getMadAccount(msalAccount);
|
|
28
|
+
return null;
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
function initiateAuthenticationClient(_0) {
|
|
32
|
+
return __async(this, arguments, function* ({
|
|
33
|
+
clientId,
|
|
34
|
+
redirectUri,
|
|
35
|
+
authority = "https://login.microsoftonline.com/statoilsrm.onmicrosoft.com/"
|
|
36
|
+
}) {
|
|
37
|
+
const config = {
|
|
38
|
+
auth: {
|
|
39
|
+
authority,
|
|
40
|
+
clientId,
|
|
41
|
+
redirectUri
|
|
42
|
+
},
|
|
43
|
+
cache: { cacheLocation: "localStorage" }
|
|
44
|
+
};
|
|
45
|
+
pca = new PublicClientApplication(config);
|
|
46
|
+
yield pca.init();
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
function authenticationClientExists() {
|
|
50
|
+
return !!pca;
|
|
51
|
+
}
|
|
52
|
+
function authenticateInteractively(scopes) {
|
|
53
|
+
return __async(this, null, function* () {
|
|
54
|
+
if (!pca) {
|
|
55
|
+
throw new Error("Unable to authenticate, authentication client does not exist");
|
|
56
|
+
}
|
|
57
|
+
const result = yield pca.acquireToken({
|
|
58
|
+
scopes
|
|
59
|
+
});
|
|
60
|
+
if (!result)
|
|
61
|
+
return null;
|
|
62
|
+
return getMadAuthenticationResult(result);
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
function getAccount() {
|
|
66
|
+
return __async(this, null, function* () {
|
|
67
|
+
return _getMadAccountFromMSAL();
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
function authenticateSilently(scopes) {
|
|
71
|
+
return __async(this, null, function* () {
|
|
72
|
+
if (!pca) {
|
|
73
|
+
throw new Error("Unable to authenticate, authentication client does not exist");
|
|
74
|
+
}
|
|
75
|
+
const msalAccount = yield _getMsalAccount();
|
|
76
|
+
if (msalAccount) {
|
|
77
|
+
const params = {
|
|
78
|
+
account: msalAccount,
|
|
79
|
+
scopes,
|
|
80
|
+
forceRefresh: false
|
|
81
|
+
};
|
|
82
|
+
const result = yield pca.acquireTokenSilent(params).catch((e) => {
|
|
83
|
+
console.log("Error while fetching token silently", e);
|
|
84
|
+
}).then((res) => res);
|
|
85
|
+
if (!result)
|
|
86
|
+
return null;
|
|
87
|
+
return getMadAuthenticationResult(result);
|
|
88
|
+
}
|
|
89
|
+
return null;
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
function signOut() {
|
|
93
|
+
return __async(this, null, function* () {
|
|
94
|
+
if (!pca) {
|
|
95
|
+
throw new Error("Unable to authenticate, authentication client does not exist");
|
|
96
|
+
}
|
|
97
|
+
const account = yield _getMsalAccount();
|
|
98
|
+
if (account) {
|
|
99
|
+
const success = yield pca.removeAccount(account);
|
|
100
|
+
return success;
|
|
101
|
+
}
|
|
102
|
+
return false;
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export {
|
|
107
|
+
initiateAuthenticationClient,
|
|
108
|
+
authenticationClientExists,
|
|
109
|
+
authenticateInteractively,
|
|
110
|
+
getAccount,
|
|
111
|
+
authenticateSilently,
|
|
112
|
+
signOut
|
|
113
|
+
};
|
|
File without changes
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import {
|
|
2
|
+
authenticateInteractively,
|
|
3
|
+
authenticateSilently,
|
|
4
|
+
authenticationClientExists,
|
|
5
|
+
initiateAuthenticationClient
|
|
6
|
+
} from "./chunk-GZV7V4EL.js";
|
|
7
|
+
import {
|
|
8
|
+
__async
|
|
9
|
+
} from "./chunk-GJUHWXAK.js";
|
|
10
|
+
|
|
11
|
+
// src/hooks/useAuthenticate.ts
|
|
12
|
+
import { useEffect, useState } from "react";
|
|
13
|
+
var useAuthenticate = ({
|
|
14
|
+
onAuthenticationSuccessful,
|
|
15
|
+
clientId,
|
|
16
|
+
redirectUri,
|
|
17
|
+
enableAutomaticAuthentication
|
|
18
|
+
}) => {
|
|
19
|
+
const [authenticationInProgress, setAuthenticationInProgress] = useState(false);
|
|
20
|
+
const [authenticationClientInitialized, setAuthenticationClientInitialized] = useState(false);
|
|
21
|
+
const withAuthenticationPromiseHandler = (promise) => __async(void 0, null, function* () {
|
|
22
|
+
setAuthenticationInProgress(true);
|
|
23
|
+
try {
|
|
24
|
+
const res = yield promise;
|
|
25
|
+
if (res)
|
|
26
|
+
onAuthenticationSuccessful(res);
|
|
27
|
+
} finally {
|
|
28
|
+
setAuthenticationInProgress(false);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
useEffect(() => {
|
|
32
|
+
const initiateClientAndMaybeAuthenticateSilently = () => __async(void 0, null, function* () {
|
|
33
|
+
yield initiateAuthenticationClient({
|
|
34
|
+
clientId,
|
|
35
|
+
redirectUri
|
|
36
|
+
});
|
|
37
|
+
if (authenticationClientExists())
|
|
38
|
+
setAuthenticationClientInitialized(true);
|
|
39
|
+
if (enableAutomaticAuthentication)
|
|
40
|
+
withAuthenticationPromiseHandler(authenticateSilently([]));
|
|
41
|
+
});
|
|
42
|
+
initiateClientAndMaybeAuthenticateSilently();
|
|
43
|
+
}, []);
|
|
44
|
+
return {
|
|
45
|
+
authenticationInProgress,
|
|
46
|
+
authenticate: () => withAuthenticationPromiseHandler(authenticateInteractively([])),
|
|
47
|
+
authenticationClientInitialized: authenticationClientExists() && authenticationClientInitialized
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export {
|
|
52
|
+
useAuthenticate
|
|
53
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useAuthenticate
|
|
3
|
+
} from "./chunk-O5YSBQA2.js";
|
|
4
|
+
import {
|
|
5
|
+
__objRest,
|
|
6
|
+
__spreadValues
|
|
7
|
+
} from "./chunk-GJUHWXAK.js";
|
|
8
|
+
|
|
9
|
+
// src/components/LoginButton.tsx
|
|
10
|
+
import React from "react";
|
|
11
|
+
import { Button } from "@equinor/mad-components";
|
|
12
|
+
var LoginButton = (_a) => {
|
|
13
|
+
var _b = _a, {
|
|
14
|
+
clientId,
|
|
15
|
+
redirectUri,
|
|
16
|
+
onAuthenticationSuccessful,
|
|
17
|
+
enableAutomaticAuthentication,
|
|
18
|
+
title = "Log in"
|
|
19
|
+
} = _b, rest = __objRest(_b, [
|
|
20
|
+
"clientId",
|
|
21
|
+
"redirectUri",
|
|
22
|
+
"onAuthenticationSuccessful",
|
|
23
|
+
"enableAutomaticAuthentication",
|
|
24
|
+
"title"
|
|
25
|
+
]);
|
|
26
|
+
const { authenticate, authenticationInProgress, authenticationClientInitialized } = useAuthenticate({
|
|
27
|
+
clientId,
|
|
28
|
+
redirectUri,
|
|
29
|
+
onAuthenticationSuccessful,
|
|
30
|
+
enableAutomaticAuthentication
|
|
31
|
+
});
|
|
32
|
+
const loginButtonIsDisabled = authenticationInProgress || authenticationClientInitialized == false;
|
|
33
|
+
return /* @__PURE__ */ React.createElement(
|
|
34
|
+
Button,
|
|
35
|
+
__spreadValues({
|
|
36
|
+
title,
|
|
37
|
+
onPress: authenticate,
|
|
38
|
+
loading: loginButtonIsDisabled,
|
|
39
|
+
disabled: loginButtonIsDisabled
|
|
40
|
+
}, rest)
|
|
41
|
+
);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export {
|
|
45
|
+
LoginButton
|
|
46
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getAccount
|
|
3
|
+
} from "./chunk-GZV7V4EL.js";
|
|
4
|
+
import {
|
|
5
|
+
__async
|
|
6
|
+
} from "./chunk-GJUHWXAK.js";
|
|
7
|
+
|
|
8
|
+
// src/hooks/useAccount.ts
|
|
9
|
+
import { useEffect, useState } from "react";
|
|
10
|
+
var useAccount = () => {
|
|
11
|
+
const [account, setAccount] = useState();
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
const getAndSetAccount = () => __async(void 0, null, function* () {
|
|
14
|
+
try {
|
|
15
|
+
const account2 = yield getAccount();
|
|
16
|
+
setAccount(account2);
|
|
17
|
+
} catch (e) {
|
|
18
|
+
throw new Error("Unable to get account", { cause: e });
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
getAndSetAccount();
|
|
22
|
+
}, []);
|
|
23
|
+
return account;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export {
|
|
27
|
+
useAccount
|
|
28
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { ButtonProps } from "@equinor/mad-components";
|
|
3
|
+
export type LoginButtonProps = Omit<ButtonProps, "onPress" | "loading" | "disabled" | "title"> & {
|
|
4
|
+
clientId: string;
|
|
5
|
+
redirectUri: string;
|
|
6
|
+
onAuthenticationSuccessful: () => void;
|
|
7
|
+
title?: ButtonProps["title"];
|
|
8
|
+
enableAutomaticAuthentication?: boolean;
|
|
9
|
+
};
|
|
10
|
+
export declare const LoginButton: ({ clientId, redirectUri, onAuthenticationSuccessful, enableAutomaticAuthentication, title, ...rest }: LoginButtonProps) => React.JSX.Element;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./LoginButton";
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import "../chunk-GUKC6UGZ.js";
|
|
2
|
+
import {
|
|
3
|
+
useAuthenticate
|
|
4
|
+
} from "../chunk-O5YSBQA2.js";
|
|
5
|
+
import {
|
|
6
|
+
useAccount
|
|
7
|
+
} from "../chunk-YGQY3H47.js";
|
|
8
|
+
import "../chunk-GZV7V4EL.js";
|
|
9
|
+
import "../chunk-GJUHWXAK.js";
|
|
10
|
+
export {
|
|
11
|
+
useAccount,
|
|
12
|
+
useAuthenticate
|
|
13
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { MadAuthenticationResult } from "../types";
|
|
2
|
+
export type UseAuthenticateProps = {
|
|
3
|
+
onAuthenticationSuccessful: (res: MadAuthenticationResult) => void;
|
|
4
|
+
redirectUri: string;
|
|
5
|
+
clientId: string;
|
|
6
|
+
enableAutomaticAuthentication?: boolean;
|
|
7
|
+
};
|
|
8
|
+
type UseAuthenticateResult = {
|
|
9
|
+
authenticate: () => void;
|
|
10
|
+
authenticationInProgress: boolean;
|
|
11
|
+
authenticationClientInitialized: boolean;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* initialize the authentication client, and authenticate the user
|
|
15
|
+
* @param {UseAuthenticateProps} params params for client initialization and authentication flow
|
|
16
|
+
* @returns authenticate function, and some helper variables
|
|
17
|
+
*/
|
|
18
|
+
export declare const useAuthenticate: ({ onAuthenticationSuccessful, clientId, redirectUri, enableAutomaticAuthentication, }: UseAuthenticateProps) => UseAuthenticateResult;
|
|
19
|
+
export {};
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import "./chunk-6F4PWJZI.js";
|
|
2
|
+
import "./chunk-HAB34YS7.js";
|
|
3
|
+
import {
|
|
4
|
+
LoginButton
|
|
5
|
+
} from "./chunk-VC6L6TBN.js";
|
|
6
|
+
import "./chunk-GUKC6UGZ.js";
|
|
7
|
+
import {
|
|
8
|
+
useAuthenticate
|
|
9
|
+
} from "./chunk-O5YSBQA2.js";
|
|
10
|
+
import {
|
|
11
|
+
useAccount
|
|
12
|
+
} from "./chunk-YGQY3H47.js";
|
|
13
|
+
import {
|
|
14
|
+
authenticateInteractively,
|
|
15
|
+
authenticateSilently,
|
|
16
|
+
authenticationClientExists,
|
|
17
|
+
getAccount,
|
|
18
|
+
initiateAuthenticationClient,
|
|
19
|
+
signOut
|
|
20
|
+
} from "./chunk-GZV7V4EL.js";
|
|
21
|
+
import "./chunk-GJUHWXAK.js";
|
|
22
|
+
export {
|
|
23
|
+
LoginButton,
|
|
24
|
+
authenticateInteractively,
|
|
25
|
+
authenticateSilently,
|
|
26
|
+
authenticationClientExists,
|
|
27
|
+
getAccount,
|
|
28
|
+
initiateAuthenticationClient,
|
|
29
|
+
signOut,
|
|
30
|
+
useAccount,
|
|
31
|
+
useAuthenticate
|
|
32
|
+
};
|
package/dist/types.d.ts
ADDED
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import "./chunk-6F4PWJZI.js";
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@equinor/mad-auth",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "A simple wrapper library for react-native-msal",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"react native",
|
|
8
|
+
"authentication",
|
|
9
|
+
"typescript",
|
|
10
|
+
"msal"
|
|
11
|
+
],
|
|
12
|
+
"author": {
|
|
13
|
+
"name": "Equinor Mobile App Delivery team (MAD)"
|
|
14
|
+
},
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/equinor/mad/issues"
|
|
18
|
+
},
|
|
19
|
+
"homepage": "https://github.com/equinor/mad/",
|
|
20
|
+
"main": "./dist/index.js",
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"peerDependencies": {
|
|
23
|
+
"react": ">=16.8",
|
|
24
|
+
"react-dom": ">=16.8",
|
|
25
|
+
"react-native": ">=0.68",
|
|
26
|
+
"react-native-msal": ">=4.0.4"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@equinor/mad-components": "0.5.4"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@equinor/eslint-config-mad": "0.2.1"
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"CODE_OF_CONDUCT.md",
|
|
36
|
+
"LICENSE",
|
|
37
|
+
"CHANGELOG.md",
|
|
38
|
+
"dist"
|
|
39
|
+
],
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "tsup-node",
|
|
42
|
+
"test": "jest",
|
|
43
|
+
"dev": "tsup-node --watch --clean=false",
|
|
44
|
+
"lint": "eslint src/"
|
|
45
|
+
}
|
|
46
|
+
}
|