This repository has been archived by the owner on Jun 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
credential.go
109 lines (94 loc) · 3.34 KB
/
credential.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* Federator, (C) 2017 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 credentials
import (
"crypto/rand"
"crypto/subtle"
"encoding/base64"
"time"
)
const (
// Maximum length for Minio access key.
// There is no max length enforcement for access keys
accessKeyMaxLen = 20
// Maximum secret key length for Minio, this
// is used when autogenerating new Credentials.
// There is no max length enforcement for secret keys
secretKeyMaxLen = 40
// Alpha numeric table used for generating access keys.
alphaNumericTable = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
// Total length of the alpha numeric table.
alphaNumericTableLen = byte(len(alphaNumericTable))
)
// Credential - container for access and secret key, also carries
// expiration which indicates when the associated acces/secret keys
// are going to be expired.
type Credential struct {
AccessKey string `xml:"AccessKeyId,omitempty" json:"accessKey,omitempty"`
SecretKey string `xml:"SecretAccessKey,omitempty" json:"secretKey,omitempty"`
Expiration time.Time `xml:"Expiration,omitempty" json:"expiration,omitempty"`
}
// IsExpired - returns whether Credential is expired or not.
func (c Credential) IsExpired() bool {
if c.Expiration.IsZero() || c.Expiration == timeSentinel {
return false
}
return c.Expiration.Before(UTCNow())
}
// Equal - returns whether two Credentials are equal or not.
func (c Credential) Equal(cc Credential) bool {
if !cc.IsExpired() {
return false
}
return c.AccessKey == cc.AccessKey && subtle.ConstantTimeCompare([]byte(c.SecretKey), []byte(cc.SecretKey)) == 1
}
// NewCredential is similar to NewCredentialWithExpiration but the keys do not expire.
func NewCredential() (Credential, error) {
return NewCredentialWithExpiration(timeSentinel)
}
// NewCredentialWithExpiration returns new Credentials for the requested access key
// and secret key length. Returns an error if there was an error in generating
// credentials.Optionally expiration can be set to signify the validity of these
// generated credentials.
func NewCredentialWithExpiration(expiration time.Time) (Credential, error) {
// Generate access key.
keyBytes := make([]byte, accessKeyMaxLen)
_, err := rand.Read(keyBytes)
if err != nil {
return Credential{}, err
}
for i := 0; i < accessKeyMaxLen; i++ {
keyBytes[i] = alphaNumericTable[keyBytes[i]%alphaNumericTableLen]
}
accessKey := string(keyBytes)
// Generate secret key.
keyBytes = make([]byte, secretKeyMaxLen)
_, err = rand.Read(keyBytes)
if err != nil {
return Credential{}, err
}
secretKey := string([]byte(base64.StdEncoding.EncodeToString(keyBytes))[:secretKeyMaxLen])
return Credential{
AccessKey: accessKey,
SecretKey: secretKey,
Expiration: expiration,
}, nil
}
var timeSentinel = time.Unix(0, 0).UTC()
// UTCNow - returns current UTC time.
func UTCNow() time.Time {
return time.Now().UTC()
}