Files
smart-go/internal/auth/oauth2/pkce.go
T
2026-04-23 18:58:13 +08:00

24 lines
687 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package oauth2
import (
"crypto/sha256"
"crypto/subtle"
"encoding/base64"
"strings"
)
// VerifyPKCES256 校验 code_verifier 是否与 code_challengeS256)一致。
func VerifyPKCES256(codeVerifier, codeChallenge string) bool {
if codeVerifier == "" || codeChallenge == "" {
return false
}
sum := sha256.Sum256([]byte(codeVerifier))
expected := base64.RawURLEncoding.EncodeToString(sum[:])
return subtle.ConstantTimeCompare([]byte(expected), []byte(codeChallenge)) == 1
}
// NormalizeCodeChallengeMethod 返回小写方法名;仅支持 S256(OAuth 2.1 推荐)。
func NormalizeCodeChallengeMethod(m string) string {
return strings.TrimSpace(strings.ToLower(m))
}