rob05c commented on a change in pull request #3932: Rewrote /user/login/token
to Go
URL: https://github.com/apache/trafficcontrol/pull/3932#discussion_r327211858
##########
File path: traffic_ops/traffic_ops_golang/login/login.go
##########
@@ -115,6 +115,47 @@ func LoginHandler(db *sqlx.DB, cfg config.Config)
http.HandlerFunc {
}
}
+func TokenLoginHandler(db *sqlx.DB, cfg config.Config) http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ defer r.Body.Close()
+ var t tc.UserToken
+ if err := json.NewDecoder(r.Body).Decode(&t); err != nil {
+ api.HandleErr(w, r, nil, http.StatusBadRequest,
fmt.Errorf("Invalid request: %v", err), nil)
+ return
+ }
+
+ tokenMatches, username, err :=
auth.CheckLocalUserToken(t.Token, db,
time.Duration(cfg.DBQueryTimeoutSeconds)*time.Second)
+ if err != nil {
+ sysErr := fmt.Errorf("Checking token: %v", err)
+ errCode := http.StatusInternalServerError
+ api.HandleErr(w, r, nil, errCode, nil, sysErr)
+ return
+ } else if !tokenMatches {
+ userErr := errors.New("Invalid token. Please contact
your administrator.")
+ errCode := http.StatusUnauthorized
+ api.HandleErr(w, r, nil, errCode, userErr, nil)
+ return
+ }
+
+ expiry := time.Now().Add(time.Hour * 6)
+ cookie := tocookie.New(username, expiry, cfg.Secrets[0])
+ httpCookie := http.Cookie{Name: "mojolicious", Value: cookie,
Path: "/", Expires: expiry, HttpOnly: true}
+ http.SetCookie(w, &httpCookie)
+ respBts, err := json.Marshal(tc.CreateAlerts(tc.SuccessLevel,
"Successfully logged in."))
+ if err != nil {
+ sysErr := fmt.Errorf("Marshaling response: %v", err)
+ errCode := http.StatusInternalServerError
+ api.HandleErr(w, r, nil, errCode, nil, sysErr)
+ return
+ }
+
+ w.Header().Set(tc.ContentType, tc.ApplicationJson)
+ w.Write(append(respBts, '\n'))
Review comment:
+1 on `\n`.
The trailing newline is still valid JSON, and makes it also a valid POSIX
file. @ocket8888 has convinced me we ought to add it to all our endpoints,
there's just no reason not to.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services