diff --git a/auth_utility.go b/auth_utility.go index 5ad13a5..d931fff 100644 --- a/auth_utility.go +++ b/auth_utility.go @@ -19,18 +19,9 @@ const saltSize = 32 const appName = "korisnicki-centar" const secret = "korisnicki-centar-api" -const RoleAdmin string = "ADMINISTRATOR" -const RoleManager string = "RUKOVODILAC" -const RoleReporter string = "REPORTER" -const RoleOperator string = "OPERATER" -const RoleAdminID uint32 = 1 -const RoleManagerID uint32 = 2 -const RoleReporterID uint32 = 3 -const RoleOperatorID uint32 = 4 - type Role struct { - name string - id uint32 + Name string `json:"name"` + ID uint32 `json:"id"` } // TokenClaims are JWT token claims. @@ -47,11 +38,6 @@ type CredentialsStruct struct { Password string `json:"password"` } -var admin Role = Role{RoleAdmin, RoleAdminID} -var manager Role = Role{RoleManager, RoleManagerID} -var reporter Role = Role{RoleReporter, RoleReporterID} -var operator Role = Role{RoleOperator, RoleOperatorID} - // generateSalt returns a string of random characters of 'saltSize' length. func generateSalt() (salt string, err error) { rawsalt := make([]byte, saltSize) @@ -65,10 +51,10 @@ func generateSalt() (salt string, err error) { return salt, nil } -// HashString hashes input string with SHA256 algorithm. +// HashString hashes input string using SHA256. // If the presalt parameter is not provided HashString will generate new salt string. // Returns hash and salt string or an error if it fails. -func HashString(str string, presalt string) (hash, salt string, err error) { +func HashString(str, presalt string) (hash, salt string, err error) { // chech if message is presalted if presalt == "" { salt, err = generateSalt() @@ -101,7 +87,7 @@ func HashString(str string, presalt string) (hash, salt string, err error) { // CreateAPIToken returns JWT token with encoded username, role, expiration date and issuer claims. // It returns an error if it fails. -func CreateAPIToken(username, role string, roleID uint32) (string, error) { +func CreateAPIToken(username string, role Role) (string, error) { var apiToken string var err error @@ -111,8 +97,8 @@ func CreateAPIToken(username, role string, roleID uint32) (string, error) { claims := TokenClaims{ username, - role, - roleID, + role.Name, + role.ID, jwt.StandardClaims{ ExpiresAt: (time.Now().Add(OneWeek)).Unix(), Issuer: appName,