safe_authenticator 0.18.1

SAFE Authenticator
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// Copyright 2018 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

#![allow(unsafe_code)]

mod revocation;
mod serialisation;
mod utils;

use crate::{
    access_container as access_container_tools,
    config::KEY_APPS,
    errors::AuthError,
    std_dirs::{DEFAULT_PRIVATE_DIRS, DEFAULT_PUBLIC_DIRS},
    test_utils::{self},
};
use safe_core::{client::Client, mdata_info};
use unwrap::unwrap;

#[cfg(feature = "mock-network")]
mod mock_routing {
    use super::utils;
    use crate::access_container as access_container_tools;
    use crate::errors::AuthError;
    use crate::std_dirs::{DEFAULT_PRIVATE_DIRS, DEFAULT_PUBLIC_DIRS};
    use crate::{test_utils, Authenticator};
    use safe_core::ipc::AuthReq;
    use safe_core::utils::generate_random_string;
    use safe_core::utils::test_utils::gen_client_id;
    use safe_core::{app_container_name, test_create_balance, ConnectionManager, CoreError};
    use safe_nd::{
        ClientRequest, Coins, Error as SndError, MDataRequest, Request, RequestType, Response,
    };
    use std::str::FromStr;
    use unwrap::unwrap;

    // Test operation recovery for std dirs creation.
    // 1. Try to create a new user's account using `safe_authenticator::Authenticator::create_acc`
    // 2. Simulate a network disconnection [1] for a randomly selected `PutMData` operation
    //    with a type_tag == `safe_core::DIR_TAG` (in the range from 3rd request to
    //    `safe_core::nfs::DEFAULT_PRIVATE_DIRS.len()`). This will meddle with creation of
    //    default directories.
    // 3. Try to log in using the same credentials that have been provided for `create_acc`.
    //    The log in operation should be successful.
    // 4. Check that after logging in the remaining default directories have been created
    //    (= operation recovery worked after log in)
    // 5. Check the access container entry in the user's config root - it must be accessible
    #[tokio::test]
    async fn std_dirs_recovery() -> Result<(), AuthError> {
        // Add a request hook to forbid root dir modification. In this case
        // account creation operation will be failed, but login still should
        // be possible afterwards.
        let locator = generate_random_string(10)?;
        let password = generate_random_string(10)?;
        let client_id = gen_client_id();

        test_create_balance(&client_id, Coins::from_str("10")?).await?;

        {
            let cm_hook = move |mut cm: ConnectionManager| -> ConnectionManager {
                let mut put_mdata_counter = 0;

                cm.set_request_hook(move |req| {
                    match req {
                        Request::MData(MDataRequest::Put(data))
                            if data.tag() == safe_core::DIR_TAG =>
                        {
                            put_mdata_counter += 1;

                            if put_mdata_counter > 4 {
                                Some(Response::Mutation(Err(SndError::InsufficientBalance)))
                            } else {
                                None
                            }
                        }
                        // Pass-through
                        _ => None,
                    }
                });
                cm
            };

            let authenticator = Authenticator::create_acc_with_hook(
                locator.clone(),
                password.clone(),
                client_id,
                || (),
                cm_hook,
            )
            .await;

            // This operation should fail
            match authenticator {
                Err(AuthError::AccountContainersCreation(_)) => (),
                Err(x) => panic!("Unexpected error {:?}", x),
                Ok(_) => panic!("Unexpected success"),
            }
        }

        // Log in using the same credentials
        let authenticator = Authenticator::login(locator, password, || ()).await?;
        let client = authenticator.client;

        // Make sure that all default directories have been created after log in.
        let std_dir_names: Vec<_> = DEFAULT_PRIVATE_DIRS
            .iter()
            .cloned()
            .chain(DEFAULT_PUBLIC_DIRS.iter().cloned())
            .collect();

        // Verify that the access container has been created and
        // fetch the entries of the root authenticator entry.
        let (_entry_version, entries) =
            access_container_tools::fetch_authenticator_entry(&client).await?;

        // Verify that all the std dirs are there.
        for name in std_dir_names {
            assert!(entries.contains_key(name));
        }

        Ok(())
    }

    // Ensure that users can log in with low account balance.
    #[test]
    fn login_with_low_balance() {
        // Register a hook prohibiting mutations and login
        let cm_hook = move |mut cm: ConnectionManager| -> ConnectionManager {
            cm.set_request_hook(move |req| {
                if req.get_type() == RequestType::Mutation {
                    Some(Response::Mutation(Err(SndError::InsufficientBalance)))
                } else {
                    // Pass-through
                    None
                }
            });
            cm
        };

        // Make sure we can log in
        let _authenticator = test_utils::create_account_and_login_with_hook(cm_hook);
    }

    // Test operation recovery for app authentication.
    //
    // 1. Create a test app and try to authenticate it (with `app_container` set to true).
    //
    // 2. Simulate a network failure after the `mutate_mdata_entries` operation (relating to the
    //    addition of the app to the user's config dir) - it should leave the app in the
    //    `Revoked` state (as it is listen in the config root, but not in the access
    //    container)
    // 3. Try to authenticate the app again, it should continue without errors
    //
    // 4. Simulate a network failure after the `ins_auth_key` operation.
    //    The authentication op should fail.
    // 5. Try to authenticate the app again, it should continue without errors
    //
    // 6. Simulate a network failure for the `set_mdata_user_permissions` operation
    //    (relating to the app's container - so that it will be created successfuly, but fail
    //    at the permissions set stage).
    // 7. Try to authenticate the app again, it should continue without errors.
    //
    // 8. Simulate a network failure for the `mutate_mdata_entries` operation
    //    (relating to update of the access container).
    // 9. Try to authenticate the app again, it should succeed now.
    //
    // 10. Check that the app's container has been created.
    // 11. Check that the app's container has required permissions.
    // 12. Check that the app's container is listed in the access container entry for
    //     the app.
    #[tokio::test]
    async fn app_authentication_recovery() {
        use safe_core::client::Client;
        let locator = generate_random_string(10).unwrap();
        let password = generate_random_string(10).unwrap();
        let client_id = gen_client_id();

        test_create_balance(&client_id, Coins::from_str("10").unwrap())
            .await
            .unwrap();

        let cm_hook = move |mut cm: ConnectionManager| -> ConnectionManager {
            cm.set_request_hook(move |req| {
                match *req {
                    // Simulate a network failure after
                    // the `mutate_mdata_entries` operation (relating to
                    // the addition of the app to the user's config dir)
                    Request::Client(ClientRequest::InsAuthKey { .. }) => {
                        Some(Response::Mutation(Err(SndError::InsufficientBalance)))
                    }
                    // Pass-through
                    _ => None,
                }
            });
            cm
        };
        let auth = Authenticator::create_acc_with_hook(
            locator.clone(),
            password.clone(),
            client_id,
            || (),
            cm_hook,
        )
        .await
        .unwrap();

        // Create a test app and try to authenticate it (with `app_container` set to true).
        let auth_req = AuthReq {
            app: test_utils::rand_app(),
            app_container: true,
            app_permissions: Default::default(),
            containers: utils::create_containers_req(),
        };
        let app_id = auth_req.app.id.clone();

        // App authentication request should fail and leave the app in the
        // `Revoked` state (as it is listed in the config root, but not in the access
        // container)
        match test_utils::register_app(&auth, &auth_req).await {
            Err(AuthError::CoreError(CoreError::DataError(SndError::InsufficientBalance))) => (),
            x => panic!("Unexpected {:?}", x),
        }

        // Simulate a network failure for the `update_container_perms` step -
        // it should fail at the second container (`_videos`)
        let cm_hook = move |mut cm: ConnectionManager| -> ConnectionManager {
            let mut reqs_counter = 0;

            cm.set_request_hook(move |req| {
                match *req {
                    Request::MData(MDataRequest::SetUserPermissions { .. }) => {
                        reqs_counter += 1;

                        if reqs_counter == 2 {
                            Some(Response::Mutation(Err(SndError::InsufficientBalance)))
                        } else {
                            None
                        }
                    }
                    // Pass-through
                    _ => None,
                }
            });
            cm
        };
        let auth =
            Authenticator::login_with_hook(locator.clone(), password.clone(), || (), cm_hook)
                .await
                .unwrap();
        match test_utils::register_app(&auth, &auth_req).await {
            Err(AuthError::CoreError(CoreError::DataError(SndError::InsufficientBalance))) => (),
            x => panic!("Unexpected {:?}", x),
        }

        // Simulate a network failure for the `app_container` setup step -
        // it should fail at the third request for `SetMDataPermissions` (after
        // setting permissions for 2 requested containers, `_video` and `_documents`)
        let cm_hook = move |mut cm: ConnectionManager| -> ConnectionManager {
            cm.set_request_hook(move |req| {
                match *req {
                    Request::MData(MDataRequest::Put { .. }) => {
                        Some(Response::Mutation(Err(SndError::InsufficientBalance)))
                    }

                    // Pass-through
                    _ => None,
                }
            });
            cm
        };
        let auth =
            Authenticator::login_with_hook(locator.clone(), password.clone(), || (), cm_hook)
                .await
                .unwrap();

        match test_utils::register_app(&auth, &auth_req).await {
            Err(AuthError::CoreError(CoreError::DataError(SndError::InsufficientBalance))) => (),
            x => panic!("Unexpected {:?}", x),
        }

        // Simulate a network failure for the `MutateMDataEntries` request, which
        // is supposed to setup the access container entry for the app
        let cm_hook = move |mut cm: ConnectionManager| -> ConnectionManager {
            cm.set_request_hook(move |req| {
                match *req {
                    Request::MData(MDataRequest::MutateEntries { .. }) => {
                        Some(Response::Mutation(Err(SndError::InsufficientBalance)))
                    }

                    // Pass-through
                    _ => None,
                }
            });
            cm
        };
        let auth =
            Authenticator::login_with_hook(locator.clone(), password.clone(), || (), cm_hook)
                .await
                .unwrap();
        match test_utils::register_app(&auth, &auth_req).await {
            Err(AuthError::CoreError(CoreError::DataError(SndError::InsufficientBalance))) => (),
            x => panic!("Unexpected {:?}", x),
        }

        // Now try to authenticate the app without network failure simulation -
        // it should succeed.
        let auth = Authenticator::login(locator, password, || ())
            .await
            .unwrap();
        let auth_granted = match test_utils::register_app(&auth, &auth_req).await {
            Ok(auth_granted) => auth_granted,
            x => panic!("Unexpected {:?}", x),
        };

        // Check that the app's container has been created and that the access container
        // contains info about all of the requested containers.
        let mut ac_entries =
            test_utils::access_container(&auth, app_id.clone(), auth_granted.clone())
                .await
                .unwrap();
        let (_videos_md, _) = unwrap!(ac_entries.remove("_videos"));
        let (_documents_md, _) = unwrap!(ac_entries.remove("_documents"));
        let (app_container, _) = unwrap!(ac_entries.remove(&app_container_name(&app_id)));

        let app_pk = auth_granted.app_keys.public_key();
        let client = auth.client;
        let version = client
            .get_mdata_version(*app_container.address())
            .await
            .unwrap();
        assert_eq!(version, 0);

        // Check that the app's container has required permissions.
        let perms = client
            .list_mdata_permissions(*app_container.address())
            .await
            .unwrap();
        assert!(perms.contains_key(&app_pk));
        assert_eq!(perms.len(), 1);
    }
}

// Test creation and content of std dirs after account creation.
#[tokio::test]
async fn test_access_container() -> Result<(), AuthError> {
    let authenticator = test_utils::create_account_and_login().await;
    let client = authenticator.client;

    let std_dir_names: Vec<_> = DEFAULT_PRIVATE_DIRS
        .iter()
        .chain(DEFAULT_PUBLIC_DIRS.iter())
        .collect();

    // Fetch the entries of the access container.
    let (_version, entries) = access_container_tools::fetch_authenticator_entry(&client).await?;

    // Verify that all the std dirs are there.
    for name in &std_dir_names {
        assert!(entries.contains_key(**name));
    }

    // Fetch all the dirs under user root dir and verify they are empty.
    let mut dirs = vec![];
    for (_, dir) in entries.into_iter() {
        let entries = client
            .list_seq_mdata_entries(dir.name(), dir.type_tag())
            .await?;
        let perms = client.list_mdata_permissions(*dir.address()).await?;
        dirs.push((entries, perms));
    }
    assert_eq!(dirs.len(), std_dir_names.len());

    for (entries, permissions) in dirs {
        assert!(entries.is_empty());
        assert!(permissions.is_empty());
    }

    Ok(())
}

// Test creation and content of config dir after account creation.
#[tokio::test]
async fn config_root_dir() -> Result<(), AuthError> {
    let authenticator = test_utils::create_account_and_login().await;
    let client = authenticator.client;

    // Fetch the entries of the config root dir.
    let dir = client.config_root_dir().await;
    let entries = client
        .list_seq_mdata_entries(dir.name(), dir.type_tag())
        .await?;

    let entries = unwrap!(mdata_info::decrypt_entries(&dir, &entries));

    // Verify it contains the required entries.
    let config = unwrap!(entries.get(KEY_APPS));
    assert!(config.data.is_empty());
    Ok(())
}