-
Notifications
You must be signed in to change notification settings - Fork 129
/
authenticator_mod_updb.go
168 lines (129 loc) · 4.74 KB
/
authenticator_mod_updb.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
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
/*
Copyright NetFoundry 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
https://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 model
import (
"encoding/base64"
"errors"
"github.com/michaelquigley/pfxlog"
"github.com/openziti/foundation/v2/errorz"
"github.com/openziti/ziti/controller/apierror"
"github.com/openziti/ziti/controller/db"
cmap "github.com/orcaman/concurrent-map/v2"
"time"
)
var _ AuthProcessor = &AuthModuleUpdb{}
const AuthMethodPassword = "password"
type AuthModuleUpdb struct {
env Env
method string
attemptsByAuthenticatorId cmap.ConcurrentMap[string, int64]
}
func NewAuthModuleUpdb(env Env) *AuthModuleUpdb {
return &AuthModuleUpdb{
env: env,
method: AuthMethodPassword,
attemptsByAuthenticatorId: cmap.New[int64](),
}
}
func (module *AuthModuleUpdb) CanHandle(method string) bool {
return method == module.method
}
func (module *AuthModuleUpdb) Process(context AuthContext) (AuthResult, error) {
logger := pfxlog.Logger().WithField("authMethod", module.method)
data := context.GetData()
username := ""
password := ""
if usernameVal := data["username"]; usernameVal != nil {
username = usernameVal.(string)
}
if passwordVal := data["password"]; passwordVal != nil {
password = passwordVal.(string)
}
if username == "" || password == "" {
return nil, errorz.NewCouldNotValidate(errors.New("username and password fields are required"))
}
logger = logger.WithField("username", username)
authenticator, err := module.env.GetManagers().Authenticator.ReadByUsername(username)
if err != nil {
logger.WithError(err).Error("could not authenticate, authenticator lookup by username errored")
return nil, err
}
if authenticator == nil {
logger.WithError(err).Error("could not authenticate, authenticator lookup returned nil")
return nil, apierror.NewInvalidAuth()
}
logger = logger.
WithField("authenticatorId", authenticator.Id).
WithField("identityId", authenticator.IdentityId)
authPolicy, identity, err := getAuthPolicyByIdentityId(module.env, module.method, authenticator.Id, authenticator.IdentityId)
if err != nil {
logger.WithError(err).Errorf("could not look up auth policy by identity id")
return nil, apierror.NewInvalidAuth()
}
if authPolicy == nil {
logger.Error("auth policy look up returned nil")
return nil, apierror.NewInvalidAuth()
}
if identity.Disabled {
logger.
WithField("disabledAt", identity.DisabledAt).
WithField("disabledUntil", identity.DisabledUntil).
Error("authentication failed, identity is disabled")
return nil, apierror.NewInvalidAuth()
}
logger = logger.WithField("authPolicyId", authPolicy.Id)
if !authPolicy.Primary.Updb.Allowed {
logger.Error("auth policy does not allow updb authentication")
return nil, apierror.NewInvalidAuth()
}
attempts := int64(0)
module.attemptsByAuthenticatorId.Upsert(authenticator.Id, 0, func(exist bool, prevAttempts int64, newValue int64) int64 {
if exist {
attempts = prevAttempts + 1
return attempts
}
return 0
})
if authPolicy.Primary.Updb.MaxAttempts != db.UpdbUnlimitedAttemptsLimit && attempts > authPolicy.Primary.Updb.MaxAttempts {
logger.WithField("attempts", attempts).WithField("maxAttempts", authPolicy.Primary.Updb.MaxAttempts).Error("updb auth failed, max attempts exceeded")
duration := time.Duration(authPolicy.Primary.Updb.LockoutDurationMinutes) * time.Minute
if err = module.env.GetManagers().Identity.Disable(authenticator.IdentityId, duration, context.GetChangeContext()); err != nil {
logger.WithError(err).Error("could not lock identity, unhandled error")
}
return nil, apierror.NewInvalidAuth()
}
updb := authenticator.ToUpdb()
salt, err := DecodeSalt(updb.Salt)
if err != nil {
return nil, apierror.NewInvalidAuth()
}
hr := module.env.GetManagers().Authenticator.ReHashPassword(password, salt)
if updb.Password != hr.Password {
return nil, apierror.NewInvalidAuth()
}
return &AuthResultBase{
identity: identity,
identityId: updb.IdentityId,
authenticator: authenticator,
authenticatorId: authenticator.Id,
env: module.env,
}, nil
}
func DecodeSalt(s string) ([]byte, error) {
salt := make([]byte, 1024)
n, err := base64.StdEncoding.Decode(salt, []byte(s))
if err != nil {
return nil, err
}
return salt[:n], nil
}