Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PKCE (RFC 7636) #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion 01-Login/web/app/callback/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/gin-gonic/gin"

"01-Login/platform/authenticator"
"01-Login/web/app/pkce"
)

// Handler for our callback.
Expand All @@ -18,8 +19,11 @@ func Handler(auth *authenticator.Authenticator) gin.HandlerFunc {
return
}

verifier := session.Get("verifier").(string)
exchangeOptions := pkce.ExchangeVerifier(verifier)

// Exchange an authorization code for a token.
token, err := auth.Exchange(ctx.Request.Context(), ctx.Query("code"))
token, err := auth.Exchange(ctx.Request.Context(), ctx.Query("code"), exchangeOptions...)
if err != nil {
ctx.String(http.StatusUnauthorized, "Failed to convert an authorization code into a token.")
return
Expand Down
11 changes: 10 additions & 1 deletion 01-Login/web/app/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/gin-gonic/gin"

"01-Login/platform/authenticator"
"01-Login/web/app/pkce"
)

// Handler for our login.
Expand All @@ -20,15 +21,23 @@ func Handler(auth *authenticator.Authenticator) gin.HandlerFunc {
return
}

verifier, err := pkce.RandomVerifier(32)
if err != nil {
ctx.String(http.StatusInternalServerError, err.Error())
return
}

// Save the state inside the session.
session := sessions.Default(ctx)
session.Set("state", state)
session.Set("verifier", verifier)
if err := session.Save(); err != nil {
ctx.String(http.StatusInternalServerError, err.Error())
return
}

ctx.Redirect(http.StatusTemporaryRedirect, auth.AuthCodeURL(state))
authCodeOptions := pkce.Sha256Challenge(verifier)
ctx.Redirect(http.StatusTemporaryRedirect, auth.AuthCodeURL(state, authCodeOptions...))
}
}

Expand Down
56 changes: 56 additions & 0 deletions 01-Login/web/app/pkce/pkce.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package pkce

import (
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"fmt"

"golang.org/x/oauth2"
)

const (
codeChallengeKey = "code_challenge"
codeChallengeMethodKey = "code_challenge_method"
codeVerifierKey = "code_verifier"

challengeMethodSha256 = "S256"
challengeMethodPlain = "plain"
)

func RandomVerifier(length int) (string, error) {
// RFC7636 4.1: Result must be between 43 and 128 characters.
if length < 32 || length > 96 {
return "", fmt.Errorf("expected length between 32 and 96, got %d", length)
}

buf := make([]byte, length)
if _, err := rand.Read(buf); err != nil {
return "", err
}
return base64.RawURLEncoding.EncodeToString(buf), nil
}

func Sha256Challenge(verifier string) []oauth2.AuthCodeOption {
sha := sha256.Sum256([]byte(verifier))
challenge := base64.RawURLEncoding.EncodeToString(sha[:])

return challengeOptions(challengeMethodSha256, challenge)
}

func PlainChallenge(verifier string) []oauth2.AuthCodeOption {
return challengeOptions(challengeMethodPlain, verifier)
}

func challengeOptions(method string, challenge string) []oauth2.AuthCodeOption {
return []oauth2.AuthCodeOption{
oauth2.SetAuthURLParam(codeChallengeMethodKey, method),
oauth2.SetAuthURLParam(codeChallengeKey, challenge),
}
}

func ExchangeVerifier(verifier string) []oauth2.AuthCodeOption {
return []oauth2.AuthCodeOption{
oauth2.SetAuthURLParam(codeVerifierKey, verifier),
}
}