-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
258 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
use anyhow::Result; | ||
use std::sync::Arc; | ||
use tower_http::trace::TraceLayer; | ||
|
||
use axum::{ | ||
extract::{Path, Query, State}, | ||
response::{Redirect, Response}, | ||
routing, Router, | ||
}; | ||
use hyper::StatusCode; | ||
use juniper::ID; | ||
use serde::Deserialize; | ||
|
||
use crate::{ | ||
oauth::github::GithubOAuthResponse, | ||
schema::{ | ||
github_repository_provider::GithubRepositoryProviderService, setting::SettingService, | ||
}, | ||
}; | ||
|
||
#[derive(Clone)] | ||
pub struct OAuthState { | ||
pub settings: Arc<dyn SettingService>, | ||
pub github_repository_provider: Arc<dyn GithubRepositoryProviderService>, | ||
} | ||
|
||
pub fn routes(state: OAuthState) -> Router { | ||
Router::new() | ||
.route("/github/login/<id>", routing::get(login)) | ||
.route("/github/callback", routing::get(callback)) | ||
.with_state(state) | ||
} | ||
|
||
fn github_redirect_url(client_id: &str, redirect_uri: &str, id: &ID) -> String { | ||
format!("https://github.com/login/oauth/authorize?client_id={client_id}&response_type=code&scope=repo&redirect_uri={redirect_uri}/repositories/oauth/callback&state={id}") | ||
} | ||
|
||
#[derive(Deserialize)] | ||
struct CallbackParams { | ||
state: ID, | ||
code: String, | ||
} | ||
|
||
macro_rules! log_error { | ||
($val:expr) => { | ||
$val.map_err(|e| { | ||
tracing::error!("{e}"); | ||
StatusCode::INTERNAL_SERVER_ERROR | ||
}) | ||
}; | ||
} | ||
|
||
async fn exchange_access_token( | ||
state: &OAuthState, | ||
params: &CallbackParams, | ||
) -> Result<GithubOAuthResponse> { | ||
let client = reqwest::Client::new(); | ||
|
||
let client_id = state | ||
.github_repository_provider | ||
.get_github_repository_provider(params.state.clone()) | ||
.await? | ||
.application_id; | ||
|
||
let secret = state | ||
.github_repository_provider | ||
.read_github_repository_provider_secret(params.state.clone()) | ||
.await?; | ||
|
||
Ok(client | ||
.post("https://github.com/login/oauth/access_token") | ||
.header(reqwest::header::ACCEPT, "application/json") | ||
.form(&[ | ||
("client_id", &client_id), | ||
("client_secret", &secret), | ||
("code", ¶ms.code), | ||
]) | ||
.send() | ||
.await? | ||
.json() | ||
.await?) | ||
} | ||
|
||
async fn callback( | ||
State(state): State<OAuthState>, | ||
Query(params): Query<CallbackParams>, | ||
) -> Result<Redirect, StatusCode> { | ||
let network_setting = log_error!(state.settings.read_network_setting().await)?; | ||
let external_url = network_setting.external_url; | ||
|
||
let response = log_error!(exchange_access_token(&state, ¶ms).await)?; | ||
dbg!(&response); | ||
log_error!( | ||
state | ||
.github_repository_provider | ||
.set_github_repository_provider_token(params.state, response.access_token) | ||
.await | ||
)?; | ||
|
||
Ok(Redirect::permanent(&external_url)) | ||
} | ||
|
||
async fn login( | ||
State(state): State<OAuthState>, | ||
Path(id): Path<ID>, | ||
) -> Result<Redirect, StatusCode> { | ||
let network_setting = log_error!(state.settings.read_network_setting().await)?; | ||
let external_url = network_setting.external_url; | ||
let client_id = log_error!( | ||
state | ||
.github_repository_provider | ||
.get_github_repository_provider(id.clone()) | ||
.await | ||
)? | ||
.application_id; | ||
Ok(Redirect::temporary(&github_redirect_url( | ||
&client_id, | ||
&external_url, | ||
&id, | ||
))) | ||
} |
20 changes: 20 additions & 0 deletions
20
ee/tabby-webserver/src/schema/github_repository_provider.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use anyhow::Result; | ||
use async_trait::async_trait; | ||
use juniper::{GraphQLObject, ID}; | ||
|
||
#[derive(GraphQLObject)] | ||
pub struct GithubRepositoryProvider { | ||
pub display_name: String, | ||
pub application_id: String, | ||
} | ||
|
||
#[async_trait] | ||
pub trait GithubRepositoryProviderService: Send + Sync { | ||
async fn get_github_repository_provider(&self, id: ID) -> Result<GithubRepositoryProvider>; | ||
async fn read_github_repository_provider_secret(&self, id: ID) -> Result<String>; | ||
async fn set_github_repository_provider_token( | ||
&self, | ||
id: ID, | ||
access_token: String, | ||
) -> Result<()>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
ee/tabby-webserver/src/service/github_repository_provider.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use anyhow::Result; | ||
use async_trait::async_trait; | ||
use juniper::ID; | ||
use tabby_db::DbConn; | ||
|
||
use crate::schema::github_repository_provider::{ | ||
GithubRepositoryProvider, GithubRepositoryProviderService, | ||
}; | ||
|
||
use super::AsRowid; | ||
|
||
struct GithubRepositoryProviderServiceImpl { | ||
db: DbConn, | ||
} | ||
|
||
pub fn new_github_repository_provider_service(db: DbConn) -> impl GithubRepositoryProviderService { | ||
GithubRepositoryProviderServiceImpl { db } | ||
} | ||
|
||
#[async_trait] | ||
impl GithubRepositoryProviderService for GithubRepositoryProviderServiceImpl { | ||
async fn get_github_repository_provider(&self, id: ID) -> Result<GithubRepositoryProvider> { | ||
let provider = self.db.get_github_provider(id.as_rowid()? as i64).await?; | ||
Ok(GithubRepositoryProvider { | ||
display_name: provider.display_name, | ||
application_id: provider.application_id, | ||
}) | ||
} | ||
|
||
async fn read_github_repository_provider_secret(&self, id: ID) -> Result<String> { | ||
let provider = self.db.get_github_provider(id.as_rowid()? as i64).await?; | ||
Ok(provider.secret) | ||
} | ||
|
||
async fn set_github_repository_provider_token( | ||
&self, | ||
id: ID, | ||
access_token: String, | ||
) -> Result<()> { | ||
self.db | ||
.update_github_provider_token(id.as_rowid()? as i64, access_token) | ||
.await?; | ||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.