Skip to content

Commit

Permalink
Add JWT auth option (#656)
Browse files Browse the repository at this point in the history
  • Loading branch information
ultram4rine authored Dec 28, 2024
1 parent 66fee24 commit ab424e2
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 6 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add `JWT` auth option (#656). This is a ClickHouse Cloud feature.

### Changed

- Improved connection test (#655).
- Updated dependencies.

## [0.8.0] - 2024-11-11

### Added
Expand Down
31 changes: 27 additions & 4 deletions connection.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@
"type": "string",
"default": "default"
},
"username": {
"title": "Username",
"type": "string",
"default": "default"
"useJWT": {
"type": "boolean",
"title": "Use JWT token instead of password",
"default": false
},
"passwordMode": {
"title": "Password mode",
Expand Down Expand Up @@ -117,6 +117,29 @@
}
]
},
"useJWT": {
"oneOf": [
{
"properties": {
"useJWT": {
"enum": [false]
},
"username": {
"title": "Username",
"type": "string",
"default": "default"
}
}
},
{
"properties": {
"useJWT": {
"enum": [true]
}
}
}
]
},
"enableTls": {
"oneOf": [
{
Expand Down
4 changes: 4 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ export async function activate(
connInfo.server = "http://" + connInfo.server;
}

if (connInfo.useJWT) {
propsToRemove.push("username");
}

if (connInfo.passwordMode) {
if (connInfo.passwordMode.toString().toLowerCase().includes("ask")) {
connInfo.askForPassword = true;
Expand Down
9 changes: 7 additions & 2 deletions src/ls/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,20 @@ export default class ClickHouseDriver

const opts = {
url: url,
username: this.credentials.username,
password: this.credentials.password,
role: this.credentials.role,
request_timeout: this.credentials.requestTimeout,
application: "sqltools-clickhouse-driver",
database: this.credentials.database,
tls: tlsConfig,
} as ClickHouseClientConfigOptions;

if (this.credentials.useJWT) {
opts.access_token = this.credentials.password;
} else {
opts.username = this.credentials.username;
opts.password = this.credentials.password;
}

this.connection = Promise.resolve(createClient(opts));
return this.connection;
}
Expand Down
4 changes: 4 additions & 0 deletions ui.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"server",
"port",
"database",
"useJWT",
"username",
"passwordMode",
"password",
Expand All @@ -12,6 +13,9 @@
"tls"
],
"password": { "ui:widget": "password" },
"useJWT": {
"ui:help": "This is a ClickHouse Cloud feature. Put token in password field"
},
"role": {
"ui:help": "Only for ClickHouse >=24.4! See https://clickhouse.com/docs/en/interfaces/http#setting-role-with-query-parameters"
},
Expand Down

0 comments on commit ab424e2

Please sign in to comment.