Commit 077dae33c8d628fad5c60e081dfa2a6651ef1df4
1 parent
d666282954
Exists in
master
and in
1 other branch
removed role constants
Showing
1 changed file
with
7 additions
and
21 deletions
Show diff stats
auth_utility.go
... | ... | @@ -19,18 +19,9 @@ const saltSize = 32 |
19 | 19 | const appName = "korisnicki-centar" |
20 | 20 | const secret = "korisnicki-centar-api" |
21 | 21 | |
22 | -const RoleAdmin string = "ADMINISTRATOR" | |
23 | -const RoleManager string = "RUKOVODILAC" | |
24 | -const RoleReporter string = "REPORTER" | |
25 | -const RoleOperator string = "OPERATER" | |
26 | -const RoleAdminID uint32 = 1 | |
27 | -const RoleManagerID uint32 = 2 | |
28 | -const RoleReporterID uint32 = 3 | |
29 | -const RoleOperatorID uint32 = 4 | |
30 | - | |
31 | 22 | type Role struct { |
32 | - name string | |
33 | - id uint32 | |
23 | + Name string `json:"name"` | |
24 | + ID uint32 `json:"id"` | |
34 | 25 | } |
35 | 26 | |
36 | 27 | // TokenClaims are JWT token claims. |
... | ... | @@ -47,11 +38,6 @@ type CredentialsStruct struct { |
47 | 38 | Password string `json:"password"` |
48 | 39 | } |
49 | 40 | |
50 | -var admin Role = Role{RoleAdmin, RoleAdminID} | |
51 | -var manager Role = Role{RoleManager, RoleManagerID} | |
52 | -var reporter Role = Role{RoleReporter, RoleReporterID} | |
53 | -var operator Role = Role{RoleOperator, RoleOperatorID} | |
54 | - | |
55 | 41 | // generateSalt returns a string of random characters of 'saltSize' length. |
56 | 42 | func generateSalt() (salt string, err error) { |
57 | 43 | rawsalt := make([]byte, saltSize) |
... | ... | @@ -65,10 +51,10 @@ func generateSalt() (salt string, err error) { |
65 | 51 | return salt, nil |
66 | 52 | } |
67 | 53 | |
68 | -// HashString hashes input string with SHA256 algorithm. | |
54 | +// HashString hashes input string using SHA256. | |
69 | 55 | // If the presalt parameter is not provided HashString will generate new salt string. |
70 | 56 | // Returns hash and salt string or an error if it fails. |
71 | -func HashString(str string, presalt string) (hash, salt string, err error) { | |
57 | +func HashString(str, presalt string) (hash, salt string, err error) { | |
72 | 58 | // chech if message is presalted |
73 | 59 | if presalt == "" { |
74 | 60 | salt, err = generateSalt() |
... | ... | @@ -101,7 +87,7 @@ func HashString(str string, presalt string) (hash, salt string, err error) { |
101 | 87 | |
102 | 88 | // CreateAPIToken returns JWT token with encoded username, role, expiration date and issuer claims. |
103 | 89 | // It returns an error if it fails. |
104 | -func CreateAPIToken(username, role string, roleID uint32) (string, error) { | |
90 | +func CreateAPIToken(username string, role Role) (string, error) { | |
105 | 91 | var apiToken string |
106 | 92 | var err error |
107 | 93 | |
... | ... | @@ -111,8 +97,8 @@ func CreateAPIToken(username, role string, roleID uint32) (string, error) { |
111 | 97 | |
112 | 98 | claims := TokenClaims{ |
113 | 99 | username, |
114 | - role, | |
115 | - roleID, | |
100 | + role.Name, | |
101 | + role.ID, | |
116 | 102 | jwt.StandardClaims{ |
117 | 103 | ExpiresAt: (time.Now().Add(OneWeek)).Unix(), |
118 | 104 | Issuer: appName, | ... | ... |