add userinfo support for OpenID (#12469)

Some identity providers like GitLab do not provide
information about group membership as part of the
identity token claims. They only expose it via OIDC compatible
'/oauth/userinfo' endpoint, as described in the OpenID
Connect 1.0 sepcification.

But this of course requires application to make sure to add
additional accessToken, since idToken cannot be re-used to
perform the same 'userinfo' call. This is why this is specialized
requirement. Gitlab seems to be the only OpenID vendor that requires
this support for the time being.

fixes #12367
This commit is contained in:
Harshavardhana 2021-09-13 16:22:14 -07:00 committed by GitHub
parent d144958669
commit af78c3925a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 160 additions and 55 deletions

View File

@ -45,6 +45,7 @@ const (
stsPolicy = "Policy" stsPolicy = "Policy"
stsToken = "Token" stsToken = "Token"
stsWebIdentityToken = "WebIdentityToken" stsWebIdentityToken = "WebIdentityToken"
stsWebIdentityAccessToken = "WebIdentityAccessToken" // only valid if UserInfo is enabled.
stsDurationSeconds = "DurationSeconds" stsDurationSeconds = "DurationSeconds"
stsLDAPUsername = "LDAPUsername" stsLDAPUsername = "LDAPUsername"
stsLDAPPassword = "LDAPPassword" stsLDAPPassword = "LDAPPassword"
@ -336,7 +337,9 @@ func (sts *stsAPIHandlers) AssumeRoleWithSSO(w http.ResponseWriter, r *http.Requ
token = r.Form.Get(stsWebIdentityToken) token = r.Form.Get(stsWebIdentityToken)
} }
m, err := v.Validate(token, r.Form.Get(stsDurationSeconds)) accessToken := r.Form.Get(stsWebIdentityAccessToken)
m, err := v.Validate(token, accessToken, r.Form.Get(stsDurationSeconds))
if err != nil { if err != nil {
switch err { switch err {
case openid.ErrTokenExpired: case openid.ErrTokenExpired:

View File

@ -16,6 +16,14 @@ The OAuth 2.0 id_token that is provided by the web identity provider. Applicatio
| *Length Constraints* | *Minimum length of 4. Maximum length of 2048.* | | *Length Constraints* | *Minimum length of 4. Maximum length of 2048.* |
| *Required* | *Yes* | | *Required* | *Yes* |
### WebIdentityAccessToken (MinIO Extension)
There are situations when identity provider does not provide user claims in `id_token` instead it needs to be retrieved from UserInfo endpoint, this extension is only useful in this scenario. This is rare so use it accordingly depending on your Identity provider implementation. `access_token` is available as part of the OIDC authentication flow similar to `id_token`.
| Params | Value |
| :-- | :-- |
| *Type* | *String* |
| *Required* | *No* |
### Version ### Version
Indicates STS API version information, the only supported value is '2011-06-15'. This value is borrowed from AWS STS API documentation for compatibility reasons. Indicates STS API version information, the only supported value is '2011-06-15'. This value is borrowed from AWS STS API documentation for compatibility reasons.

View File

@ -44,15 +44,21 @@ var (
Optional: true, Optional: true,
Type: "string", Type: "string",
}, },
config.HelpKV{
Key: ClaimUserinfo,
Description: `Enable fetching claims from UserInfo Endpoint for authenticated user`,
Optional: true,
Type: "on|off",
},
config.HelpKV{ config.HelpKV{
Key: ClaimPrefix, Key: ClaimPrefix,
Description: `JWT claim namespace prefix e.g. "customer1/"`, Description: `[DEPRECATED use 'claim_name'] JWT claim namespace prefix e.g. "customer1/"`,
Optional: true, Optional: true,
Type: "string", Type: "string",
}, },
config.HelpKV{ config.HelpKV{
Key: RedirectURI, Key: RedirectURI,
Description: `Configure custom redirect_uri for OpenID login flow callback`, Description: `[DEPRECATED use env 'MINIO_BROWSER_REDIRECT_URL'] Configure custom redirect_uri for OpenID login flow callback`,
Optional: true, Optional: true,
Type: "string", Type: "string",
}, },

View File

@ -50,6 +50,7 @@ type Config struct {
URL *xnet.URL `json:"url,omitempty"` URL *xnet.URL `json:"url,omitempty"`
ClaimPrefix string `json:"claimPrefix,omitempty"` ClaimPrefix string `json:"claimPrefix,omitempty"`
ClaimName string `json:"claimName,omitempty"` ClaimName string `json:"claimName,omitempty"`
ClaimUserinfo bool `json:"claimUserInfo,omitempty"`
RedirectURI string `json:"redirectURI,omitempty"` RedirectURI string `json:"redirectURI,omitempty"`
DiscoveryDoc DiscoveryDoc DiscoveryDoc DiscoveryDoc
ClientID string ClientID string
@ -61,6 +62,63 @@ type Config struct {
closeRespFn func(io.ReadCloser) closeRespFn func(io.ReadCloser)
} }
// UserInfo returns claims for authenticated user from userInfo endpoint.
//
// Some OIDC implementations such as GitLab do not support
// claims as part of the normal oauth2 flow, instead rely
// on service providers making calls to IDP to fetch additional
// claims available from the UserInfo endpoint
func (r *Config) UserInfo(accessToken string) (map[string]interface{}, error) {
if r.JWKS.URL == nil || r.JWKS.URL.String() == "" {
return nil, errors.New("openid not configured")
}
transport := http.DefaultTransport
if r.transport != nil {
transport = r.transport
}
client := &http.Client{
Transport: transport,
}
req, err := http.NewRequest(http.MethodPost, r.DiscoveryDoc.UserInfoEndpoint, nil)
if err != nil {
return nil, err
}
if accessToken != "" {
req.Header.Set("Authorization", "Bearer "+accessToken)
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer r.closeRespFn(resp.Body)
if resp.StatusCode != http.StatusOK {
// uncomment this for debugging when needed.
// reqBytes, _ := httputil.DumpRequest(req, false)
// fmt.Println(string(reqBytes))
// respBytes, _ := httputil.DumpResponse(resp, true)
// fmt.Println(string(respBytes))
return nil, errors.New(resp.Status)
}
dec := json.NewDecoder(resp.Body)
var claims = map[string]interface{}{}
if err = dec.Decode(&claims); err != nil {
// uncomment this for debugging when needed.
// reqBytes, _ := httputil.DumpRequest(req, false)
// fmt.Println(string(reqBytes))
// respBytes, _ := httputil.DumpResponse(resp, true)
// fmt.Println(string(respBytes))
return nil, err
}
return claims, nil
}
// LookupUser lookup userid for the provider // LookupUser lookup userid for the provider
func (r Config) LookupUser(userid string) (provider.User, error) { func (r Config) LookupUser(userid string) (provider.User, error) {
if r.provider != nil { if r.provider != nil {
@ -122,9 +180,6 @@ func (r *Config) InitializeKeycloakProvider(adminURL, realm string) error {
// PopulatePublicKey - populates a new publickey from the JWKS URL. // PopulatePublicKey - populates a new publickey from the JWKS URL.
func (r *Config) PopulatePublicKey() error { func (r *Config) PopulatePublicKey() error {
r.Lock()
defer r.Unlock()
if r.JWKS.URL == nil || r.JWKS.URL.String() == "" { if r.JWKS.URL == nil || r.JWKS.URL.String() == "" {
return nil return nil
} }
@ -135,6 +190,10 @@ func (r *Config) PopulatePublicKey() error {
client := &http.Client{ client := &http.Client{
Transport: transport, Transport: transport,
} }
r.Lock()
defer r.Unlock()
resp, err := client.Get(r.JWKS.URL.String()) resp, err := client.Get(r.JWKS.URL.String())
if err != nil { if err != nil {
return err return err
@ -234,7 +293,7 @@ func updateClaimsExpiry(dsecs string, claims map[string]interface{}) error {
} }
// Validate - validates the id_token. // Validate - validates the id_token.
func (r *Config) Validate(token, dsecs string) (map[string]interface{}, error) { func (r *Config) Validate(token, accessToken, dsecs string) (map[string]interface{}, error) {
jp := new(jwtgo.Parser) jp := new(jwtgo.Parser)
jp.ValidMethods = []string{ jp.ValidMethods = []string{
"RS256", "RS384", "RS512", "ES256", "ES384", "ES512", "RS256", "RS384", "RS512", "ES256", "ES384", "ES512",
@ -273,6 +332,23 @@ func (r *Config) Validate(token, dsecs string) (map[string]interface{}, error) {
return nil, err return nil, err
} }
// If claim user info is enabled, get claims from userInfo
// and overwrite them with the claims from JWT.
if r.ClaimUserinfo {
if accessToken == "" {
return nil, errors.New("access_token is mandatory if user_info claim is enabled")
}
uclaims, err := r.UserInfo(accessToken)
if err != nil {
return nil, err
}
for k, v := range uclaims {
if _, ok := claims[k]; !ok { // only add to claims not update it.
claims[k] = v
}
}
}
return claims, nil return claims, nil
} }
@ -286,9 +362,11 @@ const (
JwksURL = "jwks_url" JwksURL = "jwks_url"
ConfigURL = "config_url" ConfigURL = "config_url"
ClaimName = "claim_name" ClaimName = "claim_name"
ClaimUserinfo = "claim_userinfo"
ClaimPrefix = "claim_prefix" ClaimPrefix = "claim_prefix"
ClientID = "client_id" ClientID = "client_id"
ClientSecret = "client_secret" ClientSecret = "client_secret"
Vendor = "vendor" Vendor = "vendor"
Scopes = "scopes" Scopes = "scopes"
RedirectURI = "redirect_uri" RedirectURI = "redirect_uri"
@ -303,6 +381,7 @@ const (
EnvIdentityOpenIDJWKSURL = "MINIO_IDENTITY_OPENID_JWKS_URL" EnvIdentityOpenIDJWKSURL = "MINIO_IDENTITY_OPENID_JWKS_URL"
EnvIdentityOpenIDURL = "MINIO_IDENTITY_OPENID_CONFIG_URL" EnvIdentityOpenIDURL = "MINIO_IDENTITY_OPENID_CONFIG_URL"
EnvIdentityOpenIDClaimName = "MINIO_IDENTITY_OPENID_CLAIM_NAME" EnvIdentityOpenIDClaimName = "MINIO_IDENTITY_OPENID_CLAIM_NAME"
EnvIdentityOpenIDClaimUserInfo = "MINIO_IDENTITY_OPENID_CLAIM_USERINFO"
EnvIdentityOpenIDClaimPrefix = "MINIO_IDENTITY_OPENID_CLAIM_PREFIX" EnvIdentityOpenIDClaimPrefix = "MINIO_IDENTITY_OPENID_CLAIM_PREFIX"
EnvIdentityOpenIDRedirectURI = "MINIO_IDENTITY_OPENID_REDIRECT_URI" EnvIdentityOpenIDRedirectURI = "MINIO_IDENTITY_OPENID_REDIRECT_URI"
EnvIdentityOpenIDScopes = "MINIO_IDENTITY_OPENID_SCOPES" EnvIdentityOpenIDScopes = "MINIO_IDENTITY_OPENID_SCOPES"
@ -374,6 +453,10 @@ var (
Key: ClaimName, Key: ClaimName,
Value: iampolicy.PolicyName, Value: iampolicy.PolicyName,
}, },
config.KV{
Key: ClaimUserinfo,
Value: "",
},
config.KV{ config.KV{
Key: ClaimPrefix, Key: ClaimPrefix,
Value: "", Value: "",
@ -412,6 +495,7 @@ func LookupConfig(kvs config.KVS, transport *http.Transport, closeRespFn func(io
c = Config{ c = Config{
RWMutex: &sync.RWMutex{}, RWMutex: &sync.RWMutex{},
ClaimName: env.Get(EnvIdentityOpenIDClaimName, kvs.Get(ClaimName)), ClaimName: env.Get(EnvIdentityOpenIDClaimName, kvs.Get(ClaimName)),
ClaimUserinfo: env.Get(EnvIdentityOpenIDClaimUserInfo, kvs.Get(ClaimUserinfo)) == config.EnableOn,
ClaimPrefix: env.Get(EnvIdentityOpenIDClaimPrefix, kvs.Get(ClaimPrefix)), ClaimPrefix: env.Get(EnvIdentityOpenIDClaimPrefix, kvs.Get(ClaimPrefix)),
RedirectURI: env.Get(EnvIdentityOpenIDRedirectURI, kvs.Get(RedirectURI)), RedirectURI: env.Get(EnvIdentityOpenIDRedirectURI, kvs.Get(RedirectURI)),
publicKeys: make(map[string]crypto.PublicKey), publicKeys: make(map[string]crypto.PublicKey),
@ -433,6 +517,10 @@ func LookupConfig(kvs config.KVS, transport *http.Transport, closeRespFn func(io
} }
} }
if c.ClaimUserinfo && configURL == "" {
return c, errors.New("please specify config_url to enable fetching claims from UserInfo endpoint")
}
if scopeList := env.Get(EnvIdentityOpenIDScopes, kvs.Get(Scopes)); scopeList != "" { if scopeList := env.Get(EnvIdentityOpenIDScopes, kvs.Get(Scopes)); scopeList != "" {
var scopes []string var scopes []string
for _, scope := range strings.Split(scopeList, ",") { for _, scope := range strings.Split(scopeList, ",") {

View File

@ -100,7 +100,7 @@ func TestJWTAzureFail(t *testing.T) {
t.Fatalf("Unexpected id %s for the validator", cfg.ID()) t.Fatalf("Unexpected id %s for the validator", cfg.ID())
} }
if _, err := cfg.Validate(jwtToken, ""); err == nil { if _, err := cfg.Validate(jwtToken, "", ""); err == nil {
// Azure should fail due to non OIDC compliant JWT // Azure should fail due to non OIDC compliant JWT
// generated by Azure AD // generated by Azure AD
t.Fatal(err) t.Fatal(err)
@ -154,7 +154,7 @@ func TestJWT(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if _, err := cfg.Validate(u.Query().Get("Token"), ""); err == nil { if _, err := cfg.Validate(u.Query().Get("Token"), "", ""); err == nil {
t.Fatal(err) t.Fatal(err)
} }
} }

View File

@ -31,7 +31,7 @@ type ID string
type Validator interface { type Validator interface {
// Validate is a custom validator function for this provider, // Validate is a custom validator function for this provider,
// each validation is authenticationType or provider specific. // each validation is authenticationType or provider specific.
Validate(token string, duration string) (map[string]interface{}, error) Validate(idToken, accessToken, duration string) (map[string]interface{}, error)
// ID returns provider name of this provider. // ID returns provider name of this provider.
ID() ID ID() ID

View File

@ -27,7 +27,7 @@ import (
type errorValidator struct{} type errorValidator struct{}
func (e errorValidator) Validate(token, dsecs string) (map[string]interface{}, error) { func (e errorValidator) Validate(idToken, accessToken, dsecs string) (map[string]interface{}, error) {
return nil, ErrTokenExpired return nil, ErrTokenExpired
} }
@ -79,7 +79,7 @@ func TestValidators(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if _, err = v.Validate("", ""); err != ErrTokenExpired { if _, err = v.Validate("", "", ""); err != ErrTokenExpired {
t.Fatalf("Expected error %s, got %s", ErrTokenExpired, err) t.Fatalf("Expected error %s, got %s", ErrTokenExpired, err)
} }