Skip to content
Merged
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
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,32 @@ oidc.WithNotAllowedBehavior(func(ctx tanukirpc.Context[*Registry]) error {
})
```

#### PKCE

PKCE (S256) is enabled by default. `Redirect` stores the code verifier in the session, and `Callback` sends it to the token endpoint. A callback whose session has no code verifier is rejected with 400, so:

- Logins that are in progress while you deploy this change, or while servers with and without PKCE coexist, fail once. Users can log in again.
- `Redirect` and `Callback` must both be served by `oidc.Handlers` with the same setting, sharing the session.

If the provider rejects PKCE, or if you build the authorization URL or exchange the code yourself, disable it with `oidc.WithoutPKCE()`.

#### Additional authorization request parameters

Use `oidc.WithAuthCodeOptions` to add parameters to the authorization request, e.g. to let users choose an account every time, or to hint the Google Workspace domain:

```go
oidcAuth := oidc.NewHandlers(
oauth2Config,
provider,
oidc.WithAuthCodeOptions[*Registry](
oauth2.SetAuthURLParam("prompt", "select_account"),
oauth2.SetAuthURLParam("hd", "example.com"),
),
)
```

`hd` is only a hint to the provider. To restrict users by domain, use `oidc.WithAllowedDomains` as well. The options cannot override `state`, `nonce` and the PKCE parameters.

### Inertia.js support

`tanukirpc` provides an opt-in Inertia.js codec in the `github.com/mackee/tanukirpc/codec/inertiajs` package. It is not enabled by default, so API-only applications keep the default JSON/form/raw-body behavior.
Expand Down
259 changes: 259 additions & 0 deletions auth/oidc/flow_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
package oidc_test

import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"math/big"
"net/http"
"net/http/cookiejar"
"net/http/httptest"
"net/url"
"testing"
"time"

gooidc "github.com/coreos/go-oidc/v3/oidc"
gorillasessions "github.com/gorilla/sessions"
"github.com/mackee/tanukirpc"
"github.com/mackee/tanukirpc/auth/oidc"
tsessions "github.com/mackee/tanukirpc/sessions"
"github.com/mackee/tanukirpc/sessions/gorilla"
"golang.org/x/oauth2"
)

type flowRegistry struct {
accessor tsessions.Accessor
}

func (r *flowRegistry) Session() tsessions.Accessor { return r.accessor }

// fakeIdP is a minimal OIDC provider that issues an ID token carrying the nonce of the last authorize request.
// Like real providers, it verifies the PKCE code_verifier at the token endpoint
// if the authorize request carried a code_challenge.
type fakeIdP struct {
*httptest.Server
key *rsa.PrivateKey
nonce string
codeChallenge string
}

func newFakeIdP(t *testing.T) *fakeIdP {
t.Helper()
key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
t.Fatal(err)
}
idp := &fakeIdP{key: key}
mux := http.NewServeMux()
mux.HandleFunc("/.well-known/openid-configuration", func(w http.ResponseWriter, r *http.Request) {
_ = json.NewEncoder(w).Encode(map[string]any{
"issuer": idp.URL,
"authorization_endpoint": idp.URL + "/authorize",
"token_endpoint": idp.URL + "/token",
"jwks_uri": idp.URL + "/jwks",
"id_token_signing_alg_values_supported": []string{"RS256"},
})
})
mux.HandleFunc("/jwks", func(w http.ResponseWriter, r *http.Request) {
enc := base64.RawURLEncoding.EncodeToString
_ = json.NewEncoder(w).Encode(map[string]any{
"keys": []map[string]any{{
"kty": "RSA",
"alg": "RS256",
"use": "sig",
"kid": "k1",
"n": enc(key.N.Bytes()),
"e": enc(big.NewInt(int64(key.E)).Bytes()),
}},
})
})
mux.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if err := idp.verifyCodeVerifier(r.PostFormValue("code_verifier")); err != nil {
w.WriteHeader(http.StatusBadRequest)
_ = json.NewEncoder(w).Encode(map[string]any{
"error": "invalid_grant",
"error_description": err.Error(),
})
return
}
_ = json.NewEncoder(w).Encode(map[string]any{
"access_token": "access",
"token_type": "Bearer",
"expires_in": 3600,
"id_token": idp.signIDToken(t),
})
})
idp.Server = httptest.NewServer(mux)
t.Cleanup(idp.Close)
return idp
}

func (idp *fakeIdP) verifyCodeVerifier(verifier string) error {
switch {
case idp.codeChallenge == "" && verifier == "":
return nil
case idp.codeChallenge == "":
return fmt.Errorf("code_verifier was sent without code_challenge")
case verifier == "":
return fmt.Errorf("code_verifier is required")
}
sum := sha256.Sum256([]byte(verifier))
if base64.RawURLEncoding.EncodeToString(sum[:]) != idp.codeChallenge {
return fmt.Errorf("code_verifier does not match code_challenge")
}
return nil
}

func (idp *fakeIdP) signIDToken(t *testing.T) string {
enc := base64.RawURLEncoding.EncodeToString
header, _ := json.Marshal(map[string]string{"alg": "RS256", "kid": "k1", "typ": "JWT"})
payload, _ := json.Marshal(map[string]any{
"iss": idp.URL,
"sub": "subject",
"aud": "client",
"exp": time.Now().Add(time.Hour).Unix(),
"iat": time.Now().Unix(),
"nonce": idp.nonce,
})
signingInput := enc(header) + "." + enc(payload)
sum := sha256.Sum256([]byte(signingInput))
sig, err := rsa.SignPKCS1v15(rand.Reader, idp.key, crypto.SHA256, sum[:])
if err != nil {
t.Error(err)
}
return signingInput + "." + enc(sig)
}

type optionsFunc func(appURL string) []oidc.HandlersOption[*flowRegistry]

func newApp(t *testing.T, idp *fakeIdP, opts optionsFunc) *httptest.Server {
t.Helper()
return newAppWith(t, idp, opts, opts)
}

// newAppWith creates an app whose Redirect and Callback are served by Handlers with different options,
// which share the session store.
func newAppWith(t *testing.T, idp *fakeIdP, redirectOpts, callbackOpts optionsFunc) *httptest.Server {
t.Helper()
store, err := gorilla.NewStore(gorillasessions.NewCookieStore([]byte("0123456789abcdef0123456789abcdef")))
if err != nil {
t.Fatal(err)
}
provider, err := gooidc.NewProvider(t.Context(), idp.URL)
if err != nil {
t.Fatal(err)
}

app := httptest.NewUnstartedServer(nil)
appURL := "http://" + app.Listener.Addr().String()
oauth2Config := &oauth2.Config{
ClientID: "client",
ClientSecret: "secret",
RedirectURL: appURL + "/auth/callback",
Endpoint: provider.Endpoint(),
Scopes: []string{gooidc.ScopeOpenID},
}
var rOpts, cOpts []oidc.HandlersOption[*flowRegistry]
if redirectOpts != nil {
rOpts = redirectOpts(appURL)
}
if callbackOpts != nil {
cOpts = callbackOpts(appURL)
}

router := tanukirpc.NewRouter(
&flowRegistry{},
tanukirpc.WithContextFactory(tanukirpc.NewContextHookFactory(func(w http.ResponseWriter, req *http.Request) (*flowRegistry, error) {
accessor, err := store.GetAccessor(req)
if err != nil {
return nil, err
}
return &flowRegistry{accessor: accessor}, nil
})),
)
router.Get("/auth/redirect", tanukirpc.NewHandler(oidc.NewHandlers(oauth2Config, provider, rOpts...).Redirect))
router.Get("/auth/callback", tanukirpc.NewHandler(oidc.NewHandlers(oauth2Config, provider, cOpts...).Callback))
app.Config.Handler = router
app.Start()
t.Cleanup(app.Close)
return app
}

type loginFlow struct {
client *http.Client
authorize *url.URL
}

// startLogin requests Redirect and returns the authorize URL it redirects to.
func startLogin(t *testing.T, app *httptest.Server, referer, returnTo string) *loginFlow {
t.Helper()
jar, err := cookiejar.New(nil)
if err != nil {
t.Fatal(err)
}
client := &http.Client{
Jar: jar,
CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse },
}

redirect := app.URL + "/auth/redirect"
if returnTo != "" {
redirect += "?return_to=" + url.QueryEscape(returnTo)
}
req, err := http.NewRequest(http.MethodGet, redirect, nil)
if err != nil {
t.Fatal(err)
}
if referer != "" {
req.Header.Set("Referer", referer)
}
resp, err := client.Do(req)
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
if resp.StatusCode != http.StatusFound {
t.Fatalf("redirect: status = %d", resp.StatusCode)
}
authorize, err := url.Parse(resp.Header.Get("Location"))
if err != nil {
t.Fatal(err)
}
return &loginFlow{client: client, authorize: authorize}
}

// callback lets the IdP accept the authorize request and requests Callback.
func (f *loginFlow) callback(t *testing.T, idp *fakeIdP, app *httptest.Server) *http.Response {
t.Helper()
q := f.authorize.Query()
idp.nonce = q.Get("nonce")
idp.codeChallenge = ""
if q.Has("code_challenge") {
if method := q.Get("code_challenge_method"); method != "S256" {
t.Fatalf("code_challenge_method = %q, want S256", method)
}
idp.codeChallenge = q.Get("code_challenge")
}

resp, err := f.client.Get(fmt.Sprintf("%s/auth/callback?code=code&state=%s", app.URL, url.QueryEscape(q.Get("state"))))
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
return resp
}

// login runs Redirect -> (IdP) -> Callback and returns the Location of the final redirect.
func login(t *testing.T, idp *fakeIdP, app *httptest.Server, referer, returnTo string) string {
t.Helper()
resp := startLogin(t, app, referer, returnTo).callback(t, idp, app)
if resp.StatusCode != http.StatusFound {
t.Fatalf("callback: status = %d", resp.StatusCode)
}
return resp.Header.Get("Location")
}
53 changes: 51 additions & 2 deletions auth/oidc/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type Handlers[Reg sessions.RegistryWithAccessor] struct {
successBehavior func(tanukirpc.Context[Reg], *SuccessBehaviorInput) error
unauthorizedBehavior func(tanukirpc.Context[Reg]) error
notAllowedBehavior func(tanukirpc.Context[Reg]) error
authCodeOptions []oauth2.AuthCodeOption
disablePKCE bool
}

// HandlersOption is an option for Handlers.
Expand Down Expand Up @@ -107,7 +109,28 @@ func WithNotAllowedDomainBehavior[Reg sessions.RegistryWithAccessor](fn func(tan
return WithNotAllowedBehavior(fn)
}

// WithAuthCodeOptions adds options to the authorization request, such as
// oauth2.SetAuthURLParam("prompt", "select_account") or oauth2.SetAuthURLParam("hd", "example.com").
// Note that "hd" is only a hint to the provider; use WithAllowedDomains to enforce it.
// The options cannot override "state", "nonce" and the PKCE parameters set by Redirect.
func WithAuthCodeOptions[Reg sessions.RegistryWithAccessor](opts ...oauth2.AuthCodeOption) HandlersOption[Reg] {
return func(a *Handlers[Reg]) {
a.authCodeOptions = append(a.authCodeOptions, opts...)
}
}

// WithoutPKCE disables PKCE, which is enabled by default.
// Use it only if the provider rejects PKCE, or if either Redirect or Callback is not used
// and the other one does not handle the code verifier.
func WithoutPKCE[Reg sessions.RegistryWithAccessor]() HandlersOption[Reg] {
return func(a *Handlers[Reg]) {
a.disablePKCE = true
}
}

// NewHandlers creates a new Handlers.
// PKCE (S256) is enabled by default. The code verifier is stored in the session by Redirect
// and required by Callback, so both must be enabled or disabled together.
func NewHandlers[Reg sessions.RegistryWithAccessor](oauth2Config *oauth2.Config, provider *oidc.Provider, opts ...HandlersOption[Reg]) *Handlers[Reg] {
verifier := provider.Verifier(&oidc.Config{ClientID: oauth2Config.ClientID})
h := &Handlers[Reg]{
Expand Down Expand Up @@ -218,6 +241,16 @@ func (a *Handlers[Reg]) Redirect(ctx tanukirpc.Context[Reg], _ struct{}) (_resp
return struct{}{}, fmt.Errorf("failed to set nonce: %w", err)
}

// The options set by Redirect come last so that authCodeOptions cannot override them.
opts := append(append([]oauth2.AuthCodeOption{}, a.authCodeOptions...), oidc.Nonce(nonce))
if !a.disablePKCE {
verifier := oauth2.GenerateVerifier()
if err := reg.Session().Set("pkce_verifier", verifier); err != nil {
return struct{}{}, fmt.Errorf("failed to set pkce verifier: %w", err)
}
opts = append(opts, oauth2.S256ChallengeOption(verifier))
}

if err := a.setReferrer(ctx, "redirect_referrer"); err != nil {
return struct{}{}, fmt.Errorf("failed to set referrer: %w", err)
}
Expand All @@ -226,7 +259,7 @@ func (a *Handlers[Reg]) Redirect(ctx tanukirpc.Context[Reg], _ struct{}) (_resp
return struct{}{}, fmt.Errorf("failed to save session: %w", err)
}

return struct{}{}, tanukirpc.ErrorRedirectTo(http.StatusFound, a.oauth2Config.AuthCodeURL(state, oidc.Nonce(nonce)))
return struct{}{}, tanukirpc.ErrorRedirectTo(http.StatusFound, a.oauth2Config.AuthCodeURL(state, opts...))
}

type AuthCallbackRequest struct {
Expand All @@ -253,7 +286,23 @@ func (a *Handlers[Reg]) Callback(ctx tanukirpc.Context[Reg], req AuthCallbackReq
return struct{}{}, tanukirpc.WrapErrorWithStatus(http.StatusBadRequest, fmt.Errorf("request invalid"))
}

token, err := a.oauth2Config.Exchange(ctx.Request().Context(), req.Code)
var exchangeOpts []oauth2.AuthCodeOption
if !a.disablePKCE {
// Do not fall back to the exchange without PKCE; the session may have been
// started before PKCE was enabled, which is rejected like a missing state.
verifier, ok := reg.Session().Get("pkce_verifier")
if !ok {
slog.WarnContext(ctx, "pkce verifier not found")
return struct{}{}, tanukirpc.WrapErrorWithStatus(http.StatusBadRequest, fmt.Errorf("request invalid"))
}
if err := reg.Session().Remove("pkce_verifier"); err != nil {
return struct{}{}, fmt.Errorf("failed to remove pkce verifier: %w", err)
}
v, _ := verifier.(string)
exchangeOpts = append(exchangeOpts, oauth2.VerifierOption(v))
}

token, err := a.oauth2Config.Exchange(ctx.Request().Context(), req.Code, exchangeOpts...)
if err != nil {
return struct{}{}, fmt.Errorf("failed to exchange code for token: %w", err)
}
Expand Down
Loading
Loading