Skip to content

Commit 2d8fd97

Browse files
bigmontzrobsdedude
andauthored
Introduce auth config to Driver.executeQuery (#1177)
The AuthToken will be used for executing the query. By default, the query executor will use connections authenticated with the AuthToken configured on driver creation. This new configuration allows switching user and/or authorization information for the underlying transaction's lifetime. Example: ```javascript const { records } = await driver.executeQuery('RETURN $abc', { abc: 'dfe' }, { database: 'neo4j', auth: neo4j.auth.basic('otheruser', 'sup3rDup3rS3cret') }) ``` > [!NOTE] > This option is only available when the driver is connected to Neo4j Database servers which supports Bolt 5.1 or newer. --------- Co-authored-by: Robsdedude <dev@rouvenbauer.de>
1 parent 883c8b0 commit 2d8fd97

File tree

8 files changed

+63
-17
lines changed

8 files changed

+63
-17
lines changed

packages/core/src/driver.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,12 @@ class SessionConfig {
198198
/**
199199
* The {@link AuthToken} which will be used for the duration of the session.
200200
*
201-
* By default, the session will use connections authenticated with {@link AuthToken} configured in the
202-
* driver creation. This configuration allows switch user and/or authorization information for the
201+
* By default, the session will use connections authenticated with the {@link AuthToken} configured on
202+
* driver creation. This configuration allows switching user and/or authorization information for the
203203
* session lifetime.
204204
*
205-
* **Warning**: This option is only enable when the driver is connected with Neo4j Database servers
206-
* which supports Bolt 5.1 and onwards.
205+
* **Warning**: This option is only available when the driver is connected to Neo4j Database servers
206+
* which supports Bolt 5.1 or newer.
207207
*
208208
* @type {AuthToken|undefined}
209209
* @see {@link driver}
@@ -357,6 +357,7 @@ class QueryConfig<T = EagerResult> {
357357
bookmarkManager?: BookmarkManager | null
358358
resultTransformer?: ResultTransformer<T>
359359
transactionConfig?: TransactionConfig
360+
auth?: AuthToken
360361

361362
/**
362363
* @constructor
@@ -413,6 +414,21 @@ class QueryConfig<T = EagerResult> {
413414
*
414415
*/
415416
this.transactionConfig = undefined
417+
418+
/**
419+
* The {@link AuthToken} which will be used for executing the query.
420+
*
421+
* By default, the query executor will use connections authenticated with the {@link AuthToken} configured on
422+
* driver creation. This configuration allows switching user and/or authorization information for the
423+
* underlying transaction's lifetime.
424+
*
425+
* **Warning**: This option is only available when the driver is connected to Neo4j Database servers
426+
* which support Bolt 5.1 or newer.
427+
*
428+
* @type {AuthToken|undefined}
429+
* @see {@link driver}
430+
*/
431+
this.auth = undefined
416432
}
417433
}
418434

@@ -578,7 +594,8 @@ class Driver {
578594
routing: routingConfig,
579595
database: config.database,
580596
impersonatedUser: config.impersonatedUser,
581-
transactionConfig: config.transactionConfig
597+
transactionConfig: config.transactionConfig,
598+
auth: config.auth
582599
}, query, parameters)
583600
}
584601

packages/core/src/internal/query-executor.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ import BookmarkManager from '../bookmark-manager'
1919
import Session, { TransactionConfig } from '../session'
2020
import Result from '../result'
2121
import ManagedTransaction from '../transaction-managed'
22-
import { Query } from '../types'
22+
import { AuthToken, Query } from '../types'
2323
import { TELEMETRY_APIS } from './constants'
2424

25-
type SessionFactory = (config: { database?: string, bookmarkManager?: BookmarkManager, impersonatedUser?: string }) => Session
25+
type SessionFactory = (config: { database?: string, bookmarkManager?: BookmarkManager, impersonatedUser?: string, auth?: AuthToken }) => Session
2626

2727
type TransactionFunction<T> = (transactionWork: (tx: ManagedTransaction) => Promise<T>, transactionConfig?: TransactionConfig) => Promise<T>
2828

@@ -32,6 +32,7 @@ interface ExecutionConfig<T> {
3232
impersonatedUser?: string
3333
bookmarkManager?: BookmarkManager
3434
transactionConfig?: TransactionConfig
35+
auth?: AuthToken
3536
resultTransformer: (result: Result) => Promise<T>
3637
}
3738

@@ -44,7 +45,8 @@ export default class QueryExecutor {
4445
const session = this._createSession({
4546
database: config.database,
4647
bookmarkManager: config.bookmarkManager,
47-
impersonatedUser: config.impersonatedUser
48+
impersonatedUser: config.impersonatedUser,
49+
auth: config.auth
4850
})
4951

5052
// @ts-expect-error The method is private for external users

packages/core/test/driver.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ describe('Driver', () => {
484484
['config.routing=READ', 'create num $d', { d: 1 }, { routing: routing.READ }, extendsDefaultWith({ routing: routing.READ })],
485485
['config.database="dbname"', 'q', {}, { database: 'dbname' }, extendsDefaultWith({ database: 'dbname' })],
486486
['config.impersonatedUser="the_user"', 'q', {}, { impersonatedUser: 'the_user' }, extendsDefaultWith({ impersonatedUser: 'the_user' })],
487+
['config.auth={ scheme: "none", credentials: "" }', 'q', {}, { auth: { scheme: 'none', credentials: '' } }, extendsDefaultWith({ auth: { scheme: 'none', credentials: '' } })],
487488
['config.bookmarkManager=null', 'q', {}, { bookmarkManager: null }, extendsDefaultWith({ bookmarkManager: undefined })],
488489
['config.bookmarkManager set to non-null/empty', 'q', {}, { bookmarkManager: theBookmarkManager }, extendsDefaultWith({ bookmarkManager: theBookmarkManager })],
489490
['config.resultTransformer set', 'q', {}, { resultTransformer: aTransformer }, extendsDefaultWith({ resultTransformer: aTransformer })],

packages/core/test/internal/query-executor.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ describe('QueryExecutor', () => {
3939
['database set', { database: 'adb' }, { database: 'adb' }],
4040
['database undefined', { database: undefined }, { database: undefined }],
4141
['impersonatedUser set', { impersonatedUser: 'anUser' }, { impersonatedUser: 'anUser' }],
42-
['impersonatedUser undefined', { impersonatedUser: undefined }, { impersonatedUser: undefined }]
42+
['impersonatedUser undefined', { impersonatedUser: undefined }, { impersonatedUser: undefined }],
43+
['auth set', { auth: { scheme: 'none', credentials: '' } }, { auth: { scheme: 'none', credentials: '' } }],
44+
['auth undefined', { auth: undefined }, { auth: undefined }]
4345
])('should redirect % to the session creation', async (_, executorConfig, expectConfig) => {
4446
const { queryExecutor, createSession } = createExecutor()
4547

packages/neo4j-driver-deno/lib/core/driver.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,12 @@ class SessionConfig {
198198
/**
199199
* The {@link AuthToken} which will be used for the duration of the session.
200200
*
201-
* By default, the session will use connections authenticated with {@link AuthToken} configured in the
202-
* driver creation. This configuration allows switch user and/or authorization information for the
201+
* By default, the session will use connections authenticated with the {@link AuthToken} configured on
202+
* driver creation. This configuration allows switching user and/or authorization information for the
203203
* session lifetime.
204204
*
205-
* **Warning**: This option is only enable when the driver is connected with Neo4j Database servers
206-
* which supports Bolt 5.1 and onwards.
205+
* **Warning**: This option is only available when the driver is connected to Neo4j Database servers
206+
* which supports Bolt 5.1 or newer.
207207
*
208208
* @type {AuthToken|undefined}
209209
* @see {@link driver}
@@ -357,6 +357,7 @@ class QueryConfig<T = EagerResult> {
357357
bookmarkManager?: BookmarkManager | null
358358
resultTransformer?: ResultTransformer<T>
359359
transactionConfig?: TransactionConfig
360+
auth?: AuthToken
360361

361362
/**
362363
* @constructor
@@ -413,6 +414,21 @@ class QueryConfig<T = EagerResult> {
413414
*
414415
*/
415416
this.transactionConfig = undefined
417+
418+
/**
419+
* The {@link AuthToken} which will be used for executing the query.
420+
*
421+
* By default, the query executor will use connections authenticated with the {@link AuthToken} configured on
422+
* driver creation. This configuration allows switching user and/or authorization information for the
423+
* underlying transaction's lifetime.
424+
*
425+
* **Warning**: This option is only available when the driver is connected to Neo4j Database servers
426+
* which support Bolt 5.1 or newer.
427+
*
428+
* @type {AuthToken|undefined}
429+
* @see {@link driver}
430+
*/
431+
this.auth = undefined
416432
}
417433
}
418434

@@ -578,7 +594,8 @@ class Driver {
578594
routing: routingConfig,
579595
database: config.database,
580596
impersonatedUser: config.impersonatedUser,
581-
transactionConfig: config.transactionConfig
597+
transactionConfig: config.transactionConfig,
598+
auth: config.auth
582599
}, query, parameters)
583600
}
584601

packages/neo4j-driver-deno/lib/core/internal/query-executor.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ import BookmarkManager from '../bookmark-manager.ts'
1919
import Session, { TransactionConfig } from '../session.ts'
2020
import Result from '../result.ts'
2121
import ManagedTransaction from '../transaction-managed.ts'
22-
import { Query } from '../types.ts'
22+
import { AuthToken, Query } from '../types.ts'
2323
import { TELEMETRY_APIS } from './constants.ts'
2424

25-
type SessionFactory = (config: { database?: string, bookmarkManager?: BookmarkManager, impersonatedUser?: string }) => Session
25+
type SessionFactory = (config: { database?: string, bookmarkManager?: BookmarkManager, impersonatedUser?: string, auth?: AuthToken }) => Session
2626

2727
type TransactionFunction<T> = (transactionWork: (tx: ManagedTransaction) => Promise<T>, transactionConfig?: TransactionConfig) => Promise<T>
2828

@@ -32,6 +32,7 @@ interface ExecutionConfig<T> {
3232
impersonatedUser?: string
3333
bookmarkManager?: BookmarkManager
3434
transactionConfig?: TransactionConfig
35+
auth?: AuthToken
3536
resultTransformer: (result: Result) => Promise<T>
3637
}
3738

@@ -44,7 +45,8 @@ export default class QueryExecutor {
4445
const session = this._createSession({
4546
database: config.database,
4647
bookmarkManager: config.bookmarkManager,
47-
impersonatedUser: config.impersonatedUser
48+
impersonatedUser: config.impersonatedUser,
49+
auth: config.auth
4850
})
4951

5052
// @ts-expect-error The method is private for external users

packages/testkit-backend/src/feature/common.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const features = [
2626
'Feature:Bolt:Patch:UTC',
2727
'Feature:API:ConnectionAcquisitionTimeout',
2828
'Feature:API:Driver.ExecuteQuery',
29+
'Feature:API:Driver.ExecuteQuery:WithAuth',
2930
'Feature:API:Driver:NotificationsConfig',
3031
'Feature:API:Driver:GetServerInfo',
3132
'Feature:API:Driver.SupportsSessionAuth',

packages/testkit-backend/src/request-handlers.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,10 @@ export function ExecuteQuery ({ neo4j }, context, { driverId, cypher, params, co
707707
timeout: config.timeout
708708
}
709709
}
710+
711+
if (config.authorizationToken != null) {
712+
configuration.auth = context.binder.parseAuthToken(config.authorizationToken.data)
713+
}
710714
}
711715

712716
driver.executeQuery(cypher, params, configuration)

0 commit comments

Comments
 (0)