cbwaw 0.5.51

Auth for Ordinary
Documentation
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
405
406
407
408
409
410
411
412
use std::{
    error::Error,
    path::Path,
    sync::Arc,
    time::{Duration, SystemTime},
};

use bytes::{BufMut, BytesMut};
use cbwaw::{Auth, AuthClient, OsRng};
use ed25519_dalek::{SigningKey, ed25519::signature::SignerMut};
use ordinary_config::{AuthConfig, InviteConfig, InviteMode};
use saferlmdb::EnvBuilder;
use sha2::{Digest, Sha256};

#[test]
fn integration() -> Result<(), Box<dyn Error>> {
    let store_dir = Path::new(".ordinary").join("store");

    if std::fs::read_dir(&store_dir).is_ok() {
        std::fs::remove_dir_all(&store_dir)?;
    }

    std::fs::create_dir_all(&store_dir)?;

    let env = Arc::new(unsafe {
        let mut env_builder = EnvBuilder::new()?;
        env_builder.set_maxreaders(126)?;
        env_builder.set_mapsize(16384 * 64 * 10)?;
        env_builder.set_maxdbs(13)?;
        env_builder.open(
            store_dir.to_str().expect("store dir is not str"),
            &saferlmdb::open::Flags::empty(),
            0o600,
        )?
    });

    let auth = Arc::new(Auth::new(
        "example.com".into(),
        None,
        [0u8; 32],
        env.clone(),
    )?);

    // !! register

    // registration start
    let (state, req) = AuthClient::registration_start_req(b"account", b"password", None)?;
    let res = auth.registration_start(req, None, None)?;

    // registration finish
    let (private_key, req) =
        AuthClient::registration_finish_req(b"account", b"password", &state, &res)?;
    let (res, ..) = auth.registration_finish(req, None)?;

    let (mfa_code, recovery_codes) = AuthClient::decrypt_totp_mfa_code(
        &res,
        private_key,
        "example.com".into(),
        "account".into(),
    )?;

    let mut recovery_code1 = String::new();
    let mut recovery_code2 = String::new();

    for (i, c) in recovery_codes.chars().enumerate() {
        if i < 11 {
            recovery_code1 = format!("{recovery_code1}{c}");
        } else if i < 22 {
            recovery_code2 = format!("{recovery_code2}{c}");
        } else {
            break;
        }
    }

    // !! login

    // login start
    let (state, req) = AuthClient::login_start_req(b"account", b"password")?;
    let res = auth.login_start(req)?;

    // client signing keys
    let mut signing_key: SigningKey = SigningKey::generate(&mut OsRng);
    let verifying_key = signing_key.verifying_key();

    let mut mfa_input = b"example.com".to_vec();
    mfa_input.extend_from_slice(b"account");
    mfa_input.extend_from_slice(mfa_code.as_bytes());

    let mut hasher = Sha256::new();
    hasher.update(&mfa_input);
    let mfa_hash = hasher.finalize().to_vec();

    // login finish
    let (req, session_key) = AuthClient::login_finish_req(
        b"account",
        b"password",
        &mfa_hash,
        &state,
        &res,
        Some(verifying_key.as_bytes()),
    )?;
    let (res, _, _) = auth.login_finish(req, true)?;

    let mut refresh_token: BytesMut = AuthClient::decrypt_token(&res, &session_key)?.into();

    // !! tokens with client signature

    // client exp
    let exp = SystemTime::now()
        .checked_add(Duration::from_secs(5))
        .expect("time to work")
        .duration_since(SystemTime::UNIX_EPOCH)?
        .as_secs() as u32;

    refresh_token.put_u32(exp);

    let signature = signing_key.sign(&refresh_token[..]);

    refresh_token.put(&signature.to_bytes()[..]);

    // get access
    let mut access_token: BytesMut = auth.access_get(&refresh_token.into())?.into();

    // client exp
    let exp = SystemTime::now()
        .checked_add(Duration::from_secs(3))
        .expect("time to work")
        .duration_since(SystemTime::UNIX_EPOCH)?
        .as_secs() as u32;

    access_token.put_u32(exp);

    let signature = signing_key.sign(&access_token[..]);

    access_token.put(&signature.to_bytes()[..]);

    let (_account, claims) = auth.verify_access_token(&access_token[..])?;

    assert_eq!(claims.idx(0).as_vector().idx(0).as_blob().0.len(), 16);

    // !! tokens without client signature

    // login start
    let (state, req) = AuthClient::login_start_req(b"account", b"password")?;
    let res = auth.login_start(req)?;

    // login finish
    let (req, session_key) =
        AuthClient::login_finish_req(b"account", b"password", &mfa_hash, &state, &res, None)?;
    let (res, _, _) = auth.login_finish(req, true)?;

    let refresh_token = AuthClient::decrypt_token(&res, &session_key)?;

    // get access
    let access_token = auth.access_get(&refresh_token)?;

    let (_account, claims) = auth.verify_access_token(&access_token[..])?;

    assert_eq!(claims.idx(0).as_vector().idx(0).as_blob().0.len(), 16);

    // !! reset password

    // reset login start
    let (state, req) = AuthClient::reset_password_login_start_req(b"account", b"password")?;
    let res = auth.reset_password_login_start(req)?;

    // reset login finish
    let (req, session_key) = AuthClient::reset_password_login_finish_req(
        b"account",
        b"password",
        &mfa_hash,
        &state,
        &res,
        None,
    )?;
    let res = auth.reset_password_login_finish(req)?;

    let password_reset_token = AuthClient::decrypt_token(&res, &session_key)?;

    // reset re-register start
    let (state, req) = AuthClient::password_reset_registration_start_req(b"account", b"password2")?;
    let res = auth.reset_password_registration_start(req, &password_reset_token)?;

    // reset re-register finish
    let req =
        AuthClient::password_reset_registration_finish_req(b"account", b"password2", &state, &res)?;
    auth.reset_password_registration_finish(req, &password_reset_token)?;

    // login start
    let (state, req) = AuthClient::login_start_req(b"account", b"password2")?;
    let res = auth.login_start(req)?;

    // login finish
    let (req, _) =
        AuthClient::login_finish_req(b"account", b"password2", &mfa_hash, &state, &res, None)?;
    auth.login_finish(req, true)?;

    // !! forgot password

    // forgot start
    let mut hasher = Sha256::new();
    hasher.update(recovery_code1.as_bytes());
    let hashed_recovery_code1 = hasher.finalize().to_vec();

    let (state, req) =
        AuthClient::forgot_password_start_req(b"account", b"password3", &hashed_recovery_code1)?;
    let res = auth.forgot_password_start(req)?;

    // forgot finish
    let req = AuthClient::forgot_password_finish_req(
        b"account",
        b"password3",
        &state,
        &res,
        recovery_code1.as_bytes(),
    )?;
    auth.forgot_password_finish(req)?;

    // login start
    let (state, req) = AuthClient::login_start_req(b"account", b"password3")?;
    let res = auth.login_start(req)?;

    // login finish
    let (req, _) =
        AuthClient::login_finish_req(b"account", b"password3", &mfa_hash, &state, &res, None)?;
    auth.login_finish(req, true)?;

    // !! reset totp mfa

    // mfa reset start
    let (state, req) = AuthClient::reset_totp_mfa_start_req(b"account", b"password3")?;
    let res = auth.reset_totp_mfa_start(req)?;

    // mfa reset finish
    let (req, session_key) =
        AuthClient::reset_totp_mfa_finish_req(b"account", b"password3", &mfa_hash, &state, &res)?;
    let res = auth.reset_totp_mfa_finish(req)?;

    let mfa_code2 = AuthClient::decrypt_reset_totp_mfa_code(
        &res,
        &session_key,
        "example.com".into(),
        "account".into(),
    )?;

    let mut mfa_input2 = b"example.com".to_vec();
    mfa_input2.extend_from_slice(b"account");
    mfa_input2.extend_from_slice(mfa_code2.as_bytes());

    let mut hasher2 = Sha256::new();
    hasher2.update(&mfa_input2);
    let mfa_hash2 = hasher2.finalize().to_vec();

    // login 2 start
    let (state, req) = AuthClient::login_start_req(b"account", b"password3")?;
    let res = auth.login_start(req)?;

    // login 2 finish
    let (req, _) =
        AuthClient::login_finish_req(b"account", b"password3", &mfa_hash2, &state, &res, None)?;
    auth.login_finish(req, true)?;

    // !! lost totp mfa

    // mfa lost start
    let mut hasher = Sha256::new();
    hasher.update(recovery_code2.as_bytes());
    let hashed_recovery_code2 = hasher.finalize().to_vec();

    let (state, req) =
        AuthClient::lost_totp_mfa_start_req(b"account", b"password3", &hashed_recovery_code2)?;
    let res = auth.lost_totp_mfa_start(req)?;

    // mfa lost finish
    let (req, session_key) = AuthClient::lost_totp_mfa_finish_req(
        b"account",
        b"password3",
        &state,
        &res,
        recovery_code2.as_bytes(),
    )?;
    let res = auth.lost_totp_mfa_finish(req)?;

    let mfa_code3 = AuthClient::decrypt_lost_totp_mfa_code(
        &res,
        &session_key,
        "example.com".into(),
        "account".into(),
    )?;

    let mut mfa_input3 = b"example.com".to_vec();
    mfa_input3.extend_from_slice(b"account");
    mfa_input3.extend_from_slice(mfa_code3.as_bytes());

    let mut hasher3 = Sha256::new();
    hasher3.update(&mfa_input3);
    let mfa_hash3 = hasher3.finalize().to_vec();

    // login 3 start
    let (state, req) = AuthClient::login_start_req(b"account", b"password3")?;
    let res = auth.login_start(req)?;

    // login 3 finish
    let (req, _) =
        AuthClient::login_finish_req(b"account", b"password3", &mfa_hash3, &state, &res, None)?;
    auth.login_finish(req, true)?;

    // !! reset recovery codes

    // reset recovery codes start
    let (state, req) = AuthClient::reset_recovery_codes_start_req(b"account", b"password3")?;
    let res = auth.reset_recovery_codes_start(req)?;

    // reset recovery codes finish
    let (req, session_key) = AuthClient::reset_recovery_codes_finish_req(
        b"account",
        b"password3",
        &mfa_hash3,
        &state,
        &res,
    )?;
    let res = auth.reset_recovery_codes_finish(req)?;

    // extract recovery code
    let recovery_codes = AuthClient::decrypt_reset_recovery_codes(&res, &session_key)?;

    let mut recovery_code3 = String::new();

    for (i, c) in recovery_codes.chars().enumerate() {
        if i < 11 {
            recovery_code3 = format!("{recovery_code3}{c}");
        } else {
            break;
        }
    }

    // forgot start
    let mut hasher = Sha256::new();
    hasher.update(recovery_code3.as_bytes());
    let hashed_recovery_code3 = hasher.finalize().to_vec();

    let (state, req) =
        AuthClient::forgot_password_start_req(b"account", b"password4", &hashed_recovery_code3)?;
    let res = auth.forgot_password_start(req)?;

    // forgot finish
    let req = AuthClient::forgot_password_finish_req(
        b"account",
        b"password4",
        &state,
        &res,
        recovery_code3.as_bytes(),
    )?;
    auth.forgot_password_finish(req)?;

    // !! delete account

    // delete account start
    let (state, req) = AuthClient::delete_account_start_req(b"account", b"password4")?;
    let res = auth.delete_account_start(req)?;

    // delete account finish
    let req =
        AuthClient::delete_account_finish_req(b"account", b"password4", &mfa_hash3, &state, &res)?;
    auth.delete_account_finish(req)?;

    drop(auth);
    drop(env);

    // !! invite codes

    let env = Arc::new(unsafe {
        let mut env_builder = EnvBuilder::new()?;
        env_builder.set_maxreaders(126)?;
        env_builder.set_mapsize(16384 * 64 * 10)?;
        env_builder.set_maxdbs(13)?;
        env_builder.open(
            store_dir.to_str().expect("store dir is not str"),
            &saferlmdb::open::Flags::empty(),
            0o600,
        )?
    });

    let auth = Arc::new(Auth::new(
        "example.com".into(),
        Some(AuthConfig {
            invite: Some(InviteConfig {
                mode: InviteMode::Viral,
                ..Default::default()
            }),
            ..Default::default()
        }),
        [0u8; 32],
        env.clone(),
    )?);

    let invite_code = auth.api_invite_get("api.example.com", "root", None)?;

    // registration start
    let (state, req) =
        AuthClient::registration_start_req(b"account", b"password", Some(invite_code))?;
    let res = auth.registration_start(req, None, None)?;

    // registration finish
    let (private_key, req) =
        AuthClient::registration_finish_req(b"account", b"password", &state, &res)?;
    let (res, ..) = auth.registration_finish(req, None)?;

    AuthClient::decrypt_totp_mfa_code(&res, private_key, "example.com".into(), "account".into())?;

    Ok(())
}