Skip to content

Commit

Permalink
feat: authorisation implemented for api tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
marnas committed Jun 4, 2024
1 parent 90f173d commit 6a68063
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion services/auth_svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,18 @@ func (a *AuthServiceImpl) ValidateAPIToken(key string) (models.User, error) {
}

func (a *AuthServiceImpl) IsAutorised(auth *models.Authorization) (bool, error) {
// Checking if the user owns the API token
if auth.Resource == "apiTokens" {
var apiToken models.ApiToken
res := a.db.Where("id = ?", auth.ResourceID).Find(&apiToken)
if res.Error != nil {
return false, res.Error
}

if apiToken.Owner == auth.UserID {
return true, nil
}
}

roles, err := a.UserService.GetUserRoles(auth.UserID.String(), auth.Provider)
if err != nil {
Expand All @@ -175,7 +187,6 @@ func (a *AuthServiceImpl) IsAutorised(auth *models.Authorization) (bool, error)
(len(role.Resources_IDs) > 0 && role.Resources_IDs[0] == "*" || slices.Contains(role.Resources_IDs, auth.ResourceID)) &&
(role.Access == auth.Access || role.Access == "write") {
return true, nil

}
}

Expand Down

0 comments on commit 6a68063

Please sign in to comment.