fxa_client/internal/
state_manager.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::collections::{HashMap, HashSet};

use crate::{
    internal::{
        oauth::{AccessTokenInfo, RefreshToken},
        profile::Profile,
        state_persistence::state_to_json,
        CachedResponse, Config, OAuthFlow, PersistedState,
    },
    DeviceCapability, FxaRustAuthState, LocalDevice, Result, ScopedKey,
};

/// Stores and manages the current state of the FxA client
///
/// All fields are private, which means that all state mutations must go through this module.  This
/// makes it easier to reason about state changes.
pub struct StateManager {
    /// State that's persisted to disk
    persisted_state: PersistedState,
    /// In-progress OAuth flows
    flow_store: HashMap<String, OAuthFlow>,
}

impl StateManager {
    pub(crate) fn new(persisted_state: PersistedState) -> Self {
        Self {
            persisted_state,
            flow_store: HashMap::new(),
        }
    }

    pub fn serialize_persisted_state(&self) -> Result<String> {
        state_to_json(&self.persisted_state)
    }

    pub fn config(&self) -> &Config {
        &self.persisted_state.config
    }

    pub fn refresh_token(&self) -> Option<&RefreshToken> {
        self.persisted_state.refresh_token.as_ref()
    }

    pub fn session_token(&self) -> Option<&str> {
        self.persisted_state.session_token.as_deref()
    }

    /// Get our device capabilities
    ///
    /// This is the last set of capabilities passed to `initialize_device` or `ensure_capabilities`
    pub fn device_capabilities(&self) -> &HashSet<DeviceCapability> {
        &self.persisted_state.device_capabilities
    }

    /// Set our device capabilities
    pub fn set_device_capabilities(
        &mut self,
        capabilities_set: impl IntoIterator<Item = DeviceCapability>,
    ) {
        self.persisted_state.device_capabilities = HashSet::from_iter(capabilities_set);
    }

    /// Get the last known LocalDevice info sent back from the server
    pub fn server_local_device_info(&self) -> Option<&LocalDevice> {
        self.persisted_state.server_local_device_info.as_ref()
    }

    /// Update the last known LocalDevice info when getting one back from the server
    pub fn update_server_local_device_info(&mut self, local_device: LocalDevice) {
        self.persisted_state.server_local_device_info = Some(local_device)
    }

    /// Clear out the last known LocalDevice info. This means that the next call to
    /// `ensure_capabilities()` will re-send our capabilities to the server
    ///
    /// This is typically called when something may invalidate the server's knowledge of our
    /// local device capabilities, for example replacing our device info.
    pub fn clear_server_local_device_info(&mut self) {
        self.persisted_state.server_local_device_info = None
    }

    pub fn get_commands_data(&self, key: &str) -> Option<&str> {
        self.persisted_state
            .commands_data
            .get(key)
            .map(String::as_str)
    }

    pub fn set_commands_data(&mut self, key: &str, data: String) {
        self.persisted_state
            .commands_data
            .insert(key.to_string(), data);
    }

    pub fn clear_commands_data(&mut self, key: &str) {
        self.persisted_state.commands_data.remove(key);
    }

    pub fn last_handled_command_index(&self) -> Option<u64> {
        self.persisted_state.last_handled_command
    }

    pub fn set_last_handled_command_index(&mut self, idx: u64) {
        self.persisted_state.last_handled_command = Some(idx)
    }

    pub fn current_device_id(&self) -> Option<&str> {
        self.persisted_state.current_device_id.as_deref()
    }

    pub fn set_current_device_id(&mut self, device_id: String) {
        self.persisted_state.current_device_id = Some(device_id);
    }

    pub fn get_scoped_key(&self, scope: &str) -> Option<&ScopedKey> {
        self.persisted_state.scoped_keys.get(scope)
    }

    pub(crate) fn last_seen_profile(&self) -> Option<&CachedResponse<Profile>> {
        self.persisted_state.last_seen_profile.as_ref()
    }

    pub(crate) fn set_last_seen_profile(&mut self, profile: CachedResponse<Profile>) {
        self.persisted_state.last_seen_profile = Some(profile)
    }

    pub fn clear_last_seen_profile(&mut self) {
        self.persisted_state.last_seen_profile = None
    }

    pub fn get_cached_access_token(&mut self, scope: &str) -> Option<&AccessTokenInfo> {
        self.persisted_state.access_token_cache.get(scope)
    }

    pub fn add_cached_access_token(&mut self, scope: impl Into<String>, token: AccessTokenInfo) {
        self.persisted_state
            .access_token_cache
            .insert(scope.into(), token);
    }

    pub fn clear_access_token_cache(&mut self) {
        self.persisted_state.access_token_cache.clear()
    }

    /// Begin an OAuth flow.  This saves the OAuthFlow for later.  `state` must be unique to this
    /// oauth flow process.
    pub fn begin_oauth_flow(&mut self, state: impl Into<String>, flow: OAuthFlow) {
        self.flow_store.insert(state.into(), flow);
    }

    /// Get an OAuthFlow from a previous `begin_oauth_flow()` call
    ///
    /// This operation removes the OAuthFlow from the our internal map.  It can only be called once
    /// per `state` value.
    pub fn pop_oauth_flow(&mut self, state: &str) -> Option<OAuthFlow> {
        self.flow_store.remove(state)
    }

    /// Complete an OAuth flow.
    pub fn complete_oauth_flow(
        &mut self,
        scoped_keys: Vec<(String, ScopedKey)>,
        refresh_token: RefreshToken,
        new_session_token: Option<String>,
    ) {
        // When our keys change, we might need to re-register device capabilities with the server.
        // Ensure that this happens on the next call to ensure_capabilities.
        self.clear_server_local_device_info();

        for (scope, key) in scoped_keys {
            self.persisted_state.scoped_keys.insert(scope, key);
        }
        self.persisted_state.refresh_token = Some(refresh_token);
        // We prioritize the existing session token if we already have one, because we might have
        // acquired a session token before the oauth flow
        if let (None, Some(new_session_token)) = (self.session_token(), new_session_token) {
            self.set_session_token(new_session_token)
        }
        self.persisted_state.logged_out_from_auth_issues = false;
        self.flow_store.clear();
    }

    /// Called when the account is disconnected.  This clears most of the auth state, but keeps
    /// some information in order to eventually reconnect to the same user account later.
    pub fn disconnect(&mut self) {
        self.persisted_state.current_device_id = None;
        self.persisted_state.refresh_token = None;
        self.persisted_state.scoped_keys = HashMap::new();
        self.persisted_state.last_handled_command = None;
        self.persisted_state.commands_data = HashMap::new();
        self.persisted_state.access_token_cache = HashMap::new();
        self.persisted_state.device_capabilities = HashSet::new();
        self.persisted_state.server_local_device_info = None;
        self.persisted_state.session_token = None;
        self.persisted_state.logged_out_from_auth_issues = false;
        self.flow_store.clear();
    }

    /// Called when we notice authentication issues with the account state.
    ///
    /// This clears the auth state, but leaves some fields untouched. That way, if the user
    /// re-authenticates they can continue using the account without unexpected behavior.  The
    /// fields that don't change compared to `disconnect()` are:
    ///
    ///   * `current_device_id`
    ///   * `device_capabilities`
    ///   * `last_handled_command`
    pub fn on_auth_issues(&mut self) {
        self.persisted_state.refresh_token = None;
        self.persisted_state.scoped_keys = HashMap::new();
        self.persisted_state.commands_data = HashMap::new();
        self.persisted_state.access_token_cache = HashMap::new();
        self.persisted_state.server_local_device_info = None;
        self.persisted_state.session_token = None;
        self.persisted_state.logged_out_from_auth_issues = true;
        self.flow_store.clear();
    }

    /// Called when we begin an OAuth flow.
    ///
    /// This clears out tokens/keys set from the previous time we completed an oauth flow.  In
    /// particular, it clears the session token to avoid
    /// https://bugzilla.mozilla.org/show_bug.cgi?id=1887071.
    pub fn on_begin_oauth(&mut self) {
        self.persisted_state.refresh_token = None;
        self.persisted_state.scoped_keys = HashMap::new();
        self.persisted_state.commands_data = HashMap::new();
        self.persisted_state.access_token_cache = HashMap::new();
        self.persisted_state.session_token = None;
    }

    pub fn get_auth_state(&self) -> FxaRustAuthState {
        if self.persisted_state.refresh_token.is_some() {
            FxaRustAuthState::Connected
        } else if self.persisted_state.logged_out_from_auth_issues {
            FxaRustAuthState::AuthIssues
        } else {
            FxaRustAuthState::Disconnected
        }
    }

    /// Handle the auth tokens changing
    ///
    /// This method updates the token data and clears out data that may be invalidated with the
    /// token changes.
    pub fn update_tokens(&mut self, session_token: String, refresh_token: RefreshToken) {
        self.persisted_state.session_token = Some(session_token);
        self.persisted_state.refresh_token = Some(refresh_token);
        self.persisted_state.access_token_cache.clear();
        self.persisted_state.server_local_device_info = None;
    }

    /// Used by the application to test auth token issues
    pub fn simulate_temporary_auth_token_issue(&mut self) {
        for (_, access_token) in self.persisted_state.access_token_cache.iter_mut() {
            "invalid-data".clone_into(&mut access_token.token)
        }
    }

    /// Used by the application to test auth token issues
    pub fn simulate_permanent_auth_token_issue(&mut self) {
        self.persisted_state.session_token = None;
        self.persisted_state.refresh_token = None;
        self.persisted_state.access_token_cache.clear();
    }
    pub fn set_session_token(&mut self, token: String) {
        self.persisted_state.session_token = Some(token)
    }
}

#[cfg(test)]
impl StateManager {
    pub fn is_access_token_cache_empty(&self) -> bool {
        self.persisted_state.access_token_cache.is_empty()
    }

    pub fn force_refresh_token(&mut self, token: RefreshToken) {
        self.persisted_state.refresh_token = Some(token)
    }

    pub fn force_current_device_id(&mut self, device_id: impl Into<String>) {
        self.persisted_state.current_device_id = Some(device_id.into())
    }

    pub fn insert_scoped_key(&mut self, scope: impl Into<String>, key: ScopedKey) {
        self.persisted_state.scoped_keys.insert(scope.into(), key);
    }
}