1use serde::{Deserialize, Serialize};
8use ulid::Ulid;
9
10use crate::UrlBuilder;
11pub use crate::traits::*;
12
13#[derive(Deserialize, Serialize, Clone, Debug)]
14#[serde(rename_all = "snake_case", tag = "kind")]
15pub enum PostAuthAction {
16 ContinueAuthorizationGrant {
17 id: Ulid,
18 },
19 ContinueDeviceCodeGrant {
20 id: Ulid,
21 },
22 ContinueCompatSsoLogin {
23 id: Ulid,
24 },
25 ChangePassword,
26 LinkUpstream {
27 id: Ulid,
28 },
29 ManageAccount {
30 #[serde(flatten)]
31 action: Option<AccountAction>,
32 },
33}
34
35impl PostAuthAction {
36 #[must_use]
37 pub const fn continue_grant(id: Ulid) -> Self {
38 PostAuthAction::ContinueAuthorizationGrant { id }
39 }
40
41 #[must_use]
42 pub const fn continue_device_code_grant(id: Ulid) -> Self {
43 PostAuthAction::ContinueDeviceCodeGrant { id }
44 }
45
46 #[must_use]
47 pub const fn continue_compat_sso_login(id: Ulid) -> Self {
48 PostAuthAction::ContinueCompatSsoLogin { id }
49 }
50
51 #[must_use]
52 pub const fn link_upstream(id: Ulid) -> Self {
53 PostAuthAction::LinkUpstream { id }
54 }
55
56 #[must_use]
57 pub const fn manage_account(action: Option<AccountAction>) -> Self {
58 PostAuthAction::ManageAccount { action }
59 }
60
61 pub fn go_next(&self, url_builder: &UrlBuilder) -> axum::response::Redirect {
62 match self {
63 Self::ContinueAuthorizationGrant { id } => url_builder.redirect(&Consent(*id)),
64 Self::ContinueDeviceCodeGrant { id } => {
65 url_builder.redirect(&DeviceCodeConsent::new(*id))
66 }
67 Self::ContinueCompatSsoLogin { id } => {
68 url_builder.redirect(&CompatLoginSsoComplete::new(*id, None))
69 }
70 Self::ChangePassword => url_builder.redirect(&AccountPasswordChange),
71 Self::LinkUpstream { id } => url_builder.redirect(&UpstreamOAuth2Link::new(*id)),
72 Self::ManageAccount { action } => url_builder.redirect(&Account {
73 action: action.clone(),
74 }),
75 }
76 }
77}
78
79#[derive(Default, Debug, Clone)]
81pub struct OidcConfiguration;
82
83impl SimpleRoute for OidcConfiguration {
84 const PATH: &'static str = "/.well-known/openid-configuration";
85}
86
87#[derive(Default, Debug, Clone)]
89pub struct Webfinger;
90
91impl SimpleRoute for Webfinger {
92 const PATH: &'static str = "/.well-known/webfinger";
93}
94
95pub struct ChangePasswordDiscovery;
97
98impl SimpleRoute for ChangePasswordDiscovery {
99 const PATH: &'static str = "/.well-known/change-password";
100}
101
102#[derive(Default, Debug, Clone)]
104pub struct OAuth2Keys;
105
106impl SimpleRoute for OAuth2Keys {
107 const PATH: &'static str = "/oauth2/keys.json";
108}
109
110#[derive(Default, Debug, Clone)]
112pub struct OidcUserinfo;
113
114impl SimpleRoute for OidcUserinfo {
115 const PATH: &'static str = "/oauth2/userinfo";
116}
117
118#[derive(Default, Debug, Clone)]
120pub struct OAuth2Introspection;
121
122impl SimpleRoute for OAuth2Introspection {
123 const PATH: &'static str = "/oauth2/introspect";
124}
125
126#[derive(Default, Debug, Clone)]
128pub struct OAuth2Revocation;
129
130impl SimpleRoute for OAuth2Revocation {
131 const PATH: &'static str = "/oauth2/revoke";
132}
133
134#[derive(Default, Debug, Clone)]
136pub struct OAuth2TokenEndpoint;
137
138impl SimpleRoute for OAuth2TokenEndpoint {
139 const PATH: &'static str = "/oauth2/token";
140}
141
142#[derive(Default, Debug, Clone)]
144pub struct OAuth2RegistrationEndpoint;
145
146impl SimpleRoute for OAuth2RegistrationEndpoint {
147 const PATH: &'static str = "/oauth2/registration";
148}
149
150#[derive(Default, Debug, Clone)]
152pub struct OAuth2AuthorizationEndpoint;
153
154impl SimpleRoute for OAuth2AuthorizationEndpoint {
155 const PATH: &'static str = "/authorize";
156}
157
158#[derive(Default, Debug, Clone)]
160pub struct Index;
161
162impl SimpleRoute for Index {
163 const PATH: &'static str = "/";
164}
165
166#[derive(Default, Debug, Clone)]
168pub struct Healthcheck;
169
170impl SimpleRoute for Healthcheck {
171 const PATH: &'static str = "/health";
172}
173
174#[derive(Default, Debug, Clone)]
176pub struct Login {
177 post_auth_action: Option<PostAuthAction>,
178}
179
180impl Route for Login {
181 type Query = PostAuthAction;
182
183 fn route() -> &'static str {
184 "/login"
185 }
186
187 fn query(&self) -> Option<&Self::Query> {
188 self.post_auth_action.as_ref()
189 }
190}
191
192impl Login {
193 #[must_use]
194 pub const fn and_then(action: PostAuthAction) -> Self {
195 Self {
196 post_auth_action: Some(action),
197 }
198 }
199
200 #[must_use]
201 pub const fn and_continue_grant(id: Ulid) -> Self {
202 Self {
203 post_auth_action: Some(PostAuthAction::continue_grant(id)),
204 }
205 }
206
207 #[must_use]
208 pub const fn and_continue_device_code_grant(id: Ulid) -> Self {
209 Self {
210 post_auth_action: Some(PostAuthAction::continue_device_code_grant(id)),
211 }
212 }
213
214 #[must_use]
215 pub const fn and_continue_compat_sso_login(id: Ulid) -> Self {
216 Self {
217 post_auth_action: Some(PostAuthAction::continue_compat_sso_login(id)),
218 }
219 }
220
221 #[must_use]
222 pub const fn and_link_upstream(id: Ulid) -> Self {
223 Self {
224 post_auth_action: Some(PostAuthAction::link_upstream(id)),
225 }
226 }
227
228 #[must_use]
230 pub fn post_auth_action(&self) -> Option<&PostAuthAction> {
231 self.post_auth_action.as_ref()
232 }
233
234 pub fn go_next(&self, url_builder: &UrlBuilder) -> axum::response::Redirect {
235 match &self.post_auth_action {
236 Some(action) => action.go_next(url_builder),
237 None => url_builder.redirect(&Index),
238 }
239 }
240}
241
242impl From<Option<PostAuthAction>> for Login {
243 fn from(post_auth_action: Option<PostAuthAction>) -> Self {
244 Self { post_auth_action }
245 }
246}
247
248#[derive(Default, Debug, Clone)]
250pub struct Logout;
251
252impl SimpleRoute for Logout {
253 const PATH: &'static str = "/logout";
254}
255
256#[derive(Default, Debug, Clone)]
258pub struct Register {
259 post_auth_action: Option<PostAuthAction>,
260}
261
262impl Register {
263 #[must_use]
264 pub fn and_then(action: PostAuthAction) -> Self {
265 Self {
266 post_auth_action: Some(action),
267 }
268 }
269
270 #[must_use]
271 pub fn and_continue_grant(data: Ulid) -> Self {
272 Self {
273 post_auth_action: Some(PostAuthAction::continue_grant(data)),
274 }
275 }
276
277 #[must_use]
278 pub fn and_continue_compat_sso_login(data: Ulid) -> Self {
279 Self {
280 post_auth_action: Some(PostAuthAction::continue_compat_sso_login(data)),
281 }
282 }
283
284 #[must_use]
286 pub fn post_auth_action(&self) -> Option<&PostAuthAction> {
287 self.post_auth_action.as_ref()
288 }
289
290 pub fn go_next(&self, url_builder: &UrlBuilder) -> axum::response::Redirect {
291 match &self.post_auth_action {
292 Some(action) => action.go_next(url_builder),
293 None => url_builder.redirect(&Index),
294 }
295 }
296}
297
298impl Route for Register {
299 type Query = PostAuthAction;
300
301 fn route() -> &'static str {
302 "/register"
303 }
304
305 fn query(&self) -> Option<&Self::Query> {
306 self.post_auth_action.as_ref()
307 }
308}
309
310impl From<Option<PostAuthAction>> for Register {
311 fn from(post_auth_action: Option<PostAuthAction>) -> Self {
312 Self { post_auth_action }
313 }
314}
315
316#[derive(Default, Debug, Clone, Serialize, Deserialize)]
318pub struct PasswordRegister {
319 username: Option<String>,
320
321 #[serde(flatten)]
322 post_auth_action: Option<PostAuthAction>,
323}
324
325impl PasswordRegister {
326 #[must_use]
327 pub fn and_then(mut self, action: PostAuthAction) -> Self {
328 self.post_auth_action = Some(action);
329 self
330 }
331
332 #[must_use]
333 pub fn and_continue_grant(mut self, data: Ulid) -> Self {
334 self.post_auth_action = Some(PostAuthAction::continue_grant(data));
335 self
336 }
337
338 #[must_use]
339 pub fn and_continue_compat_sso_login(mut self, data: Ulid) -> Self {
340 self.post_auth_action = Some(PostAuthAction::continue_compat_sso_login(data));
341 self
342 }
343
344 #[must_use]
346 pub fn post_auth_action(&self) -> Option<&PostAuthAction> {
347 self.post_auth_action.as_ref()
348 }
349
350 #[must_use]
352 pub fn username(&self) -> Option<&str> {
353 self.username.as_deref()
354 }
355
356 pub fn go_next(&self, url_builder: &UrlBuilder) -> axum::response::Redirect {
357 match &self.post_auth_action {
358 Some(action) => action.go_next(url_builder),
359 None => url_builder.redirect(&Index),
360 }
361 }
362}
363
364impl Route for PasswordRegister {
365 type Query = Self;
366
367 fn route() -> &'static str {
368 "/register/password"
369 }
370
371 fn query(&self) -> Option<&Self::Query> {
372 Some(self)
373 }
374}
375
376impl From<Option<PostAuthAction>> for PasswordRegister {
377 fn from(post_auth_action: Option<PostAuthAction>) -> Self {
378 Self {
379 username: None,
380 post_auth_action,
381 }
382 }
383}
384
385#[derive(Debug, Clone)]
387pub struct RegisterDisplayName {
388 id: Ulid,
389}
390
391impl RegisterDisplayName {
392 #[must_use]
393 pub fn new(id: Ulid) -> Self {
394 Self { id }
395 }
396}
397
398impl Route for RegisterDisplayName {
399 type Query = ();
400 fn route() -> &'static str {
401 "/register/steps/{id}/display-name"
402 }
403
404 fn path(&self) -> std::borrow::Cow<'static, str> {
405 format!("/register/steps/{}/display-name", self.id).into()
406 }
407}
408
409#[derive(Debug, Clone)]
411pub struct RegisterVerifyEmail {
412 id: Ulid,
413}
414
415impl RegisterVerifyEmail {
416 #[must_use]
417 pub fn new(id: Ulid) -> Self {
418 Self { id }
419 }
420}
421
422impl Route for RegisterVerifyEmail {
423 type Query = ();
424 fn route() -> &'static str {
425 "/register/steps/{id}/verify-email"
426 }
427
428 fn path(&self) -> std::borrow::Cow<'static, str> {
429 format!("/register/steps/{}/verify-email", self.id).into()
430 }
431}
432
433#[derive(Debug, Clone)]
435pub struct RegisterFinish {
436 id: Ulid,
437}
438
439impl RegisterFinish {
440 #[must_use]
441 pub const fn new(id: Ulid) -> Self {
442 Self { id }
443 }
444}
445
446impl Route for RegisterFinish {
447 type Query = ();
448 fn route() -> &'static str {
449 "/register/steps/{id}/finish"
450 }
451
452 fn path(&self) -> std::borrow::Cow<'static, str> {
453 format!("/register/steps/{}/finish", self.id).into()
454 }
455}
456
457#[derive(Debug, Clone, Serialize, Deserialize)]
459#[serde(tag = "action")]
460pub enum AccountAction {
461 #[serde(rename = "org.matrix.profile")]
462 OrgMatrixProfile,
463 #[serde(rename = "profile")]
464 Profile,
465
466 #[serde(rename = "org.matrix.sessions_list")]
467 OrgMatrixSessionsList,
468 #[serde(rename = "sessions_list")]
469 SessionsList,
470
471 #[serde(rename = "org.matrix.session_view")]
472 OrgMatrixSessionView { device_id: String },
473 #[serde(rename = "session_view")]
474 SessionView { device_id: String },
475
476 #[serde(rename = "org.matrix.session_end")]
477 OrgMatrixSessionEnd { device_id: String },
478 #[serde(rename = "session_end")]
479 SessionEnd { device_id: String },
480
481 #[serde(rename = "org.matrix.cross_signing_reset")]
482 OrgMatrixCrossSigningReset,
483}
484
485#[derive(Default, Debug, Clone)]
487pub struct Account {
488 action: Option<AccountAction>,
489}
490
491impl Route for Account {
492 type Query = AccountAction;
493
494 fn route() -> &'static str {
495 "/account/"
496 }
497
498 fn query(&self) -> Option<&Self::Query> {
499 self.action.as_ref()
500 }
501}
502
503#[derive(Default, Debug, Clone)]
505pub struct AccountWildcard;
506
507impl SimpleRoute for AccountWildcard {
508 const PATH: &'static str = "/account/{*rest}";
509}
510
511#[derive(Default, Debug, Clone)]
516pub struct AccountPasswordChange;
517
518impl SimpleRoute for AccountPasswordChange {
519 const PATH: &'static str = "/account/password/change";
520}
521
522#[derive(Debug, Clone)]
524pub struct Consent(pub Ulid);
525
526impl Route for Consent {
527 type Query = ();
528 fn route() -> &'static str {
529 "/consent/{grant_id}"
530 }
531
532 fn path(&self) -> std::borrow::Cow<'static, str> {
533 format!("/consent/{}", self.0).into()
534 }
535}
536
537pub struct CompatLogin;
539
540impl SimpleRoute for CompatLogin {
541 const PATH: &'static str = "/_matrix/client/{version}/login";
542}
543
544pub struct CompatLogout;
546
547impl SimpleRoute for CompatLogout {
548 const PATH: &'static str = "/_matrix/client/{version}/logout";
549}
550
551pub struct CompatLogoutAll;
553
554impl SimpleRoute for CompatLogoutAll {
555 const PATH: &'static str = "/_matrix/client/{version}/logout/all";
556}
557
558pub struct CompatRefresh;
560
561impl SimpleRoute for CompatRefresh {
562 const PATH: &'static str = "/_matrix/client/{version}/refresh";
563}
564
565pub struct CompatLoginSsoRedirect;
567
568impl SimpleRoute for CompatLoginSsoRedirect {
569 const PATH: &'static str = "/_matrix/client/{version}/login/sso/redirect";
570}
571
572pub struct CompatLoginSsoRedirectSlash;
577
578impl SimpleRoute for CompatLoginSsoRedirectSlash {
579 const PATH: &'static str = "/_matrix/client/{version}/login/sso/redirect/";
580}
581
582pub struct CompatLoginSsoRedirectIdp;
584
585impl SimpleRoute for CompatLoginSsoRedirectIdp {
586 const PATH: &'static str = "/_matrix/client/{version}/login/sso/redirect/{idp}";
587}
588
589#[derive(Debug, Serialize, Deserialize, Clone, Copy)]
590#[serde(rename_all = "lowercase")]
591pub enum CompatLoginSsoAction {
592 Login,
593 Register,
594}
595
596#[derive(Debug, Serialize, Deserialize, Clone, Copy)]
597pub struct CompatLoginSsoActionParams {
598 #[serde(rename = "org.matrix.msc3824.action")]
599 action: CompatLoginSsoAction,
600}
601
602pub struct CompatLoginSsoComplete {
604 id: Ulid,
605 query: Option<CompatLoginSsoActionParams>,
606}
607
608impl CompatLoginSsoComplete {
609 #[must_use]
610 pub fn new(id: Ulid, action: Option<CompatLoginSsoAction>) -> Self {
611 Self {
612 id,
613 query: action.map(|action| CompatLoginSsoActionParams { action }),
614 }
615 }
616}
617
618impl Route for CompatLoginSsoComplete {
619 type Query = CompatLoginSsoActionParams;
620
621 fn query(&self) -> Option<&Self::Query> {
622 self.query.as_ref()
623 }
624
625 fn route() -> &'static str {
626 "/complete-compat-sso/{grant_id}"
627 }
628
629 fn path(&self) -> std::borrow::Cow<'static, str> {
630 format!("/complete-compat-sso/{}", self.id).into()
631 }
632}
633
634pub struct UpstreamOAuth2Authorize {
636 id: Ulid,
637 post_auth_action: Option<PostAuthAction>,
638}
639
640impl UpstreamOAuth2Authorize {
641 #[must_use]
642 pub const fn new(id: Ulid) -> Self {
643 Self {
644 id,
645 post_auth_action: None,
646 }
647 }
648
649 #[must_use]
650 pub fn and_then(mut self, action: PostAuthAction) -> Self {
651 self.post_auth_action = Some(action);
652 self
653 }
654}
655
656impl Route for UpstreamOAuth2Authorize {
657 type Query = PostAuthAction;
658 fn route() -> &'static str {
659 "/upstream/authorize/{provider_id}"
660 }
661
662 fn path(&self) -> std::borrow::Cow<'static, str> {
663 format!("/upstream/authorize/{}", self.id).into()
664 }
665
666 fn query(&self) -> Option<&Self::Query> {
667 self.post_auth_action.as_ref()
668 }
669}
670
671pub struct UpstreamOAuth2Callback {
673 id: Ulid,
674}
675
676impl UpstreamOAuth2Callback {
677 #[must_use]
678 pub const fn new(id: Ulid) -> Self {
679 Self { id }
680 }
681}
682
683impl Route for UpstreamOAuth2Callback {
684 type Query = ();
685 fn route() -> &'static str {
686 "/upstream/callback/{provider_id}"
687 }
688
689 fn path(&self) -> std::borrow::Cow<'static, str> {
690 format!("/upstream/callback/{}", self.id).into()
691 }
692}
693
694pub struct UpstreamOAuth2Link {
696 id: Ulid,
697}
698
699impl UpstreamOAuth2Link {
700 #[must_use]
701 pub const fn new(id: Ulid) -> Self {
702 Self { id }
703 }
704}
705
706impl Route for UpstreamOAuth2Link {
707 type Query = ();
708 fn route() -> &'static str {
709 "/upstream/link/{link_id}"
710 }
711
712 fn path(&self) -> std::borrow::Cow<'static, str> {
713 format!("/upstream/link/{}", self.id).into()
714 }
715}
716
717#[derive(Default, Serialize, Deserialize, Debug, Clone)]
719pub struct DeviceCodeLink {
720 code: Option<String>,
721}
722
723impl DeviceCodeLink {
724 #[must_use]
725 pub fn with_code(code: String) -> Self {
726 Self { code: Some(code) }
727 }
728}
729
730impl Route for DeviceCodeLink {
731 type Query = DeviceCodeLink;
732 fn route() -> &'static str {
733 "/link"
734 }
735
736 fn query(&self) -> Option<&Self::Query> {
737 Some(self)
738 }
739}
740
741#[derive(Default, Serialize, Deserialize, Debug, Clone)]
743pub struct DeviceCodeConsent {
744 id: Ulid,
745}
746
747impl Route for DeviceCodeConsent {
748 type Query = ();
749 fn route() -> &'static str {
750 "/device/{device_code_id}"
751 }
752
753 fn path(&self) -> std::borrow::Cow<'static, str> {
754 format!("/device/{}", self.id).into()
755 }
756}
757
758impl DeviceCodeConsent {
759 #[must_use]
760 pub fn new(id: Ulid) -> Self {
761 Self { id }
762 }
763}
764
765#[derive(Default, Serialize, Deserialize, Debug, Clone)]
767pub struct OAuth2DeviceAuthorizationEndpoint;
768
769impl SimpleRoute for OAuth2DeviceAuthorizationEndpoint {
770 const PATH: &'static str = "/oauth2/device";
771}
772
773#[derive(Default, Serialize, Deserialize, Debug, Clone)]
775pub struct AccountRecoveryStart;
776
777impl SimpleRoute for AccountRecoveryStart {
778 const PATH: &'static str = "/recover";
779}
780
781#[derive(Default, Serialize, Deserialize, Debug, Clone)]
783pub struct AccountRecoveryProgress {
784 session_id: Ulid,
785}
786
787impl AccountRecoveryProgress {
788 #[must_use]
789 pub fn new(session_id: Ulid) -> Self {
790 Self { session_id }
791 }
792}
793
794impl Route for AccountRecoveryProgress {
795 type Query = ();
796 fn route() -> &'static str {
797 "/recover/progress/{session_id}"
798 }
799
800 fn path(&self) -> std::borrow::Cow<'static, str> {
801 format!("/recover/progress/{}", self.session_id).into()
802 }
803}
804
805#[derive(Default, Serialize, Deserialize, Debug, Clone)]
808pub struct AccountRecoveryFinish {
809 ticket: String,
810}
811
812impl AccountRecoveryFinish {
813 #[must_use]
814 pub fn new(ticket: String) -> Self {
815 Self { ticket }
816 }
817}
818
819impl Route for AccountRecoveryFinish {
820 type Query = AccountRecoveryFinish;
821
822 fn route() -> &'static str {
823 "/account/password/recovery"
824 }
825
826 fn query(&self) -> Option<&Self::Query> {
827 Some(self)
828 }
829}
830
831pub struct StaticAsset {
833 path: String,
834}
835
836impl StaticAsset {
837 #[must_use]
838 pub fn new(path: String) -> Self {
839 Self { path }
840 }
841}
842
843impl Route for StaticAsset {
844 type Query = ();
845 fn route() -> &'static str {
846 "/assets/"
847 }
848
849 fn path(&self) -> std::borrow::Cow<'static, str> {
850 format!("/assets/{}", self.path).into()
851 }
852}
853
854pub struct GraphQL;
856
857impl SimpleRoute for GraphQL {
858 const PATH: &'static str = "/graphql";
859}
860
861pub struct GraphQLPlayground;
863
864impl SimpleRoute for GraphQLPlayground {
865 const PATH: &'static str = "/graphql/playground";
866}
867
868pub struct ApiSpec;
870
871impl SimpleRoute for ApiSpec {
872 const PATH: &'static str = "/api/spec.json";
873}
874
875pub struct ApiDoc;
877
878impl SimpleRoute for ApiDoc {
879 const PATH: &'static str = "/api/doc/";
880}
881
882pub struct ApiDocCallback;
884
885impl SimpleRoute for ApiDocCallback {
886 const PATH: &'static str = "/api/doc/oauth2-callback";
887}