|
@@ -5,6 +5,7 @@ use crate::{
|
|
|
config::Config,
|
|
|
schema::{self, UIDCDatabase},
|
|
|
server::{ServerStateWrapper, UIDCRequest},
|
|
|
+ UIDCError,
|
|
|
};
|
|
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
@@ -45,7 +46,7 @@ impl super::ExternalAuthenticator for GithubAuthenticator {
|
|
|
fn register_routes(&'static self, server: &mut tide::Server<ServerStateWrapper>) {
|
|
|
server
|
|
|
.at("/:realm/github_return")
|
|
|
- .get(|req: UIDCRequest| async { Ok(self.extract_login_state(req).await) });
|
|
|
+ .get(|req: UIDCRequest| async { Ok(self.extract_login_state(req).await?) });
|
|
|
}
|
|
|
|
|
|
fn generate_login_url(&self, realm: &str, redirect: &str) -> String {
|
|
@@ -91,7 +92,7 @@ impl super::ExternalAuthenticator for GithubAuthenticator {
|
|
|
fn extract_login_state(
|
|
|
&self,
|
|
|
req: UIDCRequest,
|
|
|
- ) -> impl smol::prelude::Future<Output = tide::Response> {
|
|
|
+ ) -> impl smol::prelude::Future<Output = Result<tide::Response, UIDCError>> {
|
|
|
async move {
|
|
|
let state = req.state();
|
|
|
let realm = req.param("realm").unwrap();
|
|
@@ -103,9 +104,9 @@ impl super::ExternalAuthenticator for GithubAuthenticator {
|
|
|
mode: CallbackRequestType,
|
|
|
}
|
|
|
let Ok(query) = req.query::<Query>() else {
|
|
|
- return tide::Response::builder(400)
|
|
|
+ return Ok(tide::Response::builder(400)
|
|
|
.body("Query string invalid.")
|
|
|
- .build();
|
|
|
+ .build());
|
|
|
};
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
@@ -141,9 +142,9 @@ impl super::ExternalAuthenticator for GithubAuthenticator {
|
|
|
{
|
|
|
Ok(resp) => resp,
|
|
|
Err(err) => {
|
|
|
- return tide::Response::builder(500)
|
|
|
- .body(format!("could not parse Github response for token: {err}"))
|
|
|
- .build()
|
|
|
+ return Err(UIDCError::AbortString(format!(
|
|
|
+ "could not parse Github response for token: {err}"
|
|
|
+ )))
|
|
|
}
|
|
|
};
|
|
|
|
|
@@ -167,18 +168,18 @@ impl super::ExternalAuthenticator for GithubAuthenticator {
|
|
|
{
|
|
|
Ok(resp) => resp,
|
|
|
Err(err) => {
|
|
|
- return tide::Response::builder(500)
|
|
|
- .body(format!("could not parse Github response for token: {err}"))
|
|
|
- .build()
|
|
|
+ return Err(UIDCError::AbortString(format!(
|
|
|
+ "could not parse Github response for token: {err}"
|
|
|
+ )))
|
|
|
}
|
|
|
};
|
|
|
|
|
|
let user_id = resp.id.to_string();
|
|
|
|
|
|
- let mut lease = state.pool.acquire().expect("could not acquire lease");
|
|
|
+ let mut lease = state.lease().await?;
|
|
|
|
|
|
let Some(realm) = state.db.realms.keyed(realm).get(&mut lease).ok().flatten() else {
|
|
|
- return tide::Response::builder(404).body("no such realm").build();
|
|
|
+ return Ok(tide::Response::builder(404).body("no such realm").build());
|
|
|
};
|
|
|
|
|
|
let external_auth_map = realm
|
|
@@ -195,9 +196,12 @@ impl super::ExternalAuthenticator for GithubAuthenticator {
|
|
|
match (query.mode, external_auth_map) {
|
|
|
(CallbackRequestType::Login, Some(map)) => {
|
|
|
self.handle_matching_login(req, map.internal_user_id, query.redirect.as_str())
|
|
|
+ .await
|
|
|
}
|
|
|
- (CallbackRequestType::Login, None) => self.handle_no_mapping(req, query.redirect),
|
|
|
- (CallbackRequestType::Register, _) => self.handle_registration(req),
|
|
|
+ (CallbackRequestType::Login, None) => {
|
|
|
+ self.handle_no_mapping(req, query.redirect).await
|
|
|
+ }
|
|
|
+ (CallbackRequestType::Register, _) => self.handle_registration(req).await,
|
|
|
}
|
|
|
}
|
|
|
}
|