-
Notifications
You must be signed in to change notification settings - Fork 204
/
auth_handler.go
147 lines (118 loc) · 3.91 KB
/
auth_handler.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package handlers
import (
"net/http"
"strings"
log "github.com/sirupsen/logrus"
"encoding/json"
"github.com/SpectoLabs/hoverfly/core/authentication"
"github.com/SpectoLabs/hoverfly/core/authentication/backends"
"github.com/codegangsta/negroni"
"github.com/go-zoo/bone"
)
type AuthHandler struct {
AB backends.Authentication
SecretKey []byte
JWTExpirationDelta int
Enabled bool
}
func (this *AuthHandler) RegisterRoutes(mux *bone.Mux) {
mux.Post("/api/token-auth", http.HandlerFunc(this.Login))
mux.Options("/api/token-auth", negroni.New(
negroni.HandlerFunc(this.OptionsLogin),
))
mux.Get("/api/refresh-token-auth", negroni.New(
negroni.HandlerFunc(this.RequireTokenAuthentication),
negroni.HandlerFunc(this.RefreshToken),
))
mux.Get("/api/logout", negroni.New(
negroni.HandlerFunc(this.RequireTokenAuthentication),
negroni.HandlerFunc(this.Logout),
))
mux.Get("/api/users", negroni.New(
negroni.HandlerFunc(this.RequireTokenAuthentication),
negroni.HandlerFunc(this.GetAllUsersHandler),
))
}
func (a *AuthHandler) RequireTokenAuthentication(w http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
// if auth is disabled - do not check token
if !a.Enabled {
next(w, req)
return
}
if authorizationValue := req.Header.Get("Authorization"); authorizationValue != "" {
// Should be a bearer token
if len(authorizationValue) > 6 && strings.ToUpper(authorizationValue[0:7]) == "BEARER " {
if authentication.IsJwtTokenValid(authorizationValue[7:], a.AB, a.SecretKey, a.JWTExpirationDelta) {
next(w, req)
}
}
}
WriteErrorResponse(w, "", http.StatusUnauthorized)
}
type AllUsersResponse struct {
Users []backends.User `json:"users"`
}
func (a *AuthHandler) Login(w http.ResponseWriter, r *http.Request) {
if !a.Enabled {
// returning dummy token
token := `{"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkR1bW15IHRva2VuIiwiYWRtaW4iOnRydWV9.sKfJparPo3LUmkYoGboBjVfOV3K1qWKUzqx9XFDEsAs"}`
WriteResponse(w, []byte(token))
return
}
requestUser := new(backends.User)
decoder := json.NewDecoder(r.Body)
decoder.Decode(&requestUser)
responseStatus, token := authentication.Login(requestUser, a.AB, a.SecretKey, a.JWTExpirationDelta)
if responseStatus == http.StatusOK {
WriteResponse(w, token)
} else {
WriteErrorResponse(w, "", responseStatus)
}
}
func (a *AuthHandler) OptionsLogin(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
w.Header().Add("Allow", "OPTIONS, POST")
WriteResponse(w, []byte(""))
}
func (a *AuthHandler) RefreshToken(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
requestUser := new(backends.User)
decoder := json.NewDecoder(r.Body)
decoder.Decode(&requestUser)
WriteResponse(w, authentication.RefreshToken(requestUser, a.AB, a.SecretKey, a.JWTExpirationDelta))
}
func (a *AuthHandler) Logout(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
if !a.Enabled {
w.WriteHeader(http.StatusOK)
return
}
err := authentication.Logout(r, a.AB, a.SecretKey, a.JWTExpirationDelta)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
} else {
w.WriteHeader(http.StatusOK)
}
WriteResponse(w, []byte(""))
}
// GetAllUsersHandler - returns a list of all users
func (a *AuthHandler) GetAllUsersHandler(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
users, err := a.AB.GetAllUsers()
if err == nil {
w.Header().Set("Content-Type", "application/json")
var response AllUsersResponse
response.Users = users
b, err := json.Marshal(response)
if err != nil {
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
} else {
WriteResponse(w, b)
return
}
} else {
log.WithFields(log.Fields{
"Error": err.Error(),
}).Error("Failed to get data from authentication backend!")
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(500)
return
}
}