fathom/pkg/api/auth.go

106 lines
2.3 KiB
Go
Raw Normal View History

2016-11-22 21:33:50 +00:00
package api
import (
2017-01-25 16:11:35 +00:00
"context"
2016-12-11 13:50:01 +00:00
"encoding/json"
2016-12-24 13:09:24 +00:00
"net/http"
"strings"
2016-12-24 13:09:24 +00:00
gcontext "github.com/gorilla/context"
"github.com/usefathom/fathom/pkg/datastore"
2016-11-22 21:33:50 +00:00
)
2017-01-25 16:11:35 +00:00
type key int
const (
userKey key = 0
)
2016-12-24 13:09:24 +00:00
type login struct {
2016-12-11 13:50:01 +00:00
Email string `json:"email"`
Password string `json:"password"`
}
func (l *login) Sanitize() {
l.Email = strings.ToLower(strings.TrimSpace(l.Email))
}
// URL: POST /api/session
func (api *API) LoginHandler(w http.ResponseWriter, r *http.Request) error {
2016-12-11 13:50:01 +00:00
// check login creds
var l login
err := json.NewDecoder(r.Body).Decode(&l)
if err != nil {
return err
}
l.Sanitize()
2017-01-25 16:11:35 +00:00
2018-04-25 11:45:21 +00:00
// find user with given email
u, err := api.database.GetUserByEmail(l.Email)
2018-04-25 11:45:21 +00:00
if err != nil && err != datastore.ErrNoResults {
return err
}
// compare pwd
if err == datastore.ErrNoResults || u.ComparePassword(l.Password) != nil {
2016-12-11 13:50:01 +00:00
w.WriteHeader(http.StatusUnauthorized)
return respond(w, envelope{Error: "invalid_credentials"})
2016-12-11 13:50:01 +00:00
}
// ignore error here as we want a (new) session regardless
session, _ := api.sessions.Get(r, "auth")
2016-12-11 13:50:01 +00:00
session.Values["user_id"] = u.ID
err = session.Save(r, w)
if err != nil {
return err
}
return respond(w, envelope{Data: true})
}
// URL: DELETE /api/session
func (api *API) LogoutHandler(w http.ResponseWriter, r *http.Request) error {
session, _ := api.sessions.Get(r, "auth")
2016-12-11 13:50:01 +00:00
if !session.IsNew {
session.Options.MaxAge = -1
err := session.Save(r, w)
if err != nil {
return err
}
2016-12-11 13:50:01 +00:00
}
return respond(w, envelope{Data: true})
}
/* middleware */
func (api *API) Authorize(next http.Handler) http.Handler {
2016-12-11 13:50:01 +00:00
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// clear context from request after it is handled
// see http://www.gorillatoolkit.org/pkg/sessions#overview
defer gcontext.Clear(r)
session, err := api.sessions.Get(r, "auth")
// an err is returned if cookie has been tampered with, so check that
2018-05-10 20:21:44 +00:00
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
2018-05-10 20:21:44 +00:00
return
}
2016-12-11 13:50:01 +00:00
userID, ok := session.Values["user_id"]
2018-04-25 11:45:21 +00:00
if session.IsNew || !ok {
2016-12-11 13:50:01 +00:00
w.WriteHeader(http.StatusUnauthorized)
return
}
2016-11-22 21:33:50 +00:00
2016-12-11 13:50:01 +00:00
// find user
u, err := api.database.GetUser(userID.(int64))
2016-12-11 13:50:01 +00:00
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
2016-11-22 21:33:50 +00:00
2017-01-25 16:11:35 +00:00
ctx := context.WithValue(r.Context(), userKey, u)
next.ServeHTTP(w, r.WithContext(ctx))
2016-12-11 13:50:01 +00:00
})
2016-11-22 21:33:50 +00:00
}