forked from mtusi2026/mtusi2026lab4
103 lines
2.9 KiB
Go
103 lines
2.9 KiB
Go
package auth
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"encoding/hex"
|
|
"errors"
|
|
)
|
|
|
|
// tokenBytes gives 256 bits of entropy, the contract minimum. The token is an
|
|
// opaque value: it is not a username, a UUID or a JWT.
|
|
const tokenBytes = 32
|
|
|
|
// NewToken returns a URL-safe opaque session token.
|
|
func NewToken() (string, error) {
|
|
raw := make([]byte, tokenBytes)
|
|
if _, err := rand.Read(raw); err != nil {
|
|
return "", err
|
|
}
|
|
return base64.RawURLEncoding.EncodeToString(raw), nil
|
|
}
|
|
|
|
// TokenHash is the only representation of a token that is persisted. A cache
|
|
// dump therefore does not contain bearer credentials usable against the API.
|
|
func TokenHash(token string) string {
|
|
sum := sha256.Sum256([]byte(token))
|
|
return hex.EncodeToString(sum[:])
|
|
}
|
|
|
|
// ValidTokenShape rejects absurd header values before hashing. The check is a
|
|
// superset of the 43-character tokens this server issues, so token rotation
|
|
// remains possible without rejecting old values early.
|
|
func ValidTokenShape(token string) bool {
|
|
if len(token) < 16 || len(token) > 128 {
|
|
return false
|
|
}
|
|
for i := 0; i < len(token); i++ {
|
|
c := token[i]
|
|
if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-' || c == '_' {
|
|
continue
|
|
}
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
// ErrMalformedBasic is returned for anything that is not a well-formed
|
|
// RFC 7617 Basic credential. Callers must answer with the same 401 as a
|
|
// wrong password.
|
|
var ErrMalformedBasic = errors.New("malformed basic credentials")
|
|
|
|
// ParseBasic decodes an Authorization header of the form "Basic base64(user:pass)".
|
|
// The password may contain colons and arbitrary UTF-8; only the first colon
|
|
// separates the username.
|
|
func ParseBasic(header string) (username, password string, err error) {
|
|
const scheme = "basic"
|
|
trimmed := header
|
|
if len(trimmed) < len(scheme)+1 || !equalFoldASCII(trimmed[:len(scheme)], scheme) || trimmed[len(scheme)] != ' ' {
|
|
return "", "", ErrMalformedBasic
|
|
}
|
|
encoded := trimmed[len(scheme)+1:]
|
|
if encoded == "" || len(encoded) > 1024 {
|
|
return "", "", ErrMalformedBasic
|
|
}
|
|
decoded, err := base64.StdEncoding.DecodeString(encoded)
|
|
if err != nil {
|
|
// Some clients omit the RFC 4648 padding. Accept the unpadded form as
|
|
// well, but never fall back to a different alphabet.
|
|
decoded, err = base64.RawStdEncoding.DecodeString(encoded)
|
|
if err != nil {
|
|
return "", "", ErrMalformedBasic
|
|
}
|
|
}
|
|
separator := -1
|
|
for i := 0; i < len(decoded); i++ {
|
|
if decoded[i] == ':' {
|
|
separator = i
|
|
break
|
|
}
|
|
}
|
|
if separator < 0 {
|
|
return "", "", ErrMalformedBasic
|
|
}
|
|
return string(decoded[:separator]), string(decoded[separator+1:]), nil
|
|
}
|
|
|
|
func equalFoldASCII(value, lower string) bool {
|
|
if len(value) != len(lower) {
|
|
return false
|
|
}
|
|
for i := 0; i < len(value); i++ {
|
|
c := value[i]
|
|
if c >= 'A' && c <= 'Z' {
|
|
c += 'a' - 'A'
|
|
}
|
|
if c != lower[i] {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|