Skip to content

Commit

Permalink
Add user role to atuh token
Browse files Browse the repository at this point in the history
Signed-off-by: Francesco Torchia <francesco.torchia@suse.com>
  • Loading branch information
torchiaf committed May 19, 2024
1 parent 29b485d commit 950da26
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/console/angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.local.ts"
}
]
],
"baseHref": "/"
},
"cluster-dev": {
"buildOptimizer": false,
Expand All @@ -98,7 +99,8 @@
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.cluster-dev.ts"
}
]
],
"baseHref": "/"
}
},
"defaultConfiguration": "production"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<div fxLayout="column" fxLayoutGap="15px">
<div fxLayout="row" fxLayoutAlign="start none" fxLayoutGap="15px">
ADMIN ROLE
</div>
</div>
2 changes: 1 addition & 1 deletion src/console/src/environments/environment.local.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const environment = {
production: false,
restURL: 'https://localhost:8080',
restURL: 'http://localhost:8080',
apiVersion: 'v1',
jwtToken: 'token',
languages: ['en', 'it'],
Expand Down
2 changes: 1 addition & 1 deletion src/server/authentication/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func LoginCheck(auth models.Auth) (string, error) {
return "", errors.New("password is not correct")
}

token, err := GenerateToken(user.Name)
token, err := GenerateToken(user)

if err != nil {
return "", err
Expand Down
12 changes: 10 additions & 2 deletions src/server/authentication/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
var tokenSecret = utils.IfNull(os.Getenv("API_SECRET"), "francesco")
var tokenExpiration = utils.IfNull(os.Getenv("API_TOKEN_EXPIRATION"), "24")

func GenerateToken(username string) (string, error) {
func GenerateToken(user models.User) (string, error) {

token_lifespan, err := strconv.Atoi(tokenExpiration)

Expand All @@ -29,7 +29,15 @@ func GenerateToken(username string) (string, error) {

claims := jwt.MapClaims{}
claims["authorized"] = true
claims["username"] = username
claims["id"] = user.Id
claims["username"] = user.Name

if user.IsAdmin {
claims["role"] = "Admin"
} else {
claims["role"] = "User"
}

claims["exp"] = time.Now().Add(time.Hour * time.Duration(token_lifespan)).Unix()
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)

Expand Down

0 comments on commit 950da26

Please sign in to comment.