-
Notifications
You must be signed in to change notification settings - Fork 1
/
authenticate.go
244 lines (227 loc) · 8.62 KB
/
authenticate.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
package middleware
import (
"fmt"
"net/http"
"strconv"
"strings"
"github.com/APTrust/registry/common"
"github.com/APTrust/registry/constants"
"github.com/APTrust/registry/helpers"
"github.com/APTrust/registry/pgmodels"
"github.com/gin-gonic/gin"
)
// Authenticate ensures the current user is logged in for all requests other
// than those going to "/" or static resources.
func Authenticate() gin.HandlerFunc {
return func(c *gin.Context) {
LoadCookies(c)
SetDefaultHeaders(c)
// If in maintenance mode, redirect to maintenance mode page.
// Don't redirect requests to /maintenance, or you'll get into an infinite loop.
// Don't redirect requests to / or ECS health checks will fail.
if common.Context().Config.MaintenanceMode && c.Request.URL.Path != "/maintenance" && c.Request.URL.Path != "/" {
redirectTo := "/maintenance"
if IsAPIRequest(c) || IsAPIRoute(c) {
redirectTo += "?format=json"
}
c.Redirect(http.StatusFound, redirectTo)
c.Abort()
}
var user *pgmodels.User
var err error
if !ExemptFromAuth(c) {
user, err = GetUser(c)
if err != nil {
respondToAuthError(c, err)
c.Abort()
} else {
c.Set("CurrentUser", user)
}
}
if forceCompletionOfPasswordChange(c, user) {
logPasswordChangeIncomplete(c, user)
c.HTML(http.StatusUnauthorized, "errors/show.html", gin.H{
"suppressSideNav": true,
"suppressTopNav": true,
"error": "Please finish changing your password",
})
c.Abort()
}
if forceCompletionOfTwoFactorAuth(c, user) {
log2FAIncomplete(c, user)
c.Redirect(http.StatusFound, "/users/2fa_choose/")
c.Abort()
}
c.Next()
}
}
func GetUser(c *gin.Context) (user *pgmodels.User, err error) {
apiUserEmail := c.Request.Header.Get(constants.APIUserHeader)
apiUserKey := c.Request.Header.Get(constants.APIKeyHeader)
if apiUserEmail != "" && apiUserKey != "" {
return GetUserFromAPIHeaders(c)
}
return GetUserFromSession(c)
}
// GetUserFromSession returns the User for the current session.
func GetUserFromSession(c *gin.Context) (user *pgmodels.User, err error) {
ctx := common.Context()
cookie, err := c.Cookie(ctx.Config.Cookies.SessionCookie)
if err == nil {
value := ""
if err = ctx.Config.Cookies.Secure.Decode(ctx.Config.Cookies.SessionCookie, cookie, &value); err != nil {
ctx.Log.Error().Msgf("GetUserFromSession: Error decoding session cookie: %v", err)
return nil, common.ErrDecodeCookie
}
var userID int64
userID, err = strconv.ParseInt(value, 10, 64)
if err != nil {
ctx.Log.Error().Msgf("GetUserFromSession: Session cookie contains non-numeric user id: %s", value)
return nil, common.ErrWrongDataType
}
user, err = pgmodels.UserByID(userID)
if err != nil {
ctx.Log.Error().Msgf("GetUserFromSession: Got user id from session cookie but user lookup returned error: %v", err)
}
} else {
// This is for a specific and recurrent auth error.
// https://trello.com/c/rHKhPkau
err = fmt.Errorf("%v - missing cookie is %s", err, ctx.Config.Cookies.SessionCookie)
}
return user, err
}
// GetUserFromAPIHeaders returns the current user based on the API
// auth headers.
func GetUserFromAPIHeaders(c *gin.Context) (user *pgmodels.User, err error) {
ctx := common.Context()
apiUserEmail := c.Request.Header.Get(constants.APIUserHeader)
apiUserKey := c.Request.Header.Get(constants.APIKeyHeader)
user, err = pgmodels.UserByEmail(apiUserEmail)
if err != nil {
ctx.Log.Error().Msgf("GetUserFromAPIHeaders: Attempt to look up user %s failed with error %v", apiUserEmail, err)
return nil, err
}
if common.ComparePasswords(user.EncryptedAPISecretKey, apiUserKey) {
// Set this because API requests bypass CSRF protection and
// we want to ensure user passed valid auth headers. This
// prevents a CSRF hijack where bad actor sends XHR PUT/POST
// with valid user cookie. For API, we accept the token header
// only, which won't be present in the browser environment and
// therefore can't be hijacked.
c.Set("UserIsApiAuthenticated", true)
return user, nil
}
ctx.Log.Warn().Msgf("Invalid API token from user %s at %s.", apiUserEmail, c.Request.RemoteAddr)
helpers.DeleteSessionCookie(c) // just to be extra safe
return nil, common.ErrInvalidAPICredentials
}
// LoadCookies loads the user's flash and preference cookes into
// the request context.
func LoadCookies(c *gin.Context) error {
ctx := common.Context()
err := LoadCookie(c, ctx.Config.Cookies.FlashCookie)
if err != nil && err != http.ErrNoCookie {
return err
}
err = LoadCookie(c, ctx.Config.Cookies.PrefsCookie)
if err != nil && err != http.ErrNoCookie {
return err
}
return nil
}
// LoadCookie loads a cookie's value into the request context.
func LoadCookie(c *gin.Context, name string) error {
ctx := common.Context()
cookie, err := c.Cookie(name)
if err != nil {
return err
}
value := ""
if err = ctx.Config.Cookies.Secure.Decode(name, cookie, &value); err != nil {
return common.ErrDecodeCookie
}
c.Set(name, value)
return nil
}
// SetDefaultHeaders sets headers that we want to include with every response.
// Note that it's OK for client to cache and store static resources such as
// images, scripts and stylesheets. Those are public resources containing no
// sensitive info. All other resources must use no-cache/no-store.
func SetDefaultHeaders(c *gin.Context) {
p := c.FullPath()
if !strings.HasPrefix(p, "/static") && !strings.HasPrefix(p, "/favicon") {
c.Writer.Header().Set("Cache-Control", "no-cache")
c.Writer.Header().Set("Pragma", "no-store")
}
// This header tells the client to use HTTPS only. We can only set
// this on systems that have certificates (staging, demo, production);
// it won't work on dev/localhost setups, so check to see if config
// says to run in HTTPSOnly mode.
if common.Context().Config.Cookies.HTTPSOnly {
c.Writer.Header().Set("Strict-Transport-Security", "max-age=31536000")
}
// Set these security headers on all responses.
c.Writer.Header().Set("X-XSS-Protection", "1")
c.Writer.Header().Set("X-Content-Type-Options", "nosniff")
c.Writer.Header().Set("Content-Security-Policy", "default-src 'self'; font-src 'self' fonts.gstatic.com; style-src 'self' 'unsafe-inline' fonts.googleapis.com; script-src 'self' 'unsafe-inline'")
}
func forceCompletionOfPasswordChange(c *gin.Context, currentUser *pgmodels.User) bool {
if currentUser == nil {
return false // user isn't even signed in
}
p := c.FullPath()
return currentUser.ResetPasswordToken != "" &&
!strings.HasPrefix(p, "/users/change_password/") &&
!strings.HasPrefix(p, "/errors/show/")
}
func logPasswordChangeIncomplete(c *gin.Context, currentUser *pgmodels.User) {
common.Context().Log.Warn().Msgf("Password change incomplete. User %s tried to access URL [%s]. Forcing user to complete password change.", currentUser.Email, c.Request.RequestURI)
}
func forceCompletionOfTwoFactorAuth(c *gin.Context, currentUser *pgmodels.User) bool {
if currentUser == nil {
return false // user isn't even signed in
}
p := c.FullPath()
return currentUser.ResetPasswordToken == "" &&
currentUser.AwaitingSecondFactor &&
!strings.HasPrefix(p, "/users/2fa_backup") &&
!strings.HasPrefix(p, "/users/2fa_choose") &&
!strings.HasPrefix(p, "/users/2fa_push") &&
!strings.HasPrefix(p, "/users/2fa_sms") &&
!strings.HasPrefix(p, "/users/2fa_verify")
}
func log2FAIncomplete(c *gin.Context, currentUser *pgmodels.User) {
common.Context().Log.Warn().Msgf("Two-factor auth incomplete. User %s tried to access URL [%s]. Forcing user to complete two-factor authentication.", currentUser.Email, c.Request.RequestURI)
}
func respondToAuthError(c *gin.Context, err error) {
if IsAPIRequest(c) || IsAPIRoute(c) {
msg := "API credentials are missing or invalid."
common.Context().Log.Warn().Msgf("AuthError: %s", msg)
obj := map[string]interface{}{
"StatusCode": http.StatusUnauthorized,
"Error": msg,
}
c.JSON(http.StatusUnauthorized, obj)
} else {
common.Context().Log.Warn().Msgf("AuthError: %s. IP: %s, URL: %s, Agent: %s, Referer: %s", err.Error(), c.Request.RemoteAddr, c.Request.RequestURI, c.Request.UserAgent(), c.Request.Referer())
c.HTML(http.StatusUnauthorized, "errors/show.html", gin.H{
"suppressSideNav": true,
"suppressTopNav": true,
"error": "Please log in",
"redirectURL": fmt.Sprintf("/?requrl=%s", c.Request.URL),
})
}
}
func ExemptFromAuth(c *gin.Context) bool {
p := c.FullPath()
return p == "/" ||
p == "/users/sign_in" ||
p == "/users/sign_out" ||
p == "/users/forgot_password" ||
p == "/ui_components" ||
p == "/maintenance" ||
strings.HasPrefix(p, "/static") ||
strings.HasPrefix(p, "/favicon") ||
strings.HasPrefix(p, "/error") ||
strings.HasPrefix(p, "/users/complete_password_reset/")
}