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
use std::time::Duration;

/// Represents a temporary denial of the credential to authenticate. This is used
/// to ratelimit and prevent bruteforcing of accounts. At an initial failure the
/// SoftLock is created and the count set to 1, with a unlock_at set to 1 second
/// later, and a reset_count_at: at a maximum time window for a cycle.
///
/// If the softlock already exists, and the failure count is 0, then this acts as the
/// creation where the reset_count_at window is then set.
///
/// While current_time < unlock_at, all authentication attempts are denied with a
/// message regarding the account being temporarily unavailable. Once
/// unlock_at < current_time, authentication will be processed again. If a subsequent
/// failure occurs, unlock_at is extended based on policy, and failure_count incremented.
///
/// If unlock_at < current_time, and authentication succeeds the login is allowed
/// and no changes to failure_count or unlock_at are made.
///
/// If reset_count_at < current_time, then failure_count is reset to 0 before processing.
///
/// This allows handling of max_failure_count, so that when that value from policy is
/// exceeded then unlock_at is set to reset_count_at to softlock until the cycle
/// is over (see NIST sp800-63b.). For example, reset_count_at will be 24 hours after
/// the first failed authentication attempt.
///
/// This also works for something like TOTP which allows a 60 second cycle for the
/// reset_count_at and a max number of attempts in that window (say 5). with short
/// delays in between (1 second).
//
//                                                  ┌────────────────────────┐
//                                                  │reset_at < current_time │
//                                                 ─└────────────────────────┘
//                                                │                         │
//                                                ▼
//             ┌─────┐                         .─────.       ┌────┐         │
//             │Valid│                        ╱       ╲      │Fail│
//        ┌────┴─────┴───────────────────────(count = 0)─────┴────┴┐        │
//        │                                   `.     ,'            │
//        │                                     `───'              │        │
//        │             ┌────────────────────────┐▲                │
//        │             │reset_at < current_time │                 │        │
//        │             └────────────────────────┘│                │
//        │                      ┌ ─ ─ ─ ─ ─ ─ ─ ─                 │        │
//        │                                                        │
//        │                      ├─────┬───────┬──┐                ▼        │
//        │                      │     │ Fail  │  │             .─────.
//        │                      │     │count++│  │           ,'       `.   │
//        ▼                   .─────.  └───────┘  │          ;  Locked   :
// ┌────────────┐            ╱       ╲            └─────────▶: count > 0 ;◀─┤
// │Auth Success│◀─┬─────┬──(Unlocked )                       ╲         ╱   │
// └────────────┘  │Valid│   `.     ,'                         `.     ,'    │
//                 └─────┘     `───'                             `───'      │
//                               ▲                                 │        │
//                               │                                 │        │
//                               └─────┬──────────────────────────┬┴┬───────┴──────────────────┐
//                                     │ expire_at < current_time │ │ current_time < expire_at │
//                                     └──────────────────────────┘ └──────────────────────────┘
//
//

const ONEDAY: u64 = 86400;

#[derive(Debug, Clone)]
pub enum CredSoftLockPolicy {
    Password,
    Totp(u64),
    Webauthn,
    Unrestricted,
}

impl CredSoftLockPolicy {
    /// Determine the next lock state after a failure based on this credentials
    /// policy.
    fn failure_next_state(&self, count: usize, ct: Duration) -> LockState {
        match self {
            CredSoftLockPolicy::Password => {
                let next_day_end = ct.as_secs() + ONEDAY;
                let rem = next_day_end % ONEDAY;
                let reset_at = Duration::from_secs(next_day_end - rem);

                if count < 3 {
                    LockState::Locked(count, reset_at, ct + Duration::from_secs(1))
                } else if count < 9 {
                    LockState::Locked(count, reset_at, ct + Duration::from_secs(3))
                } else if count < 25 {
                    LockState::Locked(count, reset_at, ct + Duration::from_secs(5))
                } else if count < 100 {
                    LockState::Locked(count, reset_at, ct + Duration::from_secs(10))
                } else {
                    LockState::Locked(count, reset_at, reset_at)
                }
            }
            CredSoftLockPolicy::Totp(step) => {
                // reset at is based on the next step ending.
                let next_window_end = ct.as_secs() + step;
                let rem = next_window_end % step;
                let reset_at = Duration::from_secs(next_window_end - rem);
                // We delay for 1 second, unless count is > 3, then we set
                // unlock at to reset_at.
                if count >= 3 {
                    LockState::Locked(count, reset_at, reset_at)
                } else {
                    LockState::Locked(count, reset_at, ct + Duration::from_secs(1))
                }
            }
            CredSoftLockPolicy::Webauthn => {
                // we only lock for 1 second to slow them down.
                // TODO: Could this be a DOS/Abuse vector?
                LockState::Locked(
                    count,
                    ct + Duration::from_secs(1),
                    ct + Duration::from_secs(1),
                )
            }
            CredSoftLockPolicy::Unrestricted => {
                // No action needed
                LockState::Init
            }
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum LockState {
    Init,
    // count
    // * Number of Failures in this cycle
    // unlock_at
    // * Time of next allowed check (works with delay)
    // reset_count_at
    // * The time to reset the state to init.
    //     count  reset_at  unlock_at
    Locked(usize, Duration, Duration),
    Unlocked(usize, Duration),
}

#[derive(Debug, Clone)]
pub(crate) struct CredSoftLock {
    state: LockState,
    // Policy (for determining delay times based on num failures, and when to reset?)
    policy: CredSoftLockPolicy,
}

impl CredSoftLock {
    pub fn new(policy: CredSoftLockPolicy) -> Self {
        CredSoftLock {
            state: LockState::Init,
            policy,
        }
    }

    pub fn apply_time_step(&mut self, ct: Duration) {
        // Do a reset if needed?
        let mut next_state = match self.state {
            LockState::Init => LockState::Init,
            LockState::Locked(count, reset_at, unlock_at) => {
                if ct > reset_at {
                    LockState::Init
                } else if ct > unlock_at {
                    LockState::Unlocked(count, reset_at)
                } else {
                    LockState::Locked(count, reset_at, unlock_at)
                }
            }
            LockState::Unlocked(count, reset_at) => {
                if ct > reset_at {
                    LockState::Init
                } else {
                    LockState::Unlocked(count, reset_at)
                }
            }
        };
        std::mem::swap(&mut self.state, &mut next_state);
    }

    /// Is this credential valid to proceed at this point in time.
    pub fn is_valid(&self) -> bool {
        !matches!(self.state, LockState::Locked(_count, _reset_at, _unlock_at))
    }

    /// Document a failure of authentication at this time.
    pub fn record_failure(&mut self, ct: Duration) {
        let mut next_state = match self.state {
            LockState::Init => {
                self.policy.failure_next_state(1, ct)
                // LockState::Locked(1, reset_at, unlock_at)
            }
            LockState::Locked(count, _reset_at, _unlock_at) => {
                // We should never reach this but just in case ...
                self.policy.failure_next_state(count + 1, ct)
                // LockState::Locked(count + 1, reset_at, unlock_at)
            }
            LockState::Unlocked(count, _reset_at) => {
                self.policy.failure_next_state(count + 1, ct)
                // LockState::Locked(count + 1, reset_at, unlock_at)
            }
        };
        std::mem::swap(&mut self.state, &mut next_state);
    }

    #[cfg(test)]
    pub fn is_state_init(&self) -> bool {
        matches!(self.state, LockState::Init)
    }

    #[cfg(test)]
    fn peek_state(&self) -> &LockState {
        &self.state
    }

    /*
    #[cfg(test)]
    fn set_failure_count(&mut self, count: usize) {
        let mut next_state = match self.state {
            LockState::Init => panic!(),
            LockState::Locked(_count, reset_at, unlock_at) => {
                LockState::Locked(count, reset_at, unlock_at)
            }
            LockState::Unlocked(count, reset_at) => {
                LockState::Unlocked(count, reset_at)
            }
        };
        std::mem::swap(&mut self.state, &mut next_state);
    }
    */
}

#[cfg(test)]
mod tests {
    use crate::credential::softlock::*;
    use crate::credential::totp::TOTP_DEFAULT_STEP;

    #[test]
    fn test_credential_softlock_statemachine() {
        // Check that given the set of inputs, correct decisions about
        // locking are made, and the states can be moved through.
        // ==> Check the init state.
        let mut slock = CredSoftLock::new(CredSoftLockPolicy::Password);
        assert!(slock.is_state_init());
        assert!(slock.is_valid());
        // A success does nothing, so we don't track them.
        let ct = Duration::from_secs(10);
        // Generate a failure
        // ==> trans to locked
        slock.record_failure(ct);
        assert!(
            slock.peek_state()
                == &LockState::Locked(1, Duration::from_secs(ONEDAY), Duration::from_secs(10 + 1))
        );
        // It will now fail
        // ==> trans ct < exp_at
        slock.apply_time_step(ct);
        assert!(!slock.is_valid());
        // A few seconds later it will be okay.
        // ==> trans ct < exp_at
        let ct2 = ct + Duration::from_secs(2);
        slock.apply_time_step(ct2);
        assert!(slock.is_valid());
        // Now trigger a failure now, we move back to locked.
        // ==> trans fail unlock -> lock
        slock.record_failure(ct2);
        assert!(
            slock.peek_state()
                == &LockState::Locked(2, Duration::from_secs(ONEDAY), Duration::from_secs(10 + 3))
        );
        assert!(!slock.is_valid());
        // Now check the reset_at behaviour. We need to check a locked and unlocked state.
        let mut slock2 = slock.clone();
        // This triggers the reset at from locked.
        // ==> trans locked -> init
        let ct3 = ct + Duration::from_secs(ONEDAY + 2);
        slock.apply_time_step(ct3);
        assert!(slock.is_state_init());
        assert!(slock.is_valid());
        // For slock2, we move to unlocked:
        // ==> trans unlocked -> init
        let ct4 = ct2 + Duration::from_secs(2);
        slock2.apply_time_step(ct4);
        eprintln!("{:?}", slock2.peek_state());
        assert_eq!(
            slock2.peek_state(),
            &LockState::Unlocked(2, Duration::from_secs(ONEDAY))
        );
        slock2.apply_time_step(ct3);
        assert!(slock2.is_state_init());
        assert!(slock2.is_valid());
    }

    #[test]
    fn test_credential_softlock_policy_password() {
        let policy = CredSoftLockPolicy::Password;

        assert!(
            policy.failure_next_state(1, Duration::from_secs(0))
                == LockState::Locked(1, Duration::from_secs(ONEDAY), Duration::from_secs(1))
        );

        assert!(
            policy.failure_next_state(8, Duration::from_secs(0))
                == LockState::Locked(8, Duration::from_secs(ONEDAY), Duration::from_secs(3))
        );

        assert!(
            policy.failure_next_state(24, Duration::from_secs(0))
                == LockState::Locked(24, Duration::from_secs(ONEDAY), Duration::from_secs(5))
        );

        assert!(
            policy.failure_next_state(99, Duration::from_secs(0))
                == LockState::Locked(99, Duration::from_secs(ONEDAY), Duration::from_secs(10))
        );

        assert!(
            policy.failure_next_state(100, Duration::from_secs(0))
                == LockState::Locked(
                    100,
                    Duration::from_secs(ONEDAY),
                    Duration::from_secs(ONEDAY)
                )
        );
    }

    #[test]
    fn test_credential_softlock_policy_totp() {
        let policy = CredSoftLockPolicy::Totp(TOTP_DEFAULT_STEP);

        assert!(
            policy.failure_next_state(1, Duration::from_secs(10))
                == LockState::Locked(
                    1,
                    Duration::from_secs(TOTP_DEFAULT_STEP),
                    Duration::from_secs(11)
                )
        );

        assert!(
            policy.failure_next_state(2, Duration::from_secs(10))
                == LockState::Locked(
                    2,
                    Duration::from_secs(TOTP_DEFAULT_STEP),
                    Duration::from_secs(11)
                )
        );

        assert!(
            policy.failure_next_state(3, Duration::from_secs(10))
                == LockState::Locked(
                    3,
                    Duration::from_secs(TOTP_DEFAULT_STEP),
                    Duration::from_secs(TOTP_DEFAULT_STEP)
                )
        );
    }

    #[test]
    fn test_credential_softlock_policy_webauthn() {
        let policy = CredSoftLockPolicy::Webauthn;

        assert!(
            policy.failure_next_state(1, Duration::from_secs(0))
                == LockState::Locked(1, Duration::from_secs(1), Duration::from_secs(1))
        );

        // No matter how many failures, webauthn always only delays by 1 second.
        assert!(
            policy.failure_next_state(1000, Duration::from_secs(0))
                == LockState::Locked(1000, Duration::from_secs(1), Duration::from_secs(1))
        );
    }
}