Skip to content

Commit

Permalink
feat(core): support access token
Browse files Browse the repository at this point in the history
  • Loading branch information
everpcpc committed Dec 5, 2023
1 parent 231300a commit bb9ae52
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 111 deletions.
4 changes: 2 additions & 2 deletions bindings/nodejs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ doc = false
databend-driver = { workspace = true, features = ["rustls", "flight-sql"] }

chrono = { version = "0.4", default-features = false }
napi = { version = "2.13", default-features = false, features = [
napi = { version = "2.14", default-features = false, features = [
"napi6",
"async",
"serde-json",
"chrono_date",
] }
napi-derive = "2.13"
napi-derive = "2.14"
tokio-stream = "0.1"

[build-dependencies]
Expand Down
6 changes: 3 additions & 3 deletions bindings/python/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ doc = false

[dependencies]
databend-driver = { workspace = true, features = ["rustls", "flight-sql"] }
pyo3 = { version = "0.19", features = ["abi3-py37"] }
pyo3-asyncio = { version = "0.19", features = ["tokio-runtime"] }
tokio = "1.28"
pyo3 = { version = "0.20", features = ["abi3-py37"] }
pyo3-asyncio = { version = "0.20", features = ["tokio-runtime"] }
tokio = "1.34"
tokio-stream = "0.1"
10 changes: 5 additions & 5 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ databend-driver = { workspace = true, features = ["rustls", "flight-sql"] }
anyhow = "1.0"
async-trait = "0.1"
chrono = { version = "0.4.31", default-features = false, features = ["clock"] }
clap = { version = "4.3", features = ["derive", "env"] }
comfy-table = "7.0"
csv = "1.2"
clap = { version = "4.4", features = ["derive", "env"] }
comfy-table = "7.1"
csv = "1.3"
fern = "0.6"
indicatif = "0.17"
log = "0.4"
Expand All @@ -30,7 +30,7 @@ sqlformat = "0.2"
strum = "0.25"
strum_macros = "0.25"
terminal_size = "0.3"
tokio = { version = "1.28", features = [
tokio = { version = "1.34", features = [
"macros",
"rt",
"rt-multi-thread",
Expand All @@ -41,7 +41,7 @@ tokio-stream = "0.1"
toml = "0.8"
tracing-appender = "0.2"
unicode-segmentation = "1.10"
url = { version = "2.4", default-features = false }
url = { version = "2.5", default-features = false }

[build-dependencies]
vergen = { version = "8.2", features = ["build", "git", "gix"] }
Expand Down
8 changes: 4 additions & 4 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ rustls = ["reqwest/rustls-tls"]
native-tls = ["reqwest/native-tls"]

[dependencies]
http = "0.2"
async-trait = "0.1"
log = "0.4"
once_cell = "1.18"
percent-encoding = "2.3"
reqwest = { version = "0.11", default-features = false, features = ["json", "multipart", "stream"] }
serde = { version = "1.0", default-features = false, features = ["derive"] }
serde_json = { version = "1.0", default-features = false, features = ["std"] }
tokio = { version = "1.28", features = ["macros"] }
tokio = { version = "1.34", features = ["macros"] }
tokio-retry = "0.3"
tokio-stream = "0.1"
tokio-util = { version = "0.7", features = ["io-util"] }
url = { version = "2.4", default-features = false }
uuid = { version = "1.4", features = ["v4"] }
url = { version = "2.5", default-features = false }
uuid = { version = "1.6", features = ["v4"] }

[dev-dependencies]
chrono = { version = "0.4", default-features = false, features = ["clock"] }
98 changes: 98 additions & 0 deletions core/src/auth.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright 2021 Datafuse Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use reqwest::RequestBuilder;

use crate::error::{Error, Result};

#[async_trait::async_trait]
pub trait Auth: Sync + Send {
async fn wrap(&self, builder: RequestBuilder) -> Result<RequestBuilder>;
fn username(&self) -> String;
}

#[derive(Clone)]
pub struct BasicAuth {
username: String,
password: Option<String>,
}

impl BasicAuth {
pub fn new(username: String, password: Option<String>) -> Self {
Self { username, password }
}
}

#[async_trait::async_trait]
impl Auth for BasicAuth {
async fn wrap(&self, builder: RequestBuilder) -> Result<RequestBuilder> {
Ok(builder.basic_auth(&self.username, self.password.as_deref()))
}

fn username(&self) -> String {
self.username.clone()
}
}

#[derive(Clone)]
pub struct AccessTokenAuth {
token: String,
}

impl AccessTokenAuth {
pub fn new(token: String) -> Self {
Self { token }
}
}

#[async_trait::async_trait]
impl Auth for AccessTokenAuth {
async fn wrap(&self, builder: RequestBuilder) -> Result<RequestBuilder> {
Ok(builder.bearer_auth(&self.token))
}

fn username(&self) -> String {
"token".to_string()
}
}

#[derive(Clone)]
pub struct AccessTokenFileAuth {
token_file: String,
}

impl AccessTokenFileAuth {
pub fn new(token_file: String) -> Self {
Self { token_file }
}
}

#[async_trait::async_trait]
impl Auth for AccessTokenFileAuth {
async fn wrap(&self, builder: RequestBuilder) -> Result<RequestBuilder> {
let token = tokio::fs::read_to_string(&self.token_file)
.await
.map_err(|e| {
Error::IO(format!(
"cannot read access token from file {}: {}",
self.token_file, e
))
})?;
Ok(builder.bearer_auth(token.trim()))
}

fn username(&self) -> String {
"token".to_string()
}
}
Loading

0 comments on commit bb9ae52

Please sign in to comment.