1
0
go-rss-aggregator/internal/auth/auth.go

29 lines
628 B
Go

package auth
import (
"errors"
"net/http"
"strings"
)
// GetAPIKey extracts an API Key from
// the headers of an HTTP request
// Example:
// Authorization: ApiKey {user-api-key}
func GetAPIKey(headers http.Header) (string, error) {
auth_header := headers.Get("Authorization")
if auth_header == "" {
return "", errors.New("no authentication info found")
}
header_parts := strings.Split(auth_header, " ")
if len(header_parts) != 2 {
return "", errors.New("malformed auth header")
}
if header_parts[0] != "ApiKey" {
return "", errors.New("malformed first part of auth header")
}
return header_parts[1], nil
}