/
auth.go
119 lines (108 loc) · 3.99 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
package service
import (
"errors"
"github.com/gorilla/mux"
g "github.com/warreq/gohstd/common"
bcrypt "golang.org/x/crypto/bcrypt"
"log"
"net/http"
)
// AuthorizationFilter is a function type used for determining if a user is
// authorized to perform a given request. It returns true if a user meets the
// requirements of the authorization logic, and false otherwise.
type AuthorizationFilter func(user string, r *http.Request) bool
// AuthenticationBlackList contains the sets of service/Routes for which
// authentication is not necessary. Routes listed here bypass the authentication
// middleware and subsequently the authorization middleware as well, as to allow
// anonymous access to those endpoints.
var AuthenticationBlackList map[string]struct{}
// AuthorizationFilters contains the slice of functions for each service/Route
// to use for authorizing that endpoint. If a Route isn't found in the map, it
// requires no authorization
var AuthorizationFilters map[string][]AuthorizationFilter
func init() {
AuthenticationBlackList = map[string]struct{}{
"UserRegister": {},
"GetPosts": {},
}
AuthorizationFilters = map[string][]AuthorizationFilter{
"CommandIndex": []AuthorizationFilter{RequesterOwnsResource},
"CommandCreate": []AuthorizationFilter{RequesterOwnsResource},
"CommandDelete": []AuthorizationFilter{RequesterOwnsResource},
"CommandTagCreate": []AuthorizationFilter{RequesterOwnsResource},
"UserTagShow": []AuthorizationFilter{RequesterOwnsResource},
"UserTagRename": []AuthorizationFilter{RequesterOwnsResource},
"UserTagDelete": []AuthorizationFilter{RequesterOwnsResource},
}
}
// Auth is the http.Handler middleware for performinag authentication and
// authorization on a request before furthering the request to its actual handler.
// It consults the AuthenticationBlackList and AuthorizationBlackList to know
// whether to apply one, both or neither of these steps. Auth may short-circuit
// a request, returning http status codes 401 or 403. Otherwise, the request
//is handled as normal by the inner handler.
func Auth(inner http.Handler, route string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, blacklisted := AuthenticationBlackList[route]
if blacklisted {
// Authentication isn't required -- jump directly to inner handler
inner.ServeHTTP(w, r)
return
}
username, password, ok := r.BasicAuth()
if !ok {
log.Println(r)
log.Println(username)
log.Println(password)
HttpError(w, http.StatusUnauthorized, errors.New(g.InvalidBasicAuthError))
return
}
authenticated := Authenticate(username, password)
if !authenticated {
HttpError(w, http.StatusUnauthorized, nil)
return
}
authorized := Authorize(username, r, route)
if !authorized {
HttpError(w, http.StatusForbidden, nil)
return
}
// Success
inner.ServeHTTP(w, r)
})
}
// Authenticate compares the user and password provided and compares the stored
// credential for the given user against the given password. Authentication
// returns true in the case that the given password matches the stored
// credential and false otherwise.
func Authenticate(user, password string) bool {
identity, err := userRepo.GetUserByName(user)
if err != nil {
log.Println(err)
return false
}
credential := identity.Password
err = bcrypt.CompareHashAndPassword([]byte(credential), []byte(password))
if err != nil {
return false
} else {
return true
}
}
// Authorize searches the AuthorizationFilters for functions pertaining to the
// given request and applies each filter in order. If any filter returns false,
// Authorize returns false. Otherwise and including the case of no filters,
// Authorize returns true.
func Authorize(user string, r *http.Request, route string) bool {
for _, filter := range AuthorizationFilters[route] {
if !filter(user, r) {
return false
}
}
return true
}
func RequesterOwnsResource(user string, r *http.Request) bool {
vars := mux.Vars(r)
owner := vars["username"]
return user == owner
}