-
Notifications
You must be signed in to change notification settings - Fork 171
/
authfront-login-middlewares.go
257 lines (225 loc) · 9.35 KB
/
authfront-login-middlewares.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
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
/*
* Copyright (c) 2021. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package modifiers
import (
"context"
"fmt"
"net/http"
"strconv"
"time"
restful "github.com/emicklei/go-restful/v3"
"github.com/gorilla/sessions"
"go.uber.org/zap"
"google.golang.org/protobuf/types/known/anypb"
"github.com/pydio/cells/v4/common"
"github.com/pydio/cells/v4/common/broker"
"github.com/pydio/cells/v4/common/client/grpc"
"github.com/pydio/cells/v4/common/log"
"github.com/pydio/cells/v4/common/proto/idm"
"github.com/pydio/cells/v4/common/proto/rest"
"github.com/pydio/cells/v4/common/proto/service"
"github.com/pydio/cells/v4/common/service/errors"
"github.com/pydio/cells/v4/common/service/frontend"
"github.com/pydio/cells/v4/common/utils/i18n"
json "github.com/pydio/cells/v4/common/utils/jsonx"
"github.com/pydio/cells/v4/common/utils/permissions"
)
// LoginSuccessWrapper wraps functionalities after user was successfully logged in
func LoginSuccessWrapper(middleware frontend.AuthMiddleware) frontend.AuthMiddleware {
return func(req *restful.Request, rsp *restful.Response, in *frontend.FrontSessionWithRuntimeCtx, out *rest.FrontSessionResponse, session *sessions.Session) error {
if a, ok := in.AuthInfo["type"]; !ok || a != "credentials" && a != "authorization_code" { // Ignore this middleware
return middleware(req, rsp, in, out, session)
}
// BEFORE MIDDLEWARE
// MIDDLEWARE
err := middleware(req, rsp, in, out, session)
if err != nil {
return err
}
// AFTER MIDDLEWARE
// retrieving user
username, ok := in.AuthInfo["login"]
if !ok {
return errors.New("user.not_found", "User id not found", http.StatusNotFound)
}
ctx := req.Request.Context()
// Searching user for attributes
user, err := permissions.SearchUniqueUser(ctx, username, "")
if err != nil {
return err
}
// If user is "hidden", he is not allowed to log on the main interface
if user.Attributes != nil {
_, mini := session.Values["minisite"]
if user.IsHidden() && !mini {
log.Auditer(ctx).Error(
"Hidden user ["+user.Login+"] tried to log in on main interface",
log.GetAuditId(common.AuditLoginPolicyDenial),
zap.String(common.KeyUserUuid, user.Uuid),
)
log.Logger(ctx).Error("Denied login for hidden user " + user.Login + " on main interface")
return errors.Unauthorized("hidden.user.nominisite", "You are not allowed to log in to this interface")
}
}
// Checking user is locked
if permissions.IsUserLocked(user) {
log.Auditer(ctx).Error(
"Locked user ["+user.Login+"] tried to log in.",
log.GetAuditId(common.AuditLoginPolicyDenial),
zap.String(common.KeyUserUuid, user.Uuid),
)
log.Logger(ctx).Error("lock denies login for "+user.Login, zap.Error(fmt.Errorf("blocked login")))
return errors.Unauthorized(common.ServiceUser, "User "+user.Login+" has been blocked. Contact your sysadmin.")
}
// Reset failed connections
if user.Attributes != nil {
if _, ok := user.Attributes["failedConnections"]; ok {
log.Logger(ctx).Info("[WrapWithUserLocks] Resetting user failedConnections", user.ZapLogin())
userClient := idm.NewUserServiceClient(grpc.GetClientConnFromCtx(in.RuntimeCtx, common.ServiceUser))
delete(user.Attributes, "failedConnections")
userClient.CreateUser(ctx, &idm.CreateUserRequest{User: user})
}
}
// Checking policies
cli := idm.NewPolicyEngineServiceClient(grpc.GetClientConnFromCtx(in.RuntimeCtx, common.ServicePolicy))
policyContext := make(map[string]string)
permissions.PolicyContextFromMetadata(policyContext, ctx)
subjects := permissions.PolicyRequestSubjectsFromUser(user)
// Check all subjects, if one has deny return false
policyRequest := &idm.PolicyEngineRequest{
Subjects: subjects,
Resource: "oidc",
Action: "login",
Context: policyContext,
}
if resp, err := cli.IsAllowed(ctx, policyRequest); err != nil || !resp.Allowed {
log.Auditer(ctx).Error(
"Policy denied login to ["+user.Login+"]",
log.GetAuditId(common.AuditLoginPolicyDenial),
zap.String(common.KeyUserUuid, user.Uuid),
zap.Any(common.KeyPolicyRequest, policyRequest),
zap.Error(err),
)
log.Logger(ctx).Error("policy denies login for request", zap.Any(common.KeyPolicyRequest, policyRequest), zap.Error(err))
return errors.Unauthorized(common.ServiceUser, "User "+user.Login+" is not authorized to log in")
}
if lang, ok := in.AuthInfo["lang"]; ok {
if _, o := i18n.AvailableLanguages[lang]; o {
aclClient := idm.NewACLServiceClient(grpc.GetClientConnFromCtx(in.RuntimeCtx, common.ServiceAcl))
// Remove previous value if any
delQ, _ := anypb.New(&idm.ACLSingleQuery{RoleIDs: []string{user.GetUuid()}, Actions: []*idm.ACLAction{{Name: "parameter:core.conf:lang"}}, WorkspaceIDs: []string{"PYDIO_REPO_SCOPE_ALL"}})
send, can := context.WithTimeout(ctx, 500*time.Millisecond)
defer can()
aclClient.DeleteACL(send, &idm.DeleteACLRequest{Query: &service.Query{SubQueries: []*anypb.Any{delQ}}})
// Insert new ACL with language value
_, e := aclClient.CreateACL(send, &idm.CreateACLRequest{ACL: &idm.ACL{
Action: &idm.ACLAction{Name: "parameter:core.conf:lang", Value: lang},
RoleID: user.GetUuid(),
WorkspaceID: "PYDIO_REPO_SCOPE_ALL",
}})
if e != nil {
log.Logger(ctx).Error("Cannot update language for user", user.ZapLogin(), zap.String("lang", lang), zap.Error(e))
} else {
log.Logger(ctx).Info("Updated language for "+user.GetLogin()+" to "+lang, user.ZapLogin(), zap.String("lang", lang))
}
}
}
broker.MustPublish(ctx, common.TopicIdmEvent, &idm.ChangeEvent{
Type: idm.ChangeEventType_LOGIN,
User: user,
})
return nil
}
}
// LoginFailedWrapper wraps functionalities after user failed to log in
func LoginFailedWrapper(middleware frontend.AuthMiddleware) frontend.AuthMiddleware {
return func(req *restful.Request, rsp *restful.Response, in *frontend.FrontSessionWithRuntimeCtx, out *rest.FrontSessionResponse, session *sessions.Session) error {
if a, ok := in.AuthInfo["type"]; !ok || a != "credentials" && a != "external" { // Ignore this middleware
return middleware(req, rsp, in, out, session)
}
// MIDDLEWARE
err := middleware(req, rsp, in, out, session)
if err == nil {
return nil
}
//fmt.Println("Login failed with ", err)
ctx := req.Request.Context()
username := in.AuthInfo["login"]
const maxFailedLogins = 10
// Searching user for attributes
user, _ := permissions.SearchUniqueUser(ctx, username, "")
if user == nil {
return errors.New("login.failed", "Login failed", http.StatusUnauthorized)
}
// double check if user was already locked to reduce work load
if permissions.IsUserLocked(user) {
msg := fmt.Sprintf("locked user %s is still trying to connect", user.GetLogin())
log.Logger(ctx).Warn(msg, user.ZapLogin())
return errors.New("user.locked", "User is locked - Please contact your admin", http.StatusUnauthorized)
}
var failedInt int64
if user.Attributes == nil {
user.Attributes = make(map[string]string)
}
if failed, ok := user.Attributes["failedConnections"]; ok {
failedInt, _ = strconv.ParseInt(failed, 10, 32)
}
failedInt++
user.Attributes["failedConnections"] = fmt.Sprintf("%d", failedInt)
hardLock := false
if failedInt >= maxFailedLogins {
// Set lock via attributes
var locks []string
if l, ok := user.Attributes["locks"]; ok {
var existingLocks []string
if e := json.Unmarshal([]byte(l), &existingLocks); e == nil {
for _, lock := range existingLocks {
if lock != "logout" {
locks = append(locks, lock)
}
}
}
}
locks = append(locks, "logout")
data, _ := json.Marshal(locks)
user.Attributes["locks"] = string(data)
msg := fmt.Sprintf("Locked user [%s] after %d failed connections", user.GetLogin(), maxFailedLogins)
log.Logger(ctx).Error(msg, user.ZapLogin())
log.Auditer(ctx).Error(
msg,
log.GetAuditId(common.AuditLockUser),
user.ZapLogin(),
zap.String(common.KeyUserUuid, user.GetUuid()),
)
hardLock = true
}
log.Logger(ctx).Debug(fmt.Sprintf("[WrapWithUserLocks] Updating failed connection number for user [%s]", user.GetLogin()), user.ZapLogin())
userClient := idm.NewUserServiceClient(grpc.GetClientConnFromCtx(in.RuntimeCtx, common.ServiceUser))
if _, e := userClient.CreateUser(ctx, &idm.CreateUserRequest{User: user}); e != nil {
log.Logger(ctx).Error("could not store failedConnection for user", zap.Error(e))
}
// Replacing error not to give any hint
if hardLock {
return errors.New("user.locked", "User is locked - Please contact your admin", http.StatusUnauthorized)
} else {
return errors.New("login.failed", "Login failed", http.StatusUnauthorized)
}
}
}