Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement MSC2965 action parameter #1673

Merged
merged 10 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions crates/handlers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
clippy::let_with_type_underscore,
)]

use std::{convert::Infallible, time::Duration};
use std::{borrow::Cow, convert::Infallible, time::Duration};

use axum::{
body::{Bytes, HttpBody},
extract::{FromRef, FromRequestParts, OriginalUri, State},
extract::{FromRef, FromRequestParts, OriginalUri, RawQuery, State},
http::Method,
response::{Html, IntoResponse},
routing::{get, on, post, MethodFilter},
Expand Down Expand Up @@ -265,6 +265,7 @@
)
}

#[allow(clippy::too_many_lines)]
pub fn human_router<S, B>(templates: Templates) -> Router<S, B>
where
B: HttpBody + Send + 'static,
Expand All @@ -286,7 +287,19 @@
{
Router::new()
// XXX: hard-coded redirect from /account to /account/
.route("/account", get(|| async { mas_router::Account.go() }))
.route(
"/account",
get(|RawQuery(query): RawQuery| async {
let route = mas_router::Account::route();
let destination = if let Some(query) = query {
Cow::Owned(format!("{route}?{query}"))

Check warning on line 295 in crates/handlers/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/handlers/src/lib.rs#L293-L295

Added lines #L293 - L295 were not covered by tests
} else {
Cow::Borrowed(route)

Check warning on line 297 in crates/handlers/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/handlers/src/lib.rs#L297

Added line #L297 was not covered by tests
};

axum::response::Redirect::to(&destination)

Check warning on line 300 in crates/handlers/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/handlers/src/lib.rs#L300

Added line #L300 was not covered by tests
}),
)
.route(mas_router::Account::route(), get(self::views::app::get))
.route(
mas_router::AccountWildcard::route(),
Expand Down
2 changes: 1 addition & 1 deletion crates/handlers/src/views/account/emails/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub(crate) async fn post(

next.go()
} else {
query.go_next_or_default(&mas_router::Account)
query.go_next_or_default(&mas_router::Account::default())
};

repo.save().await?;
Expand Down
4 changes: 2 additions & 2 deletions crates/handlers/src/views/account/emails/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub(crate) async fn get(

if user_email.confirmed_at.is_some() {
// This email was already verified, skip
let destination = query.go_next_or_default(&mas_router::Account);
let destination = query.go_next_or_default(&mas_router::Account::default());
return Ok((cookie_jar, destination).into_response());
}

Expand Down Expand Up @@ -145,6 +145,6 @@ pub(crate) async fn post(

repo.save().await?;

let destination = query.go_next_or_default(&mas_router::Account);
let destination = query.go_next_or_default(&mas_router::Account::default());
Ok((cookie_jar, destination).into_response())
}
2 changes: 1 addition & 1 deletion crates/handlers/src/views/account/password.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub(crate) async fn get(
) -> Result<Response, FancyError> {
// If the password manager is disabled, we can go back to the account page.
if !password_manager.is_enabled() {
return Ok(mas_router::Account.go().into_response());
return Ok(mas_router::Account::default().go().into_response());
}

let (session_info, cookie_jar) = cookie_jar.session_info();
Expand Down
8 changes: 5 additions & 3 deletions crates/handlers/src/views/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

use axum::{
extract::State,
extract::{Query, State},
response::{Html, IntoResponse},
};
use mas_axum_utils::{cookies::CookieJar, FancyError, SessionInfoExt};
Expand All @@ -24,17 +24,19 @@
#[tracing::instrument(name = "handlers.views.app.get", skip_all, err)]
pub async fn get(
State(templates): State<Templates>,
action: Option<Query<mas_router::AccountAction>>,
mut repo: BoxRepository,
cookie_jar: CookieJar,
) -> Result<impl IntoResponse, FancyError> {
let (session_info, cookie_jar) = cookie_jar.session_info();
let session = session_info.load_session(&mut repo).await?;
let action = action.map(|Query(a)| a);

Check warning on line 33 in crates/handlers/src/views/app.rs

View check run for this annotation

Codecov / codecov/patch

crates/handlers/src/views/app.rs#L33

Added line #L33 was not covered by tests

// TODO: keep the full path
// TODO: keep the full path, not just the action
if session.is_none() {
return Ok((
cookie_jar,
mas_router::Login::and_then(PostAuthAction::ManageAccount).go(),
mas_router::Login::and_then(PostAuthAction::manage_account(action)).go(),
)
.into_response());
}
Expand Down
2 changes: 1 addition & 1 deletion crates/handlers/src/views/reauth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub(crate) async fn get(
) -> Result<Response, FancyError> {
if !password_manager.is_enabled() {
// XXX: do something better here
return Ok(mas_router::Account.go().into_response());
return Ok(mas_router::Account::default().go().into_response());
}

let (csrf_token, cookie_jar) = cookie_jar.csrf_token(&clock, &mut rng);
Expand Down
2 changes: 1 addition & 1 deletion crates/handlers/src/views/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
PostAuthContextInner::LinkUpstream { provider, link }
}

PostAuthAction::ManageAccount => PostAuthContextInner::ManageAccount,
PostAuthAction::ManageAccount { .. } => PostAuthContextInner::ManageAccount,

Check warning on line 91 in crates/handlers/src/views/shared.rs

View check run for this annotation

Codecov / codecov/patch

crates/handlers/src/views/shared.rs#L91

Added line #L91 was not covered by tests
};

Ok(Some(PostAuthContext {
Expand Down
53 changes: 45 additions & 8 deletions crates/router/src/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,20 @@
#[derive(Deserialize, Serialize, Clone, Debug)]
#[serde(rename_all = "snake_case", tag = "next")]
pub enum PostAuthAction {
ContinueAuthorizationGrant { id: Ulid },
ContinueCompatSsoLogin { id: Ulid },
ContinueAuthorizationGrant {
id: Ulid,
},
ContinueCompatSsoLogin {
id: Ulid,
},
ChangePassword,
LinkUpstream { id: Ulid },
ManageAccount,
LinkUpstream {
id: Ulid,
},
ManageAccount {
#[serde(flatten)]
action: Option<AccountAction>,
},
}

impl PostAuthAction {
Expand All @@ -43,13 +52,21 @@
PostAuthAction::LinkUpstream { id }
}

#[must_use]
pub const fn manage_account(action: Option<AccountAction>) -> Self {
PostAuthAction::ManageAccount { action }
}

Check warning on line 58 in crates/router/src/endpoints.rs

View check run for this annotation

Codecov / codecov/patch

crates/router/src/endpoints.rs#L56-L58

Added lines #L56 - L58 were not covered by tests

pub fn go_next(&self) -> axum::response::Redirect {
match self {
Self::ContinueAuthorizationGrant { id } => ContinueAuthorizationGrant(*id).go(),
Self::ContinueCompatSsoLogin { id } => CompatLoginSsoComplete::new(*id, None).go(),
Self::ChangePassword => AccountPassword.go(),
Self::LinkUpstream { id } => UpstreamOAuth2Link::new(*id).go(),
Self::ManageAccount => Account.go(),
Self::ManageAccount { action } => Account {
action: action.clone(),
}
.go(),

Check warning on line 69 in crates/router/src/endpoints.rs

View check run for this annotation

Codecov / codecov/patch

crates/router/src/endpoints.rs#L66-L69

Added lines #L66 - L69 were not covered by tests
}
}
}
Expand Down Expand Up @@ -406,12 +423,32 @@
}
}

/// Actions parameters as defined by MSC2965
#[derive(Debug, Clone, Serialize, Deserialize)]

Check warning on line 427 in crates/router/src/endpoints.rs

View check run for this annotation

Codecov / codecov/patch

crates/router/src/endpoints.rs#L427

Added line #L427 was not covered by tests
#[serde(rename_all = "snake_case", tag = "action")]
pub enum AccountAction {
Profile,
SessionsList,
SessionView { device_id: String },
SessionEnd { device_id: String },
}

/// `GET /account/`
#[derive(Default, Debug, Clone)]
pub struct Account;
pub struct Account {
action: Option<AccountAction>,
}

impl SimpleRoute for Account {
const PATH: &'static str = "/account/";
impl Route for Account {
type Query = AccountAction;

fn route() -> &'static str {
"/account/"
}

fn query(&self) -> Option<&Self::Query> {
self.action.as_ref()
}

Check warning on line 451 in crates/router/src/endpoints.rs

View check run for this annotation

Codecov / codecov/patch

crates/router/src/endpoints.rs#L449-L451

Added lines #L449 - L451 were not covered by tests
}

/// `GET /account/*`
Expand Down
Loading