Skip to content

Commit 8bdefae

Browse files
authored
Merge pull request #23 from Clem-Fern/dev
0.4.2
2 parents e4c826e + 0001317 commit 8bdefae

File tree

10 files changed

+59
-37
lines changed

10 files changed

+59
-37
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rtabby-web-api"
3-
version = "0.3.0"
3+
version = "0.4.2"
44
edition = "2021"
55

66
[features]
@@ -38,4 +38,4 @@ serde = { version = "1.0.152", features = ["derive"] }
3838
serde_yaml = "0.9.16"
3939
uuid = { version = "1.6.1", features = ["serde", "v4"] }
4040
tera = { version = "1", optional = true }
41-
reqwest = { version = "0.11", features = ["json", "rustls-tls"], default-features = false, optional = true }
41+
reqwest = { version = "0.12.4", features = ["json", "rustls-tls"], default-features = false, optional = true }

src/login/env.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub const ENV_STATIC_FILES_BASE_DIR: &str = "STATIC_FILES_BASE_DIR";
22
pub const ENV_USE_HTTPS: &str = "USE_HTTPS";
3+
pub const ENV_HTTPS_CALLBACK: &str = "HTTPS_CALLBACK";
34

45
use crate::env as app_env;
56

src/login/mod.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ pub mod providers;
44
pub mod routes;
55
pub mod services;
66
pub mod error;
7-
mod tools;
87

98
use crate::env as app_env;
109

10+
use actix_web::http::uri::Scheme;
11+
12+
use log::warn;
1113
#[cfg(feature = "github-login")]
1214
use providers::github;
1315
#[cfg(feature = "gitlab-login")]
@@ -21,10 +23,34 @@ use self::providers::OauthInfo;
2123

2224
#[derive(Clone, Debug)]
2325
pub struct ProvidersConfig {
26+
pub https_callback: bool,
2427
pub available_providers: Vec<providers::Provider>,
2528
}
2629

30+
impl ProvidersConfig {
31+
32+
pub fn get_callback_scheme(&self) -> Scheme {
33+
if self.https_callback {
34+
Scheme::HTTPS
35+
} else {
36+
Scheme::HTTP
37+
}
38+
}
39+
40+
}
41+
2742
pub fn get_provider_config() -> ProvidersConfig {
43+
44+
let https_callback = if app_env::var(env::ENV_HTTPS_CALLBACK).is_ok() {
45+
app_env::var(env::ENV_HTTPS_CALLBACK).unwrap_or(String::from("false")).to_lowercase().parse().unwrap_or(false)
46+
} else if app_env::var(env::ENV_USE_HTTPS).is_ok() {
47+
// DEPRECATED
48+
warn!("\"USE_HTTPS\" deprecated. Use \"HTTPS_CALLBACK\" instead.");
49+
app_env::var(env::ENV_USE_HTTPS).unwrap_or(String::from("0")) == "1"
50+
} else {
51+
false
52+
};
53+
2854
let mut available_providers: Vec<providers::Provider> = vec![];
2955

3056
#[cfg(feature = "github-login")]
@@ -68,6 +94,7 @@ pub fn get_provider_config() -> ProvidersConfig {
6894
}
6995

7096
ProvidersConfig {
97+
https_callback,
7198
available_providers
7299
}
73100
}

src/login/providers/gitlab.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::login::error::OauthError;
22
use crate::login::providers::{get_user_info, get_access_token, OauthInfo, OauthUserInfo};
3-
use crate::login::tools;
3+
use actix_web::http::uri::Scheme;
44

55
pub mod env {
66
pub const ENV_GITLAB_APP_CLIENT_ID: &str = "GITLAB_APP_CLIENT_ID";
@@ -13,8 +13,8 @@ pub const GITLAB_OAUTH_USER_INFO_URL: &str = "https://gitlab.com/api/v4/user";
1313

1414
pub type GitlabOauthUserInfo = OauthUserInfo<i32, String>;
1515

16-
pub async fn user_info(oauth: &OauthInfo, host: String, token: String) -> Result<GitlabOauthUserInfo, OauthError> {
17-
let redirect_uri = format!("{}://{}/login/gitlab/callback", tools::scheme(), host);
16+
pub async fn user_info(scheme: Scheme, oauth: &OauthInfo, host: String, token: String) -> Result<GitlabOauthUserInfo, OauthError> {
17+
let redirect_uri = format!("{}://{}/login/gitlab/callback", scheme, host);
1818
let token = get_access_token(GITLAB_OAUTH_ACCESS_TOKEN_URL, token, oauth.client_id.clone(), oauth.client_secret.clone(), "authorization_code", Some(redirect_uri)).await?;
1919
get_user_info(GITLAB_OAUTH_USER_INFO_URL, token).await.map_err(OauthError::UserInfo)?.json::<GitlabOauthUserInfo>().await.map_err(OauthError::UserInfo)
2020
}

src/login/providers/google.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::login::error::OauthError;
22
use crate::login::providers::{get_user_info, get_access_token, OauthInfo, OauthUserInfo};
3-
use crate::login::tools;
3+
use actix_web::http::uri::Scheme;
44

55
pub mod env {
66
pub const ENV_GOOGLE_APP_CLIENT_ID: &str = "GOOGLE_APP_CLIENT_ID";
@@ -13,8 +13,8 @@ pub const GOOGLE_OAUTH_USER_INFO_URL: &str = "https://www.googleapis.com/oauth2/
1313

1414
pub type GoogleOauthUserInfo = OauthUserInfo;
1515

16-
pub async fn user_info(oauth: &OauthInfo, host: String, code: String) -> Result<GoogleOauthUserInfo, OauthError> {
17-
let redirect_uri = format!("{}://{}/login/google/callback", tools::scheme(), host);
16+
pub async fn user_info(scheme: Scheme, oauth: &OauthInfo, host: String, code: String) -> Result<GoogleOauthUserInfo, OauthError> {
17+
let redirect_uri = format!("{}://{}/login/google/callback", scheme, host);
1818
let token = get_access_token(GOOGLE_OAUTH_ACCESS_TOKEN_URL, code, oauth.client_id.clone(), oauth.client_secret.clone(), "authorization_code", Some(redirect_uri)).await?;
1919
get_user_info(GOOGLE_OAUTH_USER_INFO_URL, token).await.map_err(OauthError::UserInfo)?.json::<GoogleOauthUserInfo>().await.map_err(OauthError::UserInfo)
2020
}

src/login/providers/microsoft.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::login::error::OauthError;
22
use crate::login::providers::{get_user_info, get_access_token, OauthInfo, OauthUserInfo};
3-
use crate::login::tools;
3+
use actix_web::http::uri::Scheme;
44
use serde::Deserialize;
55

66
pub mod env {
@@ -29,8 +29,8 @@ impl From<MicrosoftOauthUserInfo> for OauthUserInfo {
2929
}
3030
}
3131

32-
pub async fn user_info(oauth: &OauthInfo, host: String, code: String) -> Result<MicrosoftOauthUserInfo, OauthError> {
33-
let redirect_uri = format!("{}://{}/login/microsoft/callback", tools::scheme(), host);
32+
pub async fn user_info(scheme: Scheme, oauth: &OauthInfo, host: String, code: String) -> Result<MicrosoftOauthUserInfo, OauthError> {
33+
let redirect_uri = format!("{}://{}/login/microsoft/callback", scheme, host);
3434
let token = get_access_token(MICROSOFT_OAUTH_ACCESS_TOKEN_URL, code, oauth.client_id.clone(), oauth.client_secret.clone(), "authorization_code", Some(redirect_uri)).await?;
3535
get_user_info(MICROSOFT_OAUTH_USER_INFO_URL, token).await.map_err(OauthError::UserInfo)?.json::<MicrosoftOauthUserInfo>().await.map_err(OauthError::UserInfo)
3636
}

src/login/providers/mod.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ use serde::{Deserialize, Serialize};
1111
use std::collections::HashMap;
1212
use std::fmt;
1313
use super::error::OauthError;
14-
use super::tools;
14+
15+
use actix_web::http::uri::Scheme;
1516

1617
#[derive(Clone, Debug)]
1718
pub struct OauthInfo {
@@ -71,11 +72,11 @@ impl Provider {
7172
}
7273
}
7374

74-
fn get_login_url_params(&self, host: String, state: String) -> Vec<(&str, String)> {
75+
fn get_login_url_params(&self, scheme: Scheme, host: String, state: String) -> Vec<(&str, String)> {
7576
let mut params = vec![
7677
("client_id", self.get_oauth_info().client_id),
7778
("state", state),
78-
("redirect_uri", format!("{}://{}/login/{}/callback", tools::scheme(), host, self.name())),
79+
("redirect_uri", format!("{}://{}/login/{}/callback", scheme, host, self.name())),
7980
];
8081

8182
#[cfg(feature = "github-login")]
@@ -103,9 +104,9 @@ impl Provider {
103104
params
104105
}
105106

106-
pub fn get_login_url(&self, host: String, state: String) -> String {
107+
pub fn get_login_url(&self, scheme: Scheme, host: String, state: String) -> String {
107108

108-
let params = self.get_login_url_params(host, state);
109+
let params = self.get_login_url_params(scheme, host, state);
109110

110111
let oauth_url = match self {
111112
#[cfg(feature = "github-login")]
@@ -122,16 +123,16 @@ impl Provider {
122123
}
123124

124125
#[allow(unused_variables)]
125-
pub async fn get_user_info(&self, host: String, token: String) -> Result<ThirdPartyUserInfo, OauthError> {
126+
pub async fn get_user_info(&self, scheme: Scheme, host: String, token: String) -> Result<ThirdPartyUserInfo, OauthError> {
126127
let user_info: OauthUserInfo = match self {
127128
#[cfg(feature = "github-login")]
128129
Self::Github(oauth) => github::user_info(oauth, host).await?.into(),
129130
#[cfg(feature = "gitlab-login")]
130-
Self::Gitlab(oauth) => gitlab::user_info(oauth, host, token).await?.into(),
131+
Self::Gitlab(oauth) => gitlab::user_info(scheme, oauth, host, token).await?.into(),
131132
#[cfg(feature = "google-login")]
132-
Self::Google(oauth) => google::user_info(oauth, host, token).await?,
133+
Self::Google(oauth) => google::user_info(scheme, oauth, host, token).await?,
133134
#[cfg(feature = "microsoft-login")]
134-
Self::Microsoft(oauth) => microsoft::user_info(oauth, host, token).await?.into(),
135+
Self::Microsoft(oauth) => microsoft::user_info(scheme, oauth, host, token).await?.into(),
135136
};
136137

137138
Ok(ThirdPartyUserInfo {

src/login/routes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ async fn login(
8989
let host = req.connection_info().host().to_string();
9090
let state = Uuid::new_v4().to_string();
9191

92-
let login_url = provider.get_login_url(host, state.clone());
92+
let login_url = provider.get_login_url(providers_config.get_callback_scheme(), host, state.clone());
9393

9494
let mut response = HttpResponse::TemporaryRedirect()
9595
.append_header(("Location", login_url))
@@ -148,7 +148,7 @@ async fn login_callback(
148148
let host = req.connection_info().host().to_string();
149149

150150
let user_info = provider
151-
.get_user_info(host, info.code.clone())
151+
.get_user_info(providers_config.get_callback_scheme(), host, info.code.clone())
152152
.await
153153
.map_err(actix_web::error::ErrorInternalServerError)?;
154154

src/login/tools.rs

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/main.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,13 @@ async fn run_app() -> Result<(), Box<dyn Error>> {
9191
let providers_config: login::ProvidersConfig = login::get_provider_config();
9292

9393
#[cfg(feature = "third-party-login")]
94-
info!("Third party login enabled: {} providers found.", providers_config.available_providers.len());
94+
{
95+
info!("Third party login enabled: {} providers found.", providers_config.available_providers.len());
96+
if providers_config.https_callback {
97+
info!("Third party login enabled: login callback will use HTTPS");
98+
}
99+
}
100+
95101

96102
let pool = storage.pool()?;
97103
let mut server = HttpServer::new(move || {

0 commit comments

Comments
 (0)