Skip to main content

dbx_tools_databricks_auth/
oauth.rs

1use crate::{oauth_endpoints, OAuthTemplate, Profile, Result, Token};
2use std::time::Duration;
3
4pub struct OAuthFlow {
5    profile: Profile,
6    http: reqwest::Client,
7    template: OAuthTemplate,
8}
9impl OAuthFlow {
10    pub fn new(profile: Profile) -> Result<Self> {
11        Ok(Self {
12            profile,
13            http: reqwest::Client::builder()
14                .redirect(reqwest::redirect::Policy::none())
15                .build()?,
16            template: OAuthTemplate::default(),
17        })
18    }
19    pub fn with_template(mut self, template: OAuthTemplate) -> Self {
20        self.template = template;
21        self
22    }
23    async fn flow(&self) -> Result<dbx_tools_auth::OAuthFlow> {
24        let endpoints = oauth_endpoints::resolve(&self.profile, &self.http).await?;
25        Ok(dbx_tools_auth::OAuthFlow::new(dbx_tools_auth::OAuthConfig {
26            provider: "databricks".into(),
27            authorization_endpoint: endpoints.authorization_endpoint,
28            token_endpoint: endpoints.token_endpoint,
29            client_id: self.profile.client_id.clone(),
30            client_secret: None,
31            scopes: self.profile.effective_scopes(),
32            extra_token_params: vec![],
33            host: Some(self.profile.host.to_string()),
34        })?
35        .with_template(self.template.clone()))
36    }
37    pub async fn login(&self, timeout: Duration) -> Result<Token> {
38        self.flow().await?.login(timeout).await
39    }
40    pub async fn refresh(&self, token: &Token) -> Result<Token> {
41        self.flow().await?.refresh(token).await
42    }
43}