mirror of
https://github.com/juanfont/headscale.git
synced 2024-11-27 12:05:26 +00:00
Use strings.Cut to simplify logic
This commit is contained in:
parent
8fed47a2be
commit
86ce0e0c66
16
api_key.go
16
api_key.go
@ -13,7 +13,6 @@ import (
|
|||||||
const (
|
const (
|
||||||
apiPrefixLength = 7
|
apiPrefixLength = 7
|
||||||
apiKeyLength = 32
|
apiKeyLength = 32
|
||||||
apiKeyParts = 2
|
|
||||||
|
|
||||||
errAPIKeyFailedToParse = Error("Failed to parse ApiKey")
|
errAPIKeyFailedToParse = Error("Failed to parse ApiKey")
|
||||||
)
|
)
|
||||||
@ -115,9 +114,9 @@ func (h *Headscale) ExpireAPIKey(key *APIKey) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *Headscale) ValidateAPIKey(keyStr string) (bool, error) {
|
func (h *Headscale) ValidateAPIKey(keyStr string) (bool, error) {
|
||||||
prefix, hash, err := splitAPIKey(keyStr)
|
prefix, hash, found := strings.Cut(keyStr, ".")
|
||||||
if err != nil {
|
if !found {
|
||||||
return false, fmt.Errorf("failed to validate api key: %w", err)
|
return false, errAPIKeyFailedToParse
|
||||||
}
|
}
|
||||||
|
|
||||||
key, err := h.GetAPIKey(prefix)
|
key, err := h.GetAPIKey(prefix)
|
||||||
@ -136,15 +135,6 @@ func (h *Headscale) ValidateAPIKey(keyStr string) (bool, error) {
|
|||||||
return true, nil
|
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 {
|
func (key *APIKey) toProto() *v1.ApiKey {
|
||||||
protoKey := v1.ApiKey{
|
protoKey := v1.ApiKey{
|
||||||
Id: key.ID,
|
Id: key.ID,
|
||||||
|
Loading…
Reference in New Issue
Block a user