1
0
mirror of https://github.com/matrix-org/matrix-authentication-service.git synced 2025-07-29 22:01:14 +03:00

Make the template contexts a bit more clear

This commit is contained in:
Quentin Gliech
2021-09-17 14:30:06 +02:00
parent 7afd82be8f
commit 463184bbb1
4 changed files with 30 additions and 17 deletions

View File

@ -27,7 +27,7 @@ use crate::{
with_templates, CsrfToken,
},
storage::SessionInfo,
templates::{TemplateContext, Templates},
templates::{EmptyContext, TemplateContext, Templates},
};
#[derive(Deserialize, Debug)]
@ -63,7 +63,7 @@ async fn get(
csrf_token: CsrfToken,
session: SessionInfo,
) -> Result<impl Reply, Rejection> {
let ctx = ().with_session(session).with_csrf(&csrf_token);
let ctx = EmptyContext.with_session(session).with_csrf(&csrf_token);
let content = templates.render_reauth(&ctx)?;
let reply = html(content);

View File

@ -31,7 +31,7 @@ use crate::{
with_templates, CsrfToken,
},
storage::{register_user, user::start_session, SessionInfo},
templates::{TemplateContext, Templates},
templates::{EmptyContext, TemplateContext, Templates},
};
#[derive(Serialize, Deserialize)]
@ -115,7 +115,7 @@ async fn get(
if maybe_session.is_some() {
Ok(Box::new(query.redirect()?))
} else {
let ctx = ().with_csrf(&csrf_token);
let ctx = EmptyContext.with_csrf(&csrf_token);
let content = templates.render_register(&ctx)?;
let reply = html(content);
let reply = cookie_saver.save_encrypted(&csrf_token, reply)?;

View File

@ -19,22 +19,31 @@ use url::Url;
use crate::{errors::ErroredForm, filters::CsrfToken, storage::SessionInfo};
/// Helper trait to construct context wrappers
pub trait TemplateContext: Sized {
fn with_session(self, current_session: SessionInfo) -> WithSession<Self> {
pub trait TemplateContext {
fn with_session(self, current_session: SessionInfo) -> WithSession<Self>
where
Self: Sized,
{
WithSession {
current_session,
inner: self,
}
}
fn maybe_with_session(self, current_session: Option<SessionInfo>) -> WithOptionalSession<Self> {
fn maybe_with_session(self, current_session: Option<SessionInfo>) -> WithOptionalSession<Self>
where
Self: Sized,
{
WithOptionalSession {
current_session,
inner: self,
}
}
fn with_csrf(self, token: &CsrfToken) -> WithCsrf<Self> {
fn with_csrf(self, token: &CsrfToken) -> WithCsrf<Self>
where
Self: Sized,
{
WithCsrf {
csrf_token: token.form_value(),
inner: self,
@ -42,13 +51,13 @@ pub trait TemplateContext: Sized {
}
}
impl TemplateContext for () {}
impl TemplateContext for EmptyContext {}
impl TemplateContext for IndexContext {}
impl TemplateContext for LoginContext {}
impl<T: Sized> TemplateContext for FormPostContext<T> {}
impl<T: Sized> TemplateContext for WithSession<T> {}
impl<T: Sized> TemplateContext for WithOptionalSession<T> {}
impl<T: Sized> TemplateContext for WithCsrf<T> {}
impl<T> TemplateContext for FormPostContext<T> {}
impl<T> TemplateContext for WithSession<T> {}
impl<T> TemplateContext for WithOptionalSession<T> {}
impl<T> TemplateContext for WithCsrf<T> {}
/// Context with a CSRF token in it
#[derive(Serialize)]
@ -77,6 +86,10 @@ pub struct WithOptionalSession<T> {
inner: T,
}
/// An empty context used for composition
#[derive(Serialize)]
pub struct EmptyContext;
// Context used by the `index.html` template
#[derive(Serialize)]
pub struct IndexContext {

View File

@ -27,8 +27,8 @@ mod context;
mod macros;
pub use self::context::{
ErrorContext, FormPostContext, IndexContext, LoginContext, LoginFormField, TemplateContext,
WithCsrf, WithOptionalSession, WithSession,
EmptyContext, ErrorContext, FormPostContext, IndexContext, LoginContext, LoginFormField,
TemplateContext, WithCsrf, WithOptionalSession, WithSession,
};
#[derive(Clone)]
@ -163,13 +163,13 @@ register_templates! {
pub fn render_login(WithCsrf<LoginContext>) { "login.html" }
/// Render the registration page
pub fn render_register(WithCsrf<()>) { "register.html" }
pub fn render_register(WithCsrf<EmptyContext>) { "register.html" }
/// Render the home page
pub fn render_index(WithCsrf<WithOptionalSession<IndexContext>>) { "index.html" }
/// Render the re-authentication form
pub fn render_reauth(WithCsrf<WithSession<()>>) { "reauth.html" }
pub fn render_reauth(WithCsrf<WithSession<EmptyContext>>) { "reauth.html" }
/// Render the form used by the form_post response mode
pub fn render_form_post<T: Serialize>(FormPostContext<T>) { "form_post.html" }