fathom/pkg/api/auth.go

134 lines
3.2 KiB
Go
Raw Normal View History

2016-11-22 21:33:50 +00:00
package api
import (
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))
}
// GET /api/session
func (api *API) GetSession(w http.ResponseWriter, r *http.Request) error {
userCount, err := api.database.CountUsers()
if err != nil {
return err
}
// if 0 users in database, dashboard is public
if userCount == 0 {
return respond(w, http.StatusOK, envelope{Data: true})
}
// if existing session, assume logged-in
session, _ := api.sessions.Get(r, "auth")
if !session.IsNew {
return respond(w, http.StatusOK, envelope{Data: true})
}
// otherwise: not logged-in yet
return respond(w, http.StatusOK, envelope{Data: false})
}
// URL: POST /api/session
func (api *API) CreateSession(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 {
return respond(w, http.StatusUnauthorized, 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, http.StatusOK, envelope{Data: true})
}
// URL: DELETE /api/session
func (api *API) DeleteSession(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, http.StatusOK, envelope{Data: true})
}
2018-09-12 07:11:50 +00:00
// Authorize is middleware that aborts the request if unauthorized
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)
// first count users in datastore
// if 0, assume dashboard is public
userCount, err := api.database.CountUsers()
2018-05-10 20:21:44 +00:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2016-12-11 13:50:01 +00:00
return
}
2016-11-22 21:33:50 +00:00
if userCount > 0 {
session, err := api.sessions.Get(r, "auth")
// an err is returned if cookie has been tampered with, so check that
if err != nil {
respond(w, http.StatusUnauthorized, envelope{Error: "unauthorized"})
return
}
userID, ok := session.Values["user_id"]
if session.IsNew || !ok {
respond(w, http.StatusUnauthorized, envelope{Error: "unauthorized"})
return
}
// validate user ID in session
if _, err := api.database.GetUser(userID.(int64)); err != nil {
respond(w, http.StatusUnauthorized, envelope{Error: "unauthorized"})
return
}
2016-12-11 13:50:01 +00:00
}
2016-11-22 21:33:50 +00:00
next.ServeHTTP(w, r)
2016-12-11 13:50:01 +00:00
})
2016-11-22 21:33:50 +00:00
}