Files
learn-cicd-starter/internal/auth/auth.go
2023-05-30 21:28:23 -06:00

24 lines
501 B
Go

package auth
import (
"errors"
"net/http"
"strings"
)
var ErrNoAuthHeaderIncluded = errors.New("no authorization header included")
// GetAPIKey -
func GetAPIKey(headers http.Header) (string, error) {
authHeader := headers.Get("Authorization")
if authHeader == "" {
return "", ErrNoAuthHeaderIncluded
}
splitAuth := strings.Split(authHeader, " ")
if len(splitAuth) < 2 || splitAuth[0] != "ApiKey" {
return "", errors.New("malformed authorization header")
}
return splitAuth[1], nil
}