Skip to content

Commit

Permalink
Base64 encode the api token
Browse files Browse the repository at this point in the history
Sometimes users skip copying the namespace which is part of the api token, because it looks unusual
  • Loading branch information
ramanan-ravi committed Nov 30, 2023
1 parent 0ea926a commit 5523ded
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions deepfence_server/handler/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func (h *Handler) APIAuthHandler(w http.ResponseWriter, r *http.Request) {
h.respondError(&BadDecoding{err}, w)
return
}
apiAuthRequest.APIToken, _ = utils.Base64RawDecode(apiAuthRequest.APIToken)
err = h.Validator.Struct(apiAuthRequest)
if err != nil {
h.respondError(&ValidatorError{err: err}, w)
Expand Down
2 changes: 1 addition & 1 deletion deepfence_server/model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type APITokenResponse struct {
}

func GetAPIToken(namespace string, apiToken uuid.UUID) string {
return namespace + ":" + apiToken.String()
return utils.Base64RawEncode(namespace + ":" + apiToken.String())
}

type APIToken struct {
Expand Down
12 changes: 12 additions & 0 deletions deepfence_utils/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,18 @@ func ToMap[T any](c T) map[string]interface{} {
return bb
}

func Base64RawDecode(s string) (string, error) {
decodedStr, err := base64.RawStdEncoding.DecodeString(s)
if err != nil {
return s, err
}
return string(decodedStr), nil
}

func Base64RawEncode(s string) string {
return base64.RawStdEncoding.EncodeToString([]byte(s))
}

// FromMap Convert map[string]interface{} into structs
// e.g:
//
Expand Down

0 comments on commit 5523ded

Please sign in to comment.