/
authentication.go
91 lines (79 loc) · 2.1 KB
/
authentication.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
package middleware
import (
"context"
"net/http"
"regexp"
"strings"
"github.com/godiscourse/godiscourse/api/models"
"github.com/godiscourse/godiscourse/api/session"
"github.com/godiscourse/godiscourse/api/views"
)
var whitelist = [][2]string{
{"GET", "^/_hc$"},
{"GET", "^/categories"},
{"GET", "^/topics"},
{"GET", "^/users"},
{"POST", "^/oauth"},
}
var userWhitelist = [][2]string{
{"GET", "^/me"},
{"POST", "^/comments"},
{"POST", "^/topics"},
{"POST", "^/me"},
}
type contextValueKey int
const keyCurrentUser contextValueKey = 1000
// CurrentUser read the user from r.Context
func CurrentUser(r *http.Request) *models.User {
user, _ := r.Context().Value(keyCurrentUser).(*models.User)
return user
}
// Authenticate handle routes by user's role
func Authenticate(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
header := r.Header.Get("Authorization")
if !strings.HasPrefix(header, "Bearer ") {
handleUnauthorized(handler, w, r)
return
}
user, err := models.AuthenticateUser(r.Context(), header[7:])
if err != nil {
views.RenderErrorResponse(w, r, err)
return
}
if user == nil {
handleUnauthorized(handler, w, r)
return
}
ctx := context.WithValue(r.Context(), keyCurrentUser, user)
if user.Role() != "admin" {
handleUserRouters(handler, w, r.WithContext(ctx))
return
}
handler.ServeHTTP(w, r.WithContext(ctx))
})
}
func handleUnauthorized(handler http.Handler, w http.ResponseWriter, r *http.Request) {
for _, pp := range whitelist {
if pp[0] != r.Method {
continue
}
if matched, _ := regexp.MatchString(pp[1], strings.ToLower(r.URL.Path)); matched {
handler.ServeHTTP(w, r)
return
}
}
views.RenderErrorResponse(w, r, session.AuthorizationError(r.Context()))
}
func handleUserRouters(handler http.Handler, w http.ResponseWriter, r *http.Request) {
for _, pp := range userWhitelist {
if pp[0] != r.Method {
continue
}
if matched, _ := regexp.MatchString(pp[1], strings.ToLower(r.URL.Path)); matched {
handler.ServeHTTP(w, r)
return
}
}
handleUnauthorized(handler, w, r)
}