Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add checks to user update processing #2158

Merged
merged 2 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions controllers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,18 @@ func updateUser(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var params = mux.Vars(r)
// start here
jwtUser, _, isadmin, err := logic.VerifyJWT(r.Header.Get("Authorization"))
if err != nil {
0xdcarns marked this conversation as resolved.
Show resolved Hide resolved
logger.Log(0, "verifyJWT error", err.Error())
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
return
}
username := params["username"]
if username != jwtUser && !isadmin {
logger.Log(0, "non-admin user", jwtUser, "attempted to update user", username)
logic.ReturnErrorResponse(w, r, logic.FormatError(errors.New("not authorizied"), "unauthorized"))
return
}
user, err := logic.GetUser(username)
if err != nil {
logger.Log(0, username,
Expand All @@ -354,6 +365,11 @@ func updateUser(w http.ResponseWriter, r *http.Request) {
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
return
}
if userchange.IsAdmin && !isadmin {
logger.Log(0, "non-admin user", jwtUser, "attempted get admin privilages")
logic.ReturnErrorResponse(w, r, logic.FormatError(errors.New("not authorizied"), "unauthorized"))
return
}
userchange.Networks = nil
user, err = logic.UpdateUser(&userchange, user)
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions logic/jwts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package logic
import (
"errors"
"fmt"
"strings"
"time"

"github.com/golang-jwt/jwt/v4"
Expand Down Expand Up @@ -101,6 +102,18 @@ func CreateUserJWT(username string, networks []string, isadmin bool) (response s
return "", err
}

// VerifyJWT verifies Auth Header
mattkasun marked this conversation as resolved.
Show resolved Hide resolved
func VerifyJWT(bearerToken string) (username string, networks []string, isadmin bool, err error) {
token := ""
tokenSplit := strings.Split(bearerToken, " ")
if len(tokenSplit) > 1 {
token = tokenSplit[1]
} else {
return "", nil, false, errors.New("invalid auth header")
}
return VerifyUserToken(token)
}

// VerifyUserToken func will used to Verify the JWT Token while using APIS
func VerifyUserToken(tokenString string) (username string, networks []string, isadmin bool, err error) {
claims := &models.UserClaims{}
Expand Down