server_test.go 3.39 KB
Newer Older
1
2
3
package sessiontest

import (
4
	"net/http"
5
	"path/filepath"
6
	"testing"
7
8
9
	"time"

	"github.com/privacybydesign/irmago/internal/test"
Sietse Ringers's avatar
Sietse Ringers committed
10
11
	"github.com/privacybydesign/irmago/server"
	"github.com/privacybydesign/irmago/server/irmaserver"
Sietse Ringers's avatar
Sietse Ringers committed
12
	"github.com/privacybydesign/irmago/server/requestorserver"
13
	"github.com/sirupsen/logrus"
14
	"github.com/stretchr/testify/require"
15
	"github.com/x-cray/logrus-prefixed-formatter"
16
17
)

18
var (
Sietse Ringers's avatar
Sietse Ringers committed
19
20
21
	httpServer      *http.Server
	irmaServer      *irmaserver.Server
	requestorServer *requestorserver.Server
22

23
24
25
	logger   = logrus.New()
	testdata = test.FindTestdataFolder(nil)
)
26

27
func init() {
28
	logger.Level = logrus.ErrorLevel
29
	logger.Formatter = &prefixed.TextFormatter{ForceFormatting: true, ForceColors: true}
30
}
31

Sietse Ringers's avatar
Sietse Ringers committed
32
func StartRequestorServer(configuration *requestorserver.Configuration) {
33
	go func() {
34
		var err error
Sietse Ringers's avatar
Sietse Ringers committed
35
		if requestorServer, err = requestorserver.New(configuration); err != nil {
36
37
			panic(err)
		}
Sietse Ringers's avatar
Sietse Ringers committed
38
		if err = requestorServer.Start(configuration); err != nil {
39
40
			panic("Starting server failed: " + err.Error())
		}
41
	}()
42
	time.Sleep(100 * time.Millisecond) // Give server time to start
43
44
}

Sietse Ringers's avatar
Sietse Ringers committed
45
func StopRequestorServer() {
46
	requestorServer.Stop()
47
48
}

Sietse Ringers's avatar
Sietse Ringers committed
49
func StartIrmaServer(t *testing.T) {
50
51
52
	testdata := test.FindTestdataFolder(t)

	logger := logrus.New()
53
	logger.Level = logrus.ErrorLevel
54
55
56
	logger.Formatter = &logrus.TextFormatter{}

	var err error
57
	irmaServer, err = irmaserver.New(&server.Configuration{
58
59
60
61
62
63
64
65
66
		URL:                   "http://localhost:48680",
		Logger:                logger,
		SchemesPath:           filepath.Join(testdata, "irma_configuration"),
		IssuerPrivateKeysPath: filepath.Join(testdata, "privatekeys"),
	})

	require.NoError(t, err)

	mux := http.NewServeMux()
Sietse Ringers's avatar
Sietse Ringers committed
67
	mux.HandleFunc("/", irmaServer.HandlerFunc())
68
69
70
71
72
73
	httpServer = &http.Server{Addr: ":48680", Handler: mux}
	go func() {
		_ = httpServer.ListenAndServe()
	}()
}

Sietse Ringers's avatar
Sietse Ringers committed
74
func StopIrmaServer() {
75
	_ = httpServer.Close()
76
77
}

Sietse Ringers's avatar
Sietse Ringers committed
78
var IrmaServerConfiguration = &requestorserver.Configuration{
79
	Configuration: &server.Configuration{
Sietse Ringers's avatar
Sietse Ringers committed
80
		URL:                   "http://localhost:48682/irma",
81
		Logger:                logger,
82
		SchemesPath:           filepath.Join(testdata, "irma_configuration"),
83
84
		IssuerPrivateKeysPath: filepath.Join(testdata, "privatekeys"),
	},
85
	DisableRequestorAuthentication: true,
86
	Port: 48682,
87
88
}

Sietse Ringers's avatar
Sietse Ringers committed
89
var JwtServerConfiguration = &requestorserver.Configuration{
90
	Configuration: &server.Configuration{
Sietse Ringers's avatar
Sietse Ringers committed
91
		URL:                   "http://localhost:48682/irma",
92
		Logger:                logger,
93
		SchemesPath:           filepath.Join(testdata, "irma_configuration"),
94
95
96
		IssuerPrivateKeysPath: filepath.Join(testdata, "privatekeys"),
	},
	Port: 48682,
97
	DisableRequestorAuthentication: false,
98
	MaxRequestAge:                  3,
Sietse Ringers's avatar
Sietse Ringers committed
99
	Permissions: requestorserver.Permissions{
100
101
102
103
		Disclosing: []string{"*"},
		Signing:    []string{"*"},
		Issuing:    []string{"*"},
	},
Sietse Ringers's avatar
Sietse Ringers committed
104
	Requestors: map[string]requestorserver.Requestor{
Sietse Ringers's avatar
Sietse Ringers committed
105
		"requestor1": {
Sietse Ringers's avatar
Sietse Ringers committed
106
			AuthenticationMethod:  requestorserver.AuthenticationMethodPublicKey,
107
			AuthenticationKeyFile: filepath.Join(testdata, "jwtkeys", "requestor1.pem"),
108
109
		},
		"requestor2": {
Sietse Ringers's avatar
Sietse Ringers committed
110
			AuthenticationMethod: requestorserver.AuthenticationMethodToken,
111
112
			AuthenticationKey:    "xa6=*&9?8jeUu5>.f-%rVg`f63pHim",
		},
113
		"requestor3": {
Sietse Ringers's avatar
Sietse Ringers committed
114
			AuthenticationMethod: requestorserver.AuthenticationMethodHmac,
115
116
			AuthenticationKey:    "eGE2PSomOT84amVVdTU+LmYtJXJWZ2BmNjNwSGltCg==",
		},
117
	},
118
	JwtPrivateKeyFile: filepath.Join(testdata, "jwtkeys", "sk.pem"),
119
}