v0.3.1 minor refactor, tests changed to standard library
This commit is contained in:
@@ -7,34 +7,30 @@ import (
|
||||
|
||||
// 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) {
|
||||
encoded, ok := strings.CutPrefix(header, "Basic ")
|
||||
if !ok {
|
||||
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 {
|
||||
username, password, ok = strings.Cut(string(decoded), ":")
|
||||
if !ok {
|
||||
return "", "", ErrAuthInvalidBasicCreds
|
||||
}
|
||||
|
||||
return credentials[:idx], credentials[idx+1:], nil
|
||||
return username, password, nil
|
||||
}
|
||||
|
||||
// ParseBearerToken extracts token from Bearer auth header
|
||||
func ParseBearerToken(header string) (token string, err error) {
|
||||
const prefix = "Bearer "
|
||||
if !strings.HasPrefix(header, prefix) {
|
||||
token, ok := strings.CutPrefix(header, "Bearer ")
|
||||
if !ok {
|
||||
return "", ErrAuthInvalidBearerFormat
|
||||
}
|
||||
|
||||
token = strings.TrimPrefix(header, prefix)
|
||||
if token == "" {
|
||||
return "", ErrAuthEmptyBearerToken
|
||||
}
|
||||
@@ -44,18 +40,8 @@ func ParseBearerToken(header string) (token string, err error) {
|
||||
|
||||
// ExtractAuthType returns authentication type from header
|
||||
func ExtractAuthType(header string) string {
|
||||
if strings.HasPrefix(header, "Basic ") {
|
||||
return "Basic"
|
||||
if authType, _, ok := strings.Cut(header, " "); ok {
|
||||
return authType
|
||||
}
|
||||
if strings.HasPrefix(header, "Bearer ") {
|
||||
return "Bearer"
|
||||
}
|
||||
|
||||
// Extract first word as auth type
|
||||
idx := strings.IndexByte(header, ' ')
|
||||
if idx > 0 {
|
||||
return header[:idx]
|
||||
}
|
||||
return ""
|
||||
return "" // Matches original behavior if no space is found or string is empty
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user