133 lines
2.8 KiB
Go
133 lines
2.8 KiB
Go
package services
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"crypto/sha256"
|
|
"crypto/subtle"
|
|
"encoding/base64"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
func newOpaqueToken(nBytes int) (string, error) {
|
|
b := make([]byte, nBytes)
|
|
if _, err := rand.Read(b); err != nil {
|
|
return "", err
|
|
}
|
|
return base64.RawURLEncoding.EncodeToString(b), nil
|
|
}
|
|
|
|
func sha256Hex(v string) string {
|
|
h := sha256.Sum256([]byte(v))
|
|
return hex.EncodeToString(h[:])
|
|
}
|
|
|
|
func pkceVerifyS256(verifier, challenge string) bool {
|
|
sum := sha256.Sum256([]byte(verifier))
|
|
got := base64.RawURLEncoding.EncodeToString(sum[:])
|
|
return subtle.ConstantTimeCompare([]byte(got), []byte(challenge)) == 1
|
|
}
|
|
|
|
func parseScopes(scope string) []string {
|
|
scope = strings.TrimSpace(scope)
|
|
if scope == "" {
|
|
return nil
|
|
}
|
|
seen := make(map[string]struct{})
|
|
var out []string
|
|
for _, p := range strings.Fields(scope) {
|
|
if _, dup := seen[p]; !dup {
|
|
seen[p] = struct{}{}
|
|
out = append(out, p)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func ScopesAllowed(clientScopesJSON json.RawMessage, requested []string) bool {
|
|
if len(requested) == 0 {
|
|
return true
|
|
}
|
|
allowed := scopesFromJSON(clientScopesJSON)
|
|
set := make(map[string]struct{}, len(allowed))
|
|
for _, a := range allowed {
|
|
set[a] = struct{}{}
|
|
}
|
|
for _, r := range requested {
|
|
if _, ok := set[r]; !ok {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func scopesFromJSON(b json.RawMessage) []string {
|
|
if len(b) == 0 {
|
|
return nil
|
|
}
|
|
var out []string
|
|
_ = json.Unmarshal(b, &out)
|
|
return out
|
|
}
|
|
|
|
func redirectURIMatches(redirectURIsJSON json.RawMessage, redirectURI string) bool {
|
|
var uris []string
|
|
if err := json.Unmarshal(redirectURIsJSON, &uris); err != nil {
|
|
return false
|
|
}
|
|
for _, u := range uris {
|
|
if u == redirectURI {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func verifyClientSecret(stored, provided string) bool {
|
|
if stored == "" {
|
|
return true
|
|
}
|
|
if provided == "" {
|
|
return false
|
|
}
|
|
if strings.HasPrefix(stored, "$2") {
|
|
return bcrypt.CompareHashAndPassword([]byte(stored), []byte(provided)) == nil
|
|
}
|
|
return subtle.ConstantTimeCompare([]byte(stored), []byte(provided)) == 1
|
|
}
|
|
|
|
func HashClientSecret(secret string) (string, error) {
|
|
if secret == "" {
|
|
return "", nil
|
|
}
|
|
b, err := bcrypt.GenerateFromPassword([]byte(secret), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
return "", fmt.Errorf("hash client secret: %w", err)
|
|
}
|
|
return string(b), nil
|
|
}
|
|
|
|
func bcryptCompare(hash, password string) error {
|
|
return bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
|
}
|
|
|
|
func ScopesContain(tokenScopes []string, required []string) bool {
|
|
if len(required) == 0 {
|
|
return true
|
|
}
|
|
set := make(map[string]struct{}, len(tokenScopes))
|
|
for _, s := range tokenScopes {
|
|
set[s] = struct{}{}
|
|
}
|
|
for _, r := range required {
|
|
if _, ok := set[r]; !ok {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|