2023-03-29 20:55:00 +00:00
|
|
|
// Copyright 2020-2023 the Pinniped contributors. All Rights Reserved.
|
2020-10-06 22:27:36 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2020-10-21 18:04:46 +00:00
|
|
|
// Package oidcclient implements a CLI OIDC login flow.
|
|
|
|
package oidcclient
|
2020-10-06 22:27:36 +00:00
|
|
|
|
|
|
|
import (
|
2021-04-20 00:59:46 +00:00
|
|
|
"bufio"
|
2020-10-06 22:27:36 +00:00
|
|
|
"context"
|
2020-12-04 23:33:53 +00:00
|
|
|
"encoding/json"
|
2021-04-20 00:59:46 +00:00
|
|
|
"errors"
|
2020-10-06 22:27:36 +00:00
|
|
|
"fmt"
|
2021-07-08 19:32:44 +00:00
|
|
|
"io"
|
2020-12-10 16:09:42 +00:00
|
|
|
"mime"
|
2020-10-06 22:27:36 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2021-04-20 00:59:46 +00:00
|
|
|
"os"
|
2020-10-21 18:05:19 +00:00
|
|
|
"sort"
|
2020-12-04 23:33:53 +00:00
|
|
|
"strings"
|
2021-07-29 22:49:16 +00:00
|
|
|
"sync"
|
2020-10-06 22:27:36 +00:00
|
|
|
"time"
|
|
|
|
|
Create username scope, required for clients to get username in ID token
- For backwards compatibility with older Pinniped CLIs, the pinniped-cli
client does not need to request the username or groups scopes for them
to be granted. For dynamic clients, the usual OAuth2 rules apply:
the client must be allowed to request the scopes according to its
configuration, and the client must actually request the scopes in the
authorization request.
- If the username scope was not granted, then there will be no username
in the ID token, and the cluster-scoped token exchange will fail since
there would be no username in the resulting cluster-scoped ID token.
- The OIDC well-known discovery endpoint lists the username and groups
scopes in the scopes_supported list, and lists the username and groups
claims in the claims_supported list.
- Add username and groups scopes to the default list of scopes
put into kubeconfig files by "pinniped get kubeconfig" CLI command,
and the default list of scopes used by "pinniped login oidc" when
no list of scopes is specified in the kubeconfig file
- The warning header about group memberships changing during upstream
refresh will only be sent to the pinniped-cli client, since it is
only intended for kubectl and it could leak the username to the
client (which may not have the username scope granted) through the
warning message text.
- Add the user's username to the session storage as a new field, so that
during upstream refresh we can compare the original username from the
initial authorization to the refreshed username, even in the case when
the username scope was not granted (and therefore the username is not
stored in the ID token claims of the session storage)
- Bump the Supervisor session storage format version from 2 to 3
due to the username field being added to the session struct
- Extract commonly used string constants related to OIDC flows to api
package.
- Change some import names to make them consistent:
- Always import github.com/coreos/go-oidc/v3/oidc as "coreosoidc"
- Always import go.pinniped.dev/generated/latest/apis/supervisor/oidc
as "oidcapi"
- Always import go.pinniped.dev/internal/oidc as "oidc"
2022-08-08 23:29:22 +00:00
|
|
|
coreosoidc "github.com/coreos/go-oidc/v3/oidc"
|
2021-04-30 19:10:04 +00:00
|
|
|
"github.com/go-logr/logr"
|
2020-10-06 22:27:36 +00:00
|
|
|
"github.com/pkg/browser"
|
|
|
|
"golang.org/x/oauth2"
|
2021-04-20 00:59:46 +00:00
|
|
|
"golang.org/x/term"
|
2020-12-04 23:33:53 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2023-03-29 20:55:00 +00:00
|
|
|
"k8s.io/utils/strings/slices"
|
2020-10-06 22:27:36 +00:00
|
|
|
|
Create username scope, required for clients to get username in ID token
- For backwards compatibility with older Pinniped CLIs, the pinniped-cli
client does not need to request the username or groups scopes for them
to be granted. For dynamic clients, the usual OAuth2 rules apply:
the client must be allowed to request the scopes according to its
configuration, and the client must actually request the scopes in the
authorization request.
- If the username scope was not granted, then there will be no username
in the ID token, and the cluster-scoped token exchange will fail since
there would be no username in the resulting cluster-scoped ID token.
- The OIDC well-known discovery endpoint lists the username and groups
scopes in the scopes_supported list, and lists the username and groups
claims in the claims_supported list.
- Add username and groups scopes to the default list of scopes
put into kubeconfig files by "pinniped get kubeconfig" CLI command,
and the default list of scopes used by "pinniped login oidc" when
no list of scopes is specified in the kubeconfig file
- The warning header about group memberships changing during upstream
refresh will only be sent to the pinniped-cli client, since it is
only intended for kubectl and it could leak the username to the
client (which may not have the username scope granted) through the
warning message text.
- Add the user's username to the session storage as a new field, so that
during upstream refresh we can compare the original username from the
initial authorization to the refreshed username, even in the case when
the username scope was not granted (and therefore the username is not
stored in the ID token claims of the session storage)
- Bump the Supervisor session storage format version from 2 to 3
due to the username field being added to the session struct
- Extract commonly used string constants related to OIDC flows to api
package.
- Change some import names to make them consistent:
- Always import github.com/coreos/go-oidc/v3/oidc as "coreosoidc"
- Always import go.pinniped.dev/generated/latest/apis/supervisor/oidc
as "oidcapi"
- Always import go.pinniped.dev/internal/oidc as "oidc"
2022-08-08 23:29:22 +00:00
|
|
|
oidcapi "go.pinniped.dev/generated/latest/apis/supervisor/oidc"
|
2023-06-22 22:12:33 +00:00
|
|
|
"go.pinniped.dev/internal/federationdomain/upstreamprovider"
|
2020-10-06 22:27:36 +00:00
|
|
|
"go.pinniped.dev/internal/httputil/httperr"
|
|
|
|
"go.pinniped.dev/internal/httputil/securityheader"
|
2021-10-20 11:59:24 +00:00
|
|
|
"go.pinniped.dev/internal/net/phttp"
|
2022-04-16 02:43:53 +00:00
|
|
|
"go.pinniped.dev/internal/plog"
|
2020-11-30 23:14:57 +00:00
|
|
|
"go.pinniped.dev/internal/upstreamoidc"
|
2020-11-17 18:46:54 +00:00
|
|
|
"go.pinniped.dev/pkg/oidcclient/nonce"
|
2020-11-30 23:02:03 +00:00
|
|
|
"go.pinniped.dev/pkg/oidcclient/oidctypes"
|
2020-11-17 18:46:54 +00:00
|
|
|
"go.pinniped.dev/pkg/oidcclient/pkce"
|
|
|
|
"go.pinniped.dev/pkg/oidcclient/state"
|
2020-10-06 22:27:36 +00:00
|
|
|
)
|
|
|
|
|
2020-10-21 18:05:19 +00:00
|
|
|
const (
|
|
|
|
// minIDTokenValidity is the minimum amount of time that a cached ID token must be still be valid to be considered.
|
|
|
|
// This is non-zero to ensure that most of the time, your ID token won't expire in the middle of a multi-step k8s
|
|
|
|
// API operation.
|
|
|
|
minIDTokenValidity = 10 * time.Minute
|
2020-10-22 21:12:02 +00:00
|
|
|
|
2020-12-16 04:11:49 +00:00
|
|
|
// httpRequestTimeout is the timeout for operations that involve one (or a few) non-interactive HTTPS requests.
|
|
|
|
// Since these don't involve any user interaction, they should always be roughly as fast as network latency.
|
|
|
|
httpRequestTimeout = 60 * time.Second
|
|
|
|
|
|
|
|
// overallTimeout is the overall time that a login is allowed to take. This includes several user interactions, so
|
|
|
|
// we set this to be relatively long.
|
|
|
|
overallTimeout = 90 * time.Minute
|
2021-04-20 00:59:46 +00:00
|
|
|
|
|
|
|
defaultLDAPUsernamePrompt = "Username: "
|
|
|
|
defaultLDAPPasswordPrompt = "Password: "
|
|
|
|
|
2021-07-19 23:20:59 +00:00
|
|
|
// For CLI-based auth, such as with LDAP upstream identity providers, the user may use these environment variables
|
|
|
|
// to avoid getting interactively prompted for username and password.
|
|
|
|
defaultUsernameEnvVarName = "PINNIPED_USERNAME"
|
2021-07-27 00:20:49 +00:00
|
|
|
defaultPasswordEnvVarName = "PINNIPED_PASSWORD" //nolint:gosec // this is not a credential
|
2021-07-19 23:20:59 +00:00
|
|
|
|
2021-04-20 00:59:46 +00:00
|
|
|
httpLocationHeaderName = "Location"
|
2020-10-21 18:05:19 +00:00
|
|
|
)
|
|
|
|
|
2021-07-27 21:10:05 +00:00
|
|
|
// stdin returns the file descriptor for stdin as an int.
|
|
|
|
func stdin() int { return int(os.Stdin.Fd()) }
|
|
|
|
|
2020-10-06 22:27:36 +00:00
|
|
|
type handlerState struct {
|
|
|
|
// Basic parameters.
|
|
|
|
ctx context.Context
|
2021-04-30 19:10:04 +00:00
|
|
|
logger logr.Logger
|
2020-10-06 22:27:36 +00:00
|
|
|
issuer string
|
|
|
|
clientID string
|
|
|
|
scopes []string
|
2020-10-21 18:05:19 +00:00
|
|
|
cache SessionCache
|
2020-10-06 22:27:36 +00:00
|
|
|
|
2021-04-17 01:30:31 +00:00
|
|
|
upstreamIdentityProviderName string
|
|
|
|
upstreamIdentityProviderType string
|
2021-05-13 17:05:56 +00:00
|
|
|
cliToSendCredentials bool
|
2021-04-17 01:30:31 +00:00
|
|
|
|
2020-12-04 23:33:53 +00:00
|
|
|
requestedAudience string
|
|
|
|
|
2020-11-16 17:54:13 +00:00
|
|
|
httpClient *http.Client
|
|
|
|
|
2020-10-06 22:27:36 +00:00
|
|
|
// Parameters of the localhost listener.
|
|
|
|
listenAddr string
|
|
|
|
callbackPath string
|
|
|
|
|
|
|
|
// Generated parameters of a login flow.
|
Create username scope, required for clients to get username in ID token
- For backwards compatibility with older Pinniped CLIs, the pinniped-cli
client does not need to request the username or groups scopes for them
to be granted. For dynamic clients, the usual OAuth2 rules apply:
the client must be allowed to request the scopes according to its
configuration, and the client must actually request the scopes in the
authorization request.
- If the username scope was not granted, then there will be no username
in the ID token, and the cluster-scoped token exchange will fail since
there would be no username in the resulting cluster-scoped ID token.
- The OIDC well-known discovery endpoint lists the username and groups
scopes in the scopes_supported list, and lists the username and groups
claims in the claims_supported list.
- Add username and groups scopes to the default list of scopes
put into kubeconfig files by "pinniped get kubeconfig" CLI command,
and the default list of scopes used by "pinniped login oidc" when
no list of scopes is specified in the kubeconfig file
- The warning header about group memberships changing during upstream
refresh will only be sent to the pinniped-cli client, since it is
only intended for kubectl and it could leak the username to the
client (which may not have the username scope granted) through the
warning message text.
- Add the user's username to the session storage as a new field, so that
during upstream refresh we can compare the original username from the
initial authorization to the refreshed username, even in the case when
the username scope was not granted (and therefore the username is not
stored in the ID token claims of the session storage)
- Bump the Supervisor session storage format version from 2 to 3
due to the username field being added to the session struct
- Extract commonly used string constants related to OIDC flows to api
package.
- Change some import names to make them consistent:
- Always import github.com/coreos/go-oidc/v3/oidc as "coreosoidc"
- Always import go.pinniped.dev/generated/latest/apis/supervisor/oidc
as "oidcapi"
- Always import go.pinniped.dev/internal/oidc as "oidc"
2022-08-08 23:29:22 +00:00
|
|
|
provider *coreosoidc.Provider
|
2020-11-30 23:14:57 +00:00
|
|
|
oauth2Config *oauth2.Config
|
2021-06-21 19:19:12 +00:00
|
|
|
useFormPost bool
|
2020-11-30 23:14:57 +00:00
|
|
|
state state.State
|
|
|
|
nonce nonce.Nonce
|
|
|
|
pkce pkce.Code
|
2020-10-06 22:27:36 +00:00
|
|
|
|
|
|
|
// External calls for things.
|
2020-12-04 23:33:53 +00:00
|
|
|
generateState func() (state.State, error)
|
|
|
|
generatePKCE func() (pkce.Code, error)
|
|
|
|
generateNonce func() (nonce.Nonce, error)
|
|
|
|
openURL func(string) error
|
2021-07-19 23:20:59 +00:00
|
|
|
getEnv func(key string) string
|
2021-07-08 19:32:44 +00:00
|
|
|
listen func(string, string) (net.Listener, error)
|
|
|
|
isTTY func(int) bool
|
2023-05-08 21:07:38 +00:00
|
|
|
getProvider func(*oauth2.Config, *coreosoidc.Provider, *http.Client) upstreamprovider.UpstreamOIDCIdentityProviderI
|
Create username scope, required for clients to get username in ID token
- For backwards compatibility with older Pinniped CLIs, the pinniped-cli
client does not need to request the username or groups scopes for them
to be granted. For dynamic clients, the usual OAuth2 rules apply:
the client must be allowed to request the scopes according to its
configuration, and the client must actually request the scopes in the
authorization request.
- If the username scope was not granted, then there will be no username
in the ID token, and the cluster-scoped token exchange will fail since
there would be no username in the resulting cluster-scoped ID token.
- The OIDC well-known discovery endpoint lists the username and groups
scopes in the scopes_supported list, and lists the username and groups
claims in the claims_supported list.
- Add username and groups scopes to the default list of scopes
put into kubeconfig files by "pinniped get kubeconfig" CLI command,
and the default list of scopes used by "pinniped login oidc" when
no list of scopes is specified in the kubeconfig file
- The warning header about group memberships changing during upstream
refresh will only be sent to the pinniped-cli client, since it is
only intended for kubectl and it could leak the username to the
client (which may not have the username scope granted) through the
warning message text.
- Add the user's username to the session storage as a new field, so that
during upstream refresh we can compare the original username from the
initial authorization to the refreshed username, even in the case when
the username scope was not granted (and therefore the username is not
stored in the ID token claims of the session storage)
- Bump the Supervisor session storage format version from 2 to 3
due to the username field being added to the session struct
- Extract commonly used string constants related to OIDC flows to api
package.
- Change some import names to make them consistent:
- Always import github.com/coreos/go-oidc/v3/oidc as "coreosoidc"
- Always import go.pinniped.dev/generated/latest/apis/supervisor/oidc
as "oidcapi"
- Always import go.pinniped.dev/internal/oidc as "oidc"
2022-08-08 23:29:22 +00:00
|
|
|
validateIDToken func(ctx context.Context, provider *coreosoidc.Provider, audience string, token string) (*coreosoidc.IDToken, error)
|
2021-06-30 20:06:37 +00:00
|
|
|
promptForValue func(ctx context.Context, promptLabel string) (string, error)
|
2021-07-29 22:49:16 +00:00
|
|
|
promptForSecret func(promptLabel string) (string, error)
|
2020-10-06 22:27:36 +00:00
|
|
|
|
|
|
|
callbacks chan callbackResult
|
|
|
|
}
|
|
|
|
|
|
|
|
type callbackResult struct {
|
2020-11-30 23:02:03 +00:00
|
|
|
token *oidctypes.Token
|
2020-10-06 22:27:36 +00:00
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2020-10-21 18:04:46 +00:00
|
|
|
// Option is an optional configuration for Login().
|
2020-10-06 22:27:36 +00:00
|
|
|
type Option func(*handlerState) error
|
|
|
|
|
|
|
|
// WithContext specifies a specific context.Context under which to perform the login. If this option is not specified,
|
|
|
|
// login happens under context.Background().
|
|
|
|
func WithContext(ctx context.Context) Option {
|
|
|
|
return func(h *handlerState) error {
|
|
|
|
h.ctx = ctx
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-28 16:34:42 +00:00
|
|
|
// WithLogger specifies a PLogger to use with the login.
|
|
|
|
// If not specified this will default to a new logger.
|
2021-04-30 19:10:04 +00:00
|
|
|
func WithLogger(logger logr.Logger) Option {
|
2021-04-28 16:34:42 +00:00
|
|
|
return func(h *handlerState) error {
|
|
|
|
h.logger = logger
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 22:27:36 +00:00
|
|
|
// WithListenPort specifies a TCP listen port on localhost, which will be used for the redirect_uri and to handle the
|
|
|
|
// authorization code callback. By default, a random high port will be chosen which requires the authorization server
|
2021-04-20 00:59:46 +00:00
|
|
|
// to support wildcard port numbers as described by https://tools.ietf.org/html/rfc8252#section-7.3:
|
2020-12-01 22:53:22 +00:00
|
|
|
//
|
|
|
|
// The authorization server MUST allow any port to be specified at the
|
|
|
|
// time of the request for loopback IP redirect URIs, to accommodate
|
|
|
|
// clients that obtain an available ephemeral port from the operating
|
|
|
|
// system at the time of the request.
|
2020-10-06 22:27:36 +00:00
|
|
|
func WithListenPort(port uint16) Option {
|
|
|
|
return func(h *handlerState) error {
|
2021-06-24 23:19:11 +00:00
|
|
|
h.listenAddr = net.JoinHostPort("localhost", fmt.Sprint(port))
|
2020-10-06 22:27:36 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithScopes sets the OAuth2 scopes to request during login. If not specified, it defaults to
|
|
|
|
// "offline_access openid email profile".
|
|
|
|
func WithScopes(scopes []string) Option {
|
|
|
|
return func(h *handlerState) error {
|
|
|
|
h.scopes = scopes
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithBrowserOpen overrides the default "open browser" functionality with a custom callback. If not specified,
|
|
|
|
// an implementation using https://github.com/pkg/browser will be used by default.
|
2021-07-07 22:17:57 +00:00
|
|
|
//
|
|
|
|
// Deprecated: this option will be removed in a future version of Pinniped. See the
|
|
|
|
// WithSkipBrowserOpen() option instead.
|
2020-10-06 22:27:36 +00:00
|
|
|
func WithBrowserOpen(openURL func(url string) error) Option {
|
|
|
|
return func(h *handlerState) error {
|
|
|
|
h.openURL = openURL
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-07 22:17:57 +00:00
|
|
|
// WithSkipBrowserOpen causes the login to only print the authorize URL, but skips attempting to
|
|
|
|
// open the user's default web browser.
|
|
|
|
func WithSkipBrowserOpen() Option {
|
|
|
|
return func(h *handlerState) error {
|
|
|
|
h.openURL = func(_ string) error { return nil }
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-09 03:26:21 +00:00
|
|
|
// WithSkipListen causes the login skip starting the localhost listener, forcing the manual copy/paste login flow.
|
|
|
|
func WithSkipListen() Option {
|
|
|
|
return func(h *handlerState) error {
|
|
|
|
h.listen = func(string, string) (net.Listener, error) { return nil, nil }
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-30 23:02:03 +00:00
|
|
|
// SessionCacheKey contains the data used to select a valid session cache entry.
|
|
|
|
type SessionCacheKey struct {
|
2023-05-08 21:07:38 +00:00
|
|
|
Issuer string `json:"issuer"`
|
|
|
|
ClientID string `json:"clientID"`
|
|
|
|
Scopes []string `json:"scopes"`
|
|
|
|
RedirectURI string `json:"redirect_uri"`
|
|
|
|
UpstreamProviderName string `json:"upstream_provider_name,omitempty"`
|
2020-11-30 23:02:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type SessionCache interface {
|
|
|
|
GetToken(SessionCacheKey) *oidctypes.Token
|
|
|
|
PutToken(SessionCacheKey, *oidctypes.Token)
|
|
|
|
}
|
|
|
|
|
2020-10-21 18:05:19 +00:00
|
|
|
// WithSessionCache sets the session cache backend for storing and retrieving previously-issued ID tokens and refresh tokens.
|
|
|
|
func WithSessionCache(cache SessionCache) Option {
|
|
|
|
return func(h *handlerState) error {
|
|
|
|
h.cache = cache
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-16 17:54:13 +00:00
|
|
|
// WithClient sets the HTTP client used to make CLI-to-provider requests.
|
|
|
|
func WithClient(httpClient *http.Client) Option {
|
|
|
|
return func(h *handlerState) error {
|
|
|
|
h.httpClient = httpClient
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-16 03:59:57 +00:00
|
|
|
// WithRequestAudience causes the login flow to perform an additional token exchange using the RFC8693 flow.
|
2020-12-04 23:33:53 +00:00
|
|
|
func WithRequestAudience(audience string) Option {
|
|
|
|
return func(h *handlerState) error {
|
|
|
|
h.requestedAudience = audience
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-13 17:05:56 +00:00
|
|
|
// WithCLISendingCredentials causes the login flow to use CLI-based prompts for username and password and causes the
|
2021-04-17 01:30:31 +00:00
|
|
|
// call to the Issuer's authorize endpoint to be made directly (no web browser) with the username and password on custom
|
|
|
|
// HTTP headers. This is only intended to be used when the issuer is a Pinniped Supervisor and the upstream identity
|
2021-08-24 19:19:29 +00:00
|
|
|
// provider type supports this style of authentication. Currently, this is supported by LDAPIdentityProviders, ActiveDirectoryIdentityProviders,
|
2021-08-12 17:00:18 +00:00
|
|
|
// and by OIDCIdentityProviders which optionally enable the resource owner password credentials grant flow.
|
2021-05-13 17:05:56 +00:00
|
|
|
// This should never be used with non-Supervisor issuers because it will send the user's password to the authorization
|
|
|
|
// endpoint as a custom header, which would be ignored but could potentially get logged somewhere by the issuer.
|
|
|
|
func WithCLISendingCredentials() Option {
|
2021-04-17 01:30:31 +00:00
|
|
|
return func(h *handlerState) error {
|
2021-05-13 17:05:56 +00:00
|
|
|
h.cliToSendCredentials = true
|
2021-04-17 01:30:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithUpstreamIdentityProvider causes the specified name and type to be sent as custom query parameters to the
|
|
|
|
// issuer's authorize endpoint. This is only intended to be used when the issuer is a Pinniped Supervisor, in which
|
|
|
|
// case it provides a mechanism to choose among several upstream identity providers.
|
|
|
|
// Other issuers will ignore these custom query parameters.
|
|
|
|
func WithUpstreamIdentityProvider(upstreamName, upstreamType string) Option {
|
|
|
|
return func(h *handlerState) error {
|
|
|
|
h.upstreamIdentityProviderName = upstreamName
|
|
|
|
h.upstreamIdentityProviderType = upstreamType
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-21 18:05:19 +00:00
|
|
|
// nopCache is a SessionCache that doesn't actually do anything.
|
|
|
|
type nopCache struct{}
|
|
|
|
|
2020-11-30 23:02:03 +00:00
|
|
|
func (*nopCache) GetToken(SessionCacheKey) *oidctypes.Token { return nil }
|
|
|
|
func (*nopCache) PutToken(SessionCacheKey, *oidctypes.Token) {}
|
2020-10-21 18:05:19 +00:00
|
|
|
|
2020-10-21 18:04:46 +00:00
|
|
|
// Login performs an OAuth2/OIDC authorization code login using a localhost listener.
|
2020-11-30 23:02:03 +00:00
|
|
|
func Login(issuer string, clientID string, opts ...Option) (*oidctypes.Token, error) {
|
2020-10-06 22:27:36 +00:00
|
|
|
h := handlerState{
|
|
|
|
issuer: issuer,
|
|
|
|
clientID: clientID,
|
|
|
|
listenAddr: "localhost:0",
|
Create username scope, required for clients to get username in ID token
- For backwards compatibility with older Pinniped CLIs, the pinniped-cli
client does not need to request the username or groups scopes for them
to be granted. For dynamic clients, the usual OAuth2 rules apply:
the client must be allowed to request the scopes according to its
configuration, and the client must actually request the scopes in the
authorization request.
- If the username scope was not granted, then there will be no username
in the ID token, and the cluster-scoped token exchange will fail since
there would be no username in the resulting cluster-scoped ID token.
- The OIDC well-known discovery endpoint lists the username and groups
scopes in the scopes_supported list, and lists the username and groups
claims in the claims_supported list.
- Add username and groups scopes to the default list of scopes
put into kubeconfig files by "pinniped get kubeconfig" CLI command,
and the default list of scopes used by "pinniped login oidc" when
no list of scopes is specified in the kubeconfig file
- The warning header about group memberships changing during upstream
refresh will only be sent to the pinniped-cli client, since it is
only intended for kubectl and it could leak the username to the
client (which may not have the username scope granted) through the
warning message text.
- Add the user's username to the session storage as a new field, so that
during upstream refresh we can compare the original username from the
initial authorization to the refreshed username, even in the case when
the username scope was not granted (and therefore the username is not
stored in the ID token claims of the session storage)
- Bump the Supervisor session storage format version from 2 to 3
due to the username field being added to the session struct
- Extract commonly used string constants related to OIDC flows to api
package.
- Change some import names to make them consistent:
- Always import github.com/coreos/go-oidc/v3/oidc as "coreosoidc"
- Always import go.pinniped.dev/generated/latest/apis/supervisor/oidc
as "oidcapi"
- Always import go.pinniped.dev/internal/oidc as "oidc"
2022-08-08 23:29:22 +00:00
|
|
|
scopes: []string{oidcapi.ScopeOfflineAccess, oidcapi.ScopeOpenID, oidcapi.ScopeEmail, oidcapi.ScopeProfile},
|
2020-10-21 18:05:19 +00:00
|
|
|
cache: &nopCache{},
|
2020-10-06 22:27:36 +00:00
|
|
|
callbackPath: "/callback",
|
|
|
|
ctx: context.Background(),
|
2021-05-03 16:13:18 +00:00
|
|
|
logger: logr.Discard(), // discard logs unless a logger is specified
|
2021-07-29 22:49:16 +00:00
|
|
|
callbacks: make(chan callbackResult, 2),
|
2021-10-20 11:59:24 +00:00
|
|
|
httpClient: phttp.Default(nil),
|
2020-10-06 22:27:36 +00:00
|
|
|
|
|
|
|
// Default implementations of external dependencies (to be mocked in tests).
|
|
|
|
generateState: state.Generate,
|
|
|
|
generateNonce: nonce.Generate,
|
|
|
|
generatePKCE: pkce.Generate,
|
|
|
|
openURL: browser.OpenURL,
|
2021-07-19 23:20:59 +00:00
|
|
|
getEnv: os.Getenv,
|
2021-07-08 19:32:44 +00:00
|
|
|
listen: net.Listen,
|
|
|
|
isTTY: term.IsTerminal,
|
2020-11-30 23:14:57 +00:00
|
|
|
getProvider: upstreamoidc.New,
|
Create username scope, required for clients to get username in ID token
- For backwards compatibility with older Pinniped CLIs, the pinniped-cli
client does not need to request the username or groups scopes for them
to be granted. For dynamic clients, the usual OAuth2 rules apply:
the client must be allowed to request the scopes according to its
configuration, and the client must actually request the scopes in the
authorization request.
- If the username scope was not granted, then there will be no username
in the ID token, and the cluster-scoped token exchange will fail since
there would be no username in the resulting cluster-scoped ID token.
- The OIDC well-known discovery endpoint lists the username and groups
scopes in the scopes_supported list, and lists the username and groups
claims in the claims_supported list.
- Add username and groups scopes to the default list of scopes
put into kubeconfig files by "pinniped get kubeconfig" CLI command,
and the default list of scopes used by "pinniped login oidc" when
no list of scopes is specified in the kubeconfig file
- The warning header about group memberships changing during upstream
refresh will only be sent to the pinniped-cli client, since it is
only intended for kubectl and it could leak the username to the
client (which may not have the username scope granted) through the
warning message text.
- Add the user's username to the session storage as a new field, so that
during upstream refresh we can compare the original username from the
initial authorization to the refreshed username, even in the case when
the username scope was not granted (and therefore the username is not
stored in the ID token claims of the session storage)
- Bump the Supervisor session storage format version from 2 to 3
due to the username field being added to the session struct
- Extract commonly used string constants related to OIDC flows to api
package.
- Change some import names to make them consistent:
- Always import github.com/coreos/go-oidc/v3/oidc as "coreosoidc"
- Always import go.pinniped.dev/generated/latest/apis/supervisor/oidc
as "oidcapi"
- Always import go.pinniped.dev/internal/oidc as "oidc"
2022-08-08 23:29:22 +00:00
|
|
|
validateIDToken: func(ctx context.Context, provider *coreosoidc.Provider, audience string, token string) (*coreosoidc.IDToken, error) {
|
|
|
|
return provider.Verifier(&coreosoidc.Config{ClientID: audience}).Verify(ctx, token)
|
2020-12-04 23:33:53 +00:00
|
|
|
},
|
2021-04-20 00:59:46 +00:00
|
|
|
promptForValue: promptForValue,
|
|
|
|
promptForSecret: promptForSecret,
|
2020-10-06 22:27:36 +00:00
|
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
|
|
if err := opt(&h); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-16 04:11:49 +00:00
|
|
|
// Copy the configured HTTP client to set a request timeout (the Go default client has no timeout configured).
|
|
|
|
httpClientWithTimeout := *h.httpClient
|
|
|
|
httpClientWithTimeout.Timeout = httpRequestTimeout
|
|
|
|
h.httpClient = &httpClientWithTimeout
|
|
|
|
|
2020-10-06 22:27:36 +00:00
|
|
|
// Always set a long, but non-infinite timeout for this operation.
|
2020-12-16 04:11:49 +00:00
|
|
|
ctx, cancel := context.WithTimeout(h.ctx, overallTimeout)
|
2020-10-06 22:27:36 +00:00
|
|
|
defer cancel()
|
Create username scope, required for clients to get username in ID token
- For backwards compatibility with older Pinniped CLIs, the pinniped-cli
client does not need to request the username or groups scopes for them
to be granted. For dynamic clients, the usual OAuth2 rules apply:
the client must be allowed to request the scopes according to its
configuration, and the client must actually request the scopes in the
authorization request.
- If the username scope was not granted, then there will be no username
in the ID token, and the cluster-scoped token exchange will fail since
there would be no username in the resulting cluster-scoped ID token.
- The OIDC well-known discovery endpoint lists the username and groups
scopes in the scopes_supported list, and lists the username and groups
claims in the claims_supported list.
- Add username and groups scopes to the default list of scopes
put into kubeconfig files by "pinniped get kubeconfig" CLI command,
and the default list of scopes used by "pinniped login oidc" when
no list of scopes is specified in the kubeconfig file
- The warning header about group memberships changing during upstream
refresh will only be sent to the pinniped-cli client, since it is
only intended for kubectl and it could leak the username to the
client (which may not have the username scope granted) through the
warning message text.
- Add the user's username to the session storage as a new field, so that
during upstream refresh we can compare the original username from the
initial authorization to the refreshed username, even in the case when
the username scope was not granted (and therefore the username is not
stored in the ID token claims of the session storage)
- Bump the Supervisor session storage format version from 2 to 3
due to the username field being added to the session struct
- Extract commonly used string constants related to OIDC flows to api
package.
- Change some import names to make them consistent:
- Always import github.com/coreos/go-oidc/v3/oidc as "coreosoidc"
- Always import go.pinniped.dev/generated/latest/apis/supervisor/oidc
as "oidcapi"
- Always import go.pinniped.dev/internal/oidc as "oidc"
2022-08-08 23:29:22 +00:00
|
|
|
ctx = coreosoidc.ClientContext(ctx, h.httpClient)
|
2020-10-06 22:27:36 +00:00
|
|
|
h.ctx = ctx
|
|
|
|
|
|
|
|
// Initialize login parameters.
|
|
|
|
var err error
|
|
|
|
h.state, err = h.generateState()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
h.nonce, err = h.generateNonce()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
h.pkce, err = h.generatePKCE()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-04 23:33:53 +00:00
|
|
|
// Do the basic login to get an access and ID token issued to our main client ID.
|
|
|
|
baseToken, err := h.baseLogin()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is no requested audience, or the requested audience matches the one we got, we're done.
|
|
|
|
if h.requestedAudience == "" || (baseToken.IDToken != nil && h.requestedAudience == baseToken.IDToken.Claims["aud"]) {
|
|
|
|
return baseToken, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform the RFC8693 token exchange.
|
|
|
|
exchangedToken, err := h.tokenExchangeRFC8693(baseToken)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to exchange token: %w", err)
|
|
|
|
}
|
|
|
|
return exchangedToken, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handlerState) baseLogin() (*oidctypes.Token, error) {
|
2020-10-21 18:05:19 +00:00
|
|
|
// Check the cache for a previous session issued with the same parameters.
|
|
|
|
sort.Strings(h.scopes)
|
|
|
|
cacheKey := SessionCacheKey{
|
|
|
|
Issuer: h.issuer,
|
|
|
|
ClientID: h.clientID,
|
|
|
|
Scopes: h.scopes,
|
|
|
|
RedirectURI: (&url.URL{Scheme: "http", Host: h.listenAddr, Path: h.callbackPath}).String(),
|
2023-05-08 21:07:38 +00:00
|
|
|
// When using a Supervisor with multiple IDPs, the cache keys need to be different for each IDP
|
|
|
|
// so a user can have multiple sessions going for each IDP at the same time.
|
|
|
|
// When using a non-Supervisor OIDC provider, then this value will be blank, so it won't be part of the key.
|
|
|
|
UpstreamProviderName: h.upstreamIdentityProviderName,
|
2020-10-21 18:05:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the ID token is still valid for a bit, return it immediately and skip the rest of the flow.
|
2020-10-22 21:12:02 +00:00
|
|
|
cached := h.cache.GetToken(cacheKey)
|
|
|
|
if cached != nil && cached.IDToken != nil && time.Until(cached.IDToken.Expiry.Time) > minIDTokenValidity {
|
2022-04-16 02:43:53 +00:00
|
|
|
h.logger.V(plog.KlogLevelDebug).Info("Pinniped: Found unexpired cached token.")
|
2020-10-21 18:05:19 +00:00
|
|
|
return cached, nil
|
|
|
|
}
|
|
|
|
|
2020-10-06 22:27:36 +00:00
|
|
|
// Perform OIDC discovery.
|
2020-12-04 23:33:53 +00:00
|
|
|
if err := h.initOIDCDiscovery(); err != nil {
|
|
|
|
return nil, err
|
2020-10-06 22:27:36 +00:00
|
|
|
}
|
|
|
|
|
2020-10-22 21:12:02 +00:00
|
|
|
// If there was a cached refresh token, attempt to use the refresh flow instead of a fresh login.
|
|
|
|
if cached != nil && cached.RefreshToken != nil && cached.RefreshToken.Token != "" {
|
2020-12-04 23:33:53 +00:00
|
|
|
freshToken, err := h.handleRefresh(h.ctx, cached.RefreshToken)
|
2020-10-22 21:12:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// If we got a fresh token, we can update the cache and return it. Otherwise we fall through to the full refresh flow.
|
|
|
|
if freshToken != nil {
|
|
|
|
h.cache.PutToken(cacheKey, freshToken)
|
|
|
|
return freshToken, nil
|
|
|
|
}
|
2020-10-06 22:27:36 +00:00
|
|
|
}
|
2020-10-22 21:12:02 +00:00
|
|
|
|
2021-04-17 01:30:31 +00:00
|
|
|
// Prepare the common options for the authorization URL. We don't have the redirect URL yet though.
|
|
|
|
authorizeOptions := []oauth2.AuthCodeOption{
|
|
|
|
oauth2.AccessTypeOffline,
|
|
|
|
h.nonce.Param(),
|
|
|
|
h.pkce.Challenge(),
|
|
|
|
h.pkce.Method(),
|
|
|
|
}
|
|
|
|
if h.upstreamIdentityProviderName != "" {
|
2021-08-18 17:20:33 +00:00
|
|
|
authorizeOptions = append(authorizeOptions,
|
Create username scope, required for clients to get username in ID token
- For backwards compatibility with older Pinniped CLIs, the pinniped-cli
client does not need to request the username or groups scopes for them
to be granted. For dynamic clients, the usual OAuth2 rules apply:
the client must be allowed to request the scopes according to its
configuration, and the client must actually request the scopes in the
authorization request.
- If the username scope was not granted, then there will be no username
in the ID token, and the cluster-scoped token exchange will fail since
there would be no username in the resulting cluster-scoped ID token.
- The OIDC well-known discovery endpoint lists the username and groups
scopes in the scopes_supported list, and lists the username and groups
claims in the claims_supported list.
- Add username and groups scopes to the default list of scopes
put into kubeconfig files by "pinniped get kubeconfig" CLI command,
and the default list of scopes used by "pinniped login oidc" when
no list of scopes is specified in the kubeconfig file
- The warning header about group memberships changing during upstream
refresh will only be sent to the pinniped-cli client, since it is
only intended for kubectl and it could leak the username to the
client (which may not have the username scope granted) through the
warning message text.
- Add the user's username to the session storage as a new field, so that
during upstream refresh we can compare the original username from the
initial authorization to the refreshed username, even in the case when
the username scope was not granted (and therefore the username is not
stored in the ID token claims of the session storage)
- Bump the Supervisor session storage format version from 2 to 3
due to the username field being added to the session struct
- Extract commonly used string constants related to OIDC flows to api
package.
- Change some import names to make them consistent:
- Always import github.com/coreos/go-oidc/v3/oidc as "coreosoidc"
- Always import go.pinniped.dev/generated/latest/apis/supervisor/oidc
as "oidcapi"
- Always import go.pinniped.dev/internal/oidc as "oidc"
2022-08-08 23:29:22 +00:00
|
|
|
oauth2.SetAuthURLParam(oidcapi.AuthorizeUpstreamIDPNameParamName, h.upstreamIdentityProviderName),
|
2021-08-18 17:20:33 +00:00
|
|
|
)
|
|
|
|
authorizeOptions = append(authorizeOptions,
|
Create username scope, required for clients to get username in ID token
- For backwards compatibility with older Pinniped CLIs, the pinniped-cli
client does not need to request the username or groups scopes for them
to be granted. For dynamic clients, the usual OAuth2 rules apply:
the client must be allowed to request the scopes according to its
configuration, and the client must actually request the scopes in the
authorization request.
- If the username scope was not granted, then there will be no username
in the ID token, and the cluster-scoped token exchange will fail since
there would be no username in the resulting cluster-scoped ID token.
- The OIDC well-known discovery endpoint lists the username and groups
scopes in the scopes_supported list, and lists the username and groups
claims in the claims_supported list.
- Add username and groups scopes to the default list of scopes
put into kubeconfig files by "pinniped get kubeconfig" CLI command,
and the default list of scopes used by "pinniped login oidc" when
no list of scopes is specified in the kubeconfig file
- The warning header about group memberships changing during upstream
refresh will only be sent to the pinniped-cli client, since it is
only intended for kubectl and it could leak the username to the
client (which may not have the username scope granted) through the
warning message text.
- Add the user's username to the session storage as a new field, so that
during upstream refresh we can compare the original username from the
initial authorization to the refreshed username, even in the case when
the username scope was not granted (and therefore the username is not
stored in the ID token claims of the session storage)
- Bump the Supervisor session storage format version from 2 to 3
due to the username field being added to the session struct
- Extract commonly used string constants related to OIDC flows to api
package.
- Change some import names to make them consistent:
- Always import github.com/coreos/go-oidc/v3/oidc as "coreosoidc"
- Always import go.pinniped.dev/generated/latest/apis/supervisor/oidc
as "oidcapi"
- Always import go.pinniped.dev/internal/oidc as "oidc"
2022-08-08 23:29:22 +00:00
|
|
|
oauth2.SetAuthURLParam(oidcapi.AuthorizeUpstreamIDPTypeParamName, h.upstreamIdentityProviderType),
|
2021-08-18 17:20:33 +00:00
|
|
|
)
|
2021-04-17 01:30:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Choose the appropriate authorization and authcode exchange strategy.
|
|
|
|
var authFunc = h.webBrowserBasedAuth
|
2021-05-13 17:05:56 +00:00
|
|
|
if h.cliToSendCredentials {
|
2021-04-17 01:30:31 +00:00
|
|
|
authFunc = h.cliBasedAuth
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform the authorize request and authcode exchange to get back OIDC tokens.
|
|
|
|
token, err := authFunc(&authorizeOptions)
|
|
|
|
|
|
|
|
// If we got tokens, put them in the cache.
|
|
|
|
if err == nil {
|
|
|
|
h.cache.PutToken(cacheKey, token)
|
|
|
|
}
|
|
|
|
|
|
|
|
return token, err
|
|
|
|
}
|
|
|
|
|
2021-05-13 17:05:56 +00:00
|
|
|
// Make a direct call to the authorize endpoint, including the user's username and password on custom http headers,
|
|
|
|
// and parse the authcode from the response. Exchange the authcode for tokens. Return the tokens or an error.
|
2021-04-17 01:30:31 +00:00
|
|
|
func (h *handlerState) cliBasedAuth(authorizeOptions *[]oauth2.AuthCodeOption) (*oidctypes.Token, error) {
|
2021-07-19 23:20:59 +00:00
|
|
|
// Ask the user for their username and password, or get them from env vars.
|
|
|
|
username, password, err := h.getUsernameAndPassword()
|
2021-04-20 00:59:46 +00:00
|
|
|
if err != nil {
|
2021-07-19 23:20:59 +00:00
|
|
|
return nil, err
|
2021-04-20 00:59:46 +00:00
|
|
|
}
|
|
|
|
|
2021-04-17 01:30:31 +00:00
|
|
|
// Make a callback URL even though we won't be listening on this port, because providing a redirect URL is
|
|
|
|
// required for OIDC authorize endpoints, and it must match the allowed redirect URL of the OIDC client
|
2021-04-20 00:59:46 +00:00
|
|
|
// registered on the server. The Supervisor oauth client does not have "localhost" in the allowed redirect
|
|
|
|
// URI list, so use 127.0.0.1.
|
|
|
|
localhostAddr := strings.ReplaceAll(h.listenAddr, "localhost", "127.0.0.1")
|
2021-04-17 01:30:31 +00:00
|
|
|
h.oauth2Config.RedirectURL = (&url.URL{
|
|
|
|
Scheme: "http",
|
2021-04-20 00:59:46 +00:00
|
|
|
Host: localhostAddr,
|
2021-04-17 01:30:31 +00:00
|
|
|
Path: h.callbackPath,
|
|
|
|
}).String()
|
|
|
|
|
|
|
|
// Now that we have a redirect URL, we can build the authorize URL.
|
2021-04-20 00:59:46 +00:00
|
|
|
authorizeURL := h.oauth2Config.AuthCodeURL(h.state.String(), *authorizeOptions...)
|
|
|
|
|
|
|
|
// Don't follow redirects automatically because we want to handle redirects here.
|
2021-12-14 20:55:35 +00:00
|
|
|
var sawRedirect bool
|
2021-04-20 00:59:46 +00:00
|
|
|
h.httpClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
|
2021-12-14 20:55:35 +00:00
|
|
|
sawRedirect = true
|
2021-04-20 00:59:46 +00:00
|
|
|
return http.ErrUseLastResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send an authorize request.
|
2021-05-12 19:57:10 +00:00
|
|
|
authCtx, authorizeCtxCancelFunc := context.WithTimeout(h.ctx, httpRequestTimeout)
|
2021-04-20 00:59:46 +00:00
|
|
|
defer authorizeCtxCancelFunc()
|
|
|
|
authReq, err := http.NewRequestWithContext(authCtx, http.MethodGet, authorizeURL, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not build authorize request: %w", err)
|
|
|
|
}
|
Create username scope, required for clients to get username in ID token
- For backwards compatibility with older Pinniped CLIs, the pinniped-cli
client does not need to request the username or groups scopes for them
to be granted. For dynamic clients, the usual OAuth2 rules apply:
the client must be allowed to request the scopes according to its
configuration, and the client must actually request the scopes in the
authorization request.
- If the username scope was not granted, then there will be no username
in the ID token, and the cluster-scoped token exchange will fail since
there would be no username in the resulting cluster-scoped ID token.
- The OIDC well-known discovery endpoint lists the username and groups
scopes in the scopes_supported list, and lists the username and groups
claims in the claims_supported list.
- Add username and groups scopes to the default list of scopes
put into kubeconfig files by "pinniped get kubeconfig" CLI command,
and the default list of scopes used by "pinniped login oidc" when
no list of scopes is specified in the kubeconfig file
- The warning header about group memberships changing during upstream
refresh will only be sent to the pinniped-cli client, since it is
only intended for kubectl and it could leak the username to the
client (which may not have the username scope granted) through the
warning message text.
- Add the user's username to the session storage as a new field, so that
during upstream refresh we can compare the original username from the
initial authorization to the refreshed username, even in the case when
the username scope was not granted (and therefore the username is not
stored in the ID token claims of the session storage)
- Bump the Supervisor session storage format version from 2 to 3
due to the username field being added to the session struct
- Extract commonly used string constants related to OIDC flows to api
package.
- Change some import names to make them consistent:
- Always import github.com/coreos/go-oidc/v3/oidc as "coreosoidc"
- Always import go.pinniped.dev/generated/latest/apis/supervisor/oidc
as "oidcapi"
- Always import go.pinniped.dev/internal/oidc as "oidc"
2022-08-08 23:29:22 +00:00
|
|
|
authReq.Header.Set(oidcapi.AuthorizeUsernameHeaderName, username)
|
|
|
|
authReq.Header.Set(oidcapi.AuthorizePasswordHeaderName, password)
|
2021-04-20 00:59:46 +00:00
|
|
|
authRes, err := h.httpClient.Do(authReq)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("authorization response error: %w", err)
|
|
|
|
}
|
2021-04-27 19:43:09 +00:00
|
|
|
_ = authRes.Body.Close() // don't need the response body, and okay if it fails to close
|
2021-04-20 00:59:46 +00:00
|
|
|
|
2021-12-14 20:55:35 +00:00
|
|
|
// A successful authorization always results in a redirect (we are flexible on the exact status code).
|
|
|
|
if !sawRedirect {
|
2021-04-20 00:59:46 +00:00
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"error getting authorization: expected to be redirected, but response status was %s", authRes.Status)
|
|
|
|
}
|
|
|
|
rawLocation := authRes.Header.Get(httpLocationHeaderName)
|
|
|
|
location, err := url.Parse(rawLocation)
|
|
|
|
if err != nil {
|
|
|
|
// This shouldn't be possible in practice because httpClient.Do() already parses the Location header.
|
|
|
|
return nil, fmt.Errorf("error getting authorization: could not parse redirect location: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the redirect was to the expected location.
|
|
|
|
if location.Scheme != "http" || location.Host != localhostAddr || location.Path != h.callbackPath {
|
|
|
|
return nil, fmt.Errorf("error getting authorization: redirected to the wrong location: %s", rawLocation)
|
|
|
|
}
|
|
|
|
|
2021-04-20 01:08:52 +00:00
|
|
|
// Validate OAuth2 state and fail if it's incorrect (to block CSRF).
|
|
|
|
if err := h.state.Validate(location.Query().Get("state")); err != nil {
|
|
|
|
return nil, fmt.Errorf("missing or invalid state parameter in authorization response: %s", rawLocation)
|
|
|
|
}
|
|
|
|
|
2021-04-20 00:59:46 +00:00
|
|
|
// Get the auth code or return the error from the server.
|
|
|
|
authCode := location.Query().Get("code")
|
|
|
|
if authCode == "" {
|
2021-04-20 01:08:52 +00:00
|
|
|
// Check for error response parameters. See https://openid.net/specs/openid-connect-core-1_0.html#AuthError.
|
2021-04-20 00:59:46 +00:00
|
|
|
requiredErrorCode := location.Query().Get("error")
|
|
|
|
optionalErrorDescription := location.Query().Get("error_description")
|
|
|
|
if optionalErrorDescription == "" {
|
|
|
|
return nil, fmt.Errorf("login failed with code %q", requiredErrorCode)
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("login failed with code %q: %s", requiredErrorCode, optionalErrorDescription)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exchange the authorization code for access, ID, and refresh tokens and perform required
|
|
|
|
// validations on the returned ID token.
|
2021-05-12 19:57:10 +00:00
|
|
|
tokenCtx, tokenCtxCancelFunc := context.WithTimeout(h.ctx, httpRequestTimeout)
|
2021-04-20 00:59:46 +00:00
|
|
|
defer tokenCtxCancelFunc()
|
2023-09-25 22:41:16 +00:00
|
|
|
token, err := h.redeemAuthCode(tokenCtx, authCode)
|
2021-04-20 00:59:46 +00:00
|
|
|
if err != nil {
|
2023-09-25 22:41:16 +00:00
|
|
|
return nil, fmt.Errorf("could not complete authorization code exchange: %w", err)
|
2021-04-20 00:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return token, nil
|
2021-04-17 01:30:31 +00:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:20:59 +00:00
|
|
|
// Prompt for the user's username and password, or read them from env vars if they are available.
|
|
|
|
func (h *handlerState) getUsernameAndPassword() (string, string, error) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
username := h.getEnv(defaultUsernameEnvVarName)
|
|
|
|
if username == "" {
|
|
|
|
username, err = h.promptForValue(h.ctx, defaultLDAPUsernamePrompt)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", fmt.Errorf("error prompting for username: %w", err)
|
|
|
|
}
|
|
|
|
} else {
|
2022-04-16 02:43:53 +00:00
|
|
|
h.logger.V(plog.KlogLevelDebug).Info("Pinniped: Read username from environment variable", "name", defaultUsernameEnvVarName)
|
2021-07-19 23:20:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
password := h.getEnv(defaultPasswordEnvVarName)
|
|
|
|
if password == "" {
|
2021-07-29 22:49:16 +00:00
|
|
|
password, err = h.promptForSecret(defaultLDAPPasswordPrompt)
|
2021-07-19 23:20:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", fmt.Errorf("error prompting for password: %w", err)
|
|
|
|
}
|
|
|
|
} else {
|
2022-04-16 02:43:53 +00:00
|
|
|
h.logger.V(plog.KlogLevelDebug).Info("Pinniped: Read password from environment variable", "name", defaultPasswordEnvVarName)
|
2021-07-19 23:20:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return username, password, nil
|
|
|
|
}
|
|
|
|
|
2021-04-17 01:30:31 +00:00
|
|
|
// Open a web browser, or ask the user to open a web browser, to visit the authorize endpoint.
|
|
|
|
// Create a localhost callback listener which exchanges the authcode for tokens. Return the tokens or an error.
|
|
|
|
func (h *handlerState) webBrowserBasedAuth(authorizeOptions *[]oauth2.AuthCodeOption) (*oidctypes.Token, error) {
|
2021-07-08 19:32:44 +00:00
|
|
|
// Attempt to open a local TCP listener, logging but otherwise ignoring any error.
|
|
|
|
listener, err := h.listen("tcp", h.listenAddr)
|
2020-10-22 21:12:02 +00:00
|
|
|
if err != nil {
|
2022-04-16 02:43:53 +00:00
|
|
|
h.logger.V(plog.KlogLevelDebug).Error(err, "could not open callback listener")
|
2021-07-08 19:32:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the listener failed to start and stdin is not a TTY, then we have no hope of succeeding,
|
|
|
|
// since we won't be able to receive the web callback and we can't prompt for the manual auth code.
|
2021-07-27 21:10:05 +00:00
|
|
|
if listener == nil && !h.isTTY(stdin()) {
|
2021-07-08 19:32:44 +00:00
|
|
|
return nil, fmt.Errorf("login failed: must have either a localhost listener or stdin must be a TTY")
|
2020-10-22 21:12:02 +00:00
|
|
|
}
|
2021-07-08 19:32:44 +00:00
|
|
|
|
|
|
|
// Update the OAuth2 redirect_uri to match the actual listener address (if there is one), or just use
|
|
|
|
// a fake ":0" port if there is no listener running.
|
|
|
|
redirectURI := url.URL{Scheme: "http", Path: h.callbackPath}
|
|
|
|
if listener == nil {
|
|
|
|
redirectURI.Host = "127.0.0.1:0"
|
|
|
|
} else {
|
|
|
|
redirectURI.Host = listener.Addr().String()
|
|
|
|
}
|
|
|
|
h.oauth2Config.RedirectURL = redirectURI.String()
|
2020-10-22 21:12:02 +00:00
|
|
|
|
2021-06-21 19:19:12 +00:00
|
|
|
// If the server supports it, request response_mode=form_post.
|
|
|
|
authParams := *authorizeOptions
|
|
|
|
if h.useFormPost {
|
|
|
|
authParams = append(authParams, oauth2.SetAuthURLParam("response_mode", "form_post"))
|
|
|
|
}
|
|
|
|
|
2021-04-17 01:30:31 +00:00
|
|
|
// Now that we have a redirect URL with the listener port, we can build the authorize URL.
|
2021-06-21 19:19:12 +00:00
|
|
|
authorizeURL := h.oauth2Config.AuthCodeURL(h.state.String(), authParams...)
|
2021-04-17 01:30:31 +00:00
|
|
|
|
2021-07-08 19:32:44 +00:00
|
|
|
// If there is a listener running, start serving the callback handler in a background goroutine.
|
|
|
|
if listener != nil {
|
|
|
|
shutdown := h.serve(listener)
|
|
|
|
defer shutdown()
|
|
|
|
}
|
2020-10-06 22:27:36 +00:00
|
|
|
|
2021-07-08 19:32:44 +00:00
|
|
|
// Open the authorize URL in the users browser, logging but otherwise ignoring any error.
|
2020-10-06 22:27:36 +00:00
|
|
|
if err := h.openURL(authorizeURL); err != nil {
|
2022-04-16 02:43:53 +00:00
|
|
|
h.logger.V(plog.KlogLevelDebug).Error(err, "could not open browser")
|
2020-10-06 22:27:36 +00:00
|
|
|
}
|
|
|
|
|
2021-07-08 19:32:44 +00:00
|
|
|
// Prompt the user to visit the authorize URL, and to paste a manually-copied auth code (if possible).
|
2021-07-29 22:49:16 +00:00
|
|
|
ctx, cancel := context.WithCancel(h.ctx)
|
|
|
|
cleanupPrompt := h.promptForWebLogin(ctx, authorizeURL, os.Stderr)
|
|
|
|
defer func() {
|
|
|
|
cancel()
|
|
|
|
cleanupPrompt()
|
|
|
|
}()
|
2021-07-08 19:32:44 +00:00
|
|
|
|
|
|
|
// Wait for either the web callback, a pasted auth code, or a timeout.
|
2020-10-06 22:27:36 +00:00
|
|
|
select {
|
|
|
|
case <-h.ctx.Done():
|
|
|
|
return nil, fmt.Errorf("timed out waiting for token callback: %w", h.ctx.Err())
|
|
|
|
case callback := <-h.callbacks:
|
|
|
|
if callback.err != nil {
|
|
|
|
return nil, fmt.Errorf("error handling callback: %w", callback.err)
|
|
|
|
}
|
|
|
|
return callback.token, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 22:49:16 +00:00
|
|
|
func (h *handlerState) promptForWebLogin(ctx context.Context, authorizeURL string, out io.Writer) func() {
|
2021-07-08 19:32:44 +00:00
|
|
|
_, _ = fmt.Fprintf(out, "Log in by visiting this link:\n\n %s\n\n", authorizeURL)
|
|
|
|
|
|
|
|
// If stdin is not a TTY, print the URL but don't prompt for the manual paste,
|
|
|
|
// since we have no way of reading it.
|
2021-07-27 21:10:05 +00:00
|
|
|
if !h.isTTY(stdin()) {
|
2021-07-29 22:49:16 +00:00
|
|
|
return func() {}
|
2021-07-08 19:32:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the server didn't support response_mode=form_post, don't bother prompting for the manual
|
|
|
|
// code because the user isn't going to have any easy way to manually copy it anyway.
|
|
|
|
if !h.useFormPost {
|
2021-07-29 22:49:16 +00:00
|
|
|
return func() {}
|
2021-07-08 19:32:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Launch the manual auth code prompt in a background goroutine, which will be cancelled
|
|
|
|
// if the parent context is cancelled (when the login succeeds or times out).
|
2021-07-29 22:49:16 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
2021-07-08 19:32:44 +00:00
|
|
|
go func() {
|
2021-07-29 22:49:16 +00:00
|
|
|
defer func() {
|
|
|
|
// Always emit a newline so the kubectl output is visually separated from the login prompts.
|
|
|
|
_, _ = fmt.Fprintln(os.Stderr)
|
|
|
|
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
code, err := h.promptForValue(ctx, " Optionally, paste your authorization code: ")
|
2021-07-08 19:32:44 +00:00
|
|
|
if err != nil {
|
2021-07-29 22:49:16 +00:00
|
|
|
// Print a visual marker to show the the prompt is no longer waiting for user input, plus a trailing
|
|
|
|
// newline that simulates the user having pressed "enter".
|
|
|
|
_, _ = fmt.Fprint(os.Stderr, "[...]\n")
|
|
|
|
|
2021-07-08 19:32:44 +00:00
|
|
|
h.callbacks <- callbackResult{err: fmt.Errorf("failed to prompt for manual authorization code: %v", err)}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-25 22:41:16 +00:00
|
|
|
// When a code is pasted, redeem it for a token and return the results on the callback channel.
|
2021-07-08 19:32:44 +00:00
|
|
|
token, err := h.redeemAuthCode(ctx, code)
|
|
|
|
h.callbacks <- callbackResult{token: token, err: err}
|
|
|
|
}()
|
2021-07-29 22:49:16 +00:00
|
|
|
return wg.Wait
|
2021-07-08 19:32:44 +00:00
|
|
|
}
|
|
|
|
|
2021-06-30 20:06:37 +00:00
|
|
|
func promptForValue(ctx context.Context, promptLabel string) (string, error) {
|
2021-07-27 21:10:05 +00:00
|
|
|
if !term.IsTerminal(stdin()) {
|
2021-04-20 00:59:46 +00:00
|
|
|
return "", errors.New("stdin is not connected to a terminal")
|
|
|
|
}
|
|
|
|
_, err := fmt.Fprint(os.Stderr, promptLabel)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("could not print prompt to stderr: %w", err)
|
|
|
|
}
|
2021-06-30 20:06:37 +00:00
|
|
|
|
2021-07-29 22:49:16 +00:00
|
|
|
type readResult struct {
|
|
|
|
text string
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
readResults := make(chan readResult)
|
2021-06-30 20:06:37 +00:00
|
|
|
go func() {
|
2021-07-29 22:49:16 +00:00
|
|
|
text, err := bufio.NewReader(os.Stdin).ReadString('\n')
|
|
|
|
readResults <- readResult{text, err}
|
|
|
|
close(readResults)
|
2021-06-30 20:06:37 +00:00
|
|
|
}()
|
|
|
|
|
2021-07-29 22:49:16 +00:00
|
|
|
// If the context is canceled, return immediately. The ReadString() operation will stay hung in the background
|
|
|
|
// goroutine indefinitely.
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return "", ctx.Err()
|
|
|
|
case r := <-readResults:
|
|
|
|
return strings.TrimSpace(r.text), r.err
|
2021-04-27 19:43:09 +00:00
|
|
|
}
|
2021-04-20 00:59:46 +00:00
|
|
|
}
|
|
|
|
|
2021-07-29 22:49:16 +00:00
|
|
|
func promptForSecret(promptLabel string) (string, error) {
|
2021-07-27 21:10:05 +00:00
|
|
|
if !term.IsTerminal(stdin()) {
|
2021-04-20 00:59:46 +00:00
|
|
|
return "", errors.New("stdin is not connected to a terminal")
|
|
|
|
}
|
|
|
|
_, err := fmt.Fprint(os.Stderr, promptLabel)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("could not print prompt to stderr: %w", err)
|
|
|
|
}
|
2021-07-27 21:10:05 +00:00
|
|
|
password, err := term.ReadPassword(stdin())
|
2021-04-20 00:59:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("could not read password: %w", err)
|
|
|
|
}
|
2021-07-29 22:49:16 +00:00
|
|
|
// term.ReadPassword swallows the newline that was typed by the user, so to
|
|
|
|
// avoid the next line of output from happening on same line as the password
|
|
|
|
// prompt, we need to print a newline.
|
|
|
|
_, err = fmt.Fprint(os.Stderr, "\n")
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("could not print newline to stderr: %w", err)
|
|
|
|
}
|
2021-04-20 00:59:46 +00:00
|
|
|
return string(password), err
|
|
|
|
}
|
|
|
|
|
2020-12-04 23:33:53 +00:00
|
|
|
func (h *handlerState) initOIDCDiscovery() error {
|
|
|
|
// Make this method idempotent so it can be called in multiple cases with no extra network requests.
|
|
|
|
if h.provider != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-16 18:41:51 +00:00
|
|
|
// Validate that the issuer URL uses https, or else we cannot trust its discovery endpoint to get the other URLs.
|
|
|
|
if err := validateURLUsesHTTPS(h.issuer, "issuer"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-16 02:43:53 +00:00
|
|
|
h.logger.V(plog.KlogLevelDebug).Info("Pinniped: Performing OIDC discovery", "issuer", h.issuer)
|
2020-12-04 23:33:53 +00:00
|
|
|
var err error
|
Create username scope, required for clients to get username in ID token
- For backwards compatibility with older Pinniped CLIs, the pinniped-cli
client does not need to request the username or groups scopes for them
to be granted. For dynamic clients, the usual OAuth2 rules apply:
the client must be allowed to request the scopes according to its
configuration, and the client must actually request the scopes in the
authorization request.
- If the username scope was not granted, then there will be no username
in the ID token, and the cluster-scoped token exchange will fail since
there would be no username in the resulting cluster-scoped ID token.
- The OIDC well-known discovery endpoint lists the username and groups
scopes in the scopes_supported list, and lists the username and groups
claims in the claims_supported list.
- Add username and groups scopes to the default list of scopes
put into kubeconfig files by "pinniped get kubeconfig" CLI command,
and the default list of scopes used by "pinniped login oidc" when
no list of scopes is specified in the kubeconfig file
- The warning header about group memberships changing during upstream
refresh will only be sent to the pinniped-cli client, since it is
only intended for kubectl and it could leak the username to the
client (which may not have the username scope granted) through the
warning message text.
- Add the user's username to the session storage as a new field, so that
during upstream refresh we can compare the original username from the
initial authorization to the refreshed username, even in the case when
the username scope was not granted (and therefore the username is not
stored in the ID token claims of the session storage)
- Bump the Supervisor session storage format version from 2 to 3
due to the username field being added to the session struct
- Extract commonly used string constants related to OIDC flows to api
package.
- Change some import names to make them consistent:
- Always import github.com/coreos/go-oidc/v3/oidc as "coreosoidc"
- Always import go.pinniped.dev/generated/latest/apis/supervisor/oidc
as "oidcapi"
- Always import go.pinniped.dev/internal/oidc as "oidc"
2022-08-08 23:29:22 +00:00
|
|
|
h.provider, err = coreosoidc.NewProvider(h.ctx, h.issuer)
|
2020-12-04 23:33:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not perform OIDC discovery for %q: %w", h.issuer, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build an OAuth2 configuration based on the OIDC discovery data and our callback endpoint.
|
|
|
|
h.oauth2Config = &oauth2.Config{
|
|
|
|
ClientID: h.clientID,
|
|
|
|
Endpoint: h.provider.Endpoint(),
|
|
|
|
Scopes: h.scopes,
|
|
|
|
}
|
2021-06-21 19:19:12 +00:00
|
|
|
|
2022-02-16 18:41:51 +00:00
|
|
|
// Validate that the discovered auth and token URLs use https. The OIDC spec for the authcode flow says:
|
|
|
|
// "Communication with the Authorization Endpoint MUST utilize TLS"
|
|
|
|
// (see https://openid.net/specs/openid-connect-core-1_0.html#AuthorizationEndpoint), and
|
|
|
|
// "Communication with the Token Endpoint MUST utilize TLS"
|
|
|
|
// (see https://openid.net/specs/openid-connect-core-1_0.html#TokenEndpoint).
|
|
|
|
if err := validateURLUsesHTTPS(h.provider.Endpoint().AuthURL, "discovered authorize URL from issuer"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := validateURLUsesHTTPS(h.provider.Endpoint().TokenURL, "discovered token URL from issuer"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-21 19:19:12 +00:00
|
|
|
// Use response_mode=form_post if the provider supports it.
|
|
|
|
var discoveryClaims struct {
|
|
|
|
ResponseModesSupported []string `json:"response_modes_supported"`
|
|
|
|
}
|
|
|
|
if err := h.provider.Claims(&discoveryClaims); err != nil {
|
|
|
|
return fmt.Errorf("could not decode response_modes_supported in OIDC discovery from %q: %w", h.issuer, err)
|
|
|
|
}
|
2023-03-29 20:55:00 +00:00
|
|
|
h.useFormPost = slices.Contains(discoveryClaims.ResponseModesSupported, "form_post")
|
2020-12-04 23:33:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-16 18:41:51 +00:00
|
|
|
func validateURLUsesHTTPS(uri string, uriName string) error {
|
|
|
|
parsed, err := url.Parse(uri)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s is not a valid URL: %w", uriName, err)
|
|
|
|
}
|
|
|
|
if parsed.Scheme != "https" {
|
|
|
|
return fmt.Errorf("%s must be an https URL, but had scheme %q instead", uriName, parsed.Scheme)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-04 23:33:53 +00:00
|
|
|
func (h *handlerState) tokenExchangeRFC8693(baseToken *oidctypes.Token) (*oidctypes.Token, error) {
|
2022-04-16 02:43:53 +00:00
|
|
|
h.logger.V(plog.KlogLevelDebug).Info("Pinniped: Performing RFC8693 token exchange", "requestedAudience", h.requestedAudience)
|
2020-12-04 23:33:53 +00:00
|
|
|
// Perform OIDC discovery. This may have already been performed if there was not a cached base token.
|
|
|
|
if err := h.initOIDCDiscovery(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Form the HTTP POST request with the parameters specified by RFC8693.
|
|
|
|
reqBody := strings.NewReader(url.Values{
|
2020-12-09 16:08:41 +00:00
|
|
|
"client_id": []string{h.clientID},
|
Create username scope, required for clients to get username in ID token
- For backwards compatibility with older Pinniped CLIs, the pinniped-cli
client does not need to request the username or groups scopes for them
to be granted. For dynamic clients, the usual OAuth2 rules apply:
the client must be allowed to request the scopes according to its
configuration, and the client must actually request the scopes in the
authorization request.
- If the username scope was not granted, then there will be no username
in the ID token, and the cluster-scoped token exchange will fail since
there would be no username in the resulting cluster-scoped ID token.
- The OIDC well-known discovery endpoint lists the username and groups
scopes in the scopes_supported list, and lists the username and groups
claims in the claims_supported list.
- Add username and groups scopes to the default list of scopes
put into kubeconfig files by "pinniped get kubeconfig" CLI command,
and the default list of scopes used by "pinniped login oidc" when
no list of scopes is specified in the kubeconfig file
- The warning header about group memberships changing during upstream
refresh will only be sent to the pinniped-cli client, since it is
only intended for kubectl and it could leak the username to the
client (which may not have the username scope granted) through the
warning message text.
- Add the user's username to the session storage as a new field, so that
during upstream refresh we can compare the original username from the
initial authorization to the refreshed username, even in the case when
the username scope was not granted (and therefore the username is not
stored in the ID token claims of the session storage)
- Bump the Supervisor session storage format version from 2 to 3
due to the username field being added to the session struct
- Extract commonly used string constants related to OIDC flows to api
package.
- Change some import names to make them consistent:
- Always import github.com/coreos/go-oidc/v3/oidc as "coreosoidc"
- Always import go.pinniped.dev/generated/latest/apis/supervisor/oidc
as "oidcapi"
- Always import go.pinniped.dev/internal/oidc as "oidc"
2022-08-08 23:29:22 +00:00
|
|
|
"grant_type": []string{oidcapi.GrantTypeTokenExchange},
|
2020-12-04 23:33:53 +00:00
|
|
|
"audience": []string{h.requestedAudience},
|
|
|
|
"subject_token": []string{baseToken.AccessToken.Token},
|
|
|
|
"subject_token_type": []string{"urn:ietf:params:oauth:token-type:access_token"},
|
|
|
|
"requested_token_type": []string{"urn:ietf:params:oauth:token-type:jwt"},
|
|
|
|
}.Encode())
|
|
|
|
req, err := http.NewRequestWithContext(h.ctx, http.MethodPost, h.oauth2Config.Endpoint.TokenURL, reqBody)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not build RFC8693 request: %w", err)
|
|
|
|
}
|
|
|
|
req.Header.Set("content-type", "application/x-www-form-urlencoded")
|
|
|
|
|
|
|
|
// Perform the request.
|
2020-12-09 16:08:41 +00:00
|
|
|
resp, err := h.httpClient.Do(req)
|
2020-12-04 23:33:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
|
|
|
|
// Expect an HTTP 200 response with "application/json" content type.
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, fmt.Errorf("unexpected HTTP response status %d", resp.StatusCode)
|
|
|
|
}
|
2020-12-10 16:09:42 +00:00
|
|
|
mediaType, _, err := mime.ParseMediaType(resp.Header.Get("content-type"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to decode content-type header: %w", err)
|
|
|
|
}
|
|
|
|
if mediaType != "application/json" {
|
|
|
|
return nil, fmt.Errorf("unexpected HTTP response content type %q", mediaType)
|
2020-12-04 23:33:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Decode the JSON response body.
|
|
|
|
var respBody struct {
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
IssuedTokenType string `json:"issued_token_type"`
|
|
|
|
TokenType string `json:"token_type"`
|
|
|
|
}
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(&respBody); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to decode response: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expect the token_type and issued_token_type response parameters to have some known values.
|
|
|
|
if respBody.TokenType != "N_A" {
|
|
|
|
return nil, fmt.Errorf("got unexpected token_type %q", respBody.TokenType)
|
|
|
|
}
|
|
|
|
if respBody.IssuedTokenType != "urn:ietf:params:oauth:token-type:jwt" {
|
|
|
|
return nil, fmt.Errorf("got unexpected issued_token_type %q", respBody.IssuedTokenType)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the returned JWT to make sure we got the audience we wanted and extract the expiration time.
|
|
|
|
stsToken, err := h.validateIDToken(h.ctx, h.provider, h.requestedAudience, respBody.AccessToken)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("received invalid JWT: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &oidctypes.Token{IDToken: &oidctypes.IDToken{
|
|
|
|
Token: respBody.AccessToken,
|
|
|
|
Expiry: metav1.NewTime(stsToken.Expiry),
|
|
|
|
}}, nil
|
|
|
|
}
|
|
|
|
|
2020-11-30 23:02:03 +00:00
|
|
|
func (h *handlerState) handleRefresh(ctx context.Context, refreshToken *oidctypes.RefreshToken) (*oidctypes.Token, error) {
|
2022-04-16 02:43:53 +00:00
|
|
|
h.logger.V(plog.KlogLevelDebug).Info("Pinniped: Refreshing cached token.")
|
2021-10-13 19:31:20 +00:00
|
|
|
upstreamOIDCIdentityProvider := h.getProvider(h.oauth2Config, h.provider, h.httpClient)
|
2020-10-22 21:12:02 +00:00
|
|
|
|
2021-10-13 19:31:20 +00:00
|
|
|
refreshed, err := upstreamOIDCIdentityProvider.PerformRefresh(ctx, refreshToken.Token)
|
2020-10-22 21:12:02 +00:00
|
|
|
if err != nil {
|
|
|
|
// Ignore errors during refresh, but return nil which will trigger the full login flow.
|
2022-04-16 02:43:53 +00:00
|
|
|
h.logger.V(plog.KlogLevelDebug).Info("Pinniped: Refresh failed.", "error", err.Error())
|
2020-10-22 21:12:02 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// The spec is not 100% clear about whether an ID token from the refresh flow should include a nonce, and at least
|
|
|
|
// some providers do not include one, so we skip the nonce validation here (but not other validations).
|
2022-01-13 02:05:10 +00:00
|
|
|
return upstreamOIDCIdentityProvider.ValidateTokenAndMergeWithUserInfo(ctx, refreshed, "", true, false)
|
2020-10-22 21:12:02 +00:00
|
|
|
}
|
|
|
|
|
2023-09-25 22:41:16 +00:00
|
|
|
// handleAuthCodeCallback is used as an http handler, so it does not run in the CLI's main goroutine.
|
|
|
|
// Upon a callback redirect request from an identity provider, it uses a callback channel to communicate
|
|
|
|
// its results back to the main thread of the CLI. The result can contain either some tokens from the
|
|
|
|
// identity provider's token endpoint, or the result can contain an error. When the result is an error,
|
|
|
|
// the CLI's main goroutine is responsible for printing that error to the terminal. At the same time,
|
|
|
|
// this function serves a web response, and that web response is rendered in the user's browser. So the
|
|
|
|
// user has two places to look for error messages: in their browser and in the CLI's terminal. Ideally,
|
|
|
|
// these messages would be the same. Note that using httperr.Wrap will cause the details of the wrapped
|
|
|
|
// err to be printed by the CLI, but not printed in the browser due to the way that the httperr package
|
|
|
|
// works, so avoid using httperr.Wrap in this function.
|
|
|
|
func (h *handlerState) handleAuthCodeCallback(w http.ResponseWriter, r *http.Request) (returnedErr error) {
|
2020-10-06 22:27:36 +00:00
|
|
|
defer func() {
|
2023-09-25 22:41:16 +00:00
|
|
|
// If we returned an error, then also report it back over the channel to the main CLI goroutine.
|
|
|
|
// Because returnedErr is the named return value, inside this defer returnedErr will hold the value
|
|
|
|
// returned by any explicit return statement.
|
|
|
|
if returnedErr != nil {
|
|
|
|
h.callbacks <- callbackResult{err: returnedErr}
|
2020-10-06 22:27:36 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-06-21 19:40:08 +00:00
|
|
|
var params url.Values
|
2022-08-24 21:45:55 +00:00
|
|
|
if h.useFormPost { //nolint:nestif
|
2022-02-09 01:30:48 +00:00
|
|
|
// Return HTTP 405 for anything that's not a POST or an OPTIONS request.
|
|
|
|
if r.Method != http.MethodPost && r.Method != http.MethodOptions {
|
2022-04-16 02:43:53 +00:00
|
|
|
h.logger.V(plog.KlogLevelDebug).Info("Pinniped: Got unexpected request on callback listener", "method", r.Method)
|
2022-02-09 01:30:48 +00:00
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
return nil // keep listening for more requests
|
|
|
|
}
|
|
|
|
|
|
|
|
// For POST and OPTIONS requests, calculate the allowed origin for CORS.
|
2023-09-25 22:41:16 +00:00
|
|
|
issuerURL, err := url.Parse(h.issuer)
|
|
|
|
if err != nil {
|
|
|
|
// Avoid using httperr.Wrap because that would hide the details of err from the browser output.
|
|
|
|
return httperr.Newf(http.StatusInternalServerError, "invalid issuer url: %s", err.Error())
|
2022-02-09 01:30:48 +00:00
|
|
|
}
|
|
|
|
allowOrigin := issuerURL.Scheme + "://" + issuerURL.Host
|
|
|
|
|
2022-02-05 00:57:37 +00:00
|
|
|
if r.Method == http.MethodOptions {
|
|
|
|
// Google Chrome decided that it should do CORS preflight checks for this Javascript form submission POST request.
|
|
|
|
// See https://developer.chrome.com/blog/private-network-access-preflight/
|
|
|
|
origin := r.Header.Get("Origin")
|
|
|
|
if origin == "" {
|
|
|
|
// The CORS preflight request should have an origin.
|
2022-04-16 02:43:53 +00:00
|
|
|
h.logger.V(plog.KlogLevelDebug).Info("Pinniped: Got OPTIONS request without origin header")
|
2022-02-05 00:57:37 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return nil // keep listening for more requests
|
|
|
|
}
|
2022-04-16 02:43:53 +00:00
|
|
|
h.logger.V(plog.KlogLevelDebug).Info("Pinniped: Got CORS preflight request from browser", "origin", origin)
|
2022-02-05 00:57:37 +00:00
|
|
|
// To tell the browser that it is okay to make the real POST request, return the following response.
|
2022-02-09 01:30:48 +00:00
|
|
|
w.Header().Set("Access-Control-Allow-Origin", allowOrigin)
|
|
|
|
w.Header().Set("Vary", "*") // supposed to use Vary when Access-Control-Allow-Origin is a specific host
|
2022-02-05 00:57:37 +00:00
|
|
|
w.Header().Set("Access-Control-Allow-Credentials", "false")
|
|
|
|
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
|
|
|
|
w.Header().Set("Access-Control-Allow-Private-Network", "true")
|
|
|
|
// If the browser would like to send some headers on the real request, allow them. Chrome doesn't
|
|
|
|
// currently send this header at the moment. This is in case some browser in the future decides to
|
|
|
|
// request to be allowed to send specific headers by using Access-Control-Request-Headers.
|
|
|
|
requestedHeaders := r.Header.Get("Access-Control-Request-Headers")
|
|
|
|
if requestedHeaders != "" {
|
|
|
|
w.Header().Set("Access-Control-Allow-Headers", requestedHeaders)
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
return nil // keep listening for more requests
|
2022-02-09 01:30:48 +00:00
|
|
|
} // Otherwise, this is a POST request...
|
2021-06-21 19:40:08 +00:00
|
|
|
|
2022-02-07 21:32:31 +00:00
|
|
|
// Parse and pull the response parameters from an application/x-www-form-urlencoded request body.
|
2023-09-25 22:41:16 +00:00
|
|
|
if err = r.ParseForm(); err != nil {
|
|
|
|
// Avoid using httperr.Wrap because that would hide the details of err from the browser output.
|
|
|
|
return httperr.Newf(http.StatusBadRequest, "invalid form: %s", err.Error())
|
2021-06-21 19:40:08 +00:00
|
|
|
}
|
|
|
|
params = r.Form
|
2022-02-09 01:30:48 +00:00
|
|
|
|
|
|
|
// Allow CORS requests for POST so in the future our Javascript code can be updated to use the fetch API's
|
|
|
|
// mode "cors", and still be compatible with older CLI versions starting with those that have this code
|
|
|
|
// for CORS headers. Updating to use CORS would allow our Javascript code (form_post.js) to see the true
|
|
|
|
// http response status from this endpoint. Note that the POST response does not need to set as many CORS
|
|
|
|
// headers as the OPTIONS preflight response.
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", allowOrigin)
|
|
|
|
w.Header().Set("Vary", "*") // supposed to use Vary when Access-Control-Allow-Origin is a specific host
|
2021-06-21 19:40:08 +00:00
|
|
|
} else {
|
|
|
|
// Return HTTP 405 for anything that's not a GET.
|
|
|
|
if r.Method != http.MethodGet {
|
2022-04-16 02:43:53 +00:00
|
|
|
h.logger.V(plog.KlogLevelDebug).Info("Pinniped: Got unexpected request on callback listener", "method", r.Method)
|
2022-02-07 21:32:31 +00:00
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
return nil // keep listening for more requests
|
2021-06-21 19:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pull response parameters from the URL query string.
|
|
|
|
params = r.URL.Query()
|
2020-10-06 22:27:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate OAuth2 state and fail if it's incorrect (to block CSRF).
|
|
|
|
if err := h.state.Validate(params.Get("state")); err != nil {
|
|
|
|
return httperr.New(http.StatusForbidden, "missing or invalid state parameter")
|
|
|
|
}
|
|
|
|
|
2021-04-20 01:08:52 +00:00
|
|
|
// Check for error response parameters. See https://openid.net/specs/openid-connect-core-1_0.html#AuthError.
|
2020-10-06 22:27:36 +00:00
|
|
|
if errorParam := params.Get("error"); errorParam != "" {
|
2021-04-20 01:08:52 +00:00
|
|
|
if errorDescParam := params.Get("error_description"); errorDescParam != "" {
|
|
|
|
return httperr.Newf(http.StatusBadRequest, "login failed with code %q: %s", errorParam, errorDescParam)
|
|
|
|
}
|
2020-10-06 22:27:36 +00:00
|
|
|
return httperr.Newf(http.StatusBadRequest, "login failed with code %q", errorParam)
|
|
|
|
}
|
|
|
|
|
2020-11-30 23:14:57 +00:00
|
|
|
// Exchange the authorization code for access, ID, and refresh tokens and perform required
|
|
|
|
// validations on the returned ID token.
|
2021-07-08 19:32:44 +00:00
|
|
|
token, err := h.redeemAuthCode(r.Context(), params.Get("code"))
|
2020-10-06 22:27:36 +00:00
|
|
|
if err != nil {
|
2023-09-25 22:41:16 +00:00
|
|
|
// Avoid using httperr.Wrap because that would hide the details of err from the browser output.
|
|
|
|
return httperr.Newf(http.StatusBadRequest, "could not complete authorization code exchange: %s", err.Error())
|
2020-10-06 22:27:36 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 21:33:36 +00:00
|
|
|
h.callbacks <- callbackResult{token: token}
|
2020-10-22 21:12:02 +00:00
|
|
|
_, _ = w.Write([]byte("you have been logged in and may now close this tab"))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-08 19:32:44 +00:00
|
|
|
func (h *handlerState) redeemAuthCode(ctx context.Context, code string) (*oidctypes.Token, error) {
|
|
|
|
return h.getProvider(h.oauth2Config, h.provider, h.httpClient).
|
|
|
|
ExchangeAuthcodeAndValidateTokens(
|
|
|
|
ctx,
|
|
|
|
code,
|
|
|
|
h.pkce,
|
|
|
|
h.nonce,
|
|
|
|
h.oauth2Config.RedirectURL,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-10-22 21:12:02 +00:00
|
|
|
func (h *handlerState) serve(listener net.Listener) func() {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.Handle(h.callbackPath, httperr.HandlerFunc(h.handleAuthCodeCallback))
|
|
|
|
srv := http.Server{
|
2022-08-24 21:45:55 +00:00
|
|
|
Handler: securityheader.Wrap(mux),
|
|
|
|
BaseContext: func(_ net.Listener) context.Context { return h.ctx },
|
|
|
|
ReadHeaderTimeout: 10 * time.Second,
|
2020-10-22 21:12:02 +00:00
|
|
|
}
|
|
|
|
go func() { _ = srv.Serve(listener) }()
|
|
|
|
return func() {
|
2021-07-29 22:49:16 +00:00
|
|
|
// Gracefully shut down the server, allowing up to 100ms for
|
2020-10-22 21:12:02 +00:00
|
|
|
// clients to receive any in-flight responses.
|
2021-07-29 22:49:16 +00:00
|
|
|
shutdownCtx, cancel := context.WithTimeout(h.ctx, 100*time.Millisecond)
|
2020-10-22 21:12:02 +00:00
|
|
|
_ = srv.Shutdown(shutdownCtx)
|
|
|
|
cancel()
|
|
|
|
}
|
2020-10-06 22:27:36 +00:00
|
|
|
}
|