@@ -2,8 +2,12 @@ package mfa
2
2
3
3
import (
4
4
"bytes"
5
+ "crypto/aes"
6
+ "crypto/rand"
7
+ "encoding/base64"
5
8
"encoding/json"
6
9
"fmt"
10
+ "io"
7
11
"net/http"
8
12
"regexp"
9
13
"testing"
@@ -13,6 +17,8 @@ import (
13
17
)
14
18
15
19
func TestApiKey_IsCorrect (t * testing.T ) {
20
+ const hashedSecret = "$2y$10$Y.FlUK8q//DfybgFzNG2lONaJwvEFxHnCRo/r60BZbITDT6rOUhGa"
21
+
16
22
tests := []struct {
17
23
name string
18
24
HashedSecret string
@@ -22,21 +28,21 @@ func TestApiKey_IsCorrect(t *testing.T) {
22
28
}{
23
29
{
24
30
name : "valid secret" ,
25
- HashedSecret : "$2y$10$Y.FlUK8q//DfybgFzNG2lONaJwvEFxHnCRo/r60BZbITDT6rOUhGa" ,
31
+ HashedSecret : hashedSecret ,
26
32
ActivatedAt : 1744896576000 ,
27
33
Given : "abc123" ,
28
34
wantErr : false ,
29
35
},
30
36
{
31
37
name : "invalid secret" ,
32
- HashedSecret : "$2y$10$Y.FlUK8q//DfybgFzNG2lONaJwvEFxHnCRo/r60BZbITDT6rOUhGa" ,
38
+ HashedSecret : hashedSecret ,
33
39
ActivatedAt : 1744896576000 ,
34
40
Given : "123abc" ,
35
41
wantErr : true ,
36
42
},
37
43
{
38
44
name : "inactive" ,
39
- HashedSecret : "$2y$10$Y.FlUK8q//DfybgFzNG2lONaJwvEFxHnCRo/r60BZbITDT6rOUhGa" ,
45
+ HashedSecret : hashedSecret ,
40
46
ActivatedAt : 0 ,
41
47
Given : "abc123" ,
42
48
wantErr : true ,
@@ -137,7 +143,7 @@ func TestApiKey_EncryptDecrypt(t *testing.T) {
137
143
}
138
144
}
139
145
140
- func (ms * MfaSuite ) TestApiKey_EncryptDecryptLegacy () {
146
+ func (ms * MfaSuite ) TestApiKeyEncryptDecryptLegacy () {
141
147
plaintext := []byte ("this is a plaintext string to be encrypted" )
142
148
key := & ApiKey {Secret : "ED86600E-3DBF-4C23-A0DA-9C55D448" }
143
149
@@ -333,6 +339,49 @@ func (ms *MfaSuite) TestNewApiKey() {
333
339
ms .Regexp (regexp .MustCompile ("[a-f0-9]{40}" ), got )
334
340
}
335
341
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
+
336
385
func (ms * MfaSuite ) TestApiKeyReEncrypt () {
337
386
oldKey := ApiKey {}
338
387
must (oldKey .Activate ())
0 commit comments