clemsbot/src/app/web/route/access_token.rs

47 lines
1.3 KiB
Rust

use cookie::{Cookie, SameSite};
use warp::{
Filter, Reply,
filters::BoxedFilter,
http::Uri,
};
use crate::app::web::{
CustomRejection,
template::index::IndexTemplate,
};
use std::collections::HashMap;
pub fn access_token_routes() -> BoxedFilter<(impl Reply, )> {
warp::get().and(access_token_page())
.or(warp::post().and(access_token_submit()))
.boxed()
}
fn access_token_page() -> BoxedFilter<(impl Reply, )> {
warp::path::end()
.map(|| IndexTemplate)
.boxed()
}
fn access_token_submit() -> BoxedFilter<(impl Reply, )> {
warp::path::end()
.and(warp::body::content_length_limit(1024))
.and(warp::body::form())
.and_then(|form: HashMap<String, String>| async move {
let token = match form.get("access_token") {
Some(token) => token,
None => return Err(warp::reject::custom(CustomRejection::InvalidForm)),
};
let cookie = Cookie::build("access_token", token)
.same_site(SameSite::Lax)
.secure(true)
.http_only(true)
.finish();
Ok(warp::reply::with_header(
warp::redirect(Uri::from_static("/")),
"Set-Cookie",
cookie.encoded().to_string(),
))
})
.boxed()
}