Commit c7aadbb39d060fc166404ee45d5ba7e7e352fc20

Authored by Marko Tikvić
1 parent 79071a5d4f
Exists in master and in 1 other branch v2

minor changes

Showing 1 changed file with 7 additions and 17 deletions   Show diff stats
... ... @@ -20,7 +20,7 @@ const secret = "korisnicki-centar-api"
20 20  
21 21 type Role struct {
22 22 Name string `json:"name"`
23   - ID uint32 `json:"id"`
  23 + ID int `json:"id"`
24 24 }
25 25  
26 26 // TokenClaims are JWT token claims.
... ... @@ -29,20 +29,13 @@ type TokenClaims struct {
29 29 TokenType string `json:"token_type"`
30 30 Username string `json:"username"`
31 31 Role string `json:"role"`
32   - RoleID uint32 `json:"role_id"`
  32 + RoleID int `json:"role_id"`
33 33 ExpiresIn int64 `json:"expires_in"`
34 34  
35 35 // extending a struct
36 36 jwt.StandardClaims
37 37 }
38 38  
39   -// CredentialsStruct is an instace of username/password values.
40   -type CredentialsStruct struct {
41   - Username string `json:"username"`
42   - Password string `json:"password"`
43   - RoleID uint32 `json:"roleID"`
44   -}
45   -
46 39 // ValidateCredentials hashes pass and salt and returns comparison result with resultHash
47 40 func ValidateCredentials(pass, salt, resultHash string) bool {
48 41 hash, _, err := CreateHash(pass, salt)
... ... @@ -112,12 +105,10 @@ func CreateAuthToken(username string, role Role) (TokenClaims, error) {
112 105 return claims, nil
113 106 }
114 107  
115   -// RefreshAuthToken prolongs JWT token's expiration date for one week.
  108 +// RefreshAuthToken returns new JWT token with sprolongs JWT token's expiration date for one week.
116 109 // It returns new JWT token or an error if it fails.
117   -func RefreshAuthToken(req *http.Request) (TokenClaims, error) {
118   - authHead := req.Header.Get("Authorization")
119   - tokenstr := strings.TrimPrefix(authHead, "Bearer ")
120   - token, err := jwt.ParseWithClaims(tokenstr, &TokenClaims{}, secretFunc)
  110 +func RefreshAuthToken(tok string) (TokenClaims, error) {
  111 + token, err := jwt.ParseWithClaims(tok, &TokenClaims{}, secretFunc)
121 112 if err != nil {
122 113 if validation, ok := err.(*jwt.ValidationError); ok {
123 114 // don't return error if token is expired
... ... @@ -174,9 +165,8 @@ func RbacCheck(req *http.Request, authRoles []string) bool {
174 165  
175 166 // ProcessRBAC returns token claims and boolean value based on user's rights to access resource specified in req.
176 167 // It exctracts user's role from the JWT token located in Authorization header of
177   -// http.Request and then compares it with the list of supplied roles and returns
178   -// true if there's a match, if "*" is provided or if the authRoles is nil.
179   -// Otherwise it returns false.
  168 +// HTTP request and then compares it with the list of supplied (authorized);
  169 +// it returns true if there's a match, if "*" is provided or if the authRoles is nil.
180 170 func ProcessRBAC(req *http.Request, authRoles []string) (*TokenClaims, bool) {
181 171 if authRoles == nil {
182 172 return nil, true
... ...