2019-10-23 01:59:13 -04:00
|
|
|
/*
|
|
|
|
* MinIO Cloud Storage, (C) 2018-2019 MinIO, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package openid
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
jwtgo "github.com/dgrijalva/jwt-go"
|
|
|
|
"github.com/minio/minio/cmd/config"
|
2019-11-29 08:27:54 -05:00
|
|
|
"github.com/minio/minio/pkg/auth"
|
2019-10-23 01:59:13 -04:00
|
|
|
"github.com/minio/minio/pkg/env"
|
2020-05-18 12:59:45 -04:00
|
|
|
iampolicy "github.com/minio/minio/pkg/iam/policy"
|
2019-10-23 01:59:13 -04:00
|
|
|
xnet "github.com/minio/minio/pkg/net"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config - OpenID Config
|
|
|
|
// RSA authentication target arguments
|
|
|
|
type Config struct {
|
|
|
|
JWKS struct {
|
|
|
|
URL *xnet.URL `json:"url"`
|
|
|
|
} `json:"jwks"`
|
|
|
|
URL *xnet.URL `json:"url,omitempty"`
|
|
|
|
ClaimPrefix string `json:"claimPrefix,omitempty"`
|
2020-01-08 20:21:58 -05:00
|
|
|
ClaimName string `json:"claimName,omitempty"`
|
2019-10-23 01:59:13 -04:00
|
|
|
DiscoveryDoc DiscoveryDoc
|
2019-11-30 00:37:42 -05:00
|
|
|
ClientID string
|
2019-10-23 01:59:13 -04:00
|
|
|
publicKeys map[string]crypto.PublicKey
|
|
|
|
transport *http.Transport
|
|
|
|
closeRespFn func(io.ReadCloser)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PopulatePublicKey - populates a new publickey from the JWKS URL.
|
|
|
|
func (r *Config) PopulatePublicKey() error {
|
|
|
|
if r.JWKS.URL == nil || r.JWKS.URL.String() == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
transport := http.DefaultTransport
|
|
|
|
if r.transport != nil {
|
|
|
|
transport = r.transport
|
|
|
|
}
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: transport,
|
|
|
|
}
|
|
|
|
resp, err := client.Get(r.JWKS.URL.String())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer r.closeRespFn(resp.Body)
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return errors.New(resp.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
var jwk JWKS
|
|
|
|
if err = json.NewDecoder(resp.Body).Decode(&jwk); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, key := range jwk.Keys {
|
|
|
|
r.publicKeys[key.Kid], err = key.DecodePublicKey()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON - decodes JSON data.
|
|
|
|
func (r *Config) UnmarshalJSON(data []byte) error {
|
|
|
|
// subtype to avoid recursive call to UnmarshalJSON()
|
|
|
|
type subConfig Config
|
|
|
|
var sr subConfig
|
|
|
|
|
|
|
|
if err := json.Unmarshal(data, &sr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ar := Config(sr)
|
|
|
|
if ar.JWKS.URL == nil || ar.JWKS.URL.String() == "" {
|
|
|
|
*r = ar
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
*r = ar
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// JWT - rs client grants provider details.
|
|
|
|
type JWT struct {
|
|
|
|
Config
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDefaultExpiration - returns the expiration seconds expected.
|
|
|
|
func GetDefaultExpiration(dsecs string) (time.Duration, error) {
|
|
|
|
defaultExpiryDuration := time.Duration(60) * time.Minute // Defaults to 1hr.
|
|
|
|
if dsecs != "" {
|
|
|
|
expirySecs, err := strconv.ParseInt(dsecs, 10, 64)
|
|
|
|
if err != nil {
|
2019-11-29 08:27:54 -05:00
|
|
|
return 0, auth.ErrInvalidDuration
|
2019-10-23 01:59:13 -04:00
|
|
|
}
|
2019-11-30 00:37:42 -05:00
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
// The duration, in seconds, of the role session.
|
|
|
|
// The value can range from 900 seconds (15 minutes)
|
|
|
|
// to 12 hours.
|
|
|
|
if expirySecs < 900 || expirySecs > 43200 {
|
2019-11-29 08:27:54 -05:00
|
|
|
return 0, auth.ErrInvalidDuration
|
2019-10-23 01:59:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
defaultExpiryDuration = time.Duration(expirySecs) * time.Second
|
|
|
|
}
|
|
|
|
return defaultExpiryDuration, nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
expAt, err := auth.ExpToInt64(expStr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultExpiryDuration, err := GetDefaultExpiration(dsecs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify if JWT expiry is lesser than default expiry duration,
|
|
|
|
// if that is the case then set the default expiration to be
|
|
|
|
// from the JWT expiry claim.
|
|
|
|
if time.Unix(expAt, 0).UTC().Sub(time.Now().UTC()) < defaultExpiryDuration {
|
|
|
|
defaultExpiryDuration = time.Unix(expAt, 0).UTC().Sub(time.Now().UTC())
|
|
|
|
} // else honor the specified expiry duration.
|
|
|
|
|
|
|
|
expiry := time.Now().UTC().Add(defaultExpiryDuration).Unix()
|
|
|
|
claims["exp"] = strconv.FormatInt(expiry, 10) // update with new expiry.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
// Validate - validates the access token.
|
|
|
|
func (p *JWT) Validate(token, dsecs string) (map[string]interface{}, error) {
|
|
|
|
jp := new(jwtgo.Parser)
|
2020-04-10 17:59:52 -04:00
|
|
|
jp.ValidMethods = []string{
|
|
|
|
"RS256", "RS384", "RS512", "ES256", "ES384", "ES512",
|
|
|
|
"RS3256", "RS3384", "RS3512", "ES3256", "ES3384", "ES3512",
|
|
|
|
}
|
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"])
|
|
|
|
}
|
|
|
|
return p.publicKeys[kid], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var claims jwtgo.MapClaims
|
|
|
|
jwtToken, err := jp.ParseWithClaims(token, &claims, keyFuncCallback)
|
|
|
|
if err != nil {
|
|
|
|
if err = p.PopulatePublicKey(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
jwtToken, err = jwtgo.ParseWithClaims(token, &claims, keyFuncCallback)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !jwtToken.Valid {
|
|
|
|
return nil, ErrTokenExpired
|
|
|
|
}
|
|
|
|
|
2019-11-29 08:27:54 -05:00
|
|
|
if err = updateClaimsExpiry(dsecs, claims); err != nil {
|
2019-10-23 01:59:13 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return claims, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// ID returns the provider name and authentication type.
|
|
|
|
func (p *JWT) ID() ID {
|
|
|
|
return "jwt"
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenID keys and envs.
|
|
|
|
const (
|
|
|
|
JwksURL = "jwks_url"
|
|
|
|
ConfigURL = "config_url"
|
2020-01-08 20:21:58 -05:00
|
|
|
ClaimName = "claim_name"
|
2019-10-23 01:59:13 -04:00
|
|
|
ClaimPrefix = "claim_prefix"
|
2019-11-30 00:37:42 -05:00
|
|
|
ClientID = "client_id"
|
2019-10-23 01:59:13 -04:00
|
|
|
|
2019-11-30 00:37:42 -05:00
|
|
|
EnvIdentityOpenIDClientID = "MINIO_IDENTITY_OPENID_CLIENT_ID"
|
2019-10-23 01:59:13 -04:00
|
|
|
EnvIdentityOpenIDJWKSURL = "MINIO_IDENTITY_OPENID_JWKS_URL"
|
|
|
|
EnvIdentityOpenIDURL = "MINIO_IDENTITY_OPENID_CONFIG_URL"
|
2020-01-08 20:21:58 -05:00
|
|
|
EnvIdentityOpenIDClaimName = "MINIO_IDENTITY_OPENID_CLAIM_NAME"
|
2019-10-23 01:59:13 -04:00
|
|
|
EnvIdentityOpenIDClaimPrefix = "MINIO_IDENTITY_OPENID_CLAIM_PREFIX"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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"`
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseDiscoveryDoc(u *xnet.URL, transport *http.Transport, closeRespFn func(io.ReadCloser)) (DiscoveryDoc, error) {
|
|
|
|
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 {
|
2020-01-21 05:46:58 -05:00
|
|
|
clnt.CloseIdleConnections()
|
2019-10-23 01:59:13 -04:00
|
|
|
return d, err
|
|
|
|
}
|
|
|
|
defer closeRespFn(resp.Body)
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return d, err
|
|
|
|
}
|
|
|
|
dec := json.NewDecoder(resp.Body)
|
|
|
|
if err = dec.Decode(&d); err != nil {
|
|
|
|
return d, err
|
|
|
|
}
|
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultKVS - default config for OpenID config
|
|
|
|
var (
|
|
|
|
DefaultKVS = config.KVS{
|
2019-11-20 18:10:24 -05:00
|
|
|
config.KV{
|
2019-11-26 13:08:25 -05:00
|
|
|
Key: ConfigURL,
|
2019-11-20 18:10:24 -05:00
|
|
|
Value: "",
|
|
|
|
},
|
2019-11-30 00:37:42 -05:00
|
|
|
config.KV{
|
|
|
|
Key: ClientID,
|
|
|
|
Value: "",
|
|
|
|
},
|
2020-01-08 20:21:58 -05:00
|
|
|
config.KV{
|
|
|
|
Key: ClaimName,
|
|
|
|
Value: iampolicy.PolicyName,
|
|
|
|
},
|
2019-11-20 18:10:24 -05:00
|
|
|
config.KV{
|
2019-11-26 13:08:25 -05:00
|
|
|
Key: ClaimPrefix,
|
2019-11-20 18:10:24 -05:00
|
|
|
Value: "",
|
|
|
|
},
|
|
|
|
config.KV{
|
2019-11-26 13:08:25 -05:00
|
|
|
Key: JwksURL,
|
2019-11-20 18:10:24 -05:00
|
|
|
Value: "",
|
|
|
|
},
|
2019-10-23 01:59:13 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-12-04 18:32:37 -05:00
|
|
|
// Enabled returns if jwks is enabled.
|
|
|
|
func Enabled(kvs config.KVS) bool {
|
|
|
|
return kvs.Get(JwksURL) != ""
|
|
|
|
}
|
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
// LookupConfig lookup jwks from config, override with any ENVs.
|
2019-11-13 20:38:05 -05:00
|
|
|
func LookupConfig(kvs config.KVS, transport *http.Transport, closeRespFn func(io.ReadCloser)) (c Config, err error) {
|
|
|
|
if err = config.CheckValidKeys(config.IdentityOpenIDSubSys, kvs, DefaultKVS); err != nil {
|
2019-10-23 01:59:13 -04:00
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
|
|
|
|
jwksURL := env.Get(EnvIamJwksURL, "") // Legacy
|
|
|
|
if jwksURL == "" {
|
2019-11-13 20:38:05 -05:00
|
|
|
jwksURL = env.Get(EnvIdentityOpenIDJWKSURL, kvs.Get(JwksURL))
|
2019-10-23 01:59:13 -04:00
|
|
|
}
|
|
|
|
|
2019-11-11 12:31:46 -05:00
|
|
|
c = Config{
|
2020-01-08 20:21:58 -05:00
|
|
|
ClaimName: env.Get(EnvIdentityOpenIDClaimName, kvs.Get(ClaimName)),
|
2019-11-13 20:38:05 -05:00
|
|
|
ClaimPrefix: env.Get(EnvIdentityOpenIDClaimPrefix, kvs.Get(ClaimPrefix)),
|
2019-11-11 12:31:46 -05:00
|
|
|
publicKeys: make(map[string]crypto.PublicKey),
|
2019-11-30 00:37:42 -05:00
|
|
|
ClientID: env.Get(EnvIdentityOpenIDClientID, kvs.Get(ClientID)),
|
2019-11-11 12:31:46 -05:00
|
|
|
transport: transport,
|
|
|
|
closeRespFn: closeRespFn,
|
|
|
|
}
|
|
|
|
|
2019-11-13 20:38:05 -05:00
|
|
|
configURL := env.Get(EnvIdentityOpenIDURL, kvs.Get(ConfigURL))
|
2019-10-23 01:59:13 -04:00
|
|
|
if configURL != "" {
|
2019-10-31 02:39:09 -04:00
|
|
|
c.URL, err = xnet.ParseHTTPURL(configURL)
|
2019-10-23 01:59:13 -04:00
|
|
|
if err != nil {
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
c.DiscoveryDoc, err = parseDiscoveryDoc(c.URL, transport, closeRespFn)
|
|
|
|
if err != nil {
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
}
|
2020-01-10 19:57:18 -05:00
|
|
|
|
|
|
|
if c.ClaimName == "" {
|
|
|
|
c.ClaimName = iampolicy.PolicyName
|
|
|
|
}
|
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
if jwksURL == "" {
|
|
|
|
// Fallback to discovery document jwksURL
|
|
|
|
jwksURL = c.DiscoveryDoc.JwksURI
|
|
|
|
}
|
2019-10-30 03:04:39 -04:00
|
|
|
|
|
|
|
if jwksURL == "" {
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2019-10-31 02:39:09 -04:00
|
|
|
c.JWKS.URL, err = xnet.ParseHTTPURL(jwksURL)
|
2019-10-30 03:04:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return c, err
|
|
|
|
}
|
2020-01-08 20:21:58 -05:00
|
|
|
|
2019-10-30 03:04:39 -04:00
|
|
|
if err = c.PopulatePublicKey(); err != nil {
|
|
|
|
return c, err
|
|
|
|
}
|
2020-01-08 20:21:58 -05:00
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewJWT - initialize new jwt authenticator.
|
|
|
|
func NewJWT(c Config) *JWT {
|
|
|
|
return &JWT{c}
|
|
|
|
}
|