24 lines
687 B
Go
24 lines
687 B
Go
package oauth2
|
||
|
||
import (
|
||
"crypto/sha256"
|
||
"crypto/subtle"
|
||
"encoding/base64"
|
||
"strings"
|
||
)
|
||
|
||
// VerifyPKCES256 校验 code_verifier 是否与 code_challenge(S256)一致。
|
||
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))
|
||
}
|