2022-05-05 16:40:06 -04:00
|
|
|
// Copyright (c) 2015-2022 MinIO, Inc.
|
2021-04-18 15:41:13 -04:00
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2019-10-23 01:59:13 -04:00
|
|
|
|
|
|
|
package openid
|
|
|
|
|
|
|
|
import (
|
2023-02-10 11:50:25 -05:00
|
|
|
"context"
|
2019-10-23 01:59:13 -04:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2020-08-28 11:15:12 -04:00
|
|
|
"sync"
|
2019-10-23 01:59:13 -04:00
|
|
|
"time"
|
|
|
|
|
2021-11-05 15:20:08 -04:00
|
|
|
jwtgo "github.com/golang-jwt/jwt/v4"
|
2021-11-26 22:22:40 -05:00
|
|
|
"github.com/minio/minio/internal/arn"
|
2021-06-01 17:59:40 -04:00
|
|
|
"github.com/minio/minio/internal/auth"
|
2024-05-24 19:05:23 -04:00
|
|
|
xnet "github.com/minio/pkg/v3/net"
|
|
|
|
"github.com/minio/pkg/v3/policy"
|
2019-10-23 01:59:13 -04:00
|
|
|
)
|
|
|
|
|
2022-04-28 21:27:09 -04:00
|
|
|
type publicKeys struct {
|
2021-07-14 21:27:25 -04:00
|
|
|
*sync.RWMutex
|
|
|
|
|
2022-04-28 21:27:09 -04:00
|
|
|
// map of kid to public key
|
2022-11-03 19:41:53 -04:00
|
|
|
pkMap map[string]interface{}
|
2022-04-28 21:27:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pk *publicKeys) parseAndAdd(b io.Reader) error {
|
|
|
|
var jwk JWKS
|
|
|
|
err := json.NewDecoder(b).Decode(&jwk)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, key := range jwk.Keys {
|
2022-11-03 19:41:53 -04:00
|
|
|
pkey, err := key.DecodePublicKey()
|
2022-04-28 21:27:09 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-03 19:41:53 -04:00
|
|
|
pk.add(key.Kid, pkey)
|
2022-04-28 21:27:09 -04:00
|
|
|
}
|
2022-11-03 19:41:53 -04:00
|
|
|
|
2022-04-28 21:27:09 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-03 19:41:53 -04:00
|
|
|
func (pk *publicKeys) add(keyID string, key interface{}) {
|
|
|
|
pk.Lock()
|
|
|
|
defer pk.Unlock()
|
|
|
|
|
|
|
|
pk.pkMap[keyID] = key
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pk *publicKeys) get(kid string) interface{} {
|
2022-04-28 21:27:09 -04:00
|
|
|
pk.RLock()
|
|
|
|
defer pk.RUnlock()
|
|
|
|
return pk.pkMap[kid]
|
|
|
|
}
|
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
// PopulatePublicKey - populates a new publickey from the JWKS URL.
|
2022-04-28 21:27:09 -04:00
|
|
|
func (r *Config) PopulatePublicKey(arn arn.ARN) error {
|
|
|
|
pCfg := r.arnProviderCfgsMap[arn]
|
|
|
|
if pCfg.JWKS.URL == nil || pCfg.JWKS.URL.String() == "" {
|
2019-10-23 01:59:13 -04:00
|
|
|
return nil
|
|
|
|
}
|
2022-11-03 19:41:53 -04:00
|
|
|
|
|
|
|
// Add client secret for the client ID for HMAC based signature.
|
|
|
|
r.pubKeys.add(pCfg.ClientID, []byte(pCfg.ClientSecret))
|
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
client := &http.Client{
|
2022-04-28 21:27:09 -04:00
|
|
|
Transport: r.transport,
|
2019-10-23 01:59:13 -04:00
|
|
|
}
|
2021-09-13 19:22:14 -04:00
|
|
|
|
2022-04-28 21:27:09 -04:00
|
|
|
resp, err := client.Get(pCfg.JWKS.URL.String())
|
2019-10-23 01:59:13 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer r.closeRespFn(resp.Body)
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return errors.New(resp.Status)
|
|
|
|
}
|
|
|
|
|
2022-04-28 21:27:09 -04:00
|
|
|
return r.pubKeys.parseAndAdd(resp.Body)
|
2019-10-23 01:59:13 -04:00
|
|
|
}
|
|
|
|
|
2022-04-28 21:27:09 -04:00
|
|
|
// ErrTokenExpired - error token expired
|
|
|
|
var (
|
|
|
|
ErrTokenExpired = errors.New("token expired")
|
|
|
|
)
|
|
|
|
|
2019-11-29 08:27:54 -05:00
|
|
|
func updateClaimsExpiry(dsecs string, claims map[string]interface{}) error {
|
|
|
|
expStr := claims["exp"]
|
|
|
|
if expStr == "" {
|
|
|
|
return ErrTokenExpired
|
|
|
|
}
|
|
|
|
|
|
|
|
// No custom duration requested, the claims can be used as is.
|
|
|
|
if dsecs == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-15 23:42:31 -05:00
|
|
|
if _, err := auth.ExpToInt64(expStr); err != nil {
|
2019-11-29 08:27:54 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultExpiryDuration, err := GetDefaultExpiration(dsecs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-04 15:03:43 -04:00
|
|
|
claims["exp"] = time.Now().UTC().Add(defaultExpiryDuration).Unix() // update with new expiry.
|
2019-11-29 08:27:54 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-28 21:27:09 -04:00
|
|
|
const (
|
|
|
|
audClaim = "aud"
|
|
|
|
azpClaim = "azp"
|
|
|
|
)
|
|
|
|
|
2021-07-28 01:38:12 -04:00
|
|
|
// Validate - validates the id_token.
|
2024-05-30 14:10:41 -04:00
|
|
|
func (r *Config) Validate(ctx context.Context, arn arn.ARN, token, accessToken, dsecs string, claims map[string]interface{}) error {
|
2019-10-23 01:59:13 -04:00
|
|
|
jp := new(jwtgo.Parser)
|
2020-04-10 17:59:52 -04:00
|
|
|
jp.ValidMethods = []string{
|
2022-11-03 19:41:53 -04:00
|
|
|
"RS256", "RS384", "RS512",
|
|
|
|
"ES256", "ES384", "ES512",
|
|
|
|
"HS256", "HS384", "HS512",
|
|
|
|
"RS3256", "RS3384", "RS3512",
|
|
|
|
"ES3256", "ES3384", "ES3512",
|
2020-04-10 17:59:52 -04:00
|
|
|
}
|
2019-10-23 01:59:13 -04:00
|
|
|
|
|
|
|
keyFuncCallback := func(jwtToken *jwtgo.Token) (interface{}, error) {
|
|
|
|
kid, ok := jwtToken.Header["kid"].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Invalid kid value %v", jwtToken.Header["kid"])
|
|
|
|
}
|
2022-04-28 21:27:09 -04:00
|
|
|
return r.pubKeys.get(kid), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pCfg, ok := r.arnProviderCfgsMap[arn]
|
|
|
|
if !ok {
|
2022-07-22 14:12:03 -04:00
|
|
|
return fmt.Errorf("Role %s does not exist", arn)
|
2019-10-23 01:59:13 -04:00
|
|
|
}
|
|
|
|
|
2024-05-30 14:10:41 -04:00
|
|
|
mclaims := jwtgo.MapClaims(claims)
|
|
|
|
jwtToken, err := jp.ParseWithClaims(token, &mclaims, keyFuncCallback)
|
2019-10-23 01:59:13 -04:00
|
|
|
if err != nil {
|
2020-08-28 11:15:12 -04:00
|
|
|
// Re-populate the public key in-case the JWKS
|
|
|
|
// pubkeys are refreshed
|
2022-04-28 21:27:09 -04:00
|
|
|
if err = r.PopulatePublicKey(arn); err != nil {
|
2022-07-22 14:12:03 -04:00
|
|
|
return err
|
2020-08-28 11:15:12 -04:00
|
|
|
}
|
2024-05-30 14:10:41 -04:00
|
|
|
jwtToken, err = jwtgo.ParseWithClaims(token, &mclaims, keyFuncCallback)
|
2020-08-28 11:15:12 -04:00
|
|
|
if err != nil {
|
2022-07-22 14:12:03 -04:00
|
|
|
return err
|
2020-08-28 11:15:12 -04:00
|
|
|
}
|
2019-10-23 01:59:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if !jwtToken.Valid {
|
2022-07-22 14:12:03 -04:00
|
|
|
return ErrTokenExpired
|
2019-10-23 01:59:13 -04:00
|
|
|
}
|
|
|
|
|
2024-05-30 14:10:41 -04:00
|
|
|
if err = updateClaimsExpiry(dsecs, mclaims); err != nil {
|
2022-07-22 14:12:03 -04:00
|
|
|
return err
|
2019-10-23 01:59:13 -04:00
|
|
|
}
|
|
|
|
|
2024-05-30 14:10:41 -04:00
|
|
|
if err = r.updateUserinfoClaims(ctx, arn, accessToken, mclaims); err != nil {
|
2022-07-22 14:12:03 -04:00
|
|
|
return err
|
2022-04-28 21:27:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate that matching clientID appears in the aud or azp claims.
|
|
|
|
|
|
|
|
// REQUIRED. Audience(s) that this ID Token is intended for.
|
|
|
|
// It MUST contain the OAuth 2.0 client_id of the Relying Party
|
|
|
|
// as an audience value. It MAY also contain identifiers for
|
|
|
|
// other audiences. In the general case, the aud value is an
|
|
|
|
// array of case sensitive strings. In the common special case
|
|
|
|
// when there is one audience, the aud value MAY be a single
|
|
|
|
// case sensitive
|
2024-05-30 14:10:41 -04:00
|
|
|
audValues, ok := policy.GetValuesFromClaims(mclaims, audClaim)
|
2022-04-28 21:27:09 -04:00
|
|
|
if !ok {
|
2022-07-22 14:12:03 -04:00
|
|
|
return errors.New("STS JWT Token has `aud` claim invalid, `aud` must match configured OpenID Client ID")
|
2022-04-28 21:27:09 -04:00
|
|
|
}
|
|
|
|
if !audValues.Contains(pCfg.ClientID) {
|
|
|
|
// if audience claims is missing, look for "azp" claims.
|
|
|
|
// OPTIONAL. Authorized party - the party to which the ID
|
|
|
|
// Token was issued. If present, it MUST contain the OAuth
|
|
|
|
// 2.0 Client ID of this party. This Claim is only needed
|
|
|
|
// when the ID Token has a single audience value and that
|
|
|
|
// audience is different than the authorized party. It MAY
|
|
|
|
// be included even when the authorized party is the same
|
|
|
|
// as the sole audience. The azp value is a case sensitive
|
|
|
|
// string containing a StringOrURI value
|
2024-05-30 14:10:41 -04:00
|
|
|
azpValues, ok := policy.GetValuesFromClaims(mclaims, azpClaim)
|
2022-04-28 21:27:09 -04:00
|
|
|
if !ok {
|
2022-07-22 14:12:03 -04:00
|
|
|
return errors.New("STS JWT Token has `azp` claim invalid, `azp` must match configured OpenID Client ID")
|
2022-04-28 21:27:09 -04:00
|
|
|
}
|
|
|
|
if !azpValues.Contains(pCfg.ClientID) {
|
2022-07-22 14:12:03 -04:00
|
|
|
return errors.New("STS JWT Token has `azp` claim invalid, `azp` must match configured OpenID Client ID")
|
2022-04-28 21:27:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-22 14:12:03 -04:00
|
|
|
return nil
|
2022-04-28 21:27:09 -04:00
|
|
|
}
|
|
|
|
|
2023-02-10 11:50:25 -05:00
|
|
|
func (r *Config) updateUserinfoClaims(ctx context.Context, arn arn.ARN, accessToken string, claims map[string]interface{}) error {
|
2022-04-28 21:27:09 -04:00
|
|
|
pCfg, ok := r.arnProviderCfgsMap[arn]
|
2021-09-13 19:22:14 -04:00
|
|
|
// If claim user info is enabled, get claims from userInfo
|
|
|
|
// and overwrite them with the claims from JWT.
|
2022-04-28 21:27:09 -04:00
|
|
|
if ok && pCfg.ClaimUserinfo {
|
2021-09-13 19:22:14 -04:00
|
|
|
if accessToken == "" {
|
2022-04-28 21:27:09 -04:00
|
|
|
return errors.New("access_token is mandatory if user_info claim is enabled")
|
2021-09-13 19:22:14 -04:00
|
|
|
}
|
2023-02-10 11:50:25 -05:00
|
|
|
uclaims, err := pCfg.UserInfo(ctx, accessToken, r.transport)
|
2021-09-13 19:22:14 -04:00
|
|
|
if err != nil {
|
2022-04-28 21:27:09 -04:00
|
|
|
return err
|
2021-09-13 19:22:14 -04:00
|
|
|
}
|
|
|
|
for k, v := range uclaims {
|
|
|
|
if _, ok := claims[k]; !ok { // only add to claims not update it.
|
|
|
|
claims[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-28 21:27:09 -04:00
|
|
|
return nil
|
2019-10-23 01:59:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// DiscoveryDoc - parses the output from openid-configuration
|
|
|
|
// for example https://accounts.google.com/.well-known/openid-configuration
|
|
|
|
type DiscoveryDoc struct {
|
|
|
|
Issuer string `json:"issuer,omitempty"`
|
|
|
|
AuthEndpoint string `json:"authorization_endpoint,omitempty"`
|
|
|
|
TokenEndpoint string `json:"token_endpoint,omitempty"`
|
2022-12-06 19:56:37 -05:00
|
|
|
EndSessionEndpoint string `json:"end_session_endpoint,omitempty"`
|
2019-10-23 01:59:13 -04:00
|
|
|
UserInfoEndpoint string `json:"userinfo_endpoint,omitempty"`
|
|
|
|
RevocationEndpoint string `json:"revocation_endpoint,omitempty"`
|
|
|
|
JwksURI string `json:"jwks_uri,omitempty"`
|
|
|
|
ResponseTypesSupported []string `json:"response_types_supported,omitempty"`
|
|
|
|
SubjectTypesSupported []string `json:"subject_types_supported,omitempty"`
|
|
|
|
IDTokenSigningAlgValuesSupported []string `json:"id_token_signing_alg_values_supported,omitempty"`
|
|
|
|
ScopesSupported []string `json:"scopes_supported,omitempty"`
|
|
|
|
TokenEndpointAuthMethods []string `json:"token_endpoint_auth_methods_supported,omitempty"`
|
|
|
|
ClaimsSupported []string `json:"claims_supported,omitempty"`
|
|
|
|
CodeChallengeMethodsSupported []string `json:"code_challenge_methods_supported,omitempty"`
|
|
|
|
}
|
|
|
|
|
2022-04-28 21:27:09 -04:00
|
|
|
func parseDiscoveryDoc(u *xnet.URL, transport http.RoundTripper, closeRespFn func(io.ReadCloser)) (DiscoveryDoc, error) {
|
2019-10-23 01:59:13 -04:00
|
|
|
d := DiscoveryDoc{}
|
|
|
|
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return d, err
|
|
|
|
}
|
|
|
|
clnt := http.Client{
|
|
|
|
Transport: transport,
|
|
|
|
}
|
|
|
|
resp, err := clnt.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return d, err
|
|
|
|
}
|
|
|
|
defer closeRespFn(resp.Body)
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2023-05-19 05:13:33 -04:00
|
|
|
return d, fmt.Errorf("unexpected error returned by %s : status(%s)", u, resp.Status)
|
2019-10-23 01:59:13 -04:00
|
|
|
}
|
|
|
|
dec := json.NewDecoder(resp.Body)
|
|
|
|
if err = dec.Decode(&d); err != nil {
|
|
|
|
return d, err
|
|
|
|
}
|
|
|
|
return d, nil
|
|
|
|
}
|