-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapka_test.go
64 lines (54 loc) · 1.68 KB
/
capka_test.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
package capka
import (
"bytes"
"encoding/base64"
"reflect"
"testing"
"time"
"github.com/go-faster/errors"
"github.com/jamesruan/sodium"
)
func TestCAPKA(t *testing.T) {
username := "alice"
password := "hunter2"
domain := "example.org"
kp, err := MakeKP(username, password, domain)
if err != nil {
t.Fatal(errors.Wrap(err, "could not create keypair"))
}
pkWant := "2ajG pbyrnXGTjQB8TEpdERkOHcCea9xGSj0tnw/ogM="
pkHave := base64.StdEncoding.EncodeToString(kp.PublicKey.Bytes)
if pkWant != pkHave {
t.Fatalf("public key mismatch: want %s, have %s", pkWant, pkHave)
}
nonce, err := base64.StdEncoding.DecodeString(GetNonce(32, time.Second*5))
if err != nil {
t.Fatal(errors.Wrap(err, "could not decode nonce"))
}
dataWant, eph := NewLoginData(username, nonce)
raw, err := dataWant.EncodeJSON(kp.SecretKey)
if err != nil {
t.Fatal(errors.Wrap(err, "could not encode login data"))
}
req, err := DecodeLoginRequestJSON(bytes.NewReader(raw))
if err != nil {
t.Fatal(errors.Wrap(err, "could not decode login request JSON"))
}
dataHave, err := req.Decode(kp.PublicKey)
if err != nil {
t.Fatal(errors.Wrap(err, "could not decode login request"))
}
if !reflect.DeepEqual(dataWant, dataHave) {
t.Fatalf("original and decoded login data mismatch:\nwant: % v\nhave: % v", dataWant, dataHave)
}
secureDataWant := sodium.Bytes(RandomBytes(32))
secureDataHave, err := Decrypt(dataHave.Encrypt(secureDataWant), eph)
if err != nil {
t.Fatal(errors.Wrap(err, "could not decrypt secure data"))
}
if !reflect.DeepEqual(secureDataWant, secureDataHave) {
t.Fatalf(
"original and decrypted secure data mismatch:\nwant: % v\nhave: % v",
secureDataWant, secureDataHave)
}
}