| 1 |
package oidc |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/sha256" |
| 5 |
|
| 6 |
"github.com/zitadel/oidc/v3/pkg/crypto" |
| 7 |
) |
| 8 |
|
| 9 |
const ( |
| 10 |
CodeChallengeMethodPlain CodeChallengeMethod = "plain" |
| 11 |
CodeChallengeMethodS256 CodeChallengeMethod = "S256" |
| 12 |
) |
| 13 |
|
| 14 |
type CodeChallengeMethod string |
| 15 |
|
| 16 |
type CodeChallenge struct { |
| 17 |
Challenge string |
| 18 |
Method CodeChallengeMethod |
| 19 |
} |
| 20 |
|
| 21 |
func NewSHACodeChallenge(code string) string { |
| 22 |
return crypto.HashString(sha256.New(), code, false) |
| 23 |
} |
| 24 |
|
| 25 |
func VerifyCodeChallenge(c *CodeChallenge, codeVerifier string) bool { |
| 26 |
if c == nil { |
| 27 |
return false |
| 28 |
} |
| 29 |
if c.Method == CodeChallengeMethodS256 { |
| 30 |
codeVerifier = NewSHACodeChallenge(codeVerifier) |
| 31 |
} |
| 32 |
return codeVerifier == c.Challenge |
| 33 |
} |