Skip to content

Commit

Permalink
Merge pull request #619 from majst01/simplify-split
Browse files Browse the repository at this point in the history
Use strings.Cut to simplify logic
  • Loading branch information
juanfont authored Jun 9, 2022
2 parents 8fed47a + 86ce0e0 commit e918ea8
Showing 1 changed file with 3 additions and 13 deletions.
16 changes: 3 additions & 13 deletions api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
const (
apiPrefixLength = 7
apiKeyLength = 32
apiKeyParts = 2

errAPIKeyFailedToParse = Error("Failed to parse ApiKey")
)
Expand Down Expand Up @@ -115,9 +114,9 @@ func (h *Headscale) ExpireAPIKey(key *APIKey) error {
}

func (h *Headscale) ValidateAPIKey(keyStr string) (bool, error) {
prefix, hash, err := splitAPIKey(keyStr)
if err != nil {
return false, fmt.Errorf("failed to validate api key: %w", err)
prefix, hash, found := strings.Cut(keyStr, ".")
if !found {
return false, errAPIKeyFailedToParse
}

key, err := h.GetAPIKey(prefix)
Expand All @@ -136,15 +135,6 @@ func (h *Headscale) ValidateAPIKey(keyStr string) (bool, error) {
return true, nil
}

func splitAPIKey(key string) (string, string, error) {
parts := strings.Split(key, ".")
if len(parts) != apiKeyParts {
return "", "", errAPIKeyFailedToParse
}

return parts[0], parts[1], nil
}

func (key *APIKey) toProto() *v1.ApiKey {
protoKey := v1.ApiKey{
Id: key.ID,
Expand Down

0 comments on commit e918ea8

Please sign in to comment.