forked from qvest-digital/loginsrv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
88 lines (70 loc) · 2.17 KB
/
main_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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"github.com/dgrijalva/jwt-go"
. "github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"os"
"strings"
"testing"
"time"
"github.com/learnfromgirls/loginsrv/logging"
"github.com/tarent/logrus"
)
func Test_BasicEndToEnd(t *testing.T) {
logging.Logger.Level = logrus.InfoLevel
//can create tmp file
d1 := []byte("dummy:secret\n")
err3 := ioutil.WriteFile("/tmp/.htpasswd", d1, 0644)
NoError(t, err3)
originalArgs := os.Args
secret := "theSecret"
os.Args = []string{"loginsrv", "-jwt-secret", secret, "-host=localhost", "-port=3000", "-simple=bob=secret", "-htpasswd=files=/tmp/.htpasswd"}
defer func() { os.Args = originalArgs }()
go main()
time.Sleep(time.Second)
// success
req, err := http.NewRequest("POST", "http://localhost:3000/login", strings.NewReader(`{"username": "bob", "password": "secret"}`))
NoError(t, err)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/jwt")
r, err := http.DefaultClient.Do(req)
NoError(t, err)
Equal(t, 200, r.StatusCode)
Equal(t, "application/jwt", r.Header.Get("Content-Type"))
b, err := ioutil.ReadAll(r.Body)
NoError(t, err)
token, err := jwt.Parse(string(b), func(*jwt.Token) (interface{}, error) {
return []byte(secret), nil
})
NoError(t, err)
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
Equal(t, "bob", claims["sub"])
} else {
t.Fail()
}
subArgon2unseal(t)
}
func subArgon2unseal(t *testing.T) {
secret := "theSecret"
// success
req, err := http.NewRequest("POST", "http://localhost:3000/login", strings.NewReader(`{"username": "vault", "password": "secret"}`))
NoError(t, err)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/jwt")
r, err := http.DefaultClient.Do(req)
NoError(t, err)
Equal(t, 200, r.StatusCode)
Equal(t, "application/jwt", r.Header.Get("Content-Type"))
b, err := ioutil.ReadAll(r.Body)
NoError(t, err)
token, err := jwt.Parse(string(b), func(*jwt.Token) (interface{}, error) {
return []byte(secret), nil
})
NoError(t, err)
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
Equal(t, "vault", claims["sub"])
} else {
t.Fail()
}
}