fn login_redirect(
    settings: Arc<Settings>,
    origin: Url,
) -> warp::http::Result<warp::http::Response<warp::hyper::Body>> {
    let state = uuid::Uuid::new_v4().to_string();
    // Construct redirect_uri based on the external hostname (that might come from X-Forwarded-Host)
    let redirect_url = if let Some(port) = origin.port() {
        format!(
            "{}://{}:{}/oauth/callback",
            origin.scheme(),
            origin.host_str().unwrap_or("localhost"),
            port
        )
    } else {
        format!(
            "{}://{}/oauth/callback",
            origin.scheme(),
            origin.host_str().unwrap_or("localhost")
        )
    };

    // Construct a hmac signed cookie so that the oauth callback can check if the request if legit.
    let mut payload = BytesMut::new();
    payload.put(state.as_bytes());
    payload.put_u8(0);
    payload.put(redirect_url.as_bytes());
    payload.put_u8(0);
    payload.put(origin.to_string().as_bytes());
    let hmac = hmac_sha256::HMAC::mac(&payload, settings.cookie_secret.as_bytes());

    let mut login_cookie = cookie::Cookie::build(
        &settings.login_cookie,
        format!(
            "{}.{}",
            base64::encode_config(payload, base64::URL_SAFE_NO_PAD),
            base64::encode_config(hmac, base64::URL_SAFE_NO_PAD)
        ),
    )
    .http_only(true)
    .path(&settings.cookie_path)
    .secure(settings.cookie_secure);
    if let Some(cookie_domain) = &settings.cookie_domain {
        login_cookie = login_cookie.domain(cookie_domain);
    }

    // Workaround: redirect_uri might not be constant
    let mut openid_client = settings.openid_client.clone();

    openid_client.redirect_uri = Some(redirect_url);

    // Actually build the entire uri to redirect to.
    let login_url = openid_client.auth_url(&openid::Options {
        state: Some(state),
        scope: Some(settings.scopes.clone()),
        ..Default::default()
    });

    return Response::builder()
        .status(StatusCode::FOUND)
        .header(header::LOCATION, login_url.as_str())
        .header(header::SET_COOKIE, login_cookie.finish().to_string())
        .body(warp::hyper::Body::empty());
}
// Construct a response to redirect to the login-page of the openid-provider.
Construct a response to redirect to the login-page of the openid-provider.
Construct a response to redirect to the login - page of the openid - provider .
fn login_redirect(
    settings: Arc<Settings>,
    origin: Url,
) -> warp::http::Result<warp::http::Response<warp::hyper::Body>> {
    let state = uuid::Uuid::new_v4().to_string();
    // Construct redirect_uri based on the external hostname (that might come from X-Forwarded-Host)
    let redirect_url = if let Some(port) = origin.port() {
        format!(
            "{}://{}:{}/oauth/callback",
            origin.scheme(),
            origin.host_str().unwrap_or("localhost"),
            port
        )
    } else {
        format!(
            "{}://{}/oauth/callback",
            origin.scheme(),
            origin.host_str().unwrap_or("localhost")
        )
    };
    // Construct a hmac signed cookie so that the oauth callback can check if the request if legit.
    let mut payload = BytesMut::new();
    payload.put(state.as_bytes());
    payload.put_u8(0);
    payload.put(redirect_url.as_bytes());
    payload.put_u8(0);
    payload.put(origin.to_string().as_bytes());
    let hmac = hmac_sha256::HMAC::mac(&payload, settings.cookie_secret.as_bytes());
    let mut login_cookie = cookie::Cookie::build(
        &settings.login_cookie,
        format!(
            "{}.{}",
            base64::encode_config(payload, base64::URL_SAFE_NO_PAD),
            base64::encode_config(hmac, base64::URL_SAFE_NO_PAD)
        ),
    )
    .http_only(true)
    .path(&settings.cookie_path)
    .secure(settings.cookie_secure);
    if let Some(cookie_domain) = &settings.cookie_domain {
        login_cookie = login_cookie.domain(cookie_domain);
    }
    // Workaround: redirect_uri might not be constant
    let mut openid_client = settings.openid_client.clone();
    openid_client.redirect_uri = Some(redirect_url);
    // Actually build the entire uri to redirect to.
    let login_url = openid_client.auth_url(&openid::Options {
        state: Some(state),
        scope: Some(settings.scopes.clone()),
        ..Default::default()
    });
    return Response::builder()
        .status(StatusCode::FOUND)
        .header(header::LOCATION, login_url.as_str())
        .header(header::SET_COOKIE, login_cookie.finish().to_string())
        .body(warp::hyper::Body::empty());
}
fn login_redirect ( settings : Arc < Settings > , origin : Url , ) -> warp :: http :: Result < warp :: http :: Response < warp :: hyper :: Body > > { let state = uuid :: Uuid :: new_v4 ( ) . to_string ( ) ; // Construct redirect_uri based on the external hostname (that might come from X-Forwarded-Host) let redirect_url = if let Some ( port ) = origin . port ( ) { format ! ( " " origin scheme ( ) origin host_str ( ) unwrap_or ( " " ) port ) } else { format ! ( " " origin scheme ( ) origin host_str ( ) unwrap_or ( " " ) ) } ; // Construct a hmac signed cookie so that the oauth callback can check if the request if legit. let mut payload = BytesMut :: new ( ) ; payload . put ( state . as_bytes ( ) ) ; payload . put_u8 ( 0 ) ; payload . put ( redirect_url . as_bytes ( ) ) ; payload . put_u8 ( 0 ) ; payload . put ( origin . to_string ( ) . as_bytes ( ) ) ; let hmac = hmac_sha256 :: HMAC :: mac ( & payload , settings . cookie_secret . as_bytes ( ) ) ; let mut login_cookie = cookie :: Cookie :: build ( & settings . login_cookie , format ! ( " " base64 encode_config ( payload base64 URL_SAFE_NO_PAD ) base64 encode_config ( hmac base64 URL_SAFE_NO_PAD ) ) , ) . http_only ( true ) . path ( & settings . cookie_path ) . secure ( settings . cookie_secure ) ; if let Some ( cookie_domain ) = & settings . cookie_domain { login_cookie = login_cookie . domain ( cookie_domain ) ; } // Workaround: redirect_uri might not be constant let mut openid_client = settings . openid_client . clone ( ) ; openid_client . redirect_uri = Some ( redirect_url ) ; // Actually build the entire uri to redirect to. let login_url = openid_client . auth_url ( & openid :: Options { state : Some ( state ) , scope : Some ( settings . scopes . clone ( ) ) , .. Default :: default ( ) } ) ; return Response :: builder ( ) . status ( StatusCode :: FOUND ) . header ( header :: LOCATION , login_url . as_str ( ) ) . header ( header :: SET_COOKIE , login_cookie . finish ( ) . to_string ( ) ) . body ( warp :: hyper :: Body :: empty ( ) ) ; }
Construct a response to redirect to the login-page of the openid-provider.
Construct a response to redirect to the login - page of the openid - provider .
settings
Arc<Settings>
origin
Url
settings
Arc<Settings>
origin
Url