-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapikey.go
159 lines (129 loc) · 4 KB
/
apikey.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package mfa
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"fmt"
"io"
"github.com/pkg/errors"
"golang.org/x/crypto/bcrypt"
)
const ApiKeyTablePK = "value"
type ApiKey struct {
Key string `json:"value"`
Secret string `json:"-"`
HashedSecret string `json:"hashedApiSecret"`
Email string `json:"email"`
CreatedAt int `json:"createdAt"`
ActivatedAt int `json:"activatedAt"`
Store *Storage `json:"-"`
}
func (k *ApiKey) Load() error {
return k.Store.Load(envConfig.ApiKeyTable, ApiKeyTablePK, k.Key, k)
}
// Hash - Generate bcrypt hash from Secret and store in HashedSecret
func (k *ApiKey) Hash() error {
if k.Secret == "" {
return errors.New("empty secret cannot be hashed")
}
hashedSecret, err := bcrypt.GenerateFromPassword([]byte(k.Secret), bcrypt.DefaultCost)
k.HashedSecret = string(hashedSecret)
return err
}
func (k *ApiKey) IsCorrect(given string) (bool, error) {
if given == "" {
return false, errors.New("secret to compare cannot be empty")
}
if k.HashedSecret == "" {
return false, errors.New("cannot compare with empty hashed secret")
}
err := bcrypt.CompareHashAndPassword([]byte(k.HashedSecret), []byte(given))
if err != nil {
return false, err
}
return true, nil
}
func (k *ApiKey) Encrypt(plaintext []byte) ([]byte, error) {
var sec []byte
var err error
sec, err = base64.StdEncoding.DecodeString(k.Secret)
if err != nil {
sec = []byte(k.Secret)
}
// create cipher block with api secret as aes key
block, err := aes.NewCipher(sec)
if err != nil {
return []byte{}, err
}
// byte array to hold encrypted content
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
// The IV needs to be unique, but not secure. Therefore it's common to
// include it at the beginning of the ciphertext.
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return []byte{}, err
}
// use CTR to encrypt plaintext
stream := cipher.NewCTR(block, iv)
stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
return ciphertext, nil
}
func (k *ApiKey) Decrypt(ciphertext []byte) ([]byte, error) {
var sec []byte
var err error
sec, err = base64.StdEncoding.DecodeString(k.Secret)
if err != nil {
sec = []byte(k.Secret)
}
block, err := aes.NewCipher(sec)
if err != nil {
return []byte{}, errors.Wrap(err, "failed to create new cipher")
}
// plaintext will hold decrypted content, it must be at least as long
// as ciphertext or decryption process will panic
plaintext := make([]byte, len(ciphertext))
// get iv from encrypted content
iv := ciphertext[:aes.BlockSize]
// use CTR to decrypt content
stream := cipher.NewCTR(block, iv)
stream.XORKeyStream(plaintext, ciphertext[aes.BlockSize:])
return plaintext, nil
}
func (k *ApiKey) DecryptLegacy(ciphertext []byte) ([]byte, error) {
var sec []byte
var err error
sec, err = base64.StdEncoding.DecodeString(k.Secret)
if err != nil {
sec = []byte(k.Secret)
}
block, err := aes.NewCipher(sec)
if err != nil {
return []byte{}, errors.Wrap(err, "failed to create new cipher")
}
// data was encrypted, then base64 encoded, then joined with a :, need to split
// on :, then decode first part as iv and second as encrypted content
parts := bytes.Split(ciphertext, []byte(":"))
if len(parts) != 2 {
return nil, fmt.Errorf("ciphertext does not look like legacy data")
}
iv := make([]byte, aes.BlockSize)
_, err = base64.StdEncoding.Decode(iv, parts[0])
if err != nil {
fmt.Printf("unable to decode iv: %s\n", err)
return nil, err
}
decodedCipher, err := base64.StdEncoding.DecodeString(string(parts[1]))
if err != nil {
fmt.Printf("unable to decode ciphertext: %s\n", err)
return nil, err
}
// plaintext will hold decrypted content, it must be at least as long
// as ciphertext or decryption process will panic
plaintext := make([]byte, len(decodedCipher))
// use CTR to decrypt content
stream := cipher.NewCTR(block, iv)
stream.XORKeyStream(plaintext, decodedCipher)
return plaintext, nil
}