/
auth.go
401 lines (359 loc) · 13.8 KB
/
auth.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package authentication
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"reflect"
"time"
"github.com/gobuffalo/pop"
"github.com/gofrs/uuid"
beeline "github.com/honeycombio/beeline-go"
"github.com/markbates/goth/providers/openidConnect"
"github.com/pkg/errors"
"go.uber.org/zap"
"github.com/transcom/mymove/pkg/auth"
"github.com/transcom/mymove/pkg/models"
)
// UserAuthMiddleware enforces that the incoming request is tied to a user session
func UserAuthMiddleware(logger *zap.Logger) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
mw := func(w http.ResponseWriter, r *http.Request) {
ctx, span := beeline.StartSpan(r.Context(), "UserAuthMiddleware")
defer span.Send()
session := auth.SessionFromRequestContext(r)
// We must have a logged in session and a user
if session == nil || session.UserID == uuid.Nil {
logger.Error("unauthorized access")
http.Error(w, http.StatusText(401), http.StatusUnauthorized)
return
}
// TODO: Add support for BackupContacts
// And this must be the right type of user for the application
if session.IsOfficeApp() && session.OfficeUserID == uuid.Nil {
logger.Error("unauthorized user for office.move.mil", zap.String("email", session.Email))
http.Error(w, http.StatusText(401), http.StatusUnauthorized)
return
}
// This must be the right type of user for the application
if session.IsTspApp() && session.TspUserID == uuid.Nil {
logger.Error("unauthorized user for tsp.move.mil", zap.String("email", session.Email))
http.Error(w, http.StatusText(401), http.StatusUnauthorized)
return
}
// Include session office, service member, tsp and user IDs to the beeline event
span.AddTraceField("auth.office_user_id", session.OfficeUserID)
span.AddTraceField("auth.service_member_id", session.ServiceMemberID)
span.AddTraceField("auth.tsp_user_id", session.TspUserID)
span.AddTraceField("auth.user_id", session.UserID)
next.ServeHTTP(w, r.WithContext(ctx))
return
}
return http.HandlerFunc(mw)
}
}
func (context Context) landingURL(session *auth.Session) string {
return fmt.Sprintf(context.callbackTemplate, session.Hostname)
}
// Context is the common handler type for auth handlers
type Context struct {
logger *zap.Logger
loginGovProvider LoginGovProvider
callbackTemplate string
}
// NewAuthContext creates an Context
func NewAuthContext(logger *zap.Logger, loginGovProvider LoginGovProvider, callbackProtocol string, callbackPort int) Context {
context := Context{
logger: logger,
loginGovProvider: loginGovProvider,
callbackTemplate: fmt.Sprintf("%s://%%s:%d/", callbackProtocol, callbackPort),
}
return context
}
// LogoutHandler handles logging the user out of login.gov
type LogoutHandler struct {
Context
clientAuthSecretKey string
noSessionTimeout bool
}
// NewLogoutHandler creates a new LogoutHandler
func NewLogoutHandler(ac Context, clientAuthSecretKey string, noSessionTimeout bool) LogoutHandler {
handler := LogoutHandler{
Context: ac,
clientAuthSecretKey: clientAuthSecretKey,
noSessionTimeout: noSessionTimeout,
}
return handler
}
func (h LogoutHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
session := auth.SessionFromRequestContext(r)
if session != nil {
redirectURL := h.landingURL(session)
if session.IDToken != "" {
var logoutURL string
// All users logged in via devlocal-auth will have this IDToken. We
// don't want to make a call to login.gov for a logout URL as it will
// fail for devlocal-auth'ed users.
if session.IDToken == "devlocal" {
logoutURL = "/"
} else {
logoutURL = h.loginGovProvider.LogoutURL(redirectURL, session.IDToken)
}
session.IDToken = ""
session.UserID = uuid.Nil
auth.WriteSessionCookie(w, session, h.clientAuthSecretKey, h.noSessionTimeout, h.logger)
http.Redirect(w, r, logoutURL, http.StatusTemporaryRedirect)
} else {
// Can't log out of login.gov without a token, redirect and let them re-auth
http.Redirect(w, r, redirectURL, http.StatusTemporaryRedirect)
}
}
}
// RedirectHandler handles redirection
type RedirectHandler struct {
Context
}
// RedirectHandler constructs the Login.gov authentication URL and redirects to it
func (h RedirectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
session := auth.SessionFromRequestContext(r)
if session != nil && session.UserID != uuid.Nil {
// User is already authenticated, redirect to landing page
http.Redirect(w, r, h.landingURL(session), http.StatusTemporaryRedirect)
return
}
authURL, err := h.loginGovProvider.AuthorizationURL(r)
if err != nil {
http.Error(w, http.StatusText(500), http.StatusInternalServerError)
return
}
http.Redirect(w, r, authURL, http.StatusTemporaryRedirect)
}
// CallbackHandler processes a callback from login.gov
type CallbackHandler struct {
Context
db *pop.Connection
clientAuthSecretKey string
noSessionTimeout bool
}
// NewCallbackHandler creates a new CallbackHandler
func NewCallbackHandler(ac Context, db *pop.Connection, clientAuthSecretKey string, noSessionTimeout bool) CallbackHandler {
handler := CallbackHandler{
Context: ac,
db: db,
clientAuthSecretKey: clientAuthSecretKey,
noSessionTimeout: noSessionTimeout,
}
return handler
}
// AuthorizationCallbackHandler handles the callback from the Login.gov authorization flow
func (h CallbackHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
_, span := beeline.StartSpan(r.Context(), reflect.TypeOf(h).Name())
defer span.Send()
session := auth.SessionFromRequestContext(r)
if session == nil {
h.logger.Error("Session missing")
http.Error(w, http.StatusText(500), http.StatusInternalServerError)
return
}
lURL := h.landingURL(session)
if err := r.URL.Query().Get("error"); len(err) > 0 {
switch err {
case "access_denied":
// The user has either cancelled or declined to authorize the client
http.Redirect(w, r, lURL, http.StatusTemporaryRedirect)
case "invalid_request":
h.logger.Error("INVALID_REQUEST error from login.gov")
http.Error(w, http.StatusText(500), http.StatusInternalServerError)
default:
h.logger.Error("unknown error from login.gov")
http.Error(w, http.StatusText(500), http.StatusInternalServerError)
}
return
}
provider, err := getLoginGovProviderForRequest(r)
if err != nil {
h.logger.Error("Get Goth provider", zap.Error(err))
http.Error(w, http.StatusText(500), http.StatusInternalServerError)
return
}
// TODO: validate the state is the same (pull from session)
openIDSession, err := fetchToken(
h.logger,
r.URL.Query().Get("code"),
provider.ClientKey,
h.loginGovProvider)
if err != nil {
http.Error(w, http.StatusText(401), http.StatusUnauthorized)
return
}
openIDUser, err := provider.FetchUser(openIDSession)
if err != nil {
h.logger.Error("Login.gov user info request", zap.Error(err))
http.Error(w, http.StatusText(500), http.StatusInternalServerError)
return
}
session.IDToken = openIDSession.IDToken
session.Email = openIDUser.Email
h.logger.Info("New Login", zap.String("OID_User", openIDUser.UserID), zap.String("OID_Email", openIDUser.Email), zap.String("Host", session.Hostname))
userIdentity, err := models.FetchUserIdentity(h.db, openIDUser.UserID)
if err == nil { // Someone we know already
if userIdentity.Disabled {
h.logger.Error("Disabled user requesting authentication", zap.String("email", session.Email))
http.Error(w, http.StatusText(403), http.StatusForbidden)
return
}
session.UserID = userIdentity.ID
span.AddField("session.user_id", session.UserID)
if userIdentity.ServiceMemberID != nil {
session.ServiceMemberID = *(userIdentity.ServiceMemberID)
span.AddField("session.service_member_id", session.ServiceMemberID)
}
if userIdentity.DpsUserID != nil {
session.DpsUserID = *(userIdentity.DpsUserID)
}
if userIdentity.OfficeUserID != nil {
session.OfficeUserID = *(userIdentity.OfficeUserID)
} else if session.IsOfficeApp() {
// In case they managed to login before the office_user record was created
officeUser, err := models.FetchOfficeUserByEmail(h.db, session.Email)
if err == models.ErrFetchNotFound {
h.logger.Error("Non-office user authenticated at office site", zap.String("email", session.Email))
http.Error(w, http.StatusText(401), http.StatusUnauthorized)
return
} else if err != nil {
h.logger.Error("Checking for office user", zap.String("email", session.Email), zap.Error(err))
http.Error(w, http.StatusText(500), http.StatusInternalServerError)
return
}
session.OfficeUserID = officeUser.ID
span.AddField("session.office_user_id", session.OfficeUserID)
officeUser.UserID = &userIdentity.ID
err = h.db.Save(officeUser)
if err != nil {
h.logger.Error("Updating office user", zap.String("email", session.Email), zap.Error(err))
http.Error(w, http.StatusText(500), http.StatusInternalServerError)
return
}
}
if userIdentity.TspUserID != nil {
session.TspUserID = *(userIdentity.TspUserID)
} else if session.IsTspApp() {
// In case they managed to login before the tsp_user record was created
tspUser, err := models.FetchTspUserByEmail(h.db, session.Email)
if err == models.ErrFetchNotFound {
h.logger.Error("Non-TSP user authenticated at tsp site", zap.String("email", session.Email))
http.Error(w, http.StatusText(401), http.StatusUnauthorized)
return
} else if err != nil {
h.logger.Error("Checking for TSP user", zap.String("email", session.Email), zap.Error(err))
http.Error(w, http.StatusText(500), http.StatusInternalServerError)
return
}
session.TspUserID = tspUser.ID
span.AddField("session.tsp_user_id", session.TspUserID)
tspUser.UserID = &userIdentity.ID
err = h.db.Save(tspUser)
if err != nil {
h.logger.Error("Updating TSP user", zap.String("email", session.Email), zap.Error(err))
http.Error(w, http.StatusText(500), http.StatusInternalServerError)
return
}
}
session.FirstName = userIdentity.FirstName()
session.LastName = userIdentity.LastName()
session.Middle = userIdentity.Middle()
} else if err == models.ErrFetchNotFound { // Never heard of them so far
var officeUser *models.OfficeUser
if session.IsOfficeApp() { // Look to see if we have OfficeUser with this email address
officeUser, err = models.FetchOfficeUserByEmail(h.db, session.Email)
if err == models.ErrFetchNotFound {
h.logger.Error("No Office user found", zap.String("email", session.Email))
http.Error(w, http.StatusText(401), http.StatusUnauthorized)
return
} else if err != nil {
h.logger.Error("Checking for office user", zap.String("email", session.Email), zap.Error(err))
http.Error(w, http.StatusText(500), http.StatusInternalServerError)
return
}
}
var tspUser *models.TspUser
if session.IsTspApp() { // Look to see if we have TspUser with this email address
tspUser, err = models.FetchTspUserByEmail(h.db, session.Email)
if err == models.ErrFetchNotFound {
h.logger.Error("No TSP user found", zap.String("email", session.Email))
http.Error(w, http.StatusText(401), http.StatusUnauthorized)
return
} else if err != nil {
h.logger.Error("Checking for TSP user", zap.String("email", session.Email), zap.Error(err))
http.Error(w, http.StatusText(500), http.StatusInternalServerError)
return
}
}
user, err := models.CreateUser(h.db, openIDUser.UserID, openIDUser.Email)
if err == nil { // Successfully created the user
session.UserID = user.ID
span.AddField("session.user_id", session.UserID)
if officeUser != nil {
session.OfficeUserID = officeUser.ID
span.AddField("session.office_user_id", session.OfficeUserID)
officeUser.UserID = &user.ID
err = h.db.Save(officeUser)
} else if tspUser != nil {
session.TspUserID = tspUser.ID
span.AddField("session.tsp_user_id", session.TspUserID)
tspUser.UserID = &user.ID
err = h.db.Save(tspUser)
}
}
if err != nil {
h.logger.Error("Error creating user", zap.Error(err))
http.Error(w, http.StatusText(500), http.StatusInternalServerError)
return
}
} else {
h.logger.Error("Error loading Identity.", zap.Error(err))
http.Error(w, http.StatusText(500), http.StatusInternalServerError)
return
}
h.logger.Info("logged in", zap.Any("session", session))
auth.WriteSessionCookie(w, session, h.clientAuthSecretKey, h.noSessionTimeout, h.logger)
http.Redirect(w, r, lURL, http.StatusTemporaryRedirect)
}
func fetchToken(logger *zap.Logger, code string, clientID string, loginGovProvider LoginGovProvider) (*openidConnect.Session, error) {
tokenURL := loginGovProvider.TokenURL()
expiry := auth.GetExpiryTimeFromMinutes(auth.SessionExpiryInMinutes)
params, err := loginGovProvider.TokenParams(code, clientID, expiry)
if err != nil {
logger.Error("Creating token endpoint params", zap.Error(err))
return nil, err
}
/* #nosec G107 */
response, err := http.PostForm(tokenURL, params)
if err != nil {
logger.Error("Post to Login.gov token endpoint", zap.Error(err))
return nil, err
}
defer response.Body.Close()
responseBody, err := ioutil.ReadAll(response.Body)
if err != nil {
logger.Error("Reading Login.gov token response", zap.Error(err))
return nil, err
}
var parsedResponse LoginGovTokenResponse
err = json.Unmarshal(responseBody, &parsedResponse)
if err != nil {
logger.Error("Parsing login.gov token", zap.Error(err))
return nil, errors.Wrap(err, "parsing login.gov")
}
if parsedResponse.Error != "" {
logger.Error("Error in Login.gov token response", zap.String("error", parsedResponse.Error))
return nil, errors.New(parsedResponse.Error)
}
// TODO: get goth session from storage instead of constructing a new one
session := openidConnect.Session{
AccessToken: parsedResponse.AccessToken,
ExpiresAt: time.Now().Add(time.Second * time.Duration(parsedResponse.ExpiresIn)),
IDToken: parsedResponse.IDToken,
}
return &session, err
}