auth backend from host and default frontend/backend
This commit is contained in:
45
src/login.rs
45
src/login.rs
@@ -10,30 +10,48 @@ use rocket::State;
|
|||||||
use std::env;
|
use std::env;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
pub struct RefHeader(pub String);
|
#[derive(Debug)]
|
||||||
|
pub struct FrontendAddr(pub String);
|
||||||
|
|
||||||
#[rocket::async_trait]
|
#[rocket::async_trait]
|
||||||
impl<'r> FromRequest<'r> for RefHeader {
|
impl<'r> FromRequest<'r> for FrontendAddr {
|
||||||
type Error = ();
|
type Error = ();
|
||||||
async fn from_request(request: &'r Request<'_>) -> Outcome<Self, Self::Error> {
|
async fn from_request(request: &'r Request<'_>) -> Outcome<Self, Self::Error> {
|
||||||
match request.headers().get_one("Referer") {
|
Outcome::Success(Self(
|
||||||
Some(h) => Outcome::Success(RefHeader(h.to_string())),
|
request
|
||||||
None => Outcome::Forward(()),
|
.headers()
|
||||||
|
.get_one("Referer")
|
||||||
|
.map(|s| s.to_string())
|
||||||
|
.unwrap_or_else(|| env::var("DEFAULT_FRONTEND").unwrap()),
|
||||||
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct BackendAddr(pub String);
|
||||||
|
|
||||||
|
#[rocket::async_trait]
|
||||||
|
impl<'r> FromRequest<'r> for BackendAddr {
|
||||||
|
type Error = ();
|
||||||
|
async fn from_request(request: &'r Request<'_>) -> Outcome<Self, Self::Error> {
|
||||||
|
Outcome::Success(Self(
|
||||||
|
request
|
||||||
|
.headers()
|
||||||
|
.get_one("Host")
|
||||||
|
.map(|s| format!("https://{}", s))
|
||||||
|
.unwrap_or_else(|| env::var("DEFAULT_BACKEND").unwrap()),
|
||||||
|
))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/?p=cs")]
|
#[get("/?p=cs")]
|
||||||
pub fn cs_login(r: RefHeader) -> Redirect {
|
pub fn cs_login(r: FrontendAddr, h: BackendAddr) -> Redirect {
|
||||||
let mast_url = env::var("MAST_BASE_URL").unwrap();
|
let mast_url = env::var("MAST_BASE_URL").unwrap();
|
||||||
let mast_cli = env::var("MAST_CLIENT").unwrap();
|
let mast_cli = env::var("MAST_CLIENT").unwrap();
|
||||||
let mast_scope = env::var("MAST_SCOPE").unwrap();
|
let mast_scope = env::var("MAST_SCOPE").unwrap();
|
||||||
|
|
||||||
let jump_to_url = Url::parse(&r.0).unwrap();
|
let jump_to_url = Url::parse(&r.0).unwrap();
|
||||||
|
let mut redirect_url = Url::parse(&h.0).unwrap();
|
||||||
let mut redirect_url = env::var("AUTH_BACKEND_URL")
|
|
||||||
.map(|url| Url::parse(&url).unwrap())
|
|
||||||
.unwrap_or_else(|_| jump_to_url.clone());
|
|
||||||
redirect_url.set_path("/_login/cs/auth");
|
redirect_url.set_path("/_login/cs/auth");
|
||||||
|
|
||||||
redirect_url = Url::parse_with_params(
|
redirect_url = Url::parse_with_params(
|
||||||
@@ -145,16 +163,13 @@ pub async fn cs_auth(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[get("/gh")]
|
#[get("/gh")]
|
||||||
pub fn gh_login(r: RefHeader) -> Redirect {
|
pub fn gh_login(r: FrontendAddr, h: BackendAddr) -> Redirect {
|
||||||
let gh_url = "https://github.com/login/oauth/authorize";
|
let gh_url = "https://github.com/login/oauth/authorize";
|
||||||
let gh_cli = env::var("GH_CLIENT").unwrap();
|
let gh_cli = env::var("GH_CLIENT").unwrap();
|
||||||
let gh_scope = "user:email";
|
let gh_scope = "user:email";
|
||||||
|
|
||||||
let jump_to_url = Url::parse(&r.0).unwrap();
|
let jump_to_url = Url::parse(&r.0).unwrap();
|
||||||
|
let mut redirect_url = Url::parse(&h.0).unwrap();
|
||||||
let mut redirect_url = env::var("AUTH_BACKEND_URL")
|
|
||||||
.map(|url| Url::parse(&url).unwrap())
|
|
||||||
.unwrap_or_else(|_| jump_to_url.clone());
|
|
||||||
redirect_url.set_path("/_login/gh/auth");
|
redirect_url.set_path("/_login/gh/auth");
|
||||||
|
|
||||||
redirect_url = Url::parse_with_params(
|
redirect_url = Url::parse_with_params(
|
||||||
|
|||||||
Reference in New Issue
Block a user