From de25e1deb6dc5ea3eac3668d0f05500763dfb3dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Tikvi=C4=87?= Date: Thu, 12 Jul 2018 12:41:26 +0200 Subject: [PATCH] removed redundant role struct --- auth.go | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) 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 } } -- 1.8.1.2