You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
124 lines
3.8 KiB
124 lines
3.8 KiB
8 years ago
|
/*
|
||
8 years ago
|
* Minio Cloud Storage, (C) 2015, 2016, 2017 Minio, Inc.
|
||
8 years ago
|
*
|
||
|
* 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.
|
||
|
*/
|
||
|
|
||
7 years ago
|
package auth
|
||
8 years ago
|
|
||
|
import (
|
||
|
"crypto/rand"
|
||
7 years ago
|
"crypto/subtle"
|
||
8 years ago
|
"encoding/base64"
|
||
7 years ago
|
"fmt"
|
||
8 years ago
|
)
|
||
|
|
||
|
const (
|
||
8 years ago
|
// Minimum length for Minio access key.
|
||
|
accessKeyMinLen = 5
|
||
|
|
||
|
// Maximum length for Minio access key.
|
||
7 years ago
|
// There is no max length enforcement for access keys
|
||
8 years ago
|
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.
|
||
7 years ago
|
// There is no max length enforcement for secret keys
|
||
|
secretKeyMaxLen = 40
|
||
8 years ago
|
|
||
|
// Alpha numeric table used for generating access keys.
|
||
|
alphaNumericTable = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||
|
|
||
|
// Total length of the alpha numeric table.
|
||
|
alphaNumericTableLen = byte(len(alphaNumericTable))
|
||
8 years ago
|
)
|
||
|
|
||
8 years ago
|
// Common errors generated for access and secret key validation.
|
||
8 years ago
|
var (
|
||
7 years ago
|
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)
|
||
8 years ago
|
)
|
||
8 years ago
|
|
||
7 years ago
|
// IsAccessKeyValid - validate access key for right length.
|
||
|
func IsAccessKeyValid(accessKey string) bool {
|
||
7 years ago
|
return len(accessKey) >= accessKeyMinLen
|
||
8 years ago
|
}
|
||
|
|
||
|
// isSecretKeyValid - validate secret key for right length.
|
||
|
func isSecretKeyValid(secretKey string) bool {
|
||
7 years ago
|
return len(secretKey) >= secretKeyMinLen
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
// Credentials holds access and secret keys.
|
||
|
type Credentials struct {
|
||
7 years ago
|
AccessKey string `json:"accessKey,omitempty"`
|
||
|
SecretKey string `json:"secretKey,omitempty"`
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
// IsValid - returns whether credential is valid or not.
|
||
7 years ago
|
func (cred Credentials) IsValid() bool {
|
||
|
return IsAccessKeyValid(cred.AccessKey) && isSecretKeyValid(cred.SecretKey)
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
// Equal - returns whether two credentials are equal or not.
|
||
|
func (cred Credentials) Equal(ccred Credentials) bool {
|
||
8 years ago
|
if !ccred.IsValid() {
|
||
|
return false
|
||
8 years ago
|
}
|
||
7 years ago
|
return cred.AccessKey == ccred.AccessKey && subtle.ConstantTimeCompare([]byte(cred.SecretKey), []byte(ccred.SecretKey)) == 1
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
7 years ago
|
// MustGetNewCredentials generates and returns new credential.
|
||
|
func MustGetNewCredentials() (cred Credentials) {
|
||
|
readBytes := func(size int) (data []byte) {
|
||
|
data = make([]byte, size)
|
||
|
if n, err := rand.Read(data); err != nil {
|
||
|
panic(err)
|
||
|
} else if n != size {
|
||
|
panic(fmt.Errorf("not enough data read. expected: %v, got: %v", size, n))
|
||
|
}
|
||
|
return
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
7 years ago
|
// Generate access key.
|
||
|
keyBytes := readBytes(accessKeyMaxLen)
|
||
|
for i := 0; i < accessKeyMaxLen; i++ {
|
||
8 years ago
|
keyBytes[i] = alphaNumericTable[keyBytes[i]%alphaNumericTableLen]
|
||
|
}
|
||
7 years ago
|
cred.AccessKey = string(keyBytes)
|
||
8 years ago
|
|
||
8 years ago
|
// Generate secret key.
|
||
7 years ago
|
keyBytes = readBytes(secretKeyMaxLen)
|
||
|
cred.SecretKey = string([]byte(base64.StdEncoding.EncodeToString(keyBytes))[:secretKeyMaxLen])
|
||
8 years ago
|
|
||
7 years ago
|
return cred
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
// 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
|
||
|
return cred, nil
|
||
8 years ago
|
}
|