You've already forked authentication-service
mirror of
https://github.com/matrix-org/matrix-authentication-service.git
synced 2025-07-31 09:24:31 +03:00
Axum migration: logout route
This commit is contained in:
@ -38,6 +38,13 @@ impl SessionInfo {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Mark the session as ended
|
||||||
|
#[must_use]
|
||||||
|
pub fn mark_session_ended(mut self) -> Self {
|
||||||
|
self.current = None;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Load the [`BrowserSession`] from database
|
/// Load the [`BrowserSession`] from database
|
||||||
pub async fn load_session(
|
pub async fn load_session(
|
||||||
&self,
|
&self,
|
||||||
|
@ -21,7 +21,12 @@
|
|||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use axum::{body::HttpBody, extract::Extension, routing::get, Router};
|
use axum::{
|
||||||
|
body::HttpBody,
|
||||||
|
extract::Extension,
|
||||||
|
routing::{get, post},
|
||||||
|
Router,
|
||||||
|
};
|
||||||
use mas_axum_utils::UrlBuilder;
|
use mas_axum_utils::UrlBuilder;
|
||||||
use mas_config::{Encrypter, RootConfig};
|
use mas_config::{Encrypter, RootConfig};
|
||||||
use mas_email::Mailer;
|
use mas_email::Mailer;
|
||||||
@ -81,6 +86,7 @@ where
|
|||||||
"/login",
|
"/login",
|
||||||
get(self::views::login::get).post(self::views::login::post),
|
get(self::views::login::get).post(self::views::login::post),
|
||||||
)
|
)
|
||||||
|
.route("/logout", post(self::views::logout::post))
|
||||||
.fallback(mas_static_files::Assets)
|
.fallback(mas_static_files::Assets)
|
||||||
.layer(Extension(pool.clone()))
|
.layer(Extension(pool.clone()))
|
||||||
.layer(Extension(templates.clone()))
|
.layer(Extension(templates.clone()))
|
||||||
|
@ -12,8 +12,6 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
#![allow(clippy::trait_duplication_in_bounds)]
|
|
||||||
|
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::{Extension, Form, Query},
|
extract::{Extension, Form, Query},
|
||||||
response::{Html, IntoResponse, Redirect, Response},
|
response::{Html, IntoResponse, Redirect, Response},
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
// Copyright 2021 The Matrix.org Foundation C.I.C.
|
// Copyright 2021, 2022 The Matrix.org Foundation C.I.C.
|
||||||
//
|
//
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
// you may not use this file except in compliance with the License.
|
// you may not use this file except in compliance with the License.
|
||||||
@ -12,34 +12,48 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
use mas_config::Encrypter;
|
use axum::{
|
||||||
use mas_data_model::BrowserSession;
|
extract::{Extension, Form},
|
||||||
use mas_storage::{user::end_session, PostgresqlBackend};
|
response::{IntoResponse, Redirect},
|
||||||
use mas_warp_utils::{
|
|
||||||
errors::WrapError,
|
|
||||||
filters::{self, csrf::protected_form, database::transaction, session::session},
|
|
||||||
};
|
};
|
||||||
use sqlx::{PgPool, Postgres, Transaction};
|
use hyper::Uri;
|
||||||
use warp::{filters::BoxedFilter, hyper::Uri, Filter, Rejection, Reply};
|
use mas_axum_utils::{
|
||||||
|
csrf::{CsrfExt, ProtectedForm},
|
||||||
|
fancy_error, FancyError, PrivateCookieJar, SessionInfoExt,
|
||||||
|
};
|
||||||
|
use mas_config::Encrypter;
|
||||||
|
use mas_storage::user::end_session;
|
||||||
|
use mas_templates::Templates;
|
||||||
|
use sqlx::PgPool;
|
||||||
|
|
||||||
pub(super) fn filter(pool: &PgPool, encrypter: &Encrypter) -> BoxedFilter<(Box<dyn Reply>,)> {
|
pub(crate) async fn post(
|
||||||
warp::path!("logout")
|
Extension(templates): Extension<Templates>,
|
||||||
.and(filters::trace::name("POST /logout"))
|
Extension(pool): Extension<PgPool>,
|
||||||
.and(warp::post())
|
cookie_jar: PrivateCookieJar<Encrypter>,
|
||||||
.and(session(pool, encrypter))
|
Form(form): Form<ProtectedForm<()>>,
|
||||||
.and(transaction(pool))
|
) -> Result<impl IntoResponse, FancyError> {
|
||||||
.and(protected_form(encrypter))
|
let mut txn = pool.begin().await.map_err(fancy_error(templates.clone()))?;
|
||||||
.and_then(post)
|
|
||||||
.boxed()
|
cookie_jar
|
||||||
}
|
.verify_form(form)
|
||||||
|
.map_err(fancy_error(templates.clone()))?;
|
||||||
async fn post(
|
|
||||||
session: BrowserSession<PostgresqlBackend>,
|
let (session_info, mut cookie_jar) = cookie_jar.session_info();
|
||||||
mut txn: Transaction<'_, Postgres>,
|
|
||||||
_form: (),
|
let maybe_session = session_info
|
||||||
) -> Result<Box<dyn Reply>, Rejection> {
|
.load_session(&mut txn)
|
||||||
end_session(&mut txn, &session).await.wrap_error()?;
|
.await
|
||||||
txn.commit().await.wrap_error()?;
|
.map_err(fancy_error(templates.clone()))?;
|
||||||
|
|
||||||
Ok(Box::new(warp::redirect(Uri::from_static("/login"))))
|
if let Some(session) = maybe_session {
|
||||||
|
end_session(&mut txn, &session)
|
||||||
|
.await
|
||||||
|
.map_err(fancy_error(templates.clone()))?;
|
||||||
|
cookie_jar = cookie_jar.update_session_info(&session_info.mark_session_ended());
|
||||||
|
}
|
||||||
|
|
||||||
|
txn.commit().await.map_err(fancy_error(templates))?;
|
||||||
|
|
||||||
|
let to = Uri::from_static("/login");
|
||||||
|
Ok((cookie_jar.headers(), Redirect::to(to)))
|
||||||
}
|
}
|
||||||
|
@ -28,8 +28,8 @@ pub mod shared;
|
|||||||
pub mod verify;
|
pub mod verify;
|
||||||
|
|
||||||
use self::{
|
use self::{
|
||||||
account::filter as account, logout::filter as logout, reauth::filter as reauth,
|
account::filter as account, reauth::filter as reauth, register::filter as register,
|
||||||
register::filter as register, verify::filter as verify,
|
verify::filter as verify,
|
||||||
};
|
};
|
||||||
pub(crate) use self::{
|
pub(crate) use self::{
|
||||||
login::LoginRequest, reauth::ReauthRequest, register::RegisterRequest, shared::PostAuthAction,
|
login::LoginRequest, reauth::ReauthRequest, register::RegisterRequest, shared::PostAuthAction,
|
||||||
@ -45,15 +45,12 @@ pub(super) fn filter(
|
|||||||
) -> BoxedFilter<(Box<dyn Reply>,)> {
|
) -> BoxedFilter<(Box<dyn Reply>,)> {
|
||||||
let account = account(pool, templates, mailer, encrypter, http_config, csrf_config);
|
let account = account(pool, templates, mailer, encrypter, http_config, csrf_config);
|
||||||
let register = register(pool, templates, encrypter, csrf_config);
|
let register = register(pool, templates, encrypter, csrf_config);
|
||||||
let logout = logout(pool, encrypter);
|
|
||||||
let reauth = reauth(pool, templates, encrypter, csrf_config);
|
let reauth = reauth(pool, templates, encrypter, csrf_config);
|
||||||
let verify = verify(pool, templates, encrypter, csrf_config);
|
let verify = verify(pool, templates, encrypter, csrf_config);
|
||||||
|
|
||||||
account
|
account
|
||||||
.or(register)
|
.or(register)
|
||||||
.unify()
|
.unify()
|
||||||
.or(logout)
|
|
||||||
.unify()
|
|
||||||
.or(reauth)
|
.or(reauth)
|
||||||
.unify()
|
.unify()
|
||||||
.or(verify)
|
.or(verify)
|
||||||
|
Reference in New Issue
Block a user