Commit 6002a8c
authored
Introduce auth rotation and session auth support (#890)
# ADR 012: re-authentication
This PR introduces two new auth mechanics for different use-cases
1) Auth Rotation
2) Session Auth (a.k.a. user switching)
**Note that all APIs introduced in this PR are previews**
See https://github.com/neo4j/neo4j-python-driver/wiki/preview-features
## 1) Auth Rotation
This is used for auth tokens that is expected to expire (e.g., SSO).
A `neo4j.auth_management.AuthManager` instance
(or `neo4j.auth_management.AsyncAuthManager` for async driver) may be passed
to the driver instead of a static auth token.
```python
import neo4j
from neo4j.auth_management import AuthManager
class MyManager(AuthManager):
... # see API dos for details
with neo4j.GraphDatabase.driver(
"neo4j://example.com:7687",
auth=MyManager(),
) as driver:
...
```
The easiest way to get started is using the provided `AuthManager`
implementation. For example:
```python
import neo4j
from neo4j.auth_management import (
AuthManagers,
ExpiringAuth,
)
def auth_provider():
# some way to getting a token
sso_token = get_sso_token()
# assume we know our tokens expire every 60 seconds
expires_in = 60
return ExpiringAuth(
auth=neo4j.bearer_auth(sso_token),
# Include a little buffer so that we fetch a new token
# *before* the old one expires
expires_in=expires_in - 10
)
with neo4j.GraphDatabase.driver(
"neo4j://example.com:7687",
auth=AuthManagers.temporal(auth_provider)
) as driver:
...
```
**Note**
This API is explicitly *not* designed for switching users.
In fact, the token returned by each manager must always belong to the same
identity. Switching identities using the `AuthManager` is undefined behavior.
## 2) Session Auth
For the purpose of switching users, `Session`s can be configured with a static
auth token. This is very similar to impersonation in that all work in the
session will be executed in the security context of the user associated with
the auth token. The major difference is that impersonation does not require or
verify authentication information of the target user, however it requires
the impersonating user to have the permission to impersonate.
**Note**
This requires Bolt protocol version 5.3 or higher (Neo4j DBMS 5.8+).
```python
import neo4j
with neo4j.GraphDatabase.driver(
"neo4j://example.com:7687",
auth=("user1", "password1"),
) as driver:
with driver.session(database="neo4j") as session:
... # do some work as user1
with driver.session(database="neo4j",
auth=("user2", "password2")) as session:
... # do some work as user2
```
## References
Depends on:
* neo4j-drivers/testkit#539
Related PRs:
* #891
Issues:
* closes #8341 parent 288dc12 commit 6002a8c
File tree
85 files changed
+6327
-2226
lines changed- docs/source
- src/neo4j
- _async_compat/network
- _async
- io
- work
- _sync
- io
- work
- _work
- time
- testkitbackend
- _async
- _sync
- tests
- integration/examples
- unit
- async_
- fixtures
- io
- work
- common
- mixed/io
- sync
- fixtures
- io
- work
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
85 files changed
+6327
-2226
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
107 | 107 | | |
108 | 108 | | |
109 | 109 | | |
110 | | - | |
| 110 | + | |
111 | 111 | | |
112 | 112 | | |
113 | 113 | | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
114 | 121 | | |
115 | 122 | | |
116 | 123 | | |
| |||
154 | 161 | | |
155 | 162 | | |
156 | 163 | | |
157 | | - | |
| 164 | + | |
| 165 | + | |
158 | 166 | | |
159 | 167 | | |
160 | 168 | | |
| |||
174 | 182 | | |
175 | 183 | | |
176 | 184 | | |
177 | | - | |
| 185 | + | |
178 | 186 | | |
179 | 187 | | |
180 | 188 | | |
| |||
184 | 192 | | |
185 | 193 | | |
186 | 194 | | |
| 195 | + | |
187 | 196 | | |
188 | 197 | | |
189 | 198 | | |
| |||
263 | 272 | | |
264 | 273 | | |
265 | 274 | | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
266 | 289 | | |
267 | 290 | | |
268 | 291 | | |
| |||
273 | 296 | | |
274 | 297 | | |
275 | 298 | | |
276 | | - | |
277 | 299 | | |
278 | 300 | | |
279 | 301 | | |
| |||
334 | 356 | | |
335 | 357 | | |
336 | 358 | | |
337 | | - | |
| 359 | + | |
338 | 360 | | |
339 | 361 | | |
340 | 362 | | |
| |||
348 | 370 | | |
349 | 371 | | |
350 | 372 | | |
351 | | - | |
| 373 | + | |
352 | 374 | | |
353 | 375 | | |
354 | 376 | | |
| |||
357 | 379 | | |
358 | 380 | | |
359 | 381 | | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
360 | 385 | | |
361 | 386 | | |
362 | 387 | | |
| |||
433 | 458 | | |
434 | 459 | | |
435 | 460 | | |
436 | | - | |
| 461 | + | |
437 | 462 | | |
438 | 463 | | |
439 | 464 | | |
| |||
784 | 809 | | |
785 | 810 | | |
786 | 811 | | |
| 812 | + | |
787 | 813 | | |
788 | 814 | | |
789 | 815 | | |
| |||
804 | 830 | | |
805 | 831 | | |
806 | 832 | | |
| 833 | + | |
807 | 834 | | |
808 | 835 | | |
809 | 836 | | |
| |||
816 | 843 | | |
817 | 844 | | |
818 | 845 | | |
819 | | - | |
| 846 | + | |
820 | 847 | | |
821 | 848 | | |
822 | 849 | | |
| |||
995 | 1022 | | |
996 | 1023 | | |
997 | 1024 | | |
998 | | - | |
| 1025 | + | |
999 | 1026 | | |
1000 | 1027 | | |
1001 | 1028 | | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
| 1032 | + | |
| 1033 | + | |
| 1034 | + | |
| 1035 | + | |
| 1036 | + | |
| 1037 | + | |
| 1038 | + | |
| 1039 | + | |
| 1040 | + | |
| 1041 | + | |
| 1042 | + | |
| 1043 | + | |
| 1044 | + | |
| 1045 | + | |
| 1046 | + | |
| 1047 | + | |
| 1048 | + | |
| 1049 | + | |
| 1050 | + | |
| 1051 | + | |
1002 | 1052 | | |
1003 | 1053 | | |
1004 | 1054 | | |
| |||
1293 | 1343 | | |
1294 | 1344 | | |
1295 | 1345 | | |
1296 | | - | |
| 1346 | + | |
1297 | 1347 | | |
1298 | 1348 | | |
1299 | 1349 | | |
| |||
1751 | 1801 | | |
1752 | 1802 | | |
1753 | 1803 | | |
| 1804 | + | |
| 1805 | + | |
1754 | 1806 | | |
1755 | 1807 | | |
1756 | 1808 | | |
| |||
1786 | 1838 | | |
1787 | 1839 | | |
1788 | 1840 | | |
| 1841 | + | |
| 1842 | + | |
| 1843 | + | |
1789 | 1844 | | |
1790 | 1845 | | |
1791 | 1846 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
43 | | - | |
| 43 | + | |
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
| |||
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
70 | | - | |
| 70 | + | |
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
| |||
118 | 118 | | |
119 | 119 | | |
120 | 120 | | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
121 | 135 | | |
122 | 136 | | |
123 | 137 | | |
| |||
136 | 150 | | |
137 | 151 | | |
138 | 152 | | |
139 | | - | |
| 153 | + | |
| 154 | + | |
140 | 155 | | |
141 | 156 | | |
142 | 157 | | |
| |||
157 | 172 | | |
158 | 173 | | |
159 | 174 | | |
160 | | - | |
| 175 | + | |
161 | 176 | | |
162 | 177 | | |
163 | 178 | | |
| |||
167 | 182 | | |
168 | 183 | | |
169 | 184 | | |
| 185 | + | |
170 | 186 | | |
171 | 187 | | |
172 | 188 | | |
| |||
202 | 218 | | |
203 | 219 | | |
204 | 220 | | |
205 | | - | |
| 221 | + | |
206 | 222 | | |
207 | 223 | | |
208 | 224 | | |
209 | 225 | | |
210 | 226 | | |
211 | 227 | | |
| 228 | + | |
212 | 229 | | |
213 | 230 | | |
214 | 231 | | |
| |||
245 | 262 | | |
246 | 263 | | |
247 | 264 | | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
248 | 279 | | |
249 | 280 | | |
250 | 281 | | |
| |||
315 | 346 | | |
316 | 347 | | |
317 | 348 | | |
318 | | - | |
| 349 | + | |
319 | 350 | | |
320 | 351 | | |
321 | 352 | | |
| |||
329 | 360 | | |
330 | 361 | | |
331 | 362 | | |
332 | | - | |
| 363 | + | |
333 | 364 | | |
334 | 365 | | |
335 | 366 | | |
| |||
338 | 369 | | |
339 | 370 | | |
340 | 371 | | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
341 | 375 | | |
342 | 376 | | |
343 | 377 | | |
344 | 378 | | |
345 | 379 | | |
346 | 380 | | |
347 | 381 | | |
348 | | - | |
349 | | - | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
350 | 387 | | |
351 | 388 | | |
352 | 389 | | |
| |||
622 | 659 | | |
623 | 660 | | |
624 | 661 | | |
625 | | - | |
| 662 | + | |
626 | 663 | | |
627 | 664 | | |
628 | 665 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
| 55 | + | |
55 | 56 | | |
56 | 57 | | |
57 | 58 | | |
| |||
140 | 141 | | |
141 | 142 | | |
142 | 143 | | |
| 144 | + | |
143 | 145 | | |
144 | 146 | | |
145 | 147 | | |
146 | 148 | | |
147 | 149 | | |
| 150 | + | |
148 | 151 | | |
149 | 152 | | |
150 | 153 | | |
| |||
0 commit comments