2016-11-22 22:33:50 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2017-01-25 17:11:35 +01:00
|
|
|
"context"
|
2016-12-11 14:50:01 +01:00
|
|
|
"encoding/json"
|
2016-12-24 15:09:24 +02:00
|
|
|
"net/http"
|
|
|
|
|
2018-04-24 10:28:23 +02:00
|
|
|
"github.com/usefathom/fathom/pkg/datastore"
|
2016-12-11 14:50:01 +01:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2016-11-22 22:33:50 +01:00
|
|
|
)
|
|
|
|
|
2017-01-25 17:11:35 +01:00
|
|
|
type key int
|
|
|
|
|
|
|
|
const (
|
|
|
|
userKey key = 0
|
|
|
|
)
|
|
|
|
|
2016-12-24 15:09:24 +02:00
|
|
|
type login struct {
|
2016-12-11 14:50:01 +01:00
|
|
|
Email string `json:"email"`
|
|
|
|
Password string `json:"password"`
|
2016-12-08 18:44:49 +01:00
|
|
|
}
|
|
|
|
|
2016-11-23 15:51:19 +01:00
|
|
|
// URL: POST /api/session
|
2018-05-15 13:30:37 +02:00
|
|
|
func (api *API) LoginHandler(w http.ResponseWriter, r *http.Request) error {
|
2016-12-11 14:50:01 +01:00
|
|
|
// check login creds
|
2017-01-13 12:46:54 +01:00
|
|
|
var l login
|
|
|
|
json.NewDecoder(r.Body).Decode(&l)
|
2017-01-25 17:11:35 +01:00
|
|
|
|
2018-04-25 13:45:21 +02:00
|
|
|
// find user with given email
|
2018-05-15 13:30:37 +02:00
|
|
|
u, err := api.database.GetUserByEmail(l.Email)
|
2018-04-25 13:45:21 +02:00
|
|
|
if err != nil && err != datastore.ErrNoResults {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-08 18:44:49 +01:00
|
|
|
|
2017-01-13 12:46:54 +01:00
|
|
|
// compare pwd
|
2018-04-25 13:45:21 +02:00
|
|
|
if err == datastore.ErrNoResults || bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(l.Password)) != nil {
|
2016-12-11 14:50:01 +01:00
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
2018-04-25 11:59:30 +02:00
|
|
|
return respond(w, envelope{Error: "invalid_credentials"})
|
2016-12-11 14:50:01 +01:00
|
|
|
}
|
2016-12-08 18:44:49 +01:00
|
|
|
|
2018-05-15 13:30:37 +02:00
|
|
|
session, _ := api.sessions.Get(r, "auth")
|
2016-12-11 14:50:01 +01:00
|
|
|
session.Values["user_id"] = u.ID
|
|
|
|
err = session.Save(r, w)
|
2018-04-25 11:59:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-08 18:44:49 +01:00
|
|
|
|
2018-04-25 11:59:30 +02:00
|
|
|
return respond(w, envelope{Data: true})
|
2018-05-15 13:30:37 +02:00
|
|
|
}
|
2016-11-23 15:51:19 +01:00
|
|
|
|
|
|
|
// URL: DELETE /api/session
|
2018-05-15 13:30:37 +02:00
|
|
|
func (api *API) LogoutHandler(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
session, _ := api.sessions.Get(r, "auth")
|
2016-12-11 14:50:01 +01:00
|
|
|
if !session.IsNew {
|
|
|
|
session.Options.MaxAge = -1
|
2018-04-25 11:59:30 +02:00
|
|
|
err := session.Save(r, w)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-11 14:50:01 +01:00
|
|
|
}
|
2016-11-23 15:51:19 +01:00
|
|
|
|
2018-04-25 11:59:30 +02:00
|
|
|
return respond(w, envelope{Data: true})
|
2018-05-15 13:30:37 +02:00
|
|
|
}
|
2016-11-23 15:51:19 +01:00
|
|
|
|
|
|
|
/* middleware */
|
2018-05-15 13:30:37 +02:00
|
|
|
func (api *API) Authorize(next http.Handler) http.Handler {
|
2016-12-11 14:50:01 +01:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2018-05-15 13:30:37 +02:00
|
|
|
session, err := api.sessions.Get(r, "auth")
|
2018-05-15 14:20:05 +02:00
|
|
|
|
|
|
|
// an err is returned if cookie has been tampered with, so check that
|
2018-05-10 22:21:44 +02:00
|
|
|
if err != nil {
|
2018-05-15 14:20:05 +02:00
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
2018-05-10 22:21:44 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-12-11 14:50:01 +01:00
|
|
|
userID, ok := session.Values["user_id"]
|
2016-12-08 18:44:49 +01:00
|
|
|
|
2018-04-25 13:45:21 +02:00
|
|
|
if session.IsNew || !ok {
|
2016-12-11 14:50:01 +01:00
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
2016-11-22 22:33:50 +01:00
|
|
|
|
2016-12-11 14:50:01 +01:00
|
|
|
// find user
|
2018-05-15 13:30:37 +02:00
|
|
|
u, err := api.database.GetUser(userID.(int64))
|
2016-12-11 14:50:01 +01:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
2016-11-22 22:33:50 +01:00
|
|
|
|
2017-01-25 17:11:35 +01:00
|
|
|
ctx := context.WithValue(r.Context(), userKey, u)
|
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
2016-12-11 14:50:01 +01:00
|
|
|
})
|
2016-11-22 22:33:50 +01:00
|
|
|
}
|