api: Add support for optional authentication

This commit is contained in:
Kévin Commaille 2024-03-07 11:42:12 +01:00 committed by Kévin Commaille
parent a8f1440698
commit f652cbb60f
3 changed files with 12 additions and 0 deletions

View File

@ -9,6 +9,7 @@ Breaking changes:
If the field is missing, push rules that depend on it will never match. However, this allows to If the field is missing, push rules that depend on it will never match. However, this allows to
match the `.m.rule.invite_for_me` push rule because usually the `invite_state` doesn't include match the `.m.rule.invite_for_me` push rule because usually the `invite_state` doesn't include
`m.room.power_levels`. `m.room.power_levels`.
- Add support for endpoints that take an optional authentication
Improvements: Improvements:

View File

@ -484,6 +484,12 @@ pub enum AuthScheme {
/// It is recommended to use the header over the query parameter. /// It is recommended to use the header over the query parameter.
AccessToken, AccessToken,
/// Authentication is optional, and it is performed by including an access token in the
/// `Authentication` http header, or an `access_token` query parameter.
///
/// It is recommended to use the header over the query parameter.
AccessTokenOptional,
/// Authentication is performed by including X-Matrix signatures in the request headers, /// Authentication is performed by including X-Matrix signatures in the request headers,
/// as defined in the federation API. /// as defined in the federation API.
ServerSignatures, ServerSignatures,

View File

@ -74,6 +74,11 @@ impl Metadata {
Some((header::AUTHORIZATION, format!("Bearer {token}").try_into()?)) Some((header::AUTHORIZATION, format!("Bearer {token}").try_into()?))
} }
AuthScheme::AccessTokenOptional => match access_token.get_required_for_endpoint() {
Some(token) => Some((header::AUTHORIZATION, format!("Bearer {token}").try_into()?)),
None => None,
},
AuthScheme::ServerSignatures => None, AuthScheme::ServerSignatures => None,
}) })
} }