This commit is contained in:
6 files changed
+85
-235
No files matched your search
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user