v0.1.0 initial commit, auth features extracted from logwisp to be a standalone utility package

This commit is contained in:
2025-11-02 13:05:37 -05:00
commit 3a662862d7
18 changed files with 1715 additions and 0 deletions

61
http.go Normal file
View File

@ -0,0 +1,61 @@
// FILE: auth/http.go
package auth
import (
"encoding/base64"
"strings"
)
// ParseBasicAuth extracts username/password from Basic auth header
func ParseBasicAuth(header string) (username, password string, err error) {
const prefix = "Basic "
if !strings.HasPrefix(header, prefix) {
return "", "", ErrAuthInvalidBasicFormat
}
encoded := strings.TrimPrefix(header, prefix)
decoded, err := base64.StdEncoding.DecodeString(encoded)
if err != nil {
return "", "", ErrAuthInvalidBasicEncoding
}
credentials := string(decoded)
idx := strings.IndexByte(credentials, ':')
if idx < 0 {
return "", "", ErrAuthInvalidBasicCreds
}
return credentials[:idx], credentials[idx+1:], nil
}
// ParseBearerToken extracts token from Bearer auth header
func ParseBearerToken(header string) (token string, err error) {
const prefix = "Bearer "
if !strings.HasPrefix(header, prefix) {
return "", ErrAuthInvalidBearerFormat
}
token = strings.TrimPrefix(header, prefix)
if token == "" {
return "", ErrAuthEmptyBearerToken
}
return token, nil
}
// ExtractAuthType returns authentication type from header
func ExtractAuthType(header string) string {
if strings.HasPrefix(header, "Basic ") {
return "Basic"
}
if strings.HasPrefix(header, "Bearer ") {
return "Bearer"
}
// Extract first word as auth type
idx := strings.IndexByte(header, ' ')
if idx > 0 {
return header[:idx]
}
return ""
}