server_test.go 3.38 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
}

49
func StartIrmaServer(t *testing.T, updatedIrmaConf bool) {
50
	testdata := test.FindTestdataFolder(t)
51
52
53
54
	irmaconf := "irma_configuration"
	if updatedIrmaConf {
		irmaconf += "_updated"
	}
55
56

	logger := logrus.New()
57
	logger.Level = logrus.ErrorLevel
58
59
60
	logger.Formatter = &logrus.TextFormatter{}

	var err error
61
	irmaServer, err = irmaserver.New(&server.Configuration{
62
63
64
		URL:         "http://localhost:48680",
		Logger:      logger,
		SchemesPath: filepath.Join(testdata, irmaconf),
65
66
67
68
69
	})

	require.NoError(t, err)

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

Sietse Ringers's avatar
Sietse Ringers committed
77
func StopIrmaServer() {
78
	_ = httpServer.Close()
79
80
}

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

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