mirror of
https://github.com/vmware-tanzu/pinniped.git
synced 2026-07-29 03:22:53 +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"
This commit is contained in:
+15
-15
@@ -21,14 +21,14 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/coreos/go-oidc/v3/oidc"
|
||||
coreosoidc "github.com/coreos/go-oidc/v3/oidc"
|
||||
"github.com/go-logr/logr"
|
||||
"github.com/pkg/browser"
|
||||
"golang.org/x/oauth2"
|
||||
"golang.org/x/term"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
|
||||
supervisoroidc "go.pinniped.dev/generated/latest/apis/supervisor/oidc"
|
||||
oidcapi "go.pinniped.dev/generated/latest/apis/supervisor/oidc"
|
||||
"go.pinniped.dev/internal/httputil/httperr"
|
||||
"go.pinniped.dev/internal/httputil/securityheader"
|
||||
"go.pinniped.dev/internal/net/phttp"
|
||||
@@ -91,7 +91,7 @@ type handlerState struct {
|
||||
callbackPath string
|
||||
|
||||
// Generated parameters of a login flow.
|
||||
provider *oidc.Provider
|
||||
provider *coreosoidc.Provider
|
||||
oauth2Config *oauth2.Config
|
||||
useFormPost bool
|
||||
state state.State
|
||||
@@ -106,8 +106,8 @@ type handlerState struct {
|
||||
getEnv func(key string) string
|
||||
listen func(string, string) (net.Listener, error)
|
||||
isTTY func(int) bool
|
||||
getProvider func(*oauth2.Config, *oidc.Provider, *http.Client) provider.UpstreamOIDCIdentityProviderI
|
||||
validateIDToken func(ctx context.Context, provider *oidc.Provider, audience string, token string) (*oidc.IDToken, error)
|
||||
getProvider func(*oauth2.Config, *coreosoidc.Provider, *http.Client) provider.UpstreamOIDCIdentityProviderI
|
||||
validateIDToken func(ctx context.Context, provider *coreosoidc.Provider, audience string, token string) (*coreosoidc.IDToken, error)
|
||||
promptForValue func(ctx context.Context, promptLabel string) (string, error)
|
||||
promptForSecret func(promptLabel string) (string, error)
|
||||
|
||||
@@ -268,7 +268,7 @@ func Login(issuer string, clientID string, opts ...Option) (*oidctypes.Token, er
|
||||
issuer: issuer,
|
||||
clientID: clientID,
|
||||
listenAddr: "localhost:0",
|
||||
scopes: []string{oidc.ScopeOfflineAccess, oidc.ScopeOpenID, "email", "profile"},
|
||||
scopes: []string{oidcapi.ScopeOfflineAccess, oidcapi.ScopeOpenID, oidcapi.ScopeEmail, oidcapi.ScopeProfile},
|
||||
cache: &nopCache{},
|
||||
callbackPath: "/callback",
|
||||
ctx: context.Background(),
|
||||
@@ -285,8 +285,8 @@ func Login(issuer string, clientID string, opts ...Option) (*oidctypes.Token, er
|
||||
listen: net.Listen,
|
||||
isTTY: term.IsTerminal,
|
||||
getProvider: upstreamoidc.New,
|
||||
validateIDToken: func(ctx context.Context, provider *oidc.Provider, audience string, token string) (*oidc.IDToken, error) {
|
||||
return provider.Verifier(&oidc.Config{ClientID: audience}).Verify(ctx, token)
|
||||
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)
|
||||
},
|
||||
promptForValue: promptForValue,
|
||||
promptForSecret: promptForSecret,
|
||||
@@ -305,7 +305,7 @@ func Login(issuer string, clientID string, opts ...Option) (*oidctypes.Token, er
|
||||
// Always set a long, but non-infinite timeout for this operation.
|
||||
ctx, cancel := context.WithTimeout(h.ctx, overallTimeout)
|
||||
defer cancel()
|
||||
ctx = oidc.ClientContext(ctx, h.httpClient)
|
||||
ctx = coreosoidc.ClientContext(ctx, h.httpClient)
|
||||
h.ctx = ctx
|
||||
|
||||
// Initialize login parameters.
|
||||
@@ -386,10 +386,10 @@ func (h *handlerState) baseLogin() (*oidctypes.Token, error) {
|
||||
}
|
||||
if h.upstreamIdentityProviderName != "" {
|
||||
authorizeOptions = append(authorizeOptions,
|
||||
oauth2.SetAuthURLParam(supervisoroidc.AuthorizeUpstreamIDPNameParamName, h.upstreamIdentityProviderName),
|
||||
oauth2.SetAuthURLParam(oidcapi.AuthorizeUpstreamIDPNameParamName, h.upstreamIdentityProviderName),
|
||||
)
|
||||
authorizeOptions = append(authorizeOptions,
|
||||
oauth2.SetAuthURLParam(supervisoroidc.AuthorizeUpstreamIDPTypeParamName, h.upstreamIdentityProviderType),
|
||||
oauth2.SetAuthURLParam(oidcapi.AuthorizeUpstreamIDPTypeParamName, h.upstreamIdentityProviderType),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -447,8 +447,8 @@ func (h *handlerState) cliBasedAuth(authorizeOptions *[]oauth2.AuthCodeOption) (
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not build authorize request: %w", err)
|
||||
}
|
||||
authReq.Header.Set(supervisoroidc.AuthorizeUsernameHeaderName, username)
|
||||
authReq.Header.Set(supervisoroidc.AuthorizePasswordHeaderName, password)
|
||||
authReq.Header.Set(oidcapi.AuthorizeUsernameHeaderName, username)
|
||||
authReq.Header.Set(oidcapi.AuthorizePasswordHeaderName, password)
|
||||
authRes, err := h.httpClient.Do(authReq)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("authorization response error: %w", err)
|
||||
@@ -710,7 +710,7 @@ func (h *handlerState) initOIDCDiscovery() error {
|
||||
|
||||
h.logger.V(plog.KlogLevelDebug).Info("Pinniped: Performing OIDC discovery", "issuer", h.issuer)
|
||||
var err error
|
||||
h.provider, err = oidc.NewProvider(h.ctx, h.issuer)
|
||||
h.provider, err = coreosoidc.NewProvider(h.ctx, h.issuer)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not perform OIDC discovery for %q: %w", h.issuer, err)
|
||||
}
|
||||
@@ -775,7 +775,7 @@ func (h *handlerState) tokenExchangeRFC8693(baseToken *oidctypes.Token) (*oidcty
|
||||
// Form the HTTP POST request with the parameters specified by RFC8693.
|
||||
reqBody := strings.NewReader(url.Values{
|
||||
"client_id": []string{h.clientID},
|
||||
"grant_type": []string{"urn:ietf:params:oauth:grant-type:token-exchange"},
|
||||
"grant_type": []string{oidcapi.GrantTypeTokenExchange},
|
||||
"audience": []string{h.requestedAudience},
|
||||
"subject_token": []string{baseToken.AccessToken.Token},
|
||||
"subject_token_type": []string{"urn:ietf:params:oauth:token-type:access_token"},
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
||||
// Copyright 2020-2022 the Pinniped contributors. All Rights Reserved.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Package nonce implements
|
||||
// Package nonce implements helpers for OIDC nonce parameter handling.
|
||||
package nonce
|
||||
|
||||
import (
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/coreos/go-oidc/v3/oidc"
|
||||
coreosoidc "github.com/coreos/go-oidc/v3/oidc"
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
@@ -36,11 +36,11 @@ func (n *Nonce) String() string {
|
||||
|
||||
// Param returns the OAuth2 auth code parameter for sending the nonce during the authorization request.
|
||||
func (n *Nonce) Param() oauth2.AuthCodeOption {
|
||||
return oidc.Nonce(string(*n))
|
||||
return coreosoidc.Nonce(string(*n))
|
||||
}
|
||||
|
||||
// Validate the returned ID token). Returns true iff the nonce matches or the returned JWT does not have a nonce.
|
||||
func (n *Nonce) Validate(token *oidc.IDToken) error {
|
||||
func (n *Nonce) Validate(token *coreosoidc.IDToken) error {
|
||||
if subtle.ConstantTimeCompare([]byte(token.Nonce), []byte(*n)) != 1 {
|
||||
return InvalidNonceError{Expected: *n, Got: Nonce(token.Nonce)}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user