Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

Commit

Permalink
💥 simplify pkce
Browse files Browse the repository at this point in the history
  • Loading branch information
ras0q committed Feb 7, 2023
1 parent fb54f54 commit 3d2ffd3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 16 deletions.
2 changes: 1 addition & 1 deletion example/webapp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func authorizeHandler(w http.ResponseWriter, r *http.Request) {
codeChallengeMethod = traqoauth2.CodeChallengePlain
}

codeChallenge, err := codeChallengeMethod.GenerateCodeChallenge(codeVerifier)
codeChallenge, err := traqoauth2.GenerateCodeChallenge(codeVerifier, codeChallengeMethod)
if err != nil {
handleInternalServerError(w, err)
return
Expand Down
19 changes: 4 additions & 15 deletions pkce.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,17 @@ const (
CodeChallengeS256 CodeChallengeMethod = "S256"
)

// String returns the string representation of the code challenge method.
func (m CodeChallengeMethod) String() string {
if m == CodeChallengePlain || m == CodeChallengeS256 {
return string(m)
}

fmt.Printf("WARN: unavailable code challenge method: %s\n", string(m))

return ""
}

// GenerateCodeChallenge generates the code challenge from the code verifier.
// Ref: https://www.rfc-editor.org/rfc/rfc7636#section-4.2
func (m CodeChallengeMethod) GenerateCodeChallenge(codeVerifier string) (string, error) {
switch m {
func GenerateCodeChallenge(codeVerifier string, codeChallengeMethod CodeChallengeMethod) (string, error) {
switch codeChallengeMethod {
case CodeChallengePlain:
return codeVerifier, nil
case CodeChallengeS256:
h := sha256.Sum256([]byte(codeVerifier))
return base64.RawURLEncoding.EncodeToString(h[:]), nil
default:
return "", fmt.Errorf("unavailable code challenge method: %s", string(m))
return "", fmt.Errorf("unavailable code challenge method: %s", codeChallengeMethod)
}
}

Expand All @@ -63,7 +52,7 @@ func WithCodeChallenge(codeChallenge string) oauth2.AuthCodeOption {
// The default value is "plain".
// If you want to use "S256", use WithCodeChallengeMethod(traqoauth2.CodeChallengeS256).
func WithCodeChallengeMethod(codeChallengeMethod CodeChallengeMethod) oauth2.AuthCodeOption {
return oauth2.SetAuthURLParam("code_challenge_method", codeChallengeMethod.String())
return oauth2.SetAuthURLParam("code_challenge_method", string(codeChallengeMethod))
}

// WithCodeVerifier sets the code_verifier parameter.
Expand Down

0 comments on commit 3d2ffd3

Please sign in to comment.