Skip to content

Commit 1d0d642

Browse files
committed
Merge branch 'develop' into feature/rotate-api-key
2 parents f64c318 + 3b0ae9e commit 1d0d642

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

apikey.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (k *ApiKey) Hash() error {
5252
return err
5353
}
5454

55-
// IsCorrect returns true if and only if the given string is a match for HashedSecret
55+
// IsCorrect returns true if and only if the key is active and the given string is a match for HashedSecret
5656
func (k *ApiKey) IsCorrect(given string) error {
5757
if k.ActivatedAt == 0 {
5858
return fmt.Errorf("key is not active: %s", k.Key)

apikey_test.go

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ package mfa
22

33
import (
44
"bytes"
5+
"crypto/aes"
6+
"crypto/rand"
7+
"encoding/base64"
58
"encoding/json"
69
"fmt"
10+
"io"
711
"net/http"
812
"regexp"
913
"testing"
@@ -13,6 +17,8 @@ import (
1317
)
1418

1519
func TestApiKey_IsCorrect(t *testing.T) {
20+
const hashedSecret = "$2y$10$Y.FlUK8q//DfybgFzNG2lONaJwvEFxHnCRo/r60BZbITDT6rOUhGa"
21+
1622
tests := []struct {
1723
name string
1824
HashedSecret string
@@ -22,21 +28,21 @@ func TestApiKey_IsCorrect(t *testing.T) {
2228
}{
2329
{
2430
name: "valid secret",
25-
HashedSecret: "$2y$10$Y.FlUK8q//DfybgFzNG2lONaJwvEFxHnCRo/r60BZbITDT6rOUhGa",
31+
HashedSecret: hashedSecret,
2632
ActivatedAt: 1744896576000,
2733
Given: "abc123",
2834
wantErr: false,
2935
},
3036
{
3137
name: "invalid secret",
32-
HashedSecret: "$2y$10$Y.FlUK8q//DfybgFzNG2lONaJwvEFxHnCRo/r60BZbITDT6rOUhGa",
38+
HashedSecret: hashedSecret,
3339
ActivatedAt: 1744896576000,
3440
Given: "123abc",
3541
wantErr: true,
3642
},
3743
{
3844
name: "inactive",
39-
HashedSecret: "$2y$10$Y.FlUK8q//DfybgFzNG2lONaJwvEFxHnCRo/r60BZbITDT6rOUhGa",
45+
HashedSecret: hashedSecret,
4046
ActivatedAt: 0,
4147
Given: "abc123",
4248
wantErr: true,
@@ -137,7 +143,7 @@ func TestApiKey_EncryptDecrypt(t *testing.T) {
137143
}
138144
}
139145

140-
func (ms *MfaSuite) TestApiKey_EncryptDecryptLegacy() {
146+
func (ms *MfaSuite) TestApiKeyEncryptDecryptLegacy() {
141147
plaintext := []byte("this is a plaintext string to be encrypted")
142148
key := &ApiKey{Secret: "ED86600E-3DBF-4C23-A0DA-9C55D448"}
143149

@@ -333,6 +339,49 @@ func (ms *MfaSuite) TestNewApiKey() {
333339
ms.Regexp(regexp.MustCompile("[a-f0-9]{40}"), got)
334340
}
335341

342+
func (ms *MfaSuite) TestNewCipherBlock() {
343+
random := make([]byte, 32)
344+
_, err := io.ReadFull(rand.Reader, random)
345+
ms.NoError(err)
346+
347+
tests := []struct {
348+
name string
349+
key string
350+
wantErr bool
351+
}{
352+
{
353+
name: "key too short",
354+
key: "0123456789012345678901234567890",
355+
wantErr: true,
356+
},
357+
{
358+
name: "key too long",
359+
key: "012345678901234567890123456789012",
360+
wantErr: true,
361+
},
362+
{
363+
name: "raw",
364+
key: string(random),
365+
},
366+
{
367+
name: "base64",
368+
key: base64.StdEncoding.EncodeToString(random),
369+
},
370+
}
371+
for _, tt := range tests {
372+
ms.Run(tt.name, func() {
373+
got, err := newCipherBlock(tt.key)
374+
if tt.wantErr {
375+
ms.Error(err)
376+
return
377+
}
378+
379+
ms.NoError(err)
380+
ms.Equal(aes.BlockSize, got.BlockSize())
381+
})
382+
}
383+
}
384+
336385
func (ms *MfaSuite) TestApiKeyReEncrypt() {
337386
oldKey := ApiKey{}
338387
must(oldKey.Activate())

0 commit comments

Comments
 (0)