2016-12-26 19:21:23 +01:00
|
|
|
/*
|
2018-10-09 23:00:01 +02:00
|
|
|
* Minio Cloud Storage, (C) 2015, 2016, 2017, 2018 Minio, Inc.
|
2016-12-26 19:21:23 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2017-10-31 19:54:32 +01:00
|
|
|
package auth
|
2016-12-26 19:21:23 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
2017-08-24 00:59:37 +02:00
|
|
|
"crypto/subtle"
|
2016-12-26 19:21:23 +01:00
|
|
|
"encoding/base64"
|
2017-10-31 19:54:32 +01:00
|
|
|
"fmt"
|
2018-10-09 23:00:01 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
jwtgo "github.com/dgrijalva/jwt-go"
|
2016-12-26 19:21:23 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-06-06 00:18:03 +02:00
|
|
|
// Minimum length for Minio access key.
|
2018-02-02 04:43:30 +01:00
|
|
|
accessKeyMinLen = 3
|
2017-06-06 00:18:03 +02:00
|
|
|
|
|
|
|
// Maximum length for Minio access key.
|
2017-08-04 05:03:37 +02:00
|
|
|
// There is no max length enforcement for access keys
|
2017-06-06 00:18:03 +02:00
|
|
|
accessKeyMaxLen = 20
|
|
|
|
|
|
|
|
// Minimum length for Minio secret key for both server and gateway mode.
|
|
|
|
secretKeyMinLen = 8
|
|
|
|
|
|
|
|
// Maximum secret key length for Minio, this
|
|
|
|
// is used when autogenerating new credentials.
|
2017-08-04 05:03:37 +02:00
|
|
|
// There is no max length enforcement for secret keys
|
|
|
|
secretKeyMaxLen = 40
|
2017-06-06 00:18:03 +02:00
|
|
|
|
|
|
|
// Alpha numeric table used for generating access keys.
|
|
|
|
alphaNumericTable = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
|
|
|
|
// Total length of the alpha numeric table.
|
|
|
|
alphaNumericTableLen = byte(len(alphaNumericTable))
|
2016-12-26 19:21:23 +01:00
|
|
|
)
|
|
|
|
|
2017-06-06 00:18:03 +02:00
|
|
|
// Common errors generated for access and secret key validation.
|
2017-03-16 08:16:06 +01:00
|
|
|
var (
|
2017-10-31 19:54:32 +01:00
|
|
|
ErrInvalidAccessKeyLength = fmt.Errorf("access key must be minimum %v or more characters long", accessKeyMinLen)
|
|
|
|
ErrInvalidSecretKeyLength = fmt.Errorf("secret key must be minimum %v or more characters long", secretKeyMinLen)
|
2017-03-16 08:16:06 +01:00
|
|
|
)
|
2017-03-16 20:21:58 +01:00
|
|
|
|
2017-10-31 19:54:32 +01:00
|
|
|
// IsAccessKeyValid - validate access key for right length.
|
|
|
|
func IsAccessKeyValid(accessKey string) bool {
|
2017-08-04 05:03:37 +02:00
|
|
|
return len(accessKey) >= accessKeyMinLen
|
2016-12-26 19:21:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// isSecretKeyValid - validate secret key for right length.
|
|
|
|
func isSecretKeyValid(secretKey string) bool {
|
2017-08-04 05:03:37 +02:00
|
|
|
return len(secretKey) >= secretKeyMinLen
|
2016-12-26 19:21:23 +01:00
|
|
|
}
|
|
|
|
|
2017-10-31 19:54:32 +01:00
|
|
|
// Credentials holds access and secret keys.
|
|
|
|
type Credentials struct {
|
2018-10-09 23:00:01 +02:00
|
|
|
AccessKey string `xml:"AccessKeyId" json:"accessKey,omitempty"`
|
|
|
|
SecretKey string `xml:"SecretAccessKey" json:"secretKey,omitempty"`
|
|
|
|
Expiration time.Time `xml:"Expiration" json:"expiration,omitempty"`
|
|
|
|
SessionToken string `xml:"SessionToken" json:"sessionToken,omitempty"`
|
|
|
|
Status string `xml:"-" json:"status,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsExpired - returns whether Credential is expired or not.
|
|
|
|
func (cred Credentials) IsExpired() bool {
|
|
|
|
if cred.Expiration.IsZero() || cred.Expiration == timeSentinel {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return cred.Expiration.Before(time.Now().UTC())
|
2017-01-27 01:51:51 +01:00
|
|
|
}
|
|
|
|
|
2017-03-16 08:16:06 +01:00
|
|
|
// IsValid - returns whether credential is valid or not.
|
2017-10-31 19:54:32 +01:00
|
|
|
func (cred Credentials) IsValid() bool {
|
2018-10-09 23:00:01 +02:00
|
|
|
// Verify credentials if its enabled or not set.
|
|
|
|
if cred.Status == "enabled" || cred.Status == "" {
|
|
|
|
return IsAccessKeyValid(cred.AccessKey) && isSecretKeyValid(cred.SecretKey) && !cred.IsExpired()
|
|
|
|
}
|
|
|
|
return false
|
2017-03-16 00:30:34 +01:00
|
|
|
}
|
|
|
|
|
2017-10-31 19:54:32 +01:00
|
|
|
// Equal - returns whether two credentials are equal or not.
|
|
|
|
func (cred Credentials) Equal(ccred Credentials) bool {
|
2017-03-16 08:16:06 +01:00
|
|
|
if !ccred.IsValid() {
|
|
|
|
return false
|
2017-01-27 01:51:51 +01:00
|
|
|
}
|
2018-10-09 23:00:01 +02:00
|
|
|
return (cred.AccessKey == ccred.AccessKey && subtle.ConstantTimeCompare([]byte(cred.SecretKey), []byte(ccred.SecretKey)) == 1 &&
|
|
|
|
subtle.ConstantTimeCompare([]byte(cred.SessionToken), []byte(ccred.SessionToken)) == 1)
|
2016-12-26 19:21:23 +01:00
|
|
|
}
|
2016-12-27 17:28:10 +01:00
|
|
|
|
2018-10-09 23:00:01 +02:00
|
|
|
var timeSentinel = time.Unix(0, 0).UTC()
|
|
|
|
|
|
|
|
// GetNewCredentialsWithMetadata generates and returns new credential with expiry.
|
|
|
|
func GetNewCredentialsWithMetadata(m map[string]interface{}, tokenSecret string) (cred Credentials, err error) {
|
2018-04-20 02:24:43 +02:00
|
|
|
readBytes := func(size int) (data []byte, err error) {
|
2017-10-31 19:54:32 +01:00
|
|
|
data = make([]byte, size)
|
2018-04-20 02:24:43 +02:00
|
|
|
var n int
|
|
|
|
if n, err = rand.Read(data); err != nil {
|
|
|
|
return nil, err
|
2017-10-31 19:54:32 +01:00
|
|
|
} else if n != size {
|
2018-04-20 02:24:43 +02:00
|
|
|
return nil, fmt.Errorf("Not enough data. Expected to read: %v bytes, got: %v bytes", size, n)
|
2017-10-31 19:54:32 +01:00
|
|
|
}
|
2018-04-20 02:24:43 +02:00
|
|
|
return data, nil
|
2016-12-27 17:28:10 +01:00
|
|
|
}
|
2017-02-07 21:51:43 +01:00
|
|
|
|
2017-10-31 19:54:32 +01:00
|
|
|
// Generate access key.
|
2018-04-20 02:24:43 +02:00
|
|
|
keyBytes, err := readBytes(accessKeyMaxLen)
|
|
|
|
if err != nil {
|
|
|
|
return cred, err
|
|
|
|
}
|
2017-10-31 19:54:32 +01:00
|
|
|
for i := 0; i < accessKeyMaxLen; i++ {
|
2017-03-16 08:16:06 +01:00
|
|
|
keyBytes[i] = alphaNumericTable[keyBytes[i]%alphaNumericTableLen]
|
|
|
|
}
|
2017-10-31 19:54:32 +01:00
|
|
|
cred.AccessKey = string(keyBytes)
|
2017-02-07 21:51:43 +01:00
|
|
|
|
2017-03-16 08:16:06 +01:00
|
|
|
// Generate secret key.
|
2018-04-20 02:24:43 +02:00
|
|
|
keyBytes, err = readBytes(secretKeyMaxLen)
|
|
|
|
if err != nil {
|
|
|
|
return cred, err
|
|
|
|
}
|
2018-10-09 23:00:01 +02:00
|
|
|
cred.SecretKey = string([]byte(base64.URLEncoding.EncodeToString(keyBytes))[:secretKeyMaxLen])
|
|
|
|
cred.Status = "enabled"
|
|
|
|
|
|
|
|
expiry, ok := m["exp"].(float64)
|
|
|
|
if !ok {
|
|
|
|
cred.Expiration = timeSentinel
|
|
|
|
return cred, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
m["accessKey"] = cred.AccessKey
|
|
|
|
jwt := jwtgo.NewWithClaims(jwtgo.SigningMethodHS512, jwtgo.MapClaims(m))
|
|
|
|
|
|
|
|
cred.Expiration = time.Unix(int64(expiry), 0)
|
|
|
|
cred.SessionToken, err = jwt.SignedString([]byte(tokenSecret))
|
|
|
|
if err != nil {
|
|
|
|
return cred, err
|
|
|
|
}
|
2017-02-07 21:51:43 +01:00
|
|
|
|
2018-04-20 02:24:43 +02:00
|
|
|
return cred, nil
|
2017-08-04 05:03:37 +02:00
|
|
|
}
|
|
|
|
|
2018-10-09 23:00:01 +02:00
|
|
|
// GetNewCredentials generates and returns new credential.
|
|
|
|
func GetNewCredentials() (cred Credentials, err error) {
|
|
|
|
return GetNewCredentialsWithMetadata(map[string]interface{}{}, "")
|
|
|
|
}
|
|
|
|
|
2017-10-31 19:54:32 +01:00
|
|
|
// CreateCredentials returns new credential with the given access key and secret key.
|
|
|
|
// Error is returned if given access key or secret key are invalid length.
|
|
|
|
func CreateCredentials(accessKey, secretKey string) (cred Credentials, err error) {
|
|
|
|
if !IsAccessKeyValid(accessKey) {
|
|
|
|
return cred, ErrInvalidAccessKeyLength
|
|
|
|
}
|
|
|
|
if !isSecretKeyValid(secretKey) {
|
|
|
|
return cred, ErrInvalidSecretKeyLength
|
|
|
|
}
|
|
|
|
cred.AccessKey = accessKey
|
|
|
|
cred.SecretKey = secretKey
|
2018-10-09 23:00:01 +02:00
|
|
|
cred.Expiration = timeSentinel
|
|
|
|
cred.Status = "enabled"
|
2017-10-31 19:54:32 +01:00
|
|
|
return cred, nil
|
2016-12-27 17:28:10 +01:00
|
|
|
}
|