Skip to content

Commit 0e07199

Browse files
Session-scoped authentication (#443)
1 parent 13d8c25 commit 0e07199

File tree

9 files changed

+156
-47
lines changed

9 files changed

+156
-47
lines changed

go-manual/modules/ROOT/pages/query-simple.adoc

+19-6
Original file line numberDiff line numberDiff line change
@@ -227,24 +227,37 @@ In other words, there is no guarantee that a write query submitted in read mode
227227

228228

229229
[#impersonation]
230+
[role=label--new-5.14]
230231
=== Run queries as a different user
231232

232-
You can execute a query under the security context of a different user with the callback `neo4j.ExecuteQueryWithImpersonatedUser("<somebodyElse>")`, specifying the name of the user to impersonate.
233-
For this to work, the user under which the `DriverWithContext` object
234-
was created needs to have the link:{neo4j-docs-base-uri}/cypher-manual/current/administration/access-control/dbms-administration#access-control-dbms-administration-impersonation[appropriate permissions].
235-
Impersonating a user is cheaper than creating a new `DriverWithContext` object.
233+
You can execute a query through a different user with the configuration callback `neo4j.ExecuteQueryWithAuthToken()`.
234+
Switching user at the query level is cheaper than creating a new `DriverWithContext` object.
235+
The query is then run within the security context of the given user (i.e., home database, permissions, etc.). +
236+
Query-scoped authentication a server version >= 5.8.
236237

237238
[source, go]
238239
----
240+
queryAuth := neo4j.BasicAuth("somebodyElse", "theirPassword", "")
239241
neo4j.ExecuteQuery(ctx, driver,
240242
"MATCH (p:Person) RETURN p.name",
241243
nil,
242244
neo4j.EagerResultTransformer,
243245
neo4j.ExecuteQueryWithDatabase("neo4j"),
244-
neo4j.ExecuteQueryWithImpersonatedUser("<somebodyElse>"))
246+
neo4j.ExecuteQueryWithAuthToken(queryAuth))
245247
----
246248

247-
When impersonating a user, the query is run within the complete security context of the impersonated user and not the authenticated user (i.e. home database, permissions, etc.).
249+
The callback `neo4j.ExecuteQueryWithImpersonatedUser()` provides a similar functionality, and is available in driver/server versions >= 4.4.
250+
The difference is that you don't need to know a user's password to impersonate them, but the user under which the `Driver` was created needs to have the link:{neo4j-docs-base-uri}/operations-manual/current/authentication-authorization/dbms-administration/#access-control-dbms-administration-impersonation[appropriate permissions].
251+
252+
[source, go]
253+
----
254+
neo4j.ExecuteQuery(ctx, driver,
255+
"MATCH (p:Person) RETURN p.name",
256+
nil,
257+
neo4j.EagerResultTransformer,
258+
neo4j.ExecuteQueryWithDatabase("neo4j"),
259+
neo4j.ExecuteQueryWithImpersonatedUser("<somebodyElse>"))
260+
----
248261

249262

250263
== A full example

go-manual/modules/ROOT/pages/transactions.adoc

+18-6
Original file line numberDiff line numberDiff line change
@@ -494,21 +494,33 @@ Similar remarks hold for the `.ExecuteRead()` and `.ExecuteWrite()` methods.
494494

495495

496496
[#impersonation]
497-
=== Run queries as a different user (impersonation)
497+
[role=label--new-5.14]
498+
=== Run queries as a different user
498499

499-
You can execute a query under the security context of a different user with the configuration parameter `ImpersonatedUser`, specifying the name of the user to impersonate.
500-
For this to work, the user under which the `DriverWithContext` was created needs to have the link:{neo4j-docs-base-uri}/operations-manual/current/authentication-authorization/dbms-administration/#access-control-dbms-administration-impersonation[appropriate permissions].
501-
Impersonating a user is cheaper than creating a new `DriverWithContext` object.
500+
You can execute a query through a different user with the configuration option `Auth`.
501+
Switching user at the session level is cheaper than creating a new `DriverWithContext` object.
502+
Queries are then run within the security context of the given user (i.e., home database, permissions, etc.). +
503+
Session-scoped authentication requires a server version >= 5.8.
502504

503505
[source, go]
504506
----
507+
sessionAuth := neo4j.BasicAuth("somebodyElse", "theirPassword", "")
505508
session := driver.NewSession(ctx, neo4j.SessionConfig{
506509
DatabaseName: "neo4j",
507-
ImpersonatedUser: "<somebodyElse>",
510+
Auth: &sessionAuth,
508511
})
509512
----
510513

511-
When impersonating a user, the query is run within the complete security context of the impersonated user and not the authenticated user (i.e. home database, permissions, etc.).
514+
The option `ImpersonatedUser` provides a similar functionality, and is available in driver/server versions >= 4.4.
515+
The difference is that you don't need to know a user's password to impersonate them, but the user under which the `Driver` was created needs to have the link:{neo4j-docs-base-uri}/operations-manual/current/authentication-authorization/dbms-administration/#access-control-dbms-administration-impersonation[appropriate permissions].
516+
517+
[source, go]
518+
----
519+
session := driver.NewSession(ctx, neo4j.SessionConfig{
520+
DatabaseName: "neo4j",
521+
ImpersonatedUser: "<somebodyElse>",
522+
})
523+
----
512524

513525

514526
== Transaction configuration

java-manual/modules/ROOT/pages/query-simple.adoc

+22-5
Original file line numberDiff line numberDiff line change
@@ -234,25 +234,42 @@ In other words, there is no guarantee that a write query submitted in read mode
234234

235235

236236
[#impersonation]
237+
[role=label--new-5.18]
237238
=== Run queries as a different user
238239

239-
You can execute a query under the security context of a different user with the method `.withImpersonatedUser("<username>")`, specifying the name of the user to impersonate.
240-
For this to work, the user under which the `Driver` was created needs to have the link:{neo4j-docs-base-uri}/cypher-manual/current/administration/access-control/dbms-administration#access-control-dbms-administration-impersonation[appropriate permissions].
241-
Impersonating a user is cheaper than creating a new `Driver` object.
240+
You can execute a query through a different user with the method `.withAuthToken()`.
241+
Switching user at the query level is cheaper than creating a new `Driver` object.
242+
The query is then run within the security context of the given user (i.e., home database, permissions, etc.). +
243+
Query-scoped authentication a server version >= 5.8.
242244

243245
[source, java, test-skip]
244246
----
247+
// import org.neo4j.driver.AuthTokens;
245248
// import org.neo4j.driver.QueryConfig;
246249
250+
var authToken = AuthTokens.basic("somebodyElse", "theirPassword");
247251
var result = driver.executableQuery("MATCH (p:Person) RETURN p.name")
248252
.withConfig(QueryConfig.builder()
249253
.withDatabase("neo4j")
250-
.withImpersonatedUser("somebodyElse")
254+
.withAuthToken(authToken)
251255
.build())
252256
.execute();
253257
----
254258

255-
When impersonating a user, the query is run within the complete security context of the impersonated user and not the authenticated user (i.e. home database, permissions, etc.).
259+
The method `.withImpersonatedUser()` provides a similar functionality, and is available in driver/server versions >= 4.4.
260+
The difference is that you don't need to know a user's password to impersonate them, but the user under which the `Driver` was created needs to have the link:{neo4j-docs-base-uri}/operations-manual/current/authentication-authorization/dbms-administration/#access-control-dbms-administration-impersonation[appropriate permissions].
261+
262+
[source, java, test-skip]
263+
----
264+
// import org.neo4j.driver.QueryConfig;
265+
266+
var result = driver.executableQuery("MATCH (p:Person) RETURN p.name")
267+
.withConfig(QueryConfig.builder()
268+
.withDatabase("neo4j")
269+
.withImpersonatedUser("somebodyElse")
270+
.build())
271+
.execute();
272+
----
256273

257274

258275
== A full example

java-manual/modules/ROOT/pages/transactions.adoc

+25-7
Original file line numberDiff line numberDiff line change
@@ -447,25 +447,43 @@ Similar remarks hold for the `.executeRead()` and `.executeWrite()` methods.
447447

448448

449449
[#impersonation]
450-
=== Run queries as a different user (impersonation)
450+
[role=label--new-5.18]
451+
=== Run queries as a different user
451452

452-
You can execute a query under the security context of a different user with the method `.withImpersonatedUser("<username>")`, specifying the name of the user to impersonate.
453-
For this to work, the user under which the `Driver` was created needs to have the link:{neo4j-docs-base-uri}/operations-manual/current/authentication-authorization/dbms-administration/#access-control-dbms-administration-impersonation[appropriate permissions].
454-
Impersonating a user is cheaper than creating a new `Driver` object.
453+
You can execute a query through a different user by providing an link:https://neo4j.com/docs/api/java-driver/current/org.neo4j.driver/org/neo4j/driver/AuthTokens.html[AuthToken] as third parameter upon session creation.
454+
Switching user at the session level is cheaper than creating a new `Driver` object.
455+
Queries are then run within the security context of the given user (i.e., home database, permissions, etc.). +
456+
Session-scoped authentication requires a server version >= 5.8.
457+
458+
[source, java]
459+
----
460+
// import org.neo4j.driver.AuthTokens;
461+
// import org.neo4j.driver.Session;
462+
// import org.neo4j.driver.SessionConfig;
463+
464+
var authToken = AuthTokens.basic("somebodyElse", "theirPassword");
465+
var session = driver.session(
466+
Session.class,
467+
SessionConfig.builder()
468+
.withDatabase("neo4j")
469+
.build(),
470+
authToken
471+
);
472+
----
473+
474+
The method `.withImpersonatedUser()` provides a similar functionality, and is available in driver/server versions >= 4.4.
475+
The difference is that you don't need to know a user's password to impersonate them, but the user under which the `Driver` was created needs to have the link:{neo4j-docs-base-uri}/operations-manual/current/authentication-authorization/dbms-administration/#access-control-dbms-administration-impersonation[appropriate permissions].
455476

456477
[source, java]
457478
----
458479
// import org.neo4j.driver.SessionConfig;
459-
// import org.neo4j.driver.RoutingControl;
460480
461481
var session = driver.session(SessionConfig.builder()
462482
.withDatabase("neo4j")
463483
.withImpersonatedUser("somebodyElse")
464484
.build());
465485
----
466486

467-
When impersonating a user, the query is run within the complete security context of the impersonated user and not the authenticated user (i.e. home database, permissions, etc.).
468-
469487

470488
== Transaction configuration
471489

javascript-manual/modules/ROOT/pages/connect.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ You connect to a database by creating a <<Driver>> object and providing a URL an
1919
let driver
2020
2121
try {
22-
driver = neo4j.driver(URI, neo4j.auth.basic(USER, PASSWORD)) // <1>
22+
driver = neo4j.driver(URI, neo4j.auth.basic(USER, PASSWORD)) // <1>
2323
const serverInfo = await driver.getServerInfo() // <2>
2424
console.log('Connection established')
2525
console.log(serverInfo)

javascript-manual/modules/ROOT/pages/query-simple.adoc

+20-5
Original file line numberDiff line numberDiff line change
@@ -210,25 +210,40 @@ In other words, there is no guarantee that a write query submitted in read mode
210210

211211

212212
[#impersonation]
213+
[role=label--new-5.14]
213214
=== Run queries as a different user
214215

215-
You can execute a query under the security context of a different user with the parameter `impersonatedUser`, specifying the name of the user to impersonate.
216-
For this to work, the user under which the `Driver` was created needs to have the link:{neo4j-docs-base-uri}/cypher-manual/current/administration/access-control/dbms-administration#access-control-dbms-administration-impersonation[appropriate permissions].
217-
Impersonating a user is cheaper than creating a new `Driver` object.
216+
You can execute a query through a different user with the configuration parameter `auth`.
217+
Switching user at the query level is cheaper than creating a new `Driver` object.
218+
The query is then run within the security context of the given user (i.e., home database, permissions, etc.). +
219+
Query-scoped authentication a server version >= 5.8.
218220

219221
[source, javascript, test-skip]
220222
----
221223
await driver.executeQuery(
222224
'MATCH (p:Person) RETURN p.name',
223225
{},
224226
{
225-
impersonatedUser: 'somebodyElse',
227+
auth: neo4j.auth.basic('somebodyElse', 'theirPassword'),
226228
database: 'neo4j'
227229
}
228230
)
229231
----
230232

231-
When impersonating a user, the query is run within the complete security context of the impersonated user and not the authenticated user (i.e. home database, permissions, etc.).
233+
The parameter `impersonatedUser` provides a similar functionality, and is available in driver/server versions >= 4.4.
234+
The difference is that you don't need to know a user's password to impersonate them, but the user under which the `Driver` was created needs to have the link:{neo4j-docs-base-uri}/operations-manual/current/authentication-authorization/dbms-administration/#access-control-dbms-administration-impersonation[appropriate permissions].
235+
236+
[source, javascript, test-skip]
237+
----
238+
await driver.executeQuery(
239+
'MATCH (p:Person) RETURN p.name',
240+
{},
241+
{
242+
impersonatedUser: 'somebodyElse',
243+
database: 'neo4j'
244+
}
245+
)
246+
----
232247

233248

234249
== A full example

javascript-manual/modules/ROOT/pages/transactions.adoc

+17-6
Original file line numberDiff line numberDiff line change
@@ -330,21 +330,32 @@ Similar remarks hold for the `.executeRead()` and `.executeWrite()` methods.
330330

331331

332332
[#impersonation]
333-
=== Run queries as a different user (impersonation)
333+
[role=label--new-5.14]
334+
=== Run queries as a different user
334335

335-
You can execute a query under the security context of a different user with the parameter `impersonatedUser`, specifying the name of the user to impersonate.
336-
For this to work, the user under which the `Driver` was created needs to have the link:{neo4j-docs-base-uri}/operations-manual/current/authentication-authorization/dbms-administration/#access-control-dbms-administration-impersonation[appropriate permissions].
337-
Impersonating a user is cheaper than creating a new `Driver` object.
336+
You can execute a query through a different user with the configuration parameter `auth`.
337+
Switching user at the session level is cheaper than creating a new `Driver` object.
338+
Queries are then run within the security context of the given user (i.e., home database, permissions, etc.). +
339+
Session-scoped authentication requires a server version >= 5.8.
338340

339341
[source, javascript]
340342
----
341343
const session = driver.session({
342344
database: 'neo4j',
343-
impersonatedUser: 'somebodyElse'
345+
auth: neo4j.auth.basic('somebodyElse', 'theirPassword')
344346
})
345347
----
346348

347-
When impersonating a user, the query is run within the complete security context of the impersonated user and not the authenticated user (i.e., home database, permissions, etc.).
349+
The parameter `impersonatedUser` provides a similar functionality, and is available in driver/server versions >= 4.4.
350+
The difference is that you don't need to know a user's password to impersonate them, but the user under which the `Driver` was created needs to have the link:{neo4j-docs-base-uri}/operations-manual/current/authentication-authorization/dbms-administration/#access-control-dbms-administration-impersonation[appropriate permissions].
351+
352+
[source, javascript]
353+
----
354+
const session = driver.session({
355+
database: 'neo4j',
356+
impersonatedUser: 'somebodyElse'
357+
})
358+
----
348359

349360

350361
== Transaction configuration

python-manual/modules/ROOT/pages/query-simple.adoc

+17-5
Original file line numberDiff line numberDiff line change
@@ -216,22 +216,34 @@ In other words, there is no guarantee that a write query submitted in read mode
216216

217217

218218
[#impersonation]
219+
[role=label--new-5.14]
219220
=== Run queries as a different user
220221

221-
You can execute a query under the security context of a different user with the parameter `impersonated_user_`, specifying the name of the user to impersonate.
222-
For this to work, the user under which the `Driver` was created needs to have the link:{neo4j-docs-base-uri}/cypher-manual/current/administration/access-control/dbms-administration#access-control-dbms-administration-impersonation[appropriate permissions].
223-
Impersonating a user is cheaper than creating a new `Driver` object.
222+
You can execute a query through a different user with the parameter `auth_`.
223+
Switching user at the query level is cheaper than creating a new `Driver` object.
224+
The query is then run within the security context of the given user (i.e., home database, permissions, etc.). +
225+
Query-scoped authentication a server version >= 5.8.
224226

225227
[source, python, test-skip]
226228
----
227229
driver.execute_query(
228230
"MATCH (p:Person) RETURN p.name",
229-
impersonated_user_="somebody_else",
231+
auth_=("somebody_else", "their_password"),
230232
database_="neo4j",
231233
)
232234
----
233235

234-
When impersonating a user, the query is run within the complete security context of the impersonated user and not the authenticated user (i.e. home database, permissions, etc.).
236+
The parameter `impersonated_user_` provides a similar functionality, and is available in driver/server versions >= 4.4.
237+
The difference is that you don't need to know a user's password to impersonate them, but the user under which the `Driver` was created needs to have the link:{neo4j-docs-base-uri}/operations-manual/current/authentication-authorization/dbms-administration/#access-control-dbms-administration-impersonation[appropriate permissions].
238+
239+
[source, python, test-skip]
240+
----
241+
driver.execute_query(
242+
"MATCH (p:Person) RETURN p.name",
243+
impersonated_user_="somebody_else",
244+
database_="neo4j",
245+
)
246+
----
235247

236248

237249
=== Transform query result

python-manual/modules/ROOT/pages/transactions.adoc

+17-6
Original file line numberDiff line numberDiff line change
@@ -410,23 +410,34 @@ Similar remarks hold for the `.executeRead()` and `.executeWrite()` methods.
410410

411411

412412
[#impersonation]
413-
=== Run queries as a different user (impersonation)
413+
[role=label--new-5.14]
414+
=== Run queries as a different user
414415

415-
You can execute a query under the security context of a different user with the parameter `impersonated_user`, specifying the name of the user to impersonate.
416-
For this to work, the user under which the `Driver` was created needs to have the link:{neo4j-docs-base-uri}/operations-manual/current/authentication-authorization/dbms-administration/#access-control-dbms-administration-impersonation[appropriate permissions].
417-
Impersonating a user is cheaper than creating a new `Driver` object.
416+
You can execute a query through a different user with the parameter `auth`.
417+
Switching user at the session level is cheaper than creating a new `Driver` object.
418+
Queries are then run within the security context of the given user (i.e., home database, permissions, etc.). +
419+
Session-scoped authentication requires a server version >= 5.8.
418420

419421
[source, python]
420422
----
421423
with driver.session(
422424
database="neo4j",
423-
impersonated_user="somebody_else"
425+
auth=("somebody_else", "their_password")
424426
) as session:
425427
...
426428
----
427429

428-
When impersonating a user, the query is run within the complete security context of the impersonated user and not the authenticated user (i.e., home database, permissions, etc.).
430+
The parameter `impersonated_user` provides a similar functionality, and is available in driver/server versions >= 4.4.
431+
The difference is that you don't need to know a user's password to impersonate them, but the user under which the `Driver` was created needs to have the link:{neo4j-docs-base-uri}/operations-manual/current/authentication-authorization/dbms-administration/#access-control-dbms-administration-impersonation[appropriate permissions].
429432

433+
[source, python]
434+
----
435+
with driver.session(
436+
database="neo4j",
437+
impersonated_user="somebody_else"
438+
) as session:
439+
...
440+
----
430441

431442
== Close sessions
432443

0 commit comments

Comments
 (0)