-
Notifications
You must be signed in to change notification settings - Fork 1
/
authentication.go
306 lines (262 loc) · 8.72 KB
/
authentication.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
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
package proxy
// Copyright 2022 Severalnines AB
//
// This file is part of cmon-proxy.
//
// cmon-proxy is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2 of the License.
//
// cmon-proxy 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with cmon-proxy. If not, see <https://www.gnu.org/licenses/>.
import (
"fmt"
"net/http"
"sync"
"time"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"github.com/rs/xid"
cmonapi "github.com/severalnines/cmon-proxy/cmon/api"
"github.com/severalnines/cmon-proxy/config"
"github.com/severalnines/cmon-proxy/proxy/api"
"github.com/severalnines/cmon-proxy/rpcserver/session"
"go.uber.org/zap"
)
type SessionData struct {
User *config.ProxyUser
Login time.Time
}
var (
// session ID <-> session data map
sesss = make(map[string]*SessionData)
sesssMtx sync.RWMutex
// the user key for the context (we put the user object there)
userKey = "user"
// the key in the browser side cookie (encrypted)
sessIdKey = "s"
)
func cleanupOldSessions() {
sesssMtx.Lock()
defer sesssMtx.Unlock()
invalidate := make([]string, 0)
// collect the outdated logins
for sessionId, data := range sesss {
if time.Since(data.Login) > session.SessionTTL {
invalidate = append(invalidate, sessionId)
// audit log of server side logouts
if data.User != nil {
zap.L().Info(
fmt.Sprintf("[AUDIT] Logout '%s' (TTL)",
data.User.Username))
}
}
}
// invalidate them, lets do also audit log
for _, sessionId := range invalidate {
delete(sesss, sessionId)
}
}
// retrieves the ProxyUser object for the actual session
func getUserForSession(ctx *gin.Context) *config.ProxyUser {
if ctx == nil {
return nil
}
cleanupOldSessions()
s := sessions.Default(ctx)
// get the unique ID
sessionId, _ := s.Get(sessIdKey).(string)
sesssMtx.RLock()
defer sesssMtx.RUnlock()
if data, found := sesss[sessionId]; found && data != nil {
return data.User
}
return nil
}
func setUserForSession(ctx *gin.Context, user *config.ProxyUser) {
s := sessions.Default(ctx)
sesssMtx.Lock()
defer sesssMtx.Unlock()
// get the unique ID
sessionId, _ := s.Get(sessIdKey).(string)
// it might be a logout (in case of 'nil' user)
if user == nil {
// remove the data and destroy the session
delete(sesss, sessionId)
session.SessionDestroy(ctx)
return
}
if len(sessionId) < 1 {
// generate one for new sessions
guid := xid.New()
sessionId = guid.String()
s.Set(sessIdKey, sessionId)
if err := s.Save(); err != nil {
zap.L().Fatal("Session.Save() error: " + err.Error())
}
}
// save the user into our session storage using the sessionID
sesss[sessionId] = &SessionData{
User: user,
Login: time.Now(),
}
ctx.Set(userKey, user)
}
// RPCAuthCheck is a middleware method
func (p *Proxy) RPCAuthMiddleware(ctx *gin.Context) {
user := getUserForSession(ctx)
if user == nil {
ctx.JSON(http.StatusUnauthorized,
&cmonapi.WithResponseData{
RequestStatus: cmonapi.RequestStatusAuthRequired,
ErrorString: "authentication is required",
})
ctx.Abort()
return
}
// we store the 'user' object in context
ctx.Set(userKey, user)
}
// RPCLoginHandler handlers the login requests
func (p *Proxy) RPCAuthLoginHandler(ctx *gin.Context) {
var req api.LoginRequest
var resp api.LoginResponse
resp.WithResponseData = &cmonapi.WithResponseData{
RequestStatus: cmonapi.RequestStatusOk,
RequestProcessed: cmonapi.NullTime{T: time.Now()},
}
if ctx.Request.Method == http.MethodPost {
if err := ctx.BindJSON(&req); err != nil {
cmonapi.CtxWriteError(ctx,
cmonapi.NewError(cmonapi.RequestStatusInvalidRequest,
fmt.Sprint("Invalid request:", err.Error())))
return
}
}
user, err := p.r.Config.GetUser(req.Username)
if err != nil {
resp.RequestStatus = cmonapi.RequestStatusAccessDenied
resp.ErrorString = fmt.Sprintf("user error: %s", err.Error())
}
if user != nil {
if err := user.ValidatePassword(req.Password); err != nil {
resp.RequestStatus = cmonapi.RequestStatusAccessDenied
resp.ErrorString = fmt.Sprintf("password error: %s", err.Error())
} else {
resp.User = user.Copy(false)
setUserForSession(ctx, user)
}
}
zap.L().Info(
fmt.Sprintf("[AUDIT] Login attempt %s '%s' (source %s / %s)",
resp.RequestStatus, req.Username, ctx.ClientIP(), ctx.Request.UserAgent()))
ctx.JSON(cmonapi.RequestStatusToStatusCode(resp.RequestStatus), resp)
}
// RPCCheckHandler checks if the user has logged in
func (p *Proxy) RPCAuthCheckHandler(ctx *gin.Context) {
var resp api.LoginResponse
resp.WithResponseData = &cmonapi.WithResponseData{
RequestProcessed: cmonapi.NullTime{T: time.Now()},
RequestStatus: cmonapi.RequestStatusAuthRequired,
ErrorString: "not authenticated",
}
if u := getUserForSession(ctx); u != nil {
resp.User = u.Copy(false)
resp.RequestStatus = cmonapi.RequestStatusOk
resp.ErrorString = ""
}
ctx.JSON(cmonapi.RequestStatusToStatusCode(resp.RequestStatus), resp)
}
// RPCLogoutHandler handlers the explicit logout requests
func (p *Proxy) RPCAuthLogoutHandler(ctx *gin.Context) {
if proxyUser := getUserForSession(ctx); proxyUser != nil {
zap.L().Info(
fmt.Sprintf("[AUDIT] Logout '%s' (explicit) (source %s / %s)",
proxyUser.Username, ctx.ClientIP(), ctx.Request.UserAgent()))
// perform logout
setUserForSession(ctx, nil)
}
var resp cmonapi.WithResponseData
resp.RequestStatus = cmonapi.RequestStatusOk
ctx.JSON(cmonapi.RequestStatusToStatusCode(resp.RequestStatus), resp)
}
func (p *Proxy) RPCAuthUpdateUserHandler(ctx *gin.Context) {
var req api.UpdateUserRequest
var resp api.LoginResponse
resp.WithResponseData = &cmonapi.WithResponseData{
RequestProcessed: cmonapi.NullTime{T: time.Now()},
RequestStatus: cmonapi.RequestStatusAuthRequired,
ErrorString: "not authenticated",
}
if err := ctx.BindJSON(&req); err != nil || req.User == nil {
cmonapi.CtxWriteError(ctx,
cmonapi.NewError(cmonapi.RequestStatusInvalidRequest,
fmt.Sprint("Invalid request:", err.Error())))
return
}
if u := getUserForSession(ctx); u != nil {
if u.Username != req.User.Username {
resp.RequestStatus = cmonapi.RequestStatusAccessDenied
resp.ErrorString = "wrong username"
} else if err := req.User.Validate(); err != nil {
resp.RequestStatus = cmonapi.RequestStatusInvalidRequest
resp.ErrorString = "wrong user: " + err.Error()
} else {
// we do not allow updating password from this request
req.User.PasswordHash = ""
if err := p.r.Config.UpdateUser(req.User); err != nil {
resp.RequestStatus = cmonapi.RequestStatusUnknownError
resp.ErrorString = "failed to update user: " + err.Error()
} else {
// also update the user in session
updatedUser, _ := p.r.Config.GetUser(req.User.Username)
setUserForSession(ctx, updatedUser)
// return the updated user instance
resp.User = updatedUser.Copy(false)
resp.RequestStatus = cmonapi.RequestStatusOk
resp.ErrorString = ""
}
}
}
ctx.JSON(cmonapi.RequestStatusToStatusCode(resp.RequestStatus), resp)
}
func (p *Proxy) RPCAuthSetPasswordHandler(ctx *gin.Context) {
var req api.SetPasswordRequest
var resp api.LoginResponse
resp.WithResponseData = &cmonapi.WithResponseData{
RequestProcessed: cmonapi.NullTime{T: time.Now()},
RequestStatus: cmonapi.RequestStatusAuthRequired,
ErrorString: "not authenticated",
}
if ctx.Request.Method == http.MethodPost {
if err := ctx.BindJSON(&req); err != nil {
cmonapi.CtxWriteError(ctx,
cmonapi.NewError(cmonapi.RequestStatusInvalidRequest,
fmt.Sprint("Invalid request:", err.Error())))
return
}
}
if u := getUserForSession(ctx); u != nil {
resp.User = u.Copy(false)
if err := u.ValidatePassword(req.OldPassword); err != nil {
resp.RequestStatus = cmonapi.RequestStatusAccessDenied
resp.ErrorString = "wrong old password"
} else if len(req.NewPassword) < 1 {
resp.RequestStatus = cmonapi.RequestStatusInvalidRequest
resp.ErrorString = "invalid new password"
} else {
resp.RequestStatus = cmonapi.RequestStatusOk
resp.ErrorString = ""
u.SetPassword(req.NewPassword)
if err := p.r.Config.UpdateUser(u); err != nil {
resp.RequestStatus = cmonapi.RequestStatusUnknownError
resp.ErrorString = "failed to update user: " + err.Error()
} else {
// also update the user in session
setUserForSession(ctx, u)
resp.RequestStatus = cmonapi.RequestStatusOk
resp.ErrorString = ""
}
}
}
ctx.JSON(cmonapi.RequestStatusToStatusCode(resp.RequestStatus), resp)
}