quanvio-react-native-sdk 0.0.0-stage → 1.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/LICENSE +201 -0
- package/README.md +198 -2
- package/lib/cjs/index.d.ts +3 -0
- package/lib/cjs/index.js +17 -0
- package/lib/cjs/sdk.d.ts +47 -0
- package/lib/cjs/sdk.js +201 -0
- package/lib/esm/index.d.ts +3 -0
- package/lib/esm/index.js +15 -0
- package/lib/esm/sdk.d.ts +47 -0
- package/lib/esm/sdk.js +199 -0
- package/package.json +78 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,199 @@
|
|
|
1
|
-
#
|
|
1
|
+
# quanvio-react-native-sdk
|
|
2
2
|
|
|
3
|
-
This
|
|
3
|
+
This is Quanvio's SDK for react-native will allow you to easily connect your application to the Quanvio One authentication system (https://one.quanvio.com) without having to implement it from scratch.
|
|
4
|
+
Quanvio SDK is very simple to use. We will show you the steps below.
|
|
5
|
+
|
|
6
|
+
## Usage in NPM environment
|
|
7
|
+
|
|
8
|
+
### Installation
|
|
9
|
+
|
|
10
|
+
~~~shell script
|
|
11
|
+
# NPM
|
|
12
|
+
npm i quanvio-react-native-sdk @react-native-async-storage/async-storage
|
|
13
|
+
|
|
14
|
+
# Yarn
|
|
15
|
+
yarn add quanvio-react-native-sdk @react-native-async-storage/async-storage
|
|
16
|
+
~~~
|
|
17
|
+
|
|
18
|
+
### Init SDK
|
|
19
|
+
|
|
20
|
+
Initialization requires 7 parameters, which are all string type:
|
|
21
|
+
|
|
22
|
+
| Name (in order) | Must | Description |
|
|
23
|
+
| ---------------- | ---- | --------------------------------------------------- |
|
|
24
|
+
| serverUrl | Yes | your Quanvio One server URL, e.g. `https://one.quanvio.com` |
|
|
25
|
+
| clientId | Yes | the Client ID of your Quanvio One application|
|
|
26
|
+
| appName | Yes | the name of your Quanvio One application |
|
|
27
|
+
| organizationName | Yes | the name of the organization connected with your Quanvio One application |
|
|
28
|
+
| redirectPath | No | the redirect URL for your Quanvio One application, e.g., `myapp://callback`; a path like `/callback` is prefixed with the current origin on the web, will be `/callback` if not provided |
|
|
29
|
+
| signinPath | No | the path of the signin URL for your Quanvio One application, will be `/api/signin` if not provided |
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
import SDK from 'quanvio-react-native-sdk'
|
|
33
|
+
|
|
34
|
+
const sdkConfig = {
|
|
35
|
+
serverUrl: 'https://one.quanvio.com',
|
|
36
|
+
clientId: 'b800a86702dd4d29ec4d',
|
|
37
|
+
appName: 'app-example',
|
|
38
|
+
organizationName: 'quanvio',
|
|
39
|
+
redirectPath: 'http://localhost:5000/callback',
|
|
40
|
+
signinPath: '/api/signin',
|
|
41
|
+
};
|
|
42
|
+
const sdk = new SDK(sdkConfig)
|
|
43
|
+
// call sdk to handle
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Usage in Expo
|
|
47
|
+
|
|
48
|
+
The SDK works in the Expo managed workflow without `react-native-webview` or manual deep-link handling: `signin()` opens the Quanvio One login page in the system auth browser and returns the access token after the redirect.
|
|
49
|
+
|
|
50
|
+
### Installation
|
|
51
|
+
|
|
52
|
+
~~~shell script
|
|
53
|
+
npx expo install quanvio-react-native-sdk @react-native-async-storage/async-storage expo-auth-session expo-web-browser expo-crypto react-native-get-random-values
|
|
54
|
+
~~~
|
|
55
|
+
|
|
56
|
+
### Configure the redirect URI
|
|
57
|
+
|
|
58
|
+
1. Add a scheme to `app.json`, so that the app can be opened by the redirect URI:
|
|
59
|
+
|
|
60
|
+
```json
|
|
61
|
+
{
|
|
62
|
+
"expo": {
|
|
63
|
+
"scheme": "myapp"
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
2. Add the redirect URI to the **Redirect URLs** of your application in Quanvio One. `AuthSession.makeRedirectUri({ path: 'callback' })` returns `myapp://callback` in a standalone or development build, and `exp://<your-ip>:8081/--/callback` in Expo Go; add the one you use.
|
|
69
|
+
|
|
70
|
+
### Sign in
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
// must be imported before the SDK: the PKCE code is generated with crypto.getRandomValues(), which Hermes does not provide
|
|
74
|
+
import 'react-native-get-random-values';
|
|
75
|
+
import * as AuthSession from 'expo-auth-session';
|
|
76
|
+
import * as WebBrowser from 'expo-web-browser';
|
|
77
|
+
import SDK from 'quanvio-react-native-sdk';
|
|
78
|
+
|
|
79
|
+
WebBrowser.maybeCompleteAuthSession();
|
|
80
|
+
|
|
81
|
+
const sdk = new SDK({
|
|
82
|
+
serverUrl: 'https://one.quanvio.com',
|
|
83
|
+
clientId: 'b800a86702dd4d29ec4d',
|
|
84
|
+
appName: 'app-example',
|
|
85
|
+
organizationName: 'quanvio',
|
|
86
|
+
redirectPath: AuthSession.makeRedirectUri({ path: 'callback' }),
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
const login = async () => {
|
|
90
|
+
const token = await sdk.signin(WebBrowser.openAuthSessionAsync);
|
|
91
|
+
if (token) {
|
|
92
|
+
const user = sdk.JwtDecode(token);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
`silentSignin()` relies on a hidden iframe and only works on the web; in native apps it calls `onFailure`.
|
|
98
|
+
|
|
99
|
+
## Usage in vanilla Javascript
|
|
100
|
+
|
|
101
|
+
### Import and init SDK
|
|
102
|
+
|
|
103
|
+
Initialization parameters are consistent with the previous node.js section:
|
|
104
|
+
|
|
105
|
+
```javascript
|
|
106
|
+
<!--init the SDK-->
|
|
107
|
+
import SDK from 'quanvio-react-native-sdk'
|
|
108
|
+
const sdkConfig = {
|
|
109
|
+
serverUrl: 'https://one.quanvio.com',
|
|
110
|
+
clientId: 'b800a86702dd4d29ec4d',
|
|
111
|
+
appName: 'app-example',
|
|
112
|
+
organizationName: 'quanvio',
|
|
113
|
+
redirectPath: 'http://localhost:5000/callback',
|
|
114
|
+
signinPath: '/api/signin',
|
|
115
|
+
};
|
|
116
|
+
const sdk = new SDK(sdkConfig)
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### Call functions in SDK
|
|
120
|
+
|
|
121
|
+
```javascript
|
|
122
|
+
// call sdk to handle
|
|
123
|
+
sdk.getSignupUrl();
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## API reference interface
|
|
127
|
+
|
|
128
|
+
#### Get sign up url
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
getSignupUrl()
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
Return the Quanvio One url that navigates to the registration screen
|
|
135
|
+
|
|
136
|
+
#### Get sign in url
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
getSigninUrl()
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
Return the Quanvio One url that navigates to the login screen
|
|
143
|
+
|
|
144
|
+
#### Get user profile page url
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
147
|
+
getUserProfileUrl(userName, account)
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
Return the url to navigate to a specific user's Quanvio One personal page
|
|
151
|
+
|
|
152
|
+
#### Get my profile page url
|
|
153
|
+
|
|
154
|
+
```typescript
|
|
155
|
+
getMyProfileUrl(account)
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
#### signin
|
|
159
|
+
|
|
160
|
+
```typescript
|
|
161
|
+
signin(openAuthSession) // e.g., sdk.signin(WebBrowser.openAuthSessionAsync)
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
Open the login page with `openAuthSession(url, redirectUri)`, which should resolve to `{ type: 'success', url }` after the browser is redirected back, and return the access token. Return `undefined` if the user cancels the login.
|
|
165
|
+
|
|
166
|
+
#### getAccessToken
|
|
167
|
+
|
|
168
|
+
```typescript
|
|
169
|
+
getAccessToken(redirectUrl); // http://localhost:5000/callback?code=b75bc5c5ac65ffa516e5&state=gjmfdgqf498
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
Handle the callback url from Quanvio One, call the back-end api to complete the login process
|
|
173
|
+
|
|
174
|
+
#### Determine whether silent sign-in is being used
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
isSilentSigninRequested()
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
We usually use this method to determine if silent login is being used. By default, if the silentSignin parameter is included in the URL and equals one, this method will return true. Of course, you can also use any method you prefer.
|
|
181
|
+
|
|
182
|
+
#### silentSignin
|
|
183
|
+
|
|
184
|
+
````typescript
|
|
185
|
+
silentSignin(onSuccess, onFailure)
|
|
186
|
+
````
|
|
187
|
+
|
|
188
|
+
First, let's explain the two parameters of this method, which are the callback methods for successful and failed login. Next, I will describe the execution process of this method. We will create a hidden "iframe" element to redirect to the login page for authentication, thereby achieving the effect of silent sign-in.
|
|
189
|
+
|
|
190
|
+
#### JwtDecode
|
|
191
|
+
|
|
192
|
+
````typescript
|
|
193
|
+
JwtDecode(jwtToken)
|
|
194
|
+
````
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
## License
|
|
198
|
+
|
|
199
|
+
[Apache-2.0](LICENSE)
|
package/lib/cjs/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright 2021 The casbin Authors. All Rights Reserved.
|
|
3
|
+
//
|
|
4
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
// you may not use this file except in compliance with the License.
|
|
6
|
+
// You may obtain a copy of the License at
|
|
7
|
+
//
|
|
8
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
//
|
|
10
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
// See the License for the specific language governing permissions and
|
|
14
|
+
// limitations under the License.
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
const sdk_1 = require("./sdk");
|
|
17
|
+
exports.default = sdk_1.default;
|
package/lib/cjs/sdk.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export interface QuanvioConfig {
|
|
2
|
+
serverUrl: string;
|
|
3
|
+
clientId: string;
|
|
4
|
+
appName: string;
|
|
5
|
+
organizationName: string;
|
|
6
|
+
redirectPath?: string;
|
|
7
|
+
signinPath?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface Account {
|
|
10
|
+
organization: string;
|
|
11
|
+
username: string;
|
|
12
|
+
type: string;
|
|
13
|
+
name: string;
|
|
14
|
+
avatar: string;
|
|
15
|
+
email: string;
|
|
16
|
+
phone: string;
|
|
17
|
+
affiliation: string;
|
|
18
|
+
tag: string;
|
|
19
|
+
language: string;
|
|
20
|
+
score: number;
|
|
21
|
+
isAdmin: boolean;
|
|
22
|
+
accessToken: string;
|
|
23
|
+
}
|
|
24
|
+
export interface AuthSessionResult {
|
|
25
|
+
type: string;
|
|
26
|
+
url?: string;
|
|
27
|
+
}
|
|
28
|
+
export type OpenAuthSession = (url: string, redirectUri: string) => Promise<AuthSessionResult>;
|
|
29
|
+
declare class QuanvioSdk {
|
|
30
|
+
private config;
|
|
31
|
+
constructor(config: QuanvioConfig);
|
|
32
|
+
private getPkce;
|
|
33
|
+
getSignupUrl(enablePassword?: boolean): Promise<string>;
|
|
34
|
+
getOrSaveState(): Promise<string>;
|
|
35
|
+
clearState(): void;
|
|
36
|
+
getRedirectUri(): string;
|
|
37
|
+
getSigninUrl(): Promise<string>;
|
|
38
|
+
signin(openAuthSession: OpenAuthSession): Promise<string | undefined>;
|
|
39
|
+
getUserProfileUrl(userName: string, account: Account): string;
|
|
40
|
+
getMyProfileUrl(account: Account, returnUrl?: String): string;
|
|
41
|
+
JwtDecode(token: string): unknown;
|
|
42
|
+
private static getQueryParam;
|
|
43
|
+
getAccessToken(redirectUrl: string): Promise<any>;
|
|
44
|
+
isSilentSigninRequested(): boolean;
|
|
45
|
+
silentSignin(onSuccess: (message: any) => void, onFailure: (message: any) => void): void;
|
|
46
|
+
}
|
|
47
|
+
export default QuanvioSdk;
|
package/lib/cjs/sdk.js
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright 2021 The casbin Authors. All Rights Reserved.
|
|
3
|
+
//
|
|
4
|
+
// Licensed under the Apache License, Version 2.0 (the 'License');
|
|
5
|
+
// you may not use this file except in compliance with the License.
|
|
6
|
+
// You may obtain a copy of the License at
|
|
7
|
+
//
|
|
8
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
//
|
|
10
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
// distributed under the License is distributed on an 'AS IS' BASIS,
|
|
12
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
// See the License for the specific language governing permissions and
|
|
14
|
+
// limitations under the License.
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
const async_storage_1 = require("@react-native-async-storage/async-storage");
|
|
17
|
+
const jwt_decode_1 = require("jwt-decode");
|
|
18
|
+
const pkce_challenge_1 = require("pkce-challenge");
|
|
19
|
+
class QuanvioSdk {
|
|
20
|
+
constructor(config) {
|
|
21
|
+
this.config = config;
|
|
22
|
+
if (config.redirectPath === undefined || config.redirectPath === null) {
|
|
23
|
+
this.config.redirectPath = '/callback';
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
// The PKCE pair is kept in AsyncStorage (localStorage on the web) instead of memory, so the code verifier
|
|
27
|
+
// survives the page redirect to Quanvio One and back, and is found by a new QuanvioSdk instance after the redirect.
|
|
28
|
+
async getPkce() {
|
|
29
|
+
const savedPkce = await async_storage_1.default.getItem('quanvio-pkce');
|
|
30
|
+
if (savedPkce !== null) {
|
|
31
|
+
return JSON.parse(savedPkce);
|
|
32
|
+
}
|
|
33
|
+
const pkce = await (0, pkce_challenge_1.default)();
|
|
34
|
+
await async_storage_1.default.setItem('quanvio-pkce', JSON.stringify(pkce));
|
|
35
|
+
return pkce;
|
|
36
|
+
}
|
|
37
|
+
async getSignupUrl(enablePassword = true) {
|
|
38
|
+
const signinUrl = await this.getSigninUrl();
|
|
39
|
+
if (enablePassword) {
|
|
40
|
+
async_storage_1.default.setItem('signinUrl', signinUrl);
|
|
41
|
+
return `${this.config.serverUrl.trim()}/signup/${this.config.appName}`;
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
return signinUrl.replace('/login/oauth/authorize', '/signup/oauth/authorize');
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
async getOrSaveState() {
|
|
48
|
+
const state = await async_storage_1.default.getItem('quanvio-state');
|
|
49
|
+
if (state !== null) {
|
|
50
|
+
return state;
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
const state = Math.random().toString(36).slice(2);
|
|
54
|
+
async_storage_1.default.setItem('quanvio-state', state);
|
|
55
|
+
return state;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
clearState() {
|
|
59
|
+
async_storage_1.default.removeItem('quanvio-state');
|
|
60
|
+
async_storage_1.default.removeItem('quanvio-pkce');
|
|
61
|
+
}
|
|
62
|
+
getRedirectUri() {
|
|
63
|
+
var _a;
|
|
64
|
+
const redirectPath = (_a = this.config.redirectPath) !== null && _a !== void 0 ? _a : '/callback';
|
|
65
|
+
if (redirectPath.includes('://') || typeof window === 'undefined' || !window.location) {
|
|
66
|
+
return redirectPath;
|
|
67
|
+
}
|
|
68
|
+
return `${window.location.origin}${redirectPath}`;
|
|
69
|
+
}
|
|
70
|
+
async getSigninUrl() {
|
|
71
|
+
const redirectUri = this.getRedirectUri();
|
|
72
|
+
const scope = 'read';
|
|
73
|
+
const state = await this.getOrSaveState();
|
|
74
|
+
const pkce = await this.getPkce();
|
|
75
|
+
return `${this.config.serverUrl.trim()}/login/oauth/authorize?client_id=${this.config.clientId}&response_type=code&redirect_uri=${encodeURIComponent(redirectUri)}&scope=${scope}&state=${state}&code_challenge=${pkce.code_challenge}&code_challenge_method=S256`;
|
|
76
|
+
}
|
|
77
|
+
// Opens the Quanvio One login page with the given auth browser, e.g., WebBrowser.openAuthSessionAsync for Expo,
|
|
78
|
+
// and exchanges the returned code for an access token. Returns undefined if the user cancels the login.
|
|
79
|
+
async signin(openAuthSession) {
|
|
80
|
+
const signinUrl = await this.getSigninUrl();
|
|
81
|
+
const result = await openAuthSession(signinUrl, this.getRedirectUri());
|
|
82
|
+
if (result.type !== 'success' || !result.url) {
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
return this.getAccessToken(result.url);
|
|
86
|
+
}
|
|
87
|
+
getUserProfileUrl(userName, account) {
|
|
88
|
+
let param = '';
|
|
89
|
+
if (account !== undefined && account !== null) {
|
|
90
|
+
param = `?access_token=${account.accessToken}`;
|
|
91
|
+
}
|
|
92
|
+
return `${this.config.serverUrl.trim()}/users/${this.config.organizationName}/${userName}${param}`;
|
|
93
|
+
}
|
|
94
|
+
getMyProfileUrl(account, returnUrl = '') {
|
|
95
|
+
let params = '';
|
|
96
|
+
if (account !== undefined && account !== null) {
|
|
97
|
+
params = `?access_token=${account.accessToken}`;
|
|
98
|
+
if (returnUrl !== '') {
|
|
99
|
+
params += `&returnUrl=${returnUrl}`;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
else if (returnUrl !== '') {
|
|
103
|
+
params = `?returnUrl=${returnUrl}`;
|
|
104
|
+
}
|
|
105
|
+
return `${this.config.serverUrl.trim()}/account${params}`;
|
|
106
|
+
}
|
|
107
|
+
JwtDecode(token) {
|
|
108
|
+
return (0, jwt_decode_1.default)(token);
|
|
109
|
+
}
|
|
110
|
+
// URLSearchParams is not fully implemented in React Native, so parse the query string by hand
|
|
111
|
+
static getQueryParam(url, name) {
|
|
112
|
+
const query = url.split('#')[0].split('?')[1];
|
|
113
|
+
if (!query) {
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
for (const pair of query.split('&')) {
|
|
117
|
+
const [key, value = ''] = pair.split('=');
|
|
118
|
+
if (decodeURIComponent(key) === name) {
|
|
119
|
+
return decodeURIComponent(value.replace(/\+/g, ' '));
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
async getAccessToken(redirectUrl) {
|
|
125
|
+
var _a;
|
|
126
|
+
const redirectUri = this.getRedirectUri();
|
|
127
|
+
if (redirectUrl.startsWith(redirectUri)) {
|
|
128
|
+
const code = QuanvioSdk.getQueryParam(redirectUrl, 'code');
|
|
129
|
+
if (code === null) {
|
|
130
|
+
console.error('Error during Signin Request:', (_a = QuanvioSdk.getQueryParam(redirectUrl, 'error')) !== null && _a !== void 0 ? _a : redirectUrl);
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
const state = QuanvioSdk.getQueryParam(redirectUrl, 'state');
|
|
134
|
+
if (state !== null) {
|
|
135
|
+
await async_storage_1.default.setItem('quanvio-state', state);
|
|
136
|
+
}
|
|
137
|
+
const codeVerifier = (await this.getPkce()).code_verifier;
|
|
138
|
+
try {
|
|
139
|
+
const response = await fetch(`${this.config.serverUrl.trim()}/api/login/oauth/access_token`, {
|
|
140
|
+
method: 'POST',
|
|
141
|
+
headers: {
|
|
142
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
143
|
+
},
|
|
144
|
+
body: `client_id=${this.config.clientId}&grant_type=authorization_code&code=${encodeURIComponent(code)}&redirect_uri=${encodeURIComponent(redirectUri)}&code_verifier=${codeVerifier}`,
|
|
145
|
+
credentials: 'include',
|
|
146
|
+
});
|
|
147
|
+
if (response.ok) {
|
|
148
|
+
const responseData = await response.json();
|
|
149
|
+
const token = responseData.access_token;
|
|
150
|
+
return token;
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
console.error('Error during AccessToken Request:', response);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
catch (error) {
|
|
157
|
+
console.error('Error during Signin Request:', error);
|
|
158
|
+
}
|
|
159
|
+
finally {
|
|
160
|
+
// a code verifier is only valid for one authorization code, the next signin gets a new one
|
|
161
|
+
await async_storage_1.default.removeItem('quanvio-pkce');
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
isSilentSigninRequested() {
|
|
166
|
+
if (typeof window === 'undefined' || !window.location) {
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
const params = new URLSearchParams(window.location.search);
|
|
170
|
+
return params.get('silentSignin') === '1';
|
|
171
|
+
}
|
|
172
|
+
silentSignin(onSuccess, onFailure) {
|
|
173
|
+
if (typeof document === 'undefined') {
|
|
174
|
+
onFailure({ tag: 'Casdoor', type: 'SilentSignin', data: 'silentSignin is not supported in React Native' });
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
const iframe = document.createElement('iframe');
|
|
178
|
+
iframe.style.display = 'none';
|
|
179
|
+
this.getSigninUrl().then(signinUrl => {
|
|
180
|
+
iframe.src = `${signinUrl}&silentSignin=1`;
|
|
181
|
+
document.body.appendChild(iframe);
|
|
182
|
+
});
|
|
183
|
+
const handleMessage = (event) => {
|
|
184
|
+
if (window !== window.parent) {
|
|
185
|
+
return null;
|
|
186
|
+
}
|
|
187
|
+
const message = event.data;
|
|
188
|
+
if (message.tag !== 'Casdoor' || message.type !== 'SilentSignin') {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
if (message.data === 'success') {
|
|
192
|
+
onSuccess(message);
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
onFailure(message);
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
window.addEventListener('message', handleMessage);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
exports.default = QuanvioSdk;
|
package/lib/esm/index.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Copyright 2021 The casbin Authors. All Rights Reserved.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
import QuanvioSdk from './sdk';
|
|
15
|
+
export default QuanvioSdk;
|
package/lib/esm/sdk.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export interface QuanvioConfig {
|
|
2
|
+
serverUrl: string;
|
|
3
|
+
clientId: string;
|
|
4
|
+
appName: string;
|
|
5
|
+
organizationName: string;
|
|
6
|
+
redirectPath?: string;
|
|
7
|
+
signinPath?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface Account {
|
|
10
|
+
organization: string;
|
|
11
|
+
username: string;
|
|
12
|
+
type: string;
|
|
13
|
+
name: string;
|
|
14
|
+
avatar: string;
|
|
15
|
+
email: string;
|
|
16
|
+
phone: string;
|
|
17
|
+
affiliation: string;
|
|
18
|
+
tag: string;
|
|
19
|
+
language: string;
|
|
20
|
+
score: number;
|
|
21
|
+
isAdmin: boolean;
|
|
22
|
+
accessToken: string;
|
|
23
|
+
}
|
|
24
|
+
export interface AuthSessionResult {
|
|
25
|
+
type: string;
|
|
26
|
+
url?: string;
|
|
27
|
+
}
|
|
28
|
+
export type OpenAuthSession = (url: string, redirectUri: string) => Promise<AuthSessionResult>;
|
|
29
|
+
declare class QuanvioSdk {
|
|
30
|
+
private config;
|
|
31
|
+
constructor(config: QuanvioConfig);
|
|
32
|
+
private getPkce;
|
|
33
|
+
getSignupUrl(enablePassword?: boolean): Promise<string>;
|
|
34
|
+
getOrSaveState(): Promise<string>;
|
|
35
|
+
clearState(): void;
|
|
36
|
+
getRedirectUri(): string;
|
|
37
|
+
getSigninUrl(): Promise<string>;
|
|
38
|
+
signin(openAuthSession: OpenAuthSession): Promise<string | undefined>;
|
|
39
|
+
getUserProfileUrl(userName: string, account: Account): string;
|
|
40
|
+
getMyProfileUrl(account: Account, returnUrl?: String): string;
|
|
41
|
+
JwtDecode(token: string): unknown;
|
|
42
|
+
private static getQueryParam;
|
|
43
|
+
getAccessToken(redirectUrl: string): Promise<any>;
|
|
44
|
+
isSilentSigninRequested(): boolean;
|
|
45
|
+
silentSignin(onSuccess: (message: any) => void, onFailure: (message: any) => void): void;
|
|
46
|
+
}
|
|
47
|
+
export default QuanvioSdk;
|
package/lib/esm/sdk.js
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
// Copyright 2021 The casbin Authors. All Rights Reserved.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the 'License');
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an 'AS IS' BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
import AsyncStorage from '@react-native-async-storage/async-storage';
|
|
15
|
+
import jwtDecode from 'jwt-decode';
|
|
16
|
+
import pkceChallenge from 'pkce-challenge';
|
|
17
|
+
class QuanvioSdk {
|
|
18
|
+
constructor(config) {
|
|
19
|
+
this.config = config;
|
|
20
|
+
if (config.redirectPath === undefined || config.redirectPath === null) {
|
|
21
|
+
this.config.redirectPath = '/callback';
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
// The PKCE pair is kept in AsyncStorage (localStorage on the web) instead of memory, so the code verifier
|
|
25
|
+
// survives the page redirect to Quanvio One and back, and is found by a new QuanvioSdk instance after the redirect.
|
|
26
|
+
async getPkce() {
|
|
27
|
+
const savedPkce = await AsyncStorage.getItem('quanvio-pkce');
|
|
28
|
+
if (savedPkce !== null) {
|
|
29
|
+
return JSON.parse(savedPkce);
|
|
30
|
+
}
|
|
31
|
+
const pkce = await pkceChallenge();
|
|
32
|
+
await AsyncStorage.setItem('quanvio-pkce', JSON.stringify(pkce));
|
|
33
|
+
return pkce;
|
|
34
|
+
}
|
|
35
|
+
async getSignupUrl(enablePassword = true) {
|
|
36
|
+
const signinUrl = await this.getSigninUrl();
|
|
37
|
+
if (enablePassword) {
|
|
38
|
+
AsyncStorage.setItem('signinUrl', signinUrl);
|
|
39
|
+
return `${this.config.serverUrl.trim()}/signup/${this.config.appName}`;
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
return signinUrl.replace('/login/oauth/authorize', '/signup/oauth/authorize');
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
async getOrSaveState() {
|
|
46
|
+
const state = await AsyncStorage.getItem('quanvio-state');
|
|
47
|
+
if (state !== null) {
|
|
48
|
+
return state;
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
const state = Math.random().toString(36).slice(2);
|
|
52
|
+
AsyncStorage.setItem('quanvio-state', state);
|
|
53
|
+
return state;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
clearState() {
|
|
57
|
+
AsyncStorage.removeItem('quanvio-state');
|
|
58
|
+
AsyncStorage.removeItem('quanvio-pkce');
|
|
59
|
+
}
|
|
60
|
+
getRedirectUri() {
|
|
61
|
+
var _a;
|
|
62
|
+
const redirectPath = (_a = this.config.redirectPath) !== null && _a !== void 0 ? _a : '/callback';
|
|
63
|
+
if (redirectPath.includes('://') || typeof window === 'undefined' || !window.location) {
|
|
64
|
+
return redirectPath;
|
|
65
|
+
}
|
|
66
|
+
return `${window.location.origin}${redirectPath}`;
|
|
67
|
+
}
|
|
68
|
+
async getSigninUrl() {
|
|
69
|
+
const redirectUri = this.getRedirectUri();
|
|
70
|
+
const scope = 'read';
|
|
71
|
+
const state = await this.getOrSaveState();
|
|
72
|
+
const pkce = await this.getPkce();
|
|
73
|
+
return `${this.config.serverUrl.trim()}/login/oauth/authorize?client_id=${this.config.clientId}&response_type=code&redirect_uri=${encodeURIComponent(redirectUri)}&scope=${scope}&state=${state}&code_challenge=${pkce.code_challenge}&code_challenge_method=S256`;
|
|
74
|
+
}
|
|
75
|
+
// Opens the Quanvio One login page with the given auth browser, e.g., WebBrowser.openAuthSessionAsync for Expo,
|
|
76
|
+
// and exchanges the returned code for an access token. Returns undefined if the user cancels the login.
|
|
77
|
+
async signin(openAuthSession) {
|
|
78
|
+
const signinUrl = await this.getSigninUrl();
|
|
79
|
+
const result = await openAuthSession(signinUrl, this.getRedirectUri());
|
|
80
|
+
if (result.type !== 'success' || !result.url) {
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
return this.getAccessToken(result.url);
|
|
84
|
+
}
|
|
85
|
+
getUserProfileUrl(userName, account) {
|
|
86
|
+
let param = '';
|
|
87
|
+
if (account !== undefined && account !== null) {
|
|
88
|
+
param = `?access_token=${account.accessToken}`;
|
|
89
|
+
}
|
|
90
|
+
return `${this.config.serverUrl.trim()}/users/${this.config.organizationName}/${userName}${param}`;
|
|
91
|
+
}
|
|
92
|
+
getMyProfileUrl(account, returnUrl = '') {
|
|
93
|
+
let params = '';
|
|
94
|
+
if (account !== undefined && account !== null) {
|
|
95
|
+
params = `?access_token=${account.accessToken}`;
|
|
96
|
+
if (returnUrl !== '') {
|
|
97
|
+
params += `&returnUrl=${returnUrl}`;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
else if (returnUrl !== '') {
|
|
101
|
+
params = `?returnUrl=${returnUrl}`;
|
|
102
|
+
}
|
|
103
|
+
return `${this.config.serverUrl.trim()}/account${params}`;
|
|
104
|
+
}
|
|
105
|
+
JwtDecode(token) {
|
|
106
|
+
return jwtDecode(token);
|
|
107
|
+
}
|
|
108
|
+
// URLSearchParams is not fully implemented in React Native, so parse the query string by hand
|
|
109
|
+
static getQueryParam(url, name) {
|
|
110
|
+
const query = url.split('#')[0].split('?')[1];
|
|
111
|
+
if (!query) {
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
for (const pair of query.split('&')) {
|
|
115
|
+
const [key, value = ''] = pair.split('=');
|
|
116
|
+
if (decodeURIComponent(key) === name) {
|
|
117
|
+
return decodeURIComponent(value.replace(/\+/g, ' '));
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
async getAccessToken(redirectUrl) {
|
|
123
|
+
var _a;
|
|
124
|
+
const redirectUri = this.getRedirectUri();
|
|
125
|
+
if (redirectUrl.startsWith(redirectUri)) {
|
|
126
|
+
const code = QuanvioSdk.getQueryParam(redirectUrl, 'code');
|
|
127
|
+
if (code === null) {
|
|
128
|
+
console.error('Error during Signin Request:', (_a = QuanvioSdk.getQueryParam(redirectUrl, 'error')) !== null && _a !== void 0 ? _a : redirectUrl);
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
const state = QuanvioSdk.getQueryParam(redirectUrl, 'state');
|
|
132
|
+
if (state !== null) {
|
|
133
|
+
await AsyncStorage.setItem('quanvio-state', state);
|
|
134
|
+
}
|
|
135
|
+
const codeVerifier = (await this.getPkce()).code_verifier;
|
|
136
|
+
try {
|
|
137
|
+
const response = await fetch(`${this.config.serverUrl.trim()}/api/login/oauth/access_token`, {
|
|
138
|
+
method: 'POST',
|
|
139
|
+
headers: {
|
|
140
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
141
|
+
},
|
|
142
|
+
body: `client_id=${this.config.clientId}&grant_type=authorization_code&code=${encodeURIComponent(code)}&redirect_uri=${encodeURIComponent(redirectUri)}&code_verifier=${codeVerifier}`,
|
|
143
|
+
credentials: 'include',
|
|
144
|
+
});
|
|
145
|
+
if (response.ok) {
|
|
146
|
+
const responseData = await response.json();
|
|
147
|
+
const token = responseData.access_token;
|
|
148
|
+
return token;
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
console.error('Error during AccessToken Request:', response);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
catch (error) {
|
|
155
|
+
console.error('Error during Signin Request:', error);
|
|
156
|
+
}
|
|
157
|
+
finally {
|
|
158
|
+
// a code verifier is only valid for one authorization code, the next signin gets a new one
|
|
159
|
+
await AsyncStorage.removeItem('quanvio-pkce');
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
isSilentSigninRequested() {
|
|
164
|
+
if (typeof window === 'undefined' || !window.location) {
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
const params = new URLSearchParams(window.location.search);
|
|
168
|
+
return params.get('silentSignin') === '1';
|
|
169
|
+
}
|
|
170
|
+
silentSignin(onSuccess, onFailure) {
|
|
171
|
+
if (typeof document === 'undefined') {
|
|
172
|
+
onFailure({ tag: 'Casdoor', type: 'SilentSignin', data: 'silentSignin is not supported in React Native' });
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
const iframe = document.createElement('iframe');
|
|
176
|
+
iframe.style.display = 'none';
|
|
177
|
+
this.getSigninUrl().then(signinUrl => {
|
|
178
|
+
iframe.src = `${signinUrl}&silentSignin=1`;
|
|
179
|
+
document.body.appendChild(iframe);
|
|
180
|
+
});
|
|
181
|
+
const handleMessage = (event) => {
|
|
182
|
+
if (window !== window.parent) {
|
|
183
|
+
return null;
|
|
184
|
+
}
|
|
185
|
+
const message = event.data;
|
|
186
|
+
if (message.tag !== 'Casdoor' || message.type !== 'SilentSignin') {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
if (message.data === 'success') {
|
|
190
|
+
onSuccess(message);
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
onFailure(message);
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
window.addEventListener('message', handleMessage);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
export default QuanvioSdk;
|
package/package.json
CHANGED
|
@@ -1,6 +1,80 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "quanvio-react-native-sdk",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "React Native client SDK for the Quanvio One authentication system (https://one.quanvio.com)",
|
|
5
|
+
"main": "lib/cjs/index.js",
|
|
6
|
+
"typings": "lib/cjs/index.d.ts",
|
|
7
|
+
"module": "lib/esm/index.js",
|
|
8
|
+
"license": "Apache-2.0",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"prepack": "run-s build",
|
|
11
|
+
"postpack": "run-s clean",
|
|
12
|
+
"build": "run-s clean && run-p build:*",
|
|
13
|
+
"build:cjs": "tsc -p tsconfig.cjs.json",
|
|
14
|
+
"build:esm": "tsc -p tsconfig.esm.json",
|
|
15
|
+
"clean": "rimraf lib",
|
|
16
|
+
"test": "jest",
|
|
17
|
+
"coverage": "jest --coverage",
|
|
18
|
+
"semantic-release": "semantic-release"
|
|
19
|
+
},
|
|
20
|
+
"jest": {
|
|
21
|
+
"maxConcurrency": 1,
|
|
22
|
+
"maxWorkers": 1,
|
|
23
|
+
"testTimeout": 30000,
|
|
24
|
+
"testEnvironment": "jsdom",
|
|
25
|
+
"transform": {
|
|
26
|
+
"^.+\\.(t|j)sx?$": "ts-jest"
|
|
27
|
+
},
|
|
28
|
+
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
|
|
29
|
+
"moduleFileExtensions": [
|
|
30
|
+
"ts",
|
|
31
|
+
"tsx",
|
|
32
|
+
"js",
|
|
33
|
+
"jsx",
|
|
34
|
+
"json",
|
|
35
|
+
"node"
|
|
36
|
+
]
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@react-native-async-storage/async-storage": "^1.19.3",
|
|
40
|
+
"@types/jest": "^27.0.2",
|
|
41
|
+
"jest": "^27.2.1",
|
|
42
|
+
"npm-run-all": "^4.1.5",
|
|
43
|
+
"rimraf": "^3.0.2",
|
|
44
|
+
"semantic-release": "^25.0.9",
|
|
45
|
+
"ts-jest": "^27.0.5",
|
|
46
|
+
"typescript": "^4.5.5"
|
|
47
|
+
},
|
|
48
|
+
"files": [
|
|
49
|
+
"lib"
|
|
50
|
+
],
|
|
51
|
+
"keywords": [
|
|
52
|
+
"auth",
|
|
53
|
+
"authn",
|
|
54
|
+
"authentication",
|
|
55
|
+
"sso",
|
|
56
|
+
"oauth",
|
|
57
|
+
"oidc",
|
|
58
|
+
"quanvio"
|
|
59
|
+
],
|
|
60
|
+
"repository": {
|
|
61
|
+
"type": "git",
|
|
62
|
+
"url": "git+https://github.com/quanvio/quanvio-react-native-sdk.git"
|
|
63
|
+
},
|
|
64
|
+
"author": {
|
|
65
|
+
"name": "Quanvio",
|
|
66
|
+
"email": "support@quanvio.com",
|
|
67
|
+
"url": "https://quanvio.com"
|
|
68
|
+
},
|
|
69
|
+
"bugs": {
|
|
70
|
+
"url": "https://github.com/quanvio/quanvio-react-native-sdk/issues"
|
|
71
|
+
},
|
|
72
|
+
"homepage": "https://github.com/quanvio/quanvio-react-native-sdk",
|
|
73
|
+
"dependencies": {
|
|
74
|
+
"jwt-decode": "^3.1.2",
|
|
75
|
+
"pkce-challenge": "3.1.0"
|
|
76
|
+
},
|
|
77
|
+
"peerDependencies": {
|
|
78
|
+
"@react-native-async-storage/async-storage": "^1.19.3 || ^2.0.0"
|
|
79
|
+
}
|
|
80
|
+
}
|