Skip to content

Commit

Permalink
Add ExecuteQueryWithAuthToken configuration option (#564)
Browse files Browse the repository at this point in the history
* Add configuration option

---------

Co-authored-by: Robsdedude <dev@rouvenbauer.de>
  • Loading branch information
StephenCathcart and robsdedude authored Feb 6, 2024
1 parent b6ae8b2 commit ed22096
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
9 changes: 9 additions & 0 deletions neo4j/driver_with_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,13 @@ func ExecuteQueryWithTransactionConfig(configurers ...func(*TransactionConfig))
}
}

// ExecuteQueryWithAuthToken configures DriverWithContext.ExecuteQuery to overwrite the AuthToken for the session.
func ExecuteQueryWithAuthToken(auth AuthToken) ExecuteQueryConfigurationOption {
return func(configuration *ExecuteQueryConfiguration) {
configuration.Auth = &auth
}
}

// ExecuteQueryConfiguration holds all the possible configuration settings for DriverWithContext.ExecuteQuery
type ExecuteQueryConfiguration struct {
Routing RoutingControl
Expand All @@ -680,6 +687,7 @@ type ExecuteQueryConfiguration struct {
BookmarkManager BookmarkManager
BoltLogger log.BoltLogger
TransactionConfigurers []func(*TransactionConfig)
Auth *AuthToken
}

// RoutingControl specifies how the query executed by DriverWithContext.ExecuteQuery is to be routed
Expand All @@ -698,6 +706,7 @@ func (c *ExecuteQueryConfiguration) toSessionConfig() SessionConfig {
DatabaseName: c.Database,
BookmarkManager: c.BookmarkManager,
BoltLogger: c.BoltLogger,
Auth: c.Auth,
}
}

Expand Down
12 changes: 12 additions & 0 deletions testkit-backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,8 @@ func (b *backend) handleRequest(req map[string]any) {
if rawConfig := data["config"]; rawConfig != nil {
executeQueryConfig := rawConfig.(map[string]any)
configurers = append(configurers, func(config *neo4j.ExecuteQueryConfiguration) {
config.BoltLogger = &streamLog{writeLine: b.writeLineLocked}

routing := executeQueryConfig["routing"]
if routing != nil {
switch routing {
Expand Down Expand Up @@ -601,6 +603,15 @@ func (b *backend) handleRequest(req map[string]any) {
if executeQueryConfig["txMeta"] != nil {
config.TransactionConfigurers = append(config.TransactionConfigurers, neo4j.WithTxMetadata(b.toTxMetadata(executeQueryConfig)))
}
// Append Auth configuration if it exists
if executeQueryConfig["authorizationToken"] != nil {
token, err := getAuth(executeQueryConfig["authorizationToken"].(map[string]any)["data"].(map[string]any))
if err != nil {
b.writeError(err)
return
}
config.Auth = &token
}
})
}

Expand Down Expand Up @@ -1123,6 +1134,7 @@ func (b *backend) handleRequest(req map[string]any) {
"Feature:API:BookmarkManager",
"Feature:API:ConnectionAcquisitionTimeout",
"Feature:API:Driver.ExecuteQuery",
"Feature:API:Driver.ExecuteQuery:WithAuth",
"Feature:API:Driver:GetServerInfo",
"Feature:API:Driver.IsEncrypted",
"Feature:API:Driver:NotificationsConfig",
Expand Down

0 comments on commit ed22096

Please sign in to comment.