auth_utility.go
6.61 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// TODO: Improve roles
package webutility
import (
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"errors"
"net/http"
"strings"
"time"
"github.com/dgrijalva/jwt-go"
)
const OneDay = time.Hour * 24
const OneWeek = OneDay * 7
const saltSize = 32
const appName = "korisnicki-centar"
const secret = "korisnicki-centar-api"
const RoleAdmin string = "ADMINISTRATOR"
const RoleManager string = "RUKOVODILAC"
const RoleReporter string = "REPORTER"
const RoleOperator string = "OPERATER"
const RoleAdminID uint32 = 1
const RoleManagerID uint32 = 2
const RoleReporterID uint32 = 3
const RoleOperatorID uint32 = 4
type Role struct {
name string
id uint32
}
// TokenClaims are JWT token claims.
type TokenClaims struct {
Username string `json:"username"`
Role string `json:"role"`
RoleID uint32 `json:"roleID"`
jwt.StandardClaims
}
// CredentialsStruct is an instace of username/password values.
type CredentialsStruct struct {
Username string `json:"username"`
Password string `json:"password"`
}
var admin Role = Role{RoleAdmin, RoleAdminID}
var manager Role = Role{RoleManager, RoleManagerID}
var reporter Role = Role{RoleReporter, RoleReporterID}
var operator Role = Role{RoleOperator, RoleOperatorID}
// generateSalt returns a string of random characters of 'saltSize' length.
func generateSalt() (salt string, err error) {
rawsalt := make([]byte, saltSize)
_, err = rand.Read(rawsalt)
if err != nil {
return "", err
}
salt = hex.EncodeToString(rawsalt)
return salt, nil
}
// HashString hashes input string with SHA256 algorithm.
// If the presalt parameter is not provided HashString will generate new salt string.
// Returns hash and salt string or an error if it fails.
func HashString(str string, presalt string) (hash, salt string, err error) {
// chech if message is presalted
if presalt == "" {
salt, err = generateSalt()
if err != nil {
return "", "", err
}
} else {
salt = presalt
}
// convert strings to raw byte slices
rawstr := []byte(str)
rawsalt, err := hex.DecodeString(salt)
if err != nil {
return "", "", err
}
rawdata := make([]byte, len(rawstr)+len(rawsalt))
rawdata = append(rawdata, rawstr...)
rawdata = append(rawdata, rawsalt...)
// hash message + salt
hasher := sha256.New()
hasher.Write(rawdata)
rawhash := hasher.Sum(nil)
hash = hex.EncodeToString(rawhash)
return hash, salt, nil
}
// CreateAPIToken returns JWT token with encoded username, role, expiration date and issuer claims.
// It returns an error if it fails.
func CreateAPIToken(username, role string, roleID uint32) (string, error) {
var apiToken string
var err error
if err != nil {
return "", err
}
claims := TokenClaims{
username,
role,
roleID,
jwt.StandardClaims{
ExpiresAt: (time.Now().Add(OneWeek)).Unix(),
Issuer: appName,
},
}
jwtToken := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
apiToken, err = jwtToken.SignedString([]byte(secret))
if err != nil {
return "", err
}
return apiToken, nil
}
// RefreshAPIToken prolongs JWT token's expiration date for one week.
// It returns new JWT token or an error if it fails.
func RefreshAPIToken(tokenString string) (string, error) {
var newToken string
tokenString = strings.TrimPrefix(tokenString, "Bearer ")
token, err := jwt.ParseWithClaims(tokenString, &TokenClaims{}, secretFunc)
if err != nil {
return "", err
}
// type assertion
claims, ok := token.Claims.(*TokenClaims)
if !ok || !token.Valid {
return "", errors.New("token is not valid")
}
claims.ExpiresAt = (time.Now().Add(OneWeek)).Unix()
jwtToken := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
newToken, err = jwtToken.SignedString([]byte(secret))
if err != nil {
return "", err
}
return newToken, nil
}
// ParseAPIToken parses JWT token claims.
// It returns a pointer to TokenClaims struct or an error if it fails.
func ParseAPIToken(tokenString string) (*TokenClaims, error) {
if ok := strings.HasPrefix(tokenString, "Bearer "); ok {
tokenString = strings.TrimPrefix(tokenString, "Bearer ")
} else {
return &TokenClaims{}, errors.New("Authorization header is incomplete")
}
token, err := jwt.ParseWithClaims(tokenString, &TokenClaims{}, secretFunc)
if err != nil {
return &TokenClaims{}, err
}
// type assertion
claims, ok := token.Claims.(*TokenClaims)
if !ok || !token.Valid {
return &TokenClaims{}, errors.New("token is not valid")
}
return claims, nil
}
func GetTokenClaims(r *http.Request) (claims *TokenClaims, err error) {
token := r.Header.Get("Authorization")
if ok := strings.HasPrefix(token, "Bearer "); ok {
token = strings.TrimPrefix(token, "Bearer ")
} else {
return &TokenClaims{}, errors.New("Authorization header is incomplete")
}
parsedToken, err := jwt.ParseWithClaims(token, &TokenClaims{}, secretFunc)
if err != nil {
return &TokenClaims{}, err
}
// type assertion
claims, ok := parsedToken.Claims.(*TokenClaims)
if !ok || !parsedToken.Valid {
return &TokenClaims{}, errors.New("token is not valid")
}
return claims, err
}
// secretFunc returns byte slice of API secret keyword.
func secretFunc(token *jwt.Token) (interface{}, error) {
return []byte(secret), nil
}
// RbacCheck returns true if role that made HTTP request is authorized to
// access the resource it is targeting.
// It exctracts user's role from the JWT token located in Authorization header of
// http.Request and then compares it with the list of supplied roles and returns
// true if there's a match, if "*" is provided or if the authRoles is nil.
// Otherwise it returns false.
func RbacCheck(req *http.Request, authRoles []string) bool {
if authRoles == nil {
return true
}
token := req.Header.Get("Authorization")
claims, err := ParseAPIToken(token)
if err != nil {
return false
}
for _, r := range authRoles {
if claims.Role == r || r == "*" {
return true
}
}
return false
}
// Rbac sets common headers and performs RBAC.
// If RBAC passes it calls the handlerFunc.
func RbacHandler(handlerFunc http.HandlerFunc, authRoles []string) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", `Accept, Content-Type,
Content-Length, Accept-Encoding, X-CSRF-Token, Authorization`)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
// TODO: Check for content type
if req.Method == "OPTIONS" {
return
}
err := req.ParseForm()
if err != nil {
BadRequestResponse(w, req)
return
}
if !RbacCheck(req, authRoles) {
UnauthorizedResponse(w, req)
return
}
// execute HandlerFunc
handlerFunc(w, req)
}
}