/
middleware.go
203 lines (170 loc) · 6.37 KB
/
middleware.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package jwt
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"errors"
"context"
jwt "github.com/dgrijalva/jwt-go"
"github.com/gkarlik/quark-go/logger"
)
// Credentials represents user credentials (username and password).
type Credentials struct {
Username string `json:"Username"` // user name
Password string `json:"Password"` // password
Properties map[string]string `json:"Properties,omitempty"` // additional properties
}
// Claims represents jwt claims.
type Claims struct {
Username string `json:"username"` // user name
Properties map[string]interface{} `json:"properties"` // additional jwt claims properties
jwt.StandardClaims // standard jwt claims properties
}
const componentName = "JwtAuthenticationMiddleware"
// AuthenticationMiddleware represents HTTP middleware responsible for authentication (JWT based).
type AuthenticationMiddleware struct {
Options Options // authentication middleware options
}
// AuthenticationFunc is a function used to authenticate user. Function receives user credentials and should return claims or an error.
type AuthenticationFunc func(credentials Credentials) (Claims, error)
// NewAuthenticationMiddleware creates instance of authentication middleware with options passed as argument.
// AuthenticationFunc and Secret options are required.
// Default context key value used to store jwt claims in request context is "Claims".
func NewAuthenticationMiddleware(opts ...Option) *AuthenticationMiddleware {
am := &AuthenticationMiddleware{
Options: Options{
ContextKey: "Claims",
},
}
for _, opt := range opts {
opt(&am.Options)
}
if am.Options.Authenticate == nil {
panic(fmt.Sprintf("[%s]: Cannot create instance - authentication function must be set!", componentName))
}
if am.Options.Secret == "" {
panic(fmt.Sprintf("[%s]: Cannot create instance - secret must be set!", componentName))
}
return am
}
func (am AuthenticationMiddleware) authenticate(w http.ResponseWriter, r *http.Request) (context.Context, error) {
logger.Log().DebugWithFields(logger.Fields{
"component": componentName,
}, "Authenticating user request")
// parse token from Authorization header
ah := r.Header.Get("Authorization")
if ah == "" {
return nil, errors.New("No authorization header")
}
s := strings.Split(ah, " ")
if len(s) != 2 || strings.ToUpper(s[0]) != "BEARER" {
return nil, errors.New("Incorrect authorization header")
}
tokenString := s[1]
if tokenString == "" {
return nil, errors.New("TokenString is empty")
}
token, err := jwt.ParseWithClaims(tokenString, &Claims{}, func(t *jwt.Token) (interface{}, error) {
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, errors.New("Unexpected signing method")
}
return []byte(am.Options.Secret), nil
})
if err != nil {
logger.Log().DebugWithFields(logger.Fields{
"error": err,
"tokenString": tokenString,
"component": componentName,
}, "Error parsing token string")
return nil, errors.New("Error parsing token string")
}
// get tokens claims and pass it into the original request
if claims, ok := token.Claims.(*Claims); ok && token.Valid {
ctx := context.WithValue(r.Context(), am.Options.ContextKey, *claims)
return ctx, nil
}
return nil, errors.New("Token is invalid")
}
// Authenticate validates token using jwt specification. It parses token from 'Authorization' header which must be in form "bearer token".
func (am AuthenticationMiddleware) Authenticate(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx, err := am.authenticate(w, r)
if err != nil {
logger.Log().ErrorWithFields(logger.Fields{"component": componentName, "error": err}, "Could not authentiate user")
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
if next != nil {
next.ServeHTTP(w, r.WithContext(ctx))
}
})
}
// AuthenticateWithNext validates token using jwt specification. It parses token from 'Authorization' header which must be in form "bearer token".
// This is method to support Negroni library.
func (am AuthenticationMiddleware) AuthenticateWithNext(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
ctx, err := am.authenticate(w, r)
if err != nil {
logger.Log().ErrorWithFields(logger.Fields{"component": componentName, "error": err}, "Could not authentiate user")
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
if next != nil {
next(w, r.WithContext(ctx))
}
}
// GenerateToken generates token using jwt specification, only HTTP POST method is allowed.
func (am AuthenticationMiddleware) GenerateToken(w http.ResponseWriter, r *http.Request) {
logger.Log().DebugWithFields(logger.Fields{
"component": componentName,
}, "Generating token for the user")
// check if method is POST
if r.Method != http.MethodPost {
logger.Log().ErrorWithFields(logger.Fields{
"method": r.Method,
"component": componentName,
}, "Incorrect http method")
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// load credentials from request body
var credentials Credentials
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&credentials); err != nil {
logger.Log().ErrorWithFields(logger.Fields{
"error": err,
"credentials": credentials,
"component": componentName,
}, "Cannot decode credentials")
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
// authenticate user using external function
claims, err := am.Options.Authenticate(credentials)
if err != nil {
logger.Log().ErrorWithFields(logger.Fields{
"error": err,
"component": componentName,
}, "Cannot authenticate user")
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
// create token with claims
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
tokenString, err := token.SignedString([]byte(am.Options.Secret))
if err != nil {
logger.Log().ErrorWithFields(logger.Fields{
"error": err,
"component": componentName,
}, "Cannot sign token")
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
logger.Log().InfoWithFields(logger.Fields{
"token": tokenString,
"component": componentName,
}, "Token generated - sending to client")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "{ \"token\": %q }", tokenString)
}