-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
accountrecovery.go
105 lines (89 loc) · 2.65 KB
/
accountrecovery.go
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
/*
* Copyright 2021 Gravitational, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package types
import (
"time"
"github.com/gravitational/trace"
)
// NewRecoveryCodes creates a new RecoveryCodes with the given codes and created
// time.
func NewRecoveryCodes(codes []RecoveryCode, created time.Time, username string) (*RecoveryCodesV1, error) {
rc := &RecoveryCodesV1{
Metadata: Metadata{
Name: username,
},
Spec: RecoveryCodesSpecV1{
Codes: codes,
Created: created,
},
}
if err := rc.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
return rc, nil
}
// CheckAndSetDefaults validates fields and populates empty fields with default values.
func (t *RecoveryCodesV1) CheckAndSetDefaults() error {
t.setStaticFields()
if err := t.Metadata.CheckAndSetDefaults(); err != nil {
return trace.Wrap(err)
}
if t.Spec.Codes == nil {
return trace.BadParameter("missing Codes field")
}
if t.Spec.Created.IsZero() {
return trace.BadParameter("missing Created field")
}
return nil
}
func (t *RecoveryCodesV1) setStaticFields() {
t.Kind = KindRecoveryCodes
t.Version = V1
}
// GetCodes returns recovery codes.
func (t *RecoveryCodesV1) GetCodes() []RecoveryCode {
return t.Spec.Codes
}
// RecoveryAttempt represents an unsuccessful attempt at recovering a user's account.
type RecoveryAttempt struct {
// Time is time of the attempt.
Time time.Time `json:"time"`
// Expires defines the time when this attempt should expire.
Expires time.Time `json:"expires"`
}
func (a *RecoveryAttempt) Check() error {
switch {
case a.Time.IsZero():
return trace.BadParameter("missing parameter time")
case a.Expires.IsZero():
return trace.BadParameter("missing parameter expires")
}
return nil
}
// IsMaxFailedRecoveryAttempt determines if user reached their max failed attempts.
// Attempts list is expected to come sorted from oldest to latest time.
func IsMaxFailedRecoveryAttempt(maxAttempts int, attempts []*RecoveryAttempt, now time.Time) bool {
var failed int
for i := len(attempts) - 1; i >= 0; i-- {
if attempts[i].Expires.After(now) {
failed++
}
if failed >= maxAttempts {
return true
}
}
return false
}