Skip to content

Add the ability to Refresh Auth Token Credentials #1051

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ coverage
*.code-workspace
/testkit/CAs
/testkit/CustomCAs
/.vs
12 changes: 11 additions & 1 deletion packages/bolt-connection/src/connection/connection-channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,17 @@ export default class ChannelConnection extends Connection {
* @return {Promise<Connection>} promise resolved with the current connection if connection is successful. Rejected promise otherwise.
*/
connect (userAgent, authToken) {
return this._initialize(userAgent, authToken)
const self = this
// credentialsRefresher is function to refresh credentials
if (authToken && authToken.credentialsRefresher) {
const maybePromise = authToken.credentialsRefresher()
return Promise.resolve(maybePromise).then(function (creds) {
authToken.credentials = creds
return self._initialize(userAgent, authToken)
})
}

return self._initialize(userAgent, authToken)
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/bolt-connection/src/packstream/packstream-v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,14 @@ class Packer {

let count = 0
for (let i = 0; i < keys.length; i++) {
if (x[keys[i]] !== undefined) {
if (x[keys[i]] !== undefined && keys[i] !== "credentialsRefresher") {
count++
}
}
this.packMapHeader(count)
for (let i = 0; i < keys.length; i++) {
const key = keys[i]
if (x[key] !== undefined) {
if (x[key] !== undefined && key !== "credentialsRefresher") {
this.packString(key)
this.packable(x[key], dehydrateStruct)()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@ describe('#unit PackStreamV1', () => {
expect(unpacked[0]).toBe(list[0])
expect(unpacked[1]).toBe(list[1])
})

it('should pack object with credentialsRefresher function', () => {
const obj = { foo: 'bar', credentialsRefresher: () => 'top-secret-password' }
const unpacked = packAndUnpack(obj)
expect(unpacked.foo).toBe(obj.foo)
expect(unpacked.credentialsRefresher).toBe(undefined)
})
})

function packAndUnpack (
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface AuthToken {
credentials: string
realm?: string
parameters?: Parameters
credentialsRefresher?: () => string | Promise<string>
}
export interface Config {
encrypted?: boolean | EncryptionLevel
Expand Down
1 change: 1 addition & 0 deletions packages/neo4j-driver-deno/lib/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface AuthToken {
credentials: string
realm?: string
parameters?: Parameters
credentialsRefresher?: () => string | Promise<string>
}
export interface Config {
encrypted?: boolean | EncryptionLevel
Expand Down