v0.4.1 merge http and simple token into one file
test / test (push) Canceled after 0s

This commit is contained in:
lixen committed 2026-09-30 15:31:32 -04:00
1 parent 2bf8715e93
commit 778ff7c8bc
6 files changed
+85 -235

No files matched your search

+33
View File
@@ -2,6 +2,7 @@ package auth
import (
"crypto/sha256"
"strings"
"sync"
)
@@ -57,3 +58,35 @@ func (v *SimpleTokenValidator) RemoveToken(token string) {
defer v.mu.Unlock()
delete(v.tokens, h)
}
// ParseBearerToken parses an RFC 6750 Bearer authorization value. The scheme is
// case-insensitive; tokens must use the b64token alphabet and contain no spaces.
// Parsing does not validate the token's signature or grant authorization.
func ParseBearerToken(header string) (string, error) {
// Bound before scanning, including an allowance for separator spaces.
if len(header) > MaxTokenLen+16 {
return "", ErrTokenTooLong
}
scheme, token, ok := strings.Cut(header, " ")
if !ok || !strings.EqualFold(scheme, "Bearer") {
return "", ErrAuthInvalidBearerFormat
}
token = strings.TrimLeft(token, " ")
if token == "" {
return "", ErrAuthEmptyBearerToken
}
if len(token) > MaxTokenLen {
return "", ErrTokenTooLong
}
padding := false
for i, ch := range []byte(token) {
if ch == '=' && i > 0 {
padding = true
continue
}
if padding || !(ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch >= '0' && ch <= '9' || strings.ContainsRune("-._~+/", rune(ch))) {
return "", ErrAuthInvalidBearerFormat
}
}
return token, nil
}