diff --git a/auth.go b/auth.go index cb2eb9b..ec049c1 100644 --- a/auth.go +++ b/auth.go @@ -15,11 +15,6 @@ import ( var appName = "webutility" var secret = "webutility" -type Role struct { - Name string `json:"name"` - ID int64 `json:"id"` -} - // TokenClaims are JWT token claims. type TokenClaims struct { // extending a struct @@ -29,7 +24,7 @@ type TokenClaims struct { Token string `json:"access_token"` TokenType string `json:"token_type"` Username string `json:"username"` - Role string `json:"role"` + RoleName string `json:"role"` RoleID int64 `json:"role_id"` ExpiresIn int64 `json:"expires_in"` } @@ -85,14 +80,14 @@ func CreateHash(str, presalt string) (hash, salt string, err error) { // CreateAuthToken returns JWT token with encoded username, role, expiration date and issuer claims. // It returns an error if it fails. -func CreateAuthToken(username string, role Role) (TokenClaims, error) { +func CreateAuthToken(username string, roleName string, roleID int64) (TokenClaims, error) { t0 := (time.Now()).Unix() t1 := (time.Now().Add(time.Hour * 24 * 7)).Unix() claims := TokenClaims{ TokenType: "Bearer", Username: username, - Role: role.Name, - RoleID: role.ID, + RoleName: roleName, + RoleID: roleID, ExpiresIn: t1 - t0, } // initialize jwt.StandardClaims fields (anonymous struct) @@ -132,7 +127,7 @@ func RefreshAuthToken(tok string) (TokenClaims, error) { } // extend token expiration date - return CreateAuthToken(claims.Username, Role{claims.Role, claims.RoleID}) + return CreateAuthToken(claims.Username, claims.RoleName, claims.RoleID) } func AuthCheck(req *http.Request, roles string) (*TokenClaims, error) { @@ -158,7 +153,7 @@ func AuthCheck(req *http.Request, roles string) (*TokenClaims, error) { parts := strings.Split(roles, ",") for i, _ := range parts { r := strings.Trim(parts[i], " ") - if claims.Role == r { + if claims.RoleName == r { return claims, nil } }