@@ -32,50 +32,47 @@ where
3232 . cloned ( )
3333 . map ( CanonicalJsonValue :: into)
3434 . map ( serde_json:: from_value)
35- . transpose ( ) ?
36- {
37- | Some ( AuthData :: Jwt ( Jwt { ref token, .. } ) ) => {
35+ . transpose ( ) ? {
36+ Some ( AuthData :: Jwt ( Jwt { ref token, .. } ) ) => {
3837 let sender_user = jwt:: validate_user ( services, token) ?;
3938 if !services. users . exists ( & sender_user) . await {
4039 return Err ! ( Request ( NotFound ( "User {sender_user} is not registered." ) ) ) ;
4140 }
42-
43- // Success!
4441 Ok ( sender_user)
4542 } ,
46- | Some ( ref auth) => {
43+ Some ( ref auth) => {
4744 let sender_user = body
4845 . sender_user
4946 . as_deref ( )
5047 . ok_or_else ( || err ! ( Request ( MissingToken ( "Missing access token." ) ) ) ) ?;
51-
5248 let ( worked, uiaainfo) = services
5349 . uiaa
5450 . try_auth ( sender_user, body. sender_device ( ) , auth, & uiaainfo)
5551 . await ?;
56-
5752 if !worked {
5853 return Err ( Error :: Uiaa ( uiaainfo) ) ;
5954 }
60-
61- // Success!
6255 Ok ( sender_user. to_owned ( ) )
6356 } ,
64- | _ => match body. json_body {
65- | Some ( ref json) => {
66- let sender_user = body
67- . sender_user
68- . as_ref ( )
69- . ok_or_else ( || err ! ( Request ( MissingToken ( "Missing access token." ) ) ) ) ?;
70-
71- uiaainfo. session = Some ( utils:: random_string ( SESSION_ID_LENGTH ) ) ;
72- services
73- . uiaa
74- . create ( sender_user, body. sender_device ( ) , & uiaainfo, json) ;
75-
76- Err ( Error :: Uiaa ( uiaainfo) )
77- } ,
78- | _ => Err ! ( Request ( NotJson ( "JSON body is not valid" ) ) ) ,
79- } ,
57+ _ => {
58+ // 如果已经通过 access token 认证(即 sender_user 存在),且没有 auth 字段,则直接通过
59+ if let Some ( sender_user) = body. sender_user . as_ref ( ) {
60+ return Ok ( sender_user. to_owned ( ) ) ;
61+ }
62+ match body. json_body {
63+ Some ( ref json) => {
64+ let sender_user = body
65+ . sender_user
66+ . as_ref ( )
67+ . ok_or_else ( || err ! ( Request ( MissingToken ( "Missing access token." ) ) ) ) ?;
68+ uiaainfo. session = Some ( utils:: random_string ( SESSION_ID_LENGTH ) ) ;
69+ services
70+ . uiaa
71+ . create ( sender_user, body. sender_device ( ) , & uiaainfo, json) ;
72+ Err ( Error :: Uiaa ( uiaainfo) )
73+ } ,
74+ _ => Err ! ( Request ( NotJson ( "JSON body is not valid" ) ) ) ,
75+ }
76+ }
8077 }
8178}
0 commit comments