From c6667c977aa1cedec7659241038c92246a7c5011 Mon Sep 17 00:00:00 2001 From: Adrian Malacoda Date: Thu, 10 May 2018 01:18:36 -0500 Subject: [PATCH] make url key in config optional, since it can be determined from request --- src/config.rs | 2 +- src/http.rs | 15 +++++++++++++++ src/oidc/provider.rs | 18 ++++++++++++++---- who-server.toml | 2 +- 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/config.rs b/src/config.rs index 71e0390..3a1568e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -9,7 +9,7 @@ use std::io::Read; #[derive(Debug, Deserialize)] pub struct Config { - pub url: String, + pub url: Option, pub keyfile: Option, pub user_store: Option, pub web: Option diff --git a/src/http.rs b/src/http.rs index 15d637f..06d1049 100644 --- a/src/http.rs +++ b/src/http.rs @@ -2,6 +2,7 @@ use rocket::request::{self, Outcome, Request, FromRequest}; use rocket::Outcome::{Success, Forward}; static REFERER_HEADER: &'static str = "referer"; +static HOST_HEADER: &'static str = "host"; static AUTH_HEADER: &'static str = "authorization"; static AUTH_SCHEME: &'static str = "Bearer "; @@ -19,6 +20,20 @@ impl<'a, 'r> FromRequest<'a, 'r> for Referer<'a> { } } +#[derive(Into)] +pub struct Host<'a>(&'a str); + +impl<'a, 'r> FromRequest<'a, 'r> for Host<'a> { + type Error = (); + + fn from_request(request: &'a Request<'r>) -> Outcome, ()> { + match request.headers().get_one(HOST_HEADER) { + Some(host) => Success(Host(host)), + None => Forward(()) + } + } +} + #[derive(Into)] pub struct BearerToken<'a>(&'a str); diff --git a/src/oidc/provider.rs b/src/oidc/provider.rs index a69bfac..8f1b86e 100644 --- a/src/oidc/provider.rs +++ b/src/oidc/provider.rs @@ -1,3 +1,4 @@ +use http::Host; use rocket::Route; use rocket::State; use rocket::request::Form; @@ -109,7 +110,7 @@ struct TokenRequest { // http://openid.net/specs/openid-connect-core-1_0.html#TokenEndpoint #[post("/oidc/token", data="
")] -fn token(key: State, config: State, token_store_mutex: State>>, form: Form) -> Option> { +fn token(key: State, config: State, token_store_mutex: State>>, form: Form, host: Option) -> Option> { println!("{:?}", form.get()); let mut token_store = token_store_mutex.lock().expect("Failed to acquire lock on token store"); let request = form.get(); @@ -119,11 +120,16 @@ fn token(key: State, config: State, token_store_mutex: State, access_token: BearerToken) -> Option< } #[get("/.well-known/openid-configuration")] -fn configuration(config: State) -> Json { - let server_url = config.url.to_owned(); +fn configuration(config: State, host: Option) -> Json { + let server_url = config.url.to_owned().or_else(|| host.map(|host| { + let host_str: &str = host.into(); + format!("http://{}/", host_str) + })).expect("Failed to determine server url"); + Json(Configuration { issuer: server_url.to_owned(), authorization_endpoint: format!("{}oidc/authenticate", server_url), diff --git a/who-server.toml b/who-server.toml index cbf73e9..c08706c 100644 --- a/who-server.toml +++ b/who-server.toml @@ -1,4 +1,4 @@ -url = "http://baragaki:8000/" +#url = "http://kraken:8000/" [web] port = 8000