@tereo-io/react 0.0.0-stage → 0.1.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 +202 -0
- package/NOTICE +4 -0
- package/README.md +167 -2
- package/dist/api.d.ts +11 -0
- package/dist/api.d.ts.map +1 -0
- package/dist/api.js +62 -0
- package/dist/api.js.map +1 -0
- package/dist/config.d.ts +69 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +41 -0
- package/dist/config.js.map +1 -0
- package/dist/events.d.ts +48 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/events.js +129 -0
- package/dist/events.js.map +1 -0
- package/dist/hooks.d.ts +21 -0
- package/dist/hooks.d.ts.map +1 -0
- package/dist/hooks.js +46 -0
- package/dist/hooks.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/logger.d.ts +3 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/logger.js +9 -0
- package/dist/logger.js.map +1 -0
- package/dist/markers.d.ts +7 -0
- package/dist/markers.d.ts.map +1 -0
- package/dist/markers.js +15 -0
- package/dist/markers.js.map +1 -0
- package/dist/permissions.d.ts +4 -0
- package/dist/permissions.d.ts.map +1 -0
- package/dist/permissions.js +64 -0
- package/dist/permissions.js.map +1 -0
- package/dist/provider.d.ts +30 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/provider.js +213 -0
- package/dist/provider.js.map +1 -0
- package/dist/refresh.d.ts +15 -0
- package/dist/refresh.d.ts.map +1 -0
- package/dist/refresh.js +98 -0
- package/dist/refresh.js.map +1 -0
- package/dist/signIn.d.ts +13 -0
- package/dist/signIn.d.ts.map +1 -0
- package/dist/signIn.js +134 -0
- package/dist/signIn.js.map +1 -0
- package/dist/signOut.d.ts +8 -0
- package/dist/signOut.d.ts.map +1 -0
- package/dist/signOut.js +69 -0
- package/dist/signOut.js.map +1 -0
- package/dist/storage.d.ts +6 -0
- package/dist/storage.d.ts.map +1 -0
- package/dist/storage.js +73 -0
- package/dist/storage.js.map +1 -0
- package/dist/storageLock.d.ts +14 -0
- package/dist/storageLock.d.ts.map +1 -0
- package/dist/storageLock.js +100 -0
- package/dist/storageLock.js.map +1 -0
- package/dist/tokens.d.ts +13 -0
- package/dist/tokens.d.ts.map +1 -0
- package/dist/tokens.js +27 -0
- package/dist/tokens.js.map +1 -0
- package/package.json +66 -4
- package/src/api.ts +82 -0
- package/src/config.ts +108 -0
- package/src/events.ts +184 -0
- package/src/hooks.ts +62 -0
- package/src/index.ts +34 -0
- package/src/logger.ts +9 -0
- package/src/markers.ts +26 -0
- package/src/permissions.ts +77 -0
- package/src/provider.tsx +291 -0
- package/src/refresh.ts +120 -0
- package/src/signIn.ts +168 -0
- package/src/signOut.ts +85 -0
- package/src/storage.ts +80 -0
- package/src/storageLock.ts +111 -0
- package/src/tokens.ts +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
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.
|
|
202
|
+
|
package/NOTICE
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,168 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @tereo-io/react
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Sign a React app in through [Tereo](https://github.com/tereo-io/tereo): OAuth with PKCE, a session
|
|
4
|
+
shared by tabs, API clients that renew their token, permission checks and live updates from core's
|
|
5
|
+
socket.
|
|
6
|
+
|
|
7
|
+
Peers: `react` ^19, `@tanstack/react-query` ^5, `axios` ^1, `socket.io-client` ^4.
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install @tereo-io/react
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
ESM only. The peers must be installed by the app; the package does not depend on any UI library.
|
|
14
|
+
|
|
15
|
+
## Setup
|
|
16
|
+
|
|
17
|
+
Mount `TereoAuthProvider` inside a React Query `QueryClientProvider`.
|
|
18
|
+
|
|
19
|
+
```tsx
|
|
20
|
+
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
21
|
+
import {
|
|
22
|
+
TereoAuthProvider,
|
|
23
|
+
type TereoAuthConfig,
|
|
24
|
+
type TereoUser,
|
|
25
|
+
} from "@tereo-io/react";
|
|
26
|
+
|
|
27
|
+
const queryClient = new QueryClient();
|
|
28
|
+
|
|
29
|
+
const config: TereoAuthConfig = {
|
|
30
|
+
coreUrl: "https://core.example.com",
|
|
31
|
+
clientId: "my-app",
|
|
32
|
+
redirectUri: `${window.location.origin}/callback`,
|
|
33
|
+
apis: { zeteo: "/api" }, // name -> base URL of each service the app calls with the user's token
|
|
34
|
+
loadUser: async (apis) => {
|
|
35
|
+
const { data } = await apis.zeteo.get<TereoUser>("/v1/me");
|
|
36
|
+
return data; // must carry `permissions: string[]`
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export const App = () => (
|
|
41
|
+
<QueryClientProvider client={queryClient}>
|
|
42
|
+
<TereoAuthProvider config={config}>
|
|
43
|
+
<Routes />
|
|
44
|
+
</TereoAuthProvider>
|
|
45
|
+
</QueryClientProvider>
|
|
46
|
+
);
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
The config is read once, at mount. Only `onEvent` follows later renders.
|
|
50
|
+
|
|
51
|
+
| Option | |
|
|
52
|
+
|--------|---|
|
|
53
|
+
| `coreUrl`, `clientId`, `redirectUri`, `apis`, `loadUser` | Required |
|
|
54
|
+
| `scopes` | Default `openid profile email offline_access` |
|
|
55
|
+
| `basePath` | Where the app is served, without a trailing slash. Default `""` |
|
|
56
|
+
| `isPublicPath(routePath)` | Routes that need no session. Added to the redirect path and `signedOutPath`, which are always public. Paths are route paths, without `basePath` |
|
|
57
|
+
| `signedOutPath` | Where a signed-out visitor lands. Public. Default `/` |
|
|
58
|
+
| `storagePrefix` | Default `tereo_${clientId}` |
|
|
59
|
+
| `tokenExpirationBuffer` | Milliseconds before expiry at which a token is renewed. Default 30000 |
|
|
60
|
+
| `requestTimeoutMs` | Default 15000 |
|
|
61
|
+
| `live`, `wsPath` | Open core's socket for live updates (default `true`), at `/ws` |
|
|
62
|
+
| `onEvent(event)` | Called for every event from core's socket, `session-revoked` included. Reads the latest `config.onEvent` |
|
|
63
|
+
| `debug` | Log internals to the console |
|
|
64
|
+
|
|
65
|
+
A visitor with no session on a path that is not public is sent to `${basePath}${signedOutPath}`.
|
|
66
|
+
If a silent session restore fails transiently (offline, a 5xx), the page stays signed out but is
|
|
67
|
+
not redirected until the next request or a reload.
|
|
68
|
+
|
|
69
|
+
## Hooks
|
|
70
|
+
|
|
71
|
+
```tsx
|
|
72
|
+
import { useApi, useAuth, usePublicApi } from "@tereo-io/react";
|
|
73
|
+
|
|
74
|
+
const Toolbar = () => {
|
|
75
|
+
const { isLoading, isSignedIn, hasPermission, signIn } = useAuth();
|
|
76
|
+
const zeteo = useApi("zeteo"); // an axios instance that sends and renews the token
|
|
77
|
+
const open = usePublicApi("zeteo"); // the same API, no token
|
|
78
|
+
|
|
79
|
+
if (isLoading) return null;
|
|
80
|
+
if (!isSignedIn) return <button onClick={() => signIn()}>Sign in</button>;
|
|
81
|
+
return hasPermission("zeteo.queries.run") ? <RunButton api={zeteo} /> : null;
|
|
82
|
+
};
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
`useAuth()` returns `user`, `isLoading`, `isSignedIn`, `isLive` (the socket is connected),
|
|
86
|
+
`hasPermission(required)`, `signIn`, `signOut`, `switchOrganization(orgId)` and `reloadUser()`.
|
|
87
|
+
`useAuth<MyUser>()` types `user`. `hasPermission` honours the hierarchy (`write` includes `read`,
|
|
88
|
+
`admin` includes all). `signIn`, `signOut` and `completeSignIn` are also exported as plain
|
|
89
|
+
functions.
|
|
90
|
+
|
|
91
|
+
The user lookup is retried twice, with backoff, unless `loadUser` fails with a 401 or 403. When
|
|
92
|
+
core revokes this tab's own session, the provider clears it locally and goes to `signedOutPath`,
|
|
93
|
+
sending nothing to core, where the session is already over.
|
|
94
|
+
|
|
95
|
+
## Callback page
|
|
96
|
+
|
|
97
|
+
`useCompleteSignIn().complete(search)` finishes sign-in on the redirect route. The exchange runs
|
|
98
|
+
once per mount, so React StrictMode's second effect is safe. `complete` rejects on failure; catch
|
|
99
|
+
it, or read `error`.
|
|
100
|
+
|
|
101
|
+
```tsx
|
|
102
|
+
import { useEffect } from "react";
|
|
103
|
+
import { useCompleteSignIn } from "@tereo-io/react";
|
|
104
|
+
|
|
105
|
+
export const Callback = () => {
|
|
106
|
+
const { complete, error, isPending } = useCompleteSignIn();
|
|
107
|
+
|
|
108
|
+
useEffect(() => {
|
|
109
|
+
complete(new URLSearchParams(window.location.search))
|
|
110
|
+
.then(({ returnTo }) => window.location.replace(returnTo ?? "/"))
|
|
111
|
+
.catch(() => {}); // the failure is in `error`
|
|
112
|
+
}, [complete]);
|
|
113
|
+
|
|
114
|
+
if (error) return <p>Sign-in failed: {error.message}</p>;
|
|
115
|
+
return isPending ? <p>Signing in...</p> : null;
|
|
116
|
+
};
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## Requirements
|
|
120
|
+
|
|
121
|
+
- The app and core are on the same site.
|
|
122
|
+
- The app's origin is in core's `CORS_ALLOWED_ORIGINS`.
|
|
123
|
+
- The login client's redirect URI is the app's callback, on the app's origin.
|
|
124
|
+
- For live updates, the login client needs `liveEvents`: the admin UI's "Live updates" toggle,
|
|
125
|
+
`liveEvents` in the admin API, or `"liveEvents": true` in a provisioning document. Without it,
|
|
126
|
+
set `live: false`: core refuses the socket ("This token is not issued for core's API") and the
|
|
127
|
+
provider gives up on live updates for that sign-in. Either way, a service's UI must not request
|
|
128
|
+
core scopes to get them: any core scope makes the token delegated, and the service's own
|
|
129
|
+
permissions are then reported as empty (its API answers 403). Core scopes are for apps that call
|
|
130
|
+
core's own API.
|
|
131
|
+
|
|
132
|
+
## Fresh permissions from your own service
|
|
133
|
+
|
|
134
|
+
When core pushes a `permissions` event, the provider reloads the user through `loadUser`. The
|
|
135
|
+
SDK's `requireAuth` caches a token's introspection for up to 60 seconds, so the service's `me`
|
|
136
|
+
route drops the entry first, or it would answer with the permissions from before the change.
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
import express from "express";
|
|
140
|
+
import { createCoreClient } from "@tereo-io/sdk";
|
|
141
|
+
|
|
142
|
+
const core = createCoreClient({ /* ... */ });
|
|
143
|
+
await core.start();
|
|
144
|
+
|
|
145
|
+
const app = express();
|
|
146
|
+
|
|
147
|
+
app.get(
|
|
148
|
+
"/v1/me",
|
|
149
|
+
async (req, _res, next) => {
|
|
150
|
+
const token = req.headers.authorization?.match(/^Bearer (.+)$/)?.[1];
|
|
151
|
+
if (token) await core.auth.invalidateToken(token);
|
|
152
|
+
next();
|
|
153
|
+
},
|
|
154
|
+
core.auth.requireAuth,
|
|
155
|
+
(req, res) => {
|
|
156
|
+
const auth = core.auth.validateUser(req);
|
|
157
|
+
res.json({ id: auth.userId, name: auth.name, permissions: auth.permissions });
|
|
158
|
+
}
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
app.use(core.auth.requireAuth); // everything else
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
## Core's API
|
|
165
|
+
|
|
166
|
+
A service UI's token is for the services in `apis`, not for core's API (`/v1/account`, ...), so
|
|
167
|
+
do not list core there. `switchOrganization` works anyway: it signs in again for the chosen
|
|
168
|
+
organization through core's authorize endpoint.
|
package/dist/api.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type AxiosInstance } from "axios";
|
|
2
|
+
/**
|
|
3
|
+
* One client per named API. `authed` sends the access token, renewing it
|
|
4
|
+
* before it expires and once after a 401; `open` sends nothing, for
|
|
5
|
+
* endpoints that need no token or use cookies.
|
|
6
|
+
*/
|
|
7
|
+
export declare const createApiClients: (apis: Record<string, string>) => {
|
|
8
|
+
authed: Record<string, AxiosInstance>;
|
|
9
|
+
open: Record<string, AxiosInstance>;
|
|
10
|
+
};
|
|
11
|
+
//# sourceMappingURL=api.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAIA,OAAc,EAEZ,KAAK,aAAa,EAEnB,MAAM,OAAO,CAAC;AAqDf;;;;GAIG;AACH,eAAO,MAAM,gBAAgB,GAC3B,MAAM,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,KAC3B;IACD,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;IACtC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;CAWrC,CAAC"}
|
package/dist/api.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { readTokens, writeTokens } from "./tokens.js";
|
|
2
|
+
import { refreshTokens } from "./refresh.js";
|
|
3
|
+
import { activeConfig } from "./config.js";
|
|
4
|
+
import { hasSession } from "./markers.js";
|
|
5
|
+
import axios from "axios";
|
|
6
|
+
const bearer = (config, accessToken) => {
|
|
7
|
+
config.headers.Authorization = `Bearer ${accessToken}`;
|
|
8
|
+
};
|
|
9
|
+
const attachAuth = (instance) => {
|
|
10
|
+
instance.interceptors.request.use(async (config) => {
|
|
11
|
+
const tokens = readTokens();
|
|
12
|
+
const nearExpiry = !tokens ||
|
|
13
|
+
Date.now() > tokens.expiresAt - activeConfig().tokenExpirationBuffer;
|
|
14
|
+
// A failed refresh fails the request; refreshTokens clears the tokens
|
|
15
|
+
// when core rejected it.
|
|
16
|
+
if (nearExpiry && hasSession()) {
|
|
17
|
+
bearer(config, (await refreshTokens()).access_token);
|
|
18
|
+
}
|
|
19
|
+
else if (tokens) {
|
|
20
|
+
bearer(config, tokens.access_token);
|
|
21
|
+
}
|
|
22
|
+
return config;
|
|
23
|
+
});
|
|
24
|
+
instance.interceptors.response.use((response) => response, async (error) => {
|
|
25
|
+
const original = error.config;
|
|
26
|
+
if (error.response?.status !== 401 || !original || original._retry) {
|
|
27
|
+
throw error;
|
|
28
|
+
}
|
|
29
|
+
if (!hasSession()) {
|
|
30
|
+
writeTokens(null);
|
|
31
|
+
throw error;
|
|
32
|
+
}
|
|
33
|
+
original._retry = true;
|
|
34
|
+
// Another tab (or a concurrent request) renewed the token after this
|
|
35
|
+
// request was sent: retry with that one rather than renew again.
|
|
36
|
+
const stored = readTokens()?.access_token;
|
|
37
|
+
if (stored && original.headers.Authorization !== `Bearer ${stored}`) {
|
|
38
|
+
bearer(original, stored);
|
|
39
|
+
return instance(original);
|
|
40
|
+
}
|
|
41
|
+
const tokens = await refreshTokens();
|
|
42
|
+
bearer(original, tokens.access_token);
|
|
43
|
+
return instance(original);
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* One client per named API. `authed` sends the access token, renewing it
|
|
48
|
+
* before it expires and once after a 401; `open` sends nothing, for
|
|
49
|
+
* endpoints that need no token or use cookies.
|
|
50
|
+
*/
|
|
51
|
+
export const createApiClients = (apis) => {
|
|
52
|
+
const authed = {};
|
|
53
|
+
const open = {};
|
|
54
|
+
for (const [name, baseURL] of Object.entries(apis)) {
|
|
55
|
+
const instance = axios.create({ baseURL });
|
|
56
|
+
attachAuth(instance);
|
|
57
|
+
authed[name] = instance;
|
|
58
|
+
open[name] = axios.create({ baseURL });
|
|
59
|
+
}
|
|
60
|
+
return { authed, open };
|
|
61
|
+
};
|
|
62
|
+
//# sourceMappingURL=api.js.map
|
package/dist/api.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api.js","sourceRoot":"","sources":["../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AG,KAAK,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC;IACzC,CAAC;IACD,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC;AAC1B,CAAC,CAAC"}
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { AxiosInstance } from "axios";
|
|
2
|
+
export interface TereoUser {
|
|
3
|
+
permissions: string[];
|
|
4
|
+
[key: string]: unknown;
|
|
5
|
+
}
|
|
6
|
+
export type TereoEvent = {
|
|
7
|
+
type: "permissions";
|
|
8
|
+
payload: {
|
|
9
|
+
reason: string;
|
|
10
|
+
timestamp: number;
|
|
11
|
+
};
|
|
12
|
+
} | {
|
|
13
|
+
type: "organization";
|
|
14
|
+
payload: {
|
|
15
|
+
reason: string;
|
|
16
|
+
organizationId?: string;
|
|
17
|
+
timestamp: number;
|
|
18
|
+
};
|
|
19
|
+
} | {
|
|
20
|
+
type: "session-revoked";
|
|
21
|
+
payload: {
|
|
22
|
+
sessionId: string;
|
|
23
|
+
timestamp: number;
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
export interface TereoAuthConfig {
|
|
27
|
+
coreUrl: string;
|
|
28
|
+
clientId: string;
|
|
29
|
+
redirectUri: string;
|
|
30
|
+
/** Name → base URL of each API the app calls with the user's token. */
|
|
31
|
+
apis: Record<string, string>;
|
|
32
|
+
loadUser: (apis: Record<string, AxiosInstance>) => Promise<TereoUser>;
|
|
33
|
+
/** @default "openid profile email offline_access" */
|
|
34
|
+
scopes?: string;
|
|
35
|
+
/** Path the app is served under, without a trailing slash. @default "" */
|
|
36
|
+
basePath?: string;
|
|
37
|
+
/** Routes that need no session, besides the redirect path and `signedOutPath`, which always are. */
|
|
38
|
+
isPublicPath?: (routePath: string) => boolean;
|
|
39
|
+
/** Where a signed-out visitor lands. Must be public. @default "/" */
|
|
40
|
+
signedOutPath?: string;
|
|
41
|
+
/** @default `tereo_${clientId}` */
|
|
42
|
+
storagePrefix?: string;
|
|
43
|
+
/** Milliseconds before expiry at which a token is renewed. @default 30000 */
|
|
44
|
+
tokenExpirationBuffer?: number;
|
|
45
|
+
/** @default 15000 */
|
|
46
|
+
requestTimeoutMs?: number;
|
|
47
|
+
/** Open core's socket for live updates. @default true */
|
|
48
|
+
live?: boolean;
|
|
49
|
+
/** @default "/ws" */
|
|
50
|
+
wsPath?: string;
|
|
51
|
+
onEvent?: (event: TereoEvent) => void;
|
|
52
|
+
/** @default false */
|
|
53
|
+
debug?: boolean;
|
|
54
|
+
}
|
|
55
|
+
export interface StorageKeys {
|
|
56
|
+
tokens: string;
|
|
57
|
+
pkce: string;
|
|
58
|
+
loggedOut: string;
|
|
59
|
+
signedIn: string;
|
|
60
|
+
postLoginPath: string;
|
|
61
|
+
}
|
|
62
|
+
export type ResolvedConfig = Required<Omit<TereoAuthConfig, "onEvent">> & Pick<TereoAuthConfig, "onEvent"> & {
|
|
63
|
+
keys: StorageKeys;
|
|
64
|
+
};
|
|
65
|
+
export declare const pathWithoutBase: (pathname: string, basePath: string) => string;
|
|
66
|
+
export declare const resolveConfig: (c: TereoAuthConfig) => ResolvedConfig;
|
|
67
|
+
export declare const setActiveConfig: (c: ResolvedConfig | null) => void;
|
|
68
|
+
export declare const activeConfig: () => ResolvedConfig;
|
|
69
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AAE3C,MAAM,WAAW,SAAS;IACxB,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED,MAAM,MAAM,UAAU,GAClB;IAAE,IAAI,EAAE,aAAa,CAAC;IAAC,OAAO,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,CA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AO,CAAC;CACjB;AAED,MAAM,WAAW,WAAW;IAC1B,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,aAAa,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,MAAM,cAAc,GAAG,QAAQ,CAAC,IAAI,CAAC,eAAe,EAAE,SAAS,CAAC,CAAC,GACrE,IAAI,CAAC,eAAe,EAAE,SAAS,CAAC,GAAG;IAAE,IAAI,EAAE,WAAW,CAAA;CAAE,CAAC;AAE3D,eAAO,MAAM,eAAe,GAAI,UAAU,MAAM,EAAE,UAAU,MAAM,KAAG,MAGvD,CAAC;AAEf,eAAO,MAAM,aAAa,GAAI,GAAG,eAAe,KAAG,cAgClD,CAAC;AAIF,eAAO,MAAM,eAAe,GAAI,GAAG,cAAc,GAAG,IAAI,KAAG,IAE1D,CAAC;AAEF,eAAO,MAAM,YAAY,QAAO,cAG/B,CAAC"}
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export const pathWithoutBase = (pathname, basePath) => basePath && (pathname === basePath || pathname.startsWith(`${basePath}/`))
|
|
2
|
+
? pathname.slice(basePath.length) || "/"
|
|
3
|
+
: pathname;
|
|
4
|
+
export const resolveConfig = (c) => {
|
|
5
|
+
const basePath = c.basePath ?? "";
|
|
6
|
+
const signedOutPath = c.signedOutPath ?? "/";
|
|
7
|
+
const storagePrefix = c.storagePrefix ?? `tereo_${c.clientId}`;
|
|
8
|
+
const redirectPath = pathWithoutBase(new URL(c.redirectUri, "http://localhost").pathname, basePath);
|
|
9
|
+
return {
|
|
10
|
+
...c,
|
|
11
|
+
scopes: c.scopes ?? "openid profile email offline_access",
|
|
12
|
+
basePath,
|
|
13
|
+
signedOutPath,
|
|
14
|
+
isPublicPath: (routePath) => routePath === redirectPath ||
|
|
15
|
+
routePath === signedOutPath ||
|
|
16
|
+
(c.isPublicPath?.(routePath) ?? false),
|
|
17
|
+
storagePrefix,
|
|
18
|
+
tokenExpirationBuffer: c.tokenExpirationBuffer ?? 30_000,
|
|
19
|
+
requestTimeoutMs: c.requestTimeoutMs ?? 15_000,
|
|
20
|
+
live: c.live ?? true,
|
|
21
|
+
wsPath: c.wsPath ?? "/ws",
|
|
22
|
+
debug: c.debug ?? false,
|
|
23
|
+
keys: {
|
|
24
|
+
tokens: `${storagePrefix}_tokens`,
|
|
25
|
+
pkce: `${storagePrefix}_pkce`,
|
|
26
|
+
loggedOut: `${storagePrefix}_logged_out`,
|
|
27
|
+
signedIn: `${storagePrefix}_signed_in`,
|
|
28
|
+
postLoginPath: `${storagePrefix}_post_login_path`,
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
let active = null;
|
|
33
|
+
export const setActiveConfig = (c) => {
|
|
34
|
+
active = c;
|
|
35
|
+
};
|
|
36
|
+
export const activeConfig = () => {
|
|
37
|
+
if (!active)
|
|
38
|
+
throw new Error("TereoAuthProvider is not mounted");
|
|
39
|
+
return active;
|
|
40
|
+
};
|
|
41
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AA2DA,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,QAAgB,EAAE,QAAgB,EAAU,EAAE,CAC5E,QAAQ,IAAI,CAAC,QAAQ,KAAK,QAAQ,IAAI,QAAQ,CAAC,UAAU,CAAC,GAAG,QAAQ,GAAG,CAAC,CAAC;IACxE,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,GAAG;IACxC,CAAC,CAAC,QAAQ,CAAC;AAEf,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,CAAkB,EAAkB,EAAE;IAClE,MAAM,QAAQ,GAAG,CAAC,CAAC,QAAQ,IAAI,EAAE,CAAC;IAClC,MA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AE;IAC/C,IAAI,CAAC,MAAM;QAAE,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC,CAAC;IACjE,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC"}
|
package/dist/events.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { TereoEvent } from "./config.js";
|
|
2
|
+
/** The slice of a socket.io client socket the live-updates loop uses. */
|
|
3
|
+
export interface EventSocket {
|
|
4
|
+
readonly active: boolean;
|
|
5
|
+
connect(): void;
|
|
6
|
+
disconnect(): void;
|
|
7
|
+
on(event: string, listener: (...args: never[]) => void): void;
|
|
8
|
+
}
|
|
9
|
+
export type SocketAuth = (cb: (data: {
|
|
10
|
+
token?: string;
|
|
11
|
+
}) => void) => void;
|
|
12
|
+
export interface TereoEventsDeps {
|
|
13
|
+
/** Builds the socket, not yet connected; `auth` is read at each connect. */
|
|
14
|
+
createSocket: (auth: SocketAuth) => EventSocket;
|
|
15
|
+
/** The stored access token, or null when signed out. */
|
|
16
|
+
accessToken: () => string | null;
|
|
17
|
+
/** Whether the stored token is still good for a handshake. */
|
|
18
|
+
tokenIsFresh: () => boolean;
|
|
19
|
+
/** The current token's session (`sid`, the refresh token's id). */
|
|
20
|
+
sessionId: () => string | null;
|
|
21
|
+
/**
|
|
22
|
+
* Refreshes the token; a refusal clears it, which signs the user out. Must
|
|
23
|
+
* make the new token what `accessToken()` returns before it resolves.
|
|
24
|
+
*/
|
|
25
|
+
refresh: () => Promise<unknown>;
|
|
26
|
+
/** Core revoked this session; it is already over server-side. */
|
|
27
|
+
sessionEnded: () => void;
|
|
28
|
+
/** Every event core sends, including revocations of the user's other sessions. */
|
|
29
|
+
onEvent: (event: TereoEvent) => void;
|
|
30
|
+
/**
|
|
31
|
+
* Core will not admit this session's token: the login client lacks
|
|
32
|
+
* `liveEvents`, or core refused the handshake again after a refresh. The
|
|
33
|
+
* loop stops until the user signs in again.
|
|
34
|
+
*/
|
|
35
|
+
refused?: (reason: string) => void;
|
|
36
|
+
/** Defaults to setTimeout; returns a cancel function. */
|
|
37
|
+
schedule?: (fn: () => void, ms: number) => () => void;
|
|
38
|
+
log?: (message: string, detail?: unknown) => void;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Core's `/ws` events for the signed-in user. Core authenticates a socket
|
|
42
|
+
* once, at the handshake, and closes it when its token expires or is
|
|
43
|
+
* revoked; socket.io does not reconnect after a server-side close or a
|
|
44
|
+
* handshake the server refused, so this does, with a fresh token. Returns a
|
|
45
|
+
* stop function.
|
|
46
|
+
*/
|
|
47
|
+
export declare const startTereoEvents: (deps: TereoEventsDeps) => (() => void);
|
|
48
|
+
//# sourceMappingURL=events.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"events.d.ts","sourceRoot":"","sources":["../src/events.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAE9C,yEAAyE;AACzE,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;IACzB,OAAO,IAAI,IAAI,CAAC;IAChB,UAAU,IAAI,IAAI,CAAC;IACnB,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,KAAK,EAAE,KAAK,IAAI,GAAG,IAAI,CAAC;CAC/D;AAED,MAAM,MAAM,UAAU,GAAG,CAAC,EAAE,EAAE,CAAC,IAAI,EAAE;IAAE,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,KAAK,IAAI,KAAK,IAAI,CAAC;AAE1E,MAAM,WAAW,eAAe;IAC9B,4EAA4E;IAC5E,YAAY,EAAE,CAAC,IAAI,EAAE,UAAU,KAAK,WAAW,CAAC;IAChD,wDAAwD;IACxD,WAAW,EAAE,MAAM,MAAM,GAAG,IAAI,CAAC;IACjC,8DAA8D;IAC9D,YAAY,EAAE,MAAM,OAAO,CAAC;IAC5B,mEAAmE;IACnE,SA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