@@ -11,7 +11,6 @@ use super::{
1111 scoped_keys:: ScopedKeysFlow ,
1212 util, FirefoxAccount ,
1313} ;
14- use crate :: auth:: UserData ;
1514use crate :: {
1615 debug, error, info, warn, AuthorizationParameters , Error , FxaServer , Result , ScopedKey ,
1716} ;
@@ -125,12 +124,27 @@ impl FirefoxAccount {
125124 Ok ( token_info)
126125 }
127126
128- /// Sets the user data (session token, email, uid)
129- pub fn set_user_data ( & mut self , user_data : UserData ) {
130- // for now, we only have use for the session token
131- // if we'd like to implement a "Signed in but not verified" state
132- // we would also consume the other parts of the user data
133- self . state . set_session_token ( user_data. session_token )
127+ /// Extracts and stores the session token from a WebChannel login JSON payload.
128+ /// The JSON payload is the `data` object from the `fxaccounts:login` WebChannel command.
129+ pub fn handle_web_channel_login ( & mut self , json_payload : & str ) -> Result < ( ) > {
130+ let data: serde_json:: Value = serde_json:: from_str ( json_payload) ?;
131+ let token = data
132+ . get ( "sessionToken" )
133+ . and_then ( |v| v. as_str ( ) )
134+ . ok_or ( Error :: NoSessionToken ) ?;
135+ self . state . set_session_token ( token. to_string ( ) ) ;
136+ Ok ( ( ) )
137+ }
138+
139+ /// Extracts the session token from a WebChannel password change JSON payload and exchanges it
140+ /// for a new refresh token via a network call.
141+ pub fn handle_web_channel_password_change ( & mut self , json_payload : & str ) -> Result < ( ) > {
142+ let data: serde_json:: Value = serde_json:: from_str ( json_payload) ?;
143+ let token = data
144+ . get ( "sessionToken" )
145+ . and_then ( |v| v. as_str ( ) )
146+ . ok_or ( Error :: NoSessionToken ) ?;
147+ self . handle_session_token_change ( token)
134148 }
135149
136150 /// Retrieve the current session token from state
@@ -141,6 +155,26 @@ impl FirefoxAccount {
141155 }
142156 }
143157
158+ /// Builds a complete `signedInUser` JSON object for a WebChannel `fxaccounts:fxa_status`
159+ /// response. Returns `None` if no session token is stored.
160+ /// `email` and `uid` are read from the cached profile; `verified` is always true because
161+ /// the account state machine only completes authentication for verified accounts.
162+ pub fn get_signed_in_user_for_web_channel ( & self ) -> Option < String > {
163+ let token = self . state . session_token ( ) ?;
164+ let profile = self . state . last_seen_profile ( ) ;
165+ let email = profile. map ( |p| p. response . email . as_str ( ) ) ;
166+ let uid = profile. map ( |p| p. response . uid . as_str ( ) ) ;
167+ Some (
168+ serde_json:: json!( {
169+ "sessionToken" : token,
170+ "email" : email,
171+ "uid" : uid,
172+ "verified" : true ,
173+ } )
174+ . to_string ( ) ,
175+ )
176+ }
177+
144178 /// Check whether user is authorized using our refresh token.
145179 pub fn check_authorization_status ( & mut self ) -> Result < IntrospectInfo > {
146180 let resp = match self . state . refresh_token ( ) {
@@ -1198,17 +1232,14 @@ mod tests {
11981232 }
11991233
12001234 #[ test]
1201- fn test_set_user_data_sets_session_token ( ) {
1235+ fn test_handle_web_channel_login_sets_session_token ( ) {
12021236 nss:: ensure_initialized ( ) ;
12031237 let config = Config :: stable_dev ( "12345678" , "https://foo.bar" ) ;
12041238 let mut fxa = FirefoxAccount :: with_config ( config) ;
1205- let user_data = UserData {
1206- session_token : String :: from ( "mock_session_token" ) ,
1207- uid : String :: from ( "mock_uid_unused" ) ,
1208- email : String :: from ( "mock_email_usued" ) ,
1209- verified : true ,
1210- } ;
1211- fxa. set_user_data ( user_data) ;
1239+ fxa. handle_web_channel_login (
1240+ r#"{"sessionToken":"mock_session_token","uid":"mock_uid","email":"mock@example.com","verified":true}"# ,
1241+ )
1242+ . unwrap ( ) ;
12121243 assert_eq ! ( fxa. get_session_token( ) . unwrap( ) , "mock_session_token" ) ;
12131244 }
12141245
@@ -1226,12 +1257,6 @@ mod tests {
12261257 . unwrap ( ) ;
12271258 let url = Url :: parse ( & url) . unwrap ( ) ;
12281259 let state = url. query_pairs ( ) . find ( |( name, _) | name == "state" ) . unwrap ( ) ;
1229- let user_data = UserData {
1230- session_token : String :: from ( "mock_session_token" ) ,
1231- uid : String :: from ( "mock_uid_unused" ) ,
1232- email : String :: from ( "mock_email_usued" ) ,
1233- verified : true ,
1234- } ;
12351260 let mut client = MockFxAClient :: new ( ) ;
12361261
12371262 client
@@ -1256,8 +1281,7 @@ mod tests {
12561281 . times ( 1 )
12571282 . returning ( |_, _| Ok ( ( ) ) ) ;
12581283 fxa. set_client ( Arc :: new ( client) ) ;
1259-
1260- fxa. set_user_data ( user_data) ;
1284+ fxa. set_session_token ( "mock_session_token" ) ;
12611285
12621286 fxa. complete_oauth_flow ( "mock_code" , state. 1 . as_ref ( ) )
12631287 . unwrap ( ) ;
0 commit comments