2020-11-04 00:17:38 +00:00
|
|
|
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
// Package auth provides a handler for the OIDC authorization endpoint.
|
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2020-11-06 22:44:58 +00:00
|
|
|
"time"
|
2020-11-04 20:19:07 +00:00
|
|
|
|
2020-12-08 01:22:34 +00:00
|
|
|
coreosoidc "github.com/coreos/go-oidc"
|
2020-11-04 23:04:50 +00:00
|
|
|
"github.com/ory/fosite"
|
2020-11-06 22:44:58 +00:00
|
|
|
"github.com/ory/fosite/handler/openid"
|
|
|
|
"github.com/ory/fosite/token/jwt"
|
2020-11-11 01:58:00 +00:00
|
|
|
"golang.org/x/oauth2"
|
|
|
|
|
2020-11-04 00:17:38 +00:00
|
|
|
"go.pinniped.dev/internal/httputil/httperr"
|
2020-11-13 23:59:51 +00:00
|
|
|
"go.pinniped.dev/internal/oidc"
|
2020-11-11 01:58:00 +00:00
|
|
|
"go.pinniped.dev/internal/oidc/csrftoken"
|
2020-11-04 00:17:38 +00:00
|
|
|
"go.pinniped.dev/internal/oidc/provider"
|
2020-11-11 01:58:00 +00:00
|
|
|
"go.pinniped.dev/internal/plog"
|
2020-11-17 18:46:54 +00:00
|
|
|
"go.pinniped.dev/pkg/oidcclient/nonce"
|
|
|
|
"go.pinniped.dev/pkg/oidcclient/pkce"
|
2020-11-11 01:58:00 +00:00
|
|
|
)
|
|
|
|
|
2020-11-04 00:17:38 +00:00
|
|
|
func NewHandler(
|
2020-11-19 16:35:23 +00:00
|
|
|
downstreamIssuer string,
|
2020-11-13 23:59:51 +00:00
|
|
|
idpListGetter oidc.IDPListGetter,
|
2020-11-04 23:04:50 +00:00
|
|
|
oauthHelper fosite.OAuth2Provider,
|
2020-11-11 01:58:00 +00:00
|
|
|
generateCSRF func() (csrftoken.CSRFToken, error),
|
2020-11-04 00:17:38 +00:00
|
|
|
generatePKCE func() (pkce.Code, error),
|
|
|
|
generateNonce func() (nonce.Nonce, error),
|
2020-11-16 19:41:00 +00:00
|
|
|
upstreamStateEncoder oidc.Encoder,
|
|
|
|
cookieCodec oidc.Codec,
|
2020-11-04 00:17:38 +00:00
|
|
|
) http.Handler {
|
|
|
|
return httperr.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
if r.Method != http.MethodPost && r.Method != http.MethodGet {
|
|
|
|
// https://openid.net/specs/openid-connect-core-1_0.html#AuthRequest
|
|
|
|
// Authorization Servers MUST support the use of the HTTP GET and POST methods defined in
|
|
|
|
// RFC 2616 [RFC2616] at the Authorization Endpoint.
|
|
|
|
return httperr.Newf(http.StatusMethodNotAllowed, "%s (try GET or POST)", r.Method)
|
|
|
|
}
|
|
|
|
|
2020-11-20 21:56:35 +00:00
|
|
|
csrfFromCookie := readCSRFCookie(r, cookieCodec)
|
2020-11-12 23:36:59 +00:00
|
|
|
|
2020-11-04 23:04:50 +00:00
|
|
|
authorizeRequester, err := oauthHelper.NewAuthorizeRequest(r.Context(), r)
|
2020-11-04 15:15:19 +00:00
|
|
|
if err != nil {
|
2020-12-04 15:06:55 +00:00
|
|
|
plog.Info("authorize request error", oidc.FositeErrorForLog(err)...)
|
2020-11-04 15:15:19 +00:00
|
|
|
oauthHelper.WriteAuthorizeError(w, authorizeRequester, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-04 23:04:50 +00:00
|
|
|
upstreamIDP, err := chooseUpstreamIDP(idpListGetter)
|
2020-11-04 20:19:07 +00:00
|
|
|
if err != nil {
|
2020-11-11 20:29:14 +00:00
|
|
|
plog.WarningErr("authorize upstream config", err)
|
2020-11-04 23:04:50 +00:00
|
|
|
return err
|
2020-11-04 20:19:07 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 22:44:58 +00:00
|
|
|
// Grant the openid scope (for now) if they asked for it so that `NewAuthorizeResponse` will perform its OIDC validations.
|
2020-12-08 01:22:34 +00:00
|
|
|
oidc.GrantScopeIfRequested(authorizeRequester, coreosoidc.ScopeOpenID)
|
|
|
|
// There don't seem to be any validations inside `NewAuthorizeResponse` related to the offline_access scope
|
|
|
|
// at this time, however we will temporarily grant the scope just in case that changes in a future release of fosite.
|
|
|
|
oidc.GrantScopeIfRequested(authorizeRequester, coreosoidc.ScopeOfflineAccess)
|
2020-11-06 22:44:58 +00:00
|
|
|
|
2020-12-09 15:36:45 +00:00
|
|
|
// Grant the Pinniped STS scope if requested.
|
|
|
|
oidc.GrantScopeIfRequested(authorizeRequester, "pinniped.sts.unrestricted")
|
|
|
|
|
2020-11-06 22:44:58 +00:00
|
|
|
now := time.Now()
|
|
|
|
_, err = oauthHelper.NewAuthorizeResponse(r.Context(), authorizeRequester, &openid.DefaultSession{
|
|
|
|
Claims: &jwt.IDTokenClaims{
|
|
|
|
// Temporary claim values to allow `NewAuthorizeResponse` to perform other OIDC validations.
|
|
|
|
Subject: "none",
|
|
|
|
AuthTime: now,
|
|
|
|
RequestedAt: now,
|
|
|
|
},
|
|
|
|
})
|
2020-11-04 00:17:38 +00:00
|
|
|
if err != nil {
|
2020-12-04 15:06:55 +00:00
|
|
|
plog.Info("authorize response error", oidc.FositeErrorForLog(err)...)
|
2020-11-04 23:04:50 +00:00
|
|
|
oauthHelper.WriteAuthorizeError(w, authorizeRequester, err)
|
|
|
|
return nil
|
2020-11-04 00:17:38 +00:00
|
|
|
}
|
|
|
|
|
2020-11-11 01:58:00 +00:00
|
|
|
csrfValue, nonceValue, pkceValue, err := generateValues(generateCSRF, generateNonce, generatePKCE)
|
2020-11-04 00:17:38 +00:00
|
|
|
if err != nil {
|
2020-11-11 20:29:14 +00:00
|
|
|
plog.Error("authorize generate error", err)
|
2020-11-04 00:17:38 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-11-12 23:36:59 +00:00
|
|
|
if csrfFromCookie != "" {
|
|
|
|
csrfValue = csrfFromCookie
|
|
|
|
}
|
2020-11-04 00:17:38 +00:00
|
|
|
|
|
|
|
upstreamOAuthConfig := oauth2.Config{
|
2020-11-18 21:38:13 +00:00
|
|
|
ClientID: upstreamIDP.GetClientID(),
|
2020-11-04 00:17:38 +00:00
|
|
|
Endpoint: oauth2.Endpoint{
|
2020-11-18 21:38:13 +00:00
|
|
|
AuthURL: upstreamIDP.GetAuthorizationURL().String(),
|
2020-11-04 00:17:38 +00:00
|
|
|
},
|
2020-11-20 21:14:45 +00:00
|
|
|
RedirectURL: fmt.Sprintf("%s/callback", downstreamIssuer),
|
2020-11-18 21:38:13 +00:00
|
|
|
Scopes: upstreamIDP.GetScopes(),
|
2020-11-04 00:17:38 +00:00
|
|
|
}
|
|
|
|
|
2020-11-20 21:14:45 +00:00
|
|
|
encodedStateParamValue, err := upstreamStateParam(
|
|
|
|
authorizeRequester,
|
|
|
|
upstreamIDP.GetName(),
|
|
|
|
nonceValue,
|
|
|
|
csrfValue,
|
|
|
|
pkceValue,
|
|
|
|
upstreamStateEncoder,
|
|
|
|
)
|
2020-11-11 20:29:14 +00:00
|
|
|
if err != nil {
|
|
|
|
plog.Error("authorize upstream state param error", err)
|
|
|
|
return err
|
2020-11-11 01:58:00 +00:00
|
|
|
}
|
2020-11-11 20:29:14 +00:00
|
|
|
|
2020-11-12 23:36:59 +00:00
|
|
|
if csrfFromCookie == "" {
|
|
|
|
// We did not receive an incoming CSRF cookie, so write a new one.
|
|
|
|
err := addCSRFSetCookieHeader(w, csrfValue, cookieCodec)
|
|
|
|
if err != nil {
|
|
|
|
plog.Error("error setting CSRF cookie", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-11-11 01:58:00 +00:00
|
|
|
|
2020-12-12 01:13:27 +00:00
|
|
|
authCodeOptions := []oauth2.AuthCodeOption{
|
|
|
|
oauth2.AccessTypeOffline,
|
|
|
|
nonceValue.Param(),
|
|
|
|
pkceValue.Challenge(),
|
|
|
|
pkceValue.Method(),
|
|
|
|
}
|
|
|
|
|
|
|
|
promptParam := r.Form.Get("prompt")
|
|
|
|
if promptParam != "" && oidc.ScopeWasRequested(authorizeRequester, coreosoidc.ScopeOpenID) {
|
|
|
|
authCodeOptions = append(authCodeOptions, oauth2.SetAuthURLParam("prompt", promptParam))
|
|
|
|
}
|
|
|
|
|
2020-11-04 00:17:38 +00:00
|
|
|
http.Redirect(w, r,
|
|
|
|
upstreamOAuthConfig.AuthCodeURL(
|
2020-11-11 01:58:00 +00:00
|
|
|
encodedStateParamValue,
|
2020-12-12 01:13:27 +00:00
|
|
|
authCodeOptions...,
|
2020-11-04 00:17:38 +00:00
|
|
|
),
|
|
|
|
302,
|
|
|
|
)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-10 01:20:57 +00:00
|
|
|
func readCSRFCookie(r *http.Request, codec oidc.Decoder) csrftoken.CSRFToken {
|
2020-11-16 16:47:49 +00:00
|
|
|
receivedCSRFCookie, err := r.Cookie(oidc.CSRFCookieName)
|
2020-11-12 23:36:59 +00:00
|
|
|
if err != nil {
|
|
|
|
// Error means that the cookie was not found
|
2020-11-20 21:56:35 +00:00
|
|
|
return ""
|
2020-11-12 23:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var csrfFromCookie csrftoken.CSRFToken
|
2020-11-16 16:47:49 +00:00
|
|
|
err = codec.Decode(oidc.CSRFCookieEncodingName, receivedCSRFCookie.Value, &csrfFromCookie)
|
2020-11-12 23:36:59 +00:00
|
|
|
if err != nil {
|
2020-11-20 21:56:35 +00:00
|
|
|
// We can ignore any errors and just make a new cookie. Hopefully this will
|
|
|
|
// make the user experience better if, for example, the server rotated
|
|
|
|
// cookie signing keys and then a user submitted a very old cookie.
|
|
|
|
return ""
|
2020-11-12 23:36:59 +00:00
|
|
|
}
|
|
|
|
|
2020-11-20 21:56:35 +00:00
|
|
|
return csrfFromCookie
|
2020-11-12 23:36:59 +00:00
|
|
|
}
|
|
|
|
|
2020-11-18 21:38:13 +00:00
|
|
|
func chooseUpstreamIDP(idpListGetter oidc.IDPListGetter) (provider.UpstreamOIDCIdentityProviderI, error) {
|
2020-11-04 00:17:38 +00:00
|
|
|
allUpstreamIDPs := idpListGetter.GetIDPList()
|
|
|
|
if len(allUpstreamIDPs) == 0 {
|
|
|
|
return nil, httperr.New(
|
|
|
|
http.StatusUnprocessableEntity,
|
|
|
|
"No upstream providers are configured",
|
|
|
|
)
|
|
|
|
} else if len(allUpstreamIDPs) > 1 {
|
|
|
|
return nil, httperr.New(
|
|
|
|
http.StatusUnprocessableEntity,
|
|
|
|
"Too many upstream providers are configured (support for multiple upstreams is not yet implemented)",
|
|
|
|
)
|
|
|
|
}
|
2020-11-18 21:38:13 +00:00
|
|
|
return allUpstreamIDPs[0], nil
|
2020-11-04 00:17:38 +00:00
|
|
|
}
|
|
|
|
|
2020-11-11 01:58:00 +00:00
|
|
|
func generateValues(
|
|
|
|
generateCSRF func() (csrftoken.CSRFToken, error),
|
2020-11-04 00:17:38 +00:00
|
|
|
generateNonce func() (nonce.Nonce, error),
|
|
|
|
generatePKCE func() (pkce.Code, error),
|
2020-11-11 01:58:00 +00:00
|
|
|
) (csrftoken.CSRFToken, nonce.Nonce, pkce.Code, error) {
|
|
|
|
csrfValue, err := generateCSRF()
|
2020-11-04 00:17:38 +00:00
|
|
|
if err != nil {
|
2020-11-11 01:58:00 +00:00
|
|
|
return "", "", "", httperr.Wrap(http.StatusInternalServerError, "error generating CSRF token", err)
|
2020-11-04 00:17:38 +00:00
|
|
|
}
|
|
|
|
nonceValue, err := generateNonce()
|
|
|
|
if err != nil {
|
|
|
|
return "", "", "", httperr.Wrap(http.StatusInternalServerError, "error generating nonce param", err)
|
|
|
|
}
|
|
|
|
pkceValue, err := generatePKCE()
|
|
|
|
if err != nil {
|
|
|
|
return "", "", "", httperr.Wrap(http.StatusInternalServerError, "error generating PKCE param", err)
|
|
|
|
}
|
2020-11-11 01:58:00 +00:00
|
|
|
return csrfValue, nonceValue, pkceValue, nil
|
2020-11-04 00:17:38 +00:00
|
|
|
}
|
2020-11-10 18:33:52 +00:00
|
|
|
|
2020-11-11 20:29:14 +00:00
|
|
|
func upstreamStateParam(
|
|
|
|
authorizeRequester fosite.AuthorizeRequester,
|
2020-11-20 21:14:45 +00:00
|
|
|
upstreamName string,
|
2020-11-11 20:29:14 +00:00
|
|
|
nonceValue nonce.Nonce,
|
|
|
|
csrfValue csrftoken.CSRFToken,
|
|
|
|
pkceValue pkce.Code,
|
2020-11-16 19:41:00 +00:00
|
|
|
encoder oidc.Encoder,
|
2020-11-11 20:29:14 +00:00
|
|
|
) (string, error) {
|
2020-11-16 19:41:00 +00:00
|
|
|
stateParamData := oidc.UpstreamStateParamData{
|
|
|
|
AuthParams: authorizeRequester.GetRequestForm().Encode(),
|
2020-11-20 21:14:45 +00:00
|
|
|
UpstreamName: upstreamName,
|
2020-11-16 19:41:00 +00:00
|
|
|
Nonce: nonceValue,
|
|
|
|
CSRFToken: csrfValue,
|
|
|
|
PKCECode: pkceValue,
|
|
|
|
FormatVersion: oidc.UpstreamStateParamFormatVersion,
|
2020-11-11 20:29:14 +00:00
|
|
|
}
|
2020-11-16 19:41:00 +00:00
|
|
|
encodedStateParamValue, err := encoder.Encode(oidc.UpstreamStateParamEncodingName, stateParamData)
|
2020-11-11 20:29:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", httperr.Wrap(http.StatusInternalServerError, "error encoding upstream state param", err)
|
|
|
|
}
|
|
|
|
return encodedStateParamValue, nil
|
|
|
|
}
|
|
|
|
|
2020-12-10 01:20:57 +00:00
|
|
|
func addCSRFSetCookieHeader(w http.ResponseWriter, csrfValue csrftoken.CSRFToken, codec oidc.Encoder) error {
|
2020-11-16 16:47:49 +00:00
|
|
|
encodedCSRFValue, err := codec.Encode(oidc.CSRFCookieEncodingName, csrfValue)
|
2020-11-12 23:36:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return httperr.Wrap(http.StatusInternalServerError, "error encoding CSRF cookie", err)
|
|
|
|
}
|
|
|
|
|
2020-11-11 20:29:14 +00:00
|
|
|
http.SetCookie(w, &http.Cookie{
|
2020-11-16 16:47:49 +00:00
|
|
|
Name: oidc.CSRFCookieName,
|
2020-11-12 23:36:59 +00:00
|
|
|
Value: encodedCSRFValue,
|
2020-11-11 20:29:14 +00:00
|
|
|
HttpOnly: true,
|
2020-12-04 03:23:58 +00:00
|
|
|
SameSite: http.SameSiteLaxMode,
|
2020-11-11 20:29:14 +00:00
|
|
|
Secure: true,
|
2020-12-01 23:01:22 +00:00
|
|
|
Path: "/",
|
2020-11-11 20:29:14 +00:00
|
|
|
})
|
2020-11-12 23:36:59 +00:00
|
|
|
|
|
|
|
return nil
|
2020-11-11 20:29:14 +00:00
|
|
|
}
|