mirror of
https://github.com/vmware-tanzu/pinniped.git
synced 2026-09-26 01:44:17 +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:
@@ -16,6 +16,7 @@ import (
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/utils/strings/slices"
|
||||
|
||||
oidcapi "go.pinniped.dev/generated/latest/apis/supervisor/oidc"
|
||||
"go.pinniped.dev/internal/authenticators"
|
||||
"go.pinniped.dev/internal/constable"
|
||||
"go.pinniped.dev/internal/oidc"
|
||||
@@ -27,7 +28,7 @@ import (
|
||||
|
||||
const (
|
||||
// The name of the email claim from https://openid.net/specs/openid-connect-core-1_0.html#StandardClaims
|
||||
emailClaimName = "email"
|
||||
emailClaimName = oidcapi.ScopeEmail
|
||||
|
||||
// The name of the email_verified claim from https://openid.net/specs/openid-connect-core-1_0.html#StandardClaims
|
||||
emailVerifiedClaimName = "email_verified"
|
||||
@@ -55,11 +56,12 @@ func MakeDownstreamSession(subject string, username string, groups []string, gra
|
||||
if groups == nil {
|
||||
groups = []string{}
|
||||
}
|
||||
openIDSession.IDTokenClaims().Extra = map[string]interface{}{
|
||||
oidc.DownstreamUsernameClaim: username,
|
||||
openIDSession.IDTokenClaims().Extra = map[string]interface{}{}
|
||||
if slices.Contains(grantedScopes, oidcapi.ScopeUsername) {
|
||||
openIDSession.IDTokenClaims().Extra[oidcapi.IDTokenClaimUsername] = username
|
||||
}
|
||||
if slices.Contains(grantedScopes, oidc.DownstreamGroupsScope) {
|
||||
openIDSession.IDTokenClaims().Extra[oidc.DownstreamGroupsClaim] = groups
|
||||
if slices.Contains(grantedScopes, oidcapi.ScopeGroups) {
|
||||
openIDSession.IDTokenClaims().Extra[oidcapi.IDTokenClaimGroups] = groups
|
||||
}
|
||||
return openIDSession
|
||||
}
|
||||
@@ -68,8 +70,10 @@ func MakeDownstreamLDAPOrADCustomSessionData(
|
||||
ldapUpstream provider.UpstreamLDAPIdentityProviderI,
|
||||
idpType psession.ProviderType,
|
||||
authenticateResponse *authenticators.Response,
|
||||
username string,
|
||||
) *psession.CustomSessionData {
|
||||
customSessionData := &psession.CustomSessionData{
|
||||
Username: username,
|
||||
ProviderUID: ldapUpstream.GetResourceUID(),
|
||||
ProviderName: ldapUpstream.GetName(),
|
||||
ProviderType: idpType,
|
||||
@@ -92,17 +96,22 @@ func MakeDownstreamLDAPOrADCustomSessionData(
|
||||
return customSessionData
|
||||
}
|
||||
|
||||
func MakeDownstreamOIDCCustomSessionData(oidcUpstream provider.UpstreamOIDCIdentityProviderI, token *oidctypes.Token) (*psession.CustomSessionData, error) {
|
||||
upstreamSubject, err := ExtractStringClaimValue(oidc.IDTokenSubjectClaim, oidcUpstream.GetName(), token.IDToken.Claims)
|
||||
func MakeDownstreamOIDCCustomSessionData(
|
||||
oidcUpstream provider.UpstreamOIDCIdentityProviderI,
|
||||
token *oidctypes.Token,
|
||||
username string,
|
||||
) (*psession.CustomSessionData, error) {
|
||||
upstreamSubject, err := ExtractStringClaimValue(oidcapi.IDTokenClaimSubject, oidcUpstream.GetName(), token.IDToken.Claims)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
upstreamIssuer, err := ExtractStringClaimValue(oidc.IDTokenIssuerClaim, oidcUpstream.GetName(), token.IDToken.Claims)
|
||||
upstreamIssuer, err := ExtractStringClaimValue(oidcapi.IDTokenClaimIssuer, oidcUpstream.GetName(), token.IDToken.Claims)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
customSessionData := &psession.CustomSessionData{
|
||||
Username: username,
|
||||
ProviderUID: oidcUpstream.GetResourceUID(),
|
||||
ProviderName: oidcUpstream.GetName(),
|
||||
ProviderType: psession.ProviderTypeOIDC,
|
||||
@@ -148,11 +157,30 @@ func MakeDownstreamOIDCCustomSessionData(oidcUpstream provider.UpstreamOIDCIdent
|
||||
return customSessionData, nil
|
||||
}
|
||||
|
||||
// GrantScopesIfRequested auto-grants the scopes for which we do not require end-user approval, if they were requested.
|
||||
func GrantScopesIfRequested(authorizeRequester fosite.AuthorizeRequester, scopes []string) {
|
||||
for _, scope := range scopes {
|
||||
// AutoApproveScopes auto-grants the scopes which we support and for which we do not require end-user approval,
|
||||
// if they were requested. This should only be called after it has been validated that the client is allowed to request
|
||||
// the scopes that it requested (which is a check performed by fosite).
|
||||
func AutoApproveScopes(authorizeRequester fosite.AuthorizeRequester) {
|
||||
for _, scope := range []string{
|
||||
oidcapi.ScopeOpenID,
|
||||
oidcapi.ScopeOfflineAccess,
|
||||
oidcapi.ScopeRequestAudience,
|
||||
oidcapi.ScopeUsername,
|
||||
oidcapi.ScopeGroups,
|
||||
} {
|
||||
oidc.GrantScopeIfRequested(authorizeRequester, scope)
|
||||
}
|
||||
|
||||
// For backwards-compatibility with old pinniped CLI binaries which never request the username and groups scopes
|
||||
// (because those scopes did not exist yet when those CLIs were released), grant/approve the username and groups
|
||||
// scopes even if the CLI did not request them. Basically, pretend that the CLI requested them and auto-approve
|
||||
// them. Newer versions of the CLI binaries will request these scopes, so after enough time has passed that
|
||||
// we can assume the old versions of the CLI are no longer in use in the wild, then we can remove this code and
|
||||
// just let the above logic handle all clients.
|
||||
if authorizeRequester.GetClient().GetID() == oidcapi.ClientIDPinnipedCLI {
|
||||
authorizeRequester.GrantScope(oidcapi.ScopeUsername)
|
||||
authorizeRequester.GrantScope(oidcapi.ScopeGroups)
|
||||
}
|
||||
}
|
||||
|
||||
// GetDownstreamIdentityFromUpstreamIDToken returns the mapped subject, username, and group names, in that order.
|
||||
@@ -179,11 +207,11 @@ func getSubjectAndUsernameFromUpstreamIDToken(
|
||||
) (string, string, error) {
|
||||
// The spec says the "sub" claim is only unique per issuer,
|
||||
// so we will prepend the issuer string to make it globally unique.
|
||||
upstreamIssuer, err := ExtractStringClaimValue(oidc.IDTokenIssuerClaim, upstreamIDPConfig.GetName(), idTokenClaims)
|
||||
upstreamIssuer, err := ExtractStringClaimValue(oidcapi.IDTokenClaimIssuer, upstreamIDPConfig.GetName(), idTokenClaims)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
upstreamSubject, err := ExtractStringClaimValue(oidc.IDTokenSubjectClaim, upstreamIDPConfig.GetName(), idTokenClaims)
|
||||
upstreamSubject, err := ExtractStringClaimValue(oidcapi.IDTokenClaimSubject, upstreamIDPConfig.GetName(), idTokenClaims)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
@@ -266,13 +294,13 @@ func DownstreamSubjectFromUpstreamLDAP(ldapUpstream provider.UpstreamLDAPIdentit
|
||||
|
||||
func DownstreamLDAPSubject(uid string, ldapURL url.URL) string {
|
||||
q := ldapURL.Query()
|
||||
q.Set(oidc.IDTokenSubjectClaim, uid)
|
||||
q.Set(oidcapi.IDTokenClaimSubject, uid)
|
||||
ldapURL.RawQuery = q.Encode()
|
||||
return ldapURL.String()
|
||||
}
|
||||
|
||||
func downstreamSubjectFromUpstreamOIDC(upstreamIssuerAsString string, upstreamSubject string) string {
|
||||
return fmt.Sprintf("%s?%s=%s", upstreamIssuerAsString, oidc.IDTokenSubjectClaim, url.QueryEscape(upstreamSubject))
|
||||
return fmt.Sprintf("%s?%s=%s", upstreamIssuerAsString, oidcapi.IDTokenClaimSubject, url.QueryEscape(upstreamSubject))
|
||||
}
|
||||
|
||||
// GetGroupsFromUpstreamIDToken returns mapped group names coerced into a slice of strings.
|
||||
|
||||
Reference in New Issue
Block a user