add AuthScheme AccessTokenOptional in ruma_wrapper

Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
strawberry 2024-03-08 00:33:24 -05:00 committed by June
parent 5a15dab7a9
commit 4dfd5a7c15

View file

@ -83,7 +83,7 @@ where
appservice_registration appservice_registration
{ {
match metadata.authentication { match metadata.authentication {
AuthScheme::AccessToken => { AuthScheme::AccessToken | AuthScheme::AccessTokenOptional => {
let user_id = query_params.user_id.map_or_else( let user_id = query_params.user_id.map_or_else(
|| { || {
UserId::parse_with_server_name( UserId::parse_with_server_name(
@ -95,7 +95,7 @@ where
|s| UserId::parse(s).unwrap(), |s| UserId::parse(s).unwrap(),
); );
if !services().users.exists(&user_id).unwrap() { if !services().users.exists(&user_id)? {
return Err(Error::BadRequest(ErrorKind::Forbidden, "User does not exist.")); return Err(Error::BadRequest(ErrorKind::Forbidden, "User does not exist."));
} }
@ -113,7 +113,7 @@ where
_ => return Err(Error::BadRequest(ErrorKind::MissingToken, "Missing access token.")), _ => return Err(Error::BadRequest(ErrorKind::MissingToken, "Missing access token.")),
}; };
match services().users.find_from_token(token).unwrap() { match services().users.find_from_token(token)? {
None => { None => {
return Err(Error::BadRequest( return Err(Error::BadRequest(
ErrorKind::UnknownToken { ErrorKind::UnknownToken {
@ -127,6 +127,27 @@ where
}, },
} }
}, },
AuthScheme::AccessTokenOptional => {
let token = token.unwrap_or("");
if token.is_empty() {
(None, None, None, false)
} else {
match services().users.find_from_token(token)? {
None => {
return Err(Error::BadRequest(
ErrorKind::UnknownToken {
soft_logout: false,
},
"Unknown access token.",
))
},
Some((user_id, device_id)) => {
(Some(user_id), Some(OwnedDeviceId::from(device_id)), None, false)
},
}
}
},
AuthScheme::ServerSignatures => { AuthScheme::ServerSignatures => {
let TypedHeader(Authorization(x_matrix)) = let TypedHeader(Authorization(x_matrix)) =
parts.extract::<TypedHeader<Authorization<XMatrix>>>().await.map_err(|e| { parts.extract::<TypedHeader<Authorization<XMatrix>>>().await.map_err(|e| {
@ -219,7 +240,7 @@ where
_ => return Err(Error::BadRequest(ErrorKind::MissingToken, "Missing access token.")), _ => return Err(Error::BadRequest(ErrorKind::MissingToken, "Missing access token.")),
}; };
match services().users.find_from_token(token).unwrap() { match services().users.find_from_token(token)? {
None => { None => {
return Err(Error::BadRequest( return Err(Error::BadRequest(
ErrorKind::UnknownToken { ErrorKind::UnknownToken {