Commit de25e1deb6dc5ea3eac3668d0f05500763dfb3dc
1 parent
0feac50590
Exists in
master
removed redundant role struct
Showing
1 changed file
with
6 additions
and
11 deletions
Show diff stats
auth.go
... | ... | @@ -15,11 +15,6 @@ import ( |
15 | 15 | var appName = "webutility" |
16 | 16 | var secret = "webutility" |
17 | 17 | |
18 | -type Role struct { | |
19 | - Name string `json:"name"` | |
20 | - ID int64 `json:"id"` | |
21 | -} | |
22 | - | |
23 | 18 | // TokenClaims are JWT token claims. |
24 | 19 | type TokenClaims struct { |
25 | 20 | // extending a struct |
... | ... | @@ -29,7 +24,7 @@ type TokenClaims struct { |
29 | 24 | Token string `json:"access_token"` |
30 | 25 | TokenType string `json:"token_type"` |
31 | 26 | Username string `json:"username"` |
32 | - Role string `json:"role"` | |
27 | + RoleName string `json:"role"` | |
33 | 28 | RoleID int64 `json:"role_id"` |
34 | 29 | ExpiresIn int64 `json:"expires_in"` |
35 | 30 | } |
... | ... | @@ -85,14 +80,14 @@ func CreateHash(str, presalt string) (hash, salt string, err error) { |
85 | 80 | |
86 | 81 | // CreateAuthToken returns JWT token with encoded username, role, expiration date and issuer claims. |
87 | 82 | // It returns an error if it fails. |
88 | -func CreateAuthToken(username string, role Role) (TokenClaims, error) { | |
83 | +func CreateAuthToken(username string, roleName string, roleID int64) (TokenClaims, error) { | |
89 | 84 | t0 := (time.Now()).Unix() |
90 | 85 | t1 := (time.Now().Add(time.Hour * 24 * 7)).Unix() |
91 | 86 | claims := TokenClaims{ |
92 | 87 | TokenType: "Bearer", |
93 | 88 | Username: username, |
94 | - Role: role.Name, | |
95 | - RoleID: role.ID, | |
89 | + RoleName: roleName, | |
90 | + RoleID: roleID, | |
96 | 91 | ExpiresIn: t1 - t0, |
97 | 92 | } |
98 | 93 | // initialize jwt.StandardClaims fields (anonymous struct) |
... | ... | @@ -132,7 +127,7 @@ func RefreshAuthToken(tok string) (TokenClaims, error) { |
132 | 127 | } |
133 | 128 | |
134 | 129 | // extend token expiration date |
135 | - return CreateAuthToken(claims.Username, Role{claims.Role, claims.RoleID}) | |
130 | + return CreateAuthToken(claims.Username, claims.RoleName, claims.RoleID) | |
136 | 131 | } |
137 | 132 | |
138 | 133 | func AuthCheck(req *http.Request, roles string) (*TokenClaims, error) { |
... | ... | @@ -158,7 +153,7 @@ func AuthCheck(req *http.Request, roles string) (*TokenClaims, error) { |
158 | 153 | parts := strings.Split(roles, ",") |
159 | 154 | for i, _ := range parts { |
160 | 155 | r := strings.Trim(parts[i], " ") |
161 | - if claims.Role == r { | |
156 | + if claims.RoleName == r { | |
162 | 157 | return claims, nil |
163 | 158 | } |
164 | 159 | } | ... | ... |