forked from usefathom/fathom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
133 lines (110 loc) · 3.17 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
package api
import (
"encoding/json"
"net/http"
"strings"
gcontext "github.com/gorilla/context"
"github.com/usefathom/fathom/pkg/datastore"
)
type key int
const (
userKey key = 0
)
type login struct {
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 {
// check login creds
var l login
err := json.NewDecoder(r.Body).Decode(&l)
if err != nil {
return err
}
l.Sanitize()
// find user with given email
u, err := api.database.GetUserByEmail(l.Email)
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"})
}
// ignore error here as we want a (new) session regardless
session, _ := api.sessions.Get(r, "auth")
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")
if !session.IsNew {
session.Options.MaxAge = -1
err := session.Save(r, w)
if err != nil {
return err
}
}
return respond(w, http.StatusOK, envelope{Data: true})
}
// Authorize is middleware that aborts the request if unauthorized
func (api *API) Authorize(next http.Handler) http.Handler {
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()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
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
}
}
next.ServeHTTP(w, r)
})
}