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

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

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

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

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

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

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

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

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

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

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

	require.NoError(t, err)

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

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

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

Sietse Ringers's avatar
Sietse Ringers committed
93
var JwtServerConfiguration = &requestorserver.Configuration{
94
	Configuration: &server.Configuration{
Sietse Ringers's avatar
Sietse Ringers committed
95
		URL:                   "http://localhost:48682/irma",
96
		Logger:                logger,
97
		SchemesPath:           filepath.Join(testdata, "irma_configuration"),
98
99
100
		IssuerPrivateKeysPath: filepath.Join(testdata, "privatekeys"),
	},
	Port: 48682,
101
	DisableRequestorAuthentication: false,
102
	MaxRequestAge:                  3,
Sietse Ringers's avatar
Sietse Ringers committed
103
	Permissions: requestorserver.Permissions{
104
105
106
107
		Disclosing: []string{"*"},
		Signing:    []string{"*"},
		Issuing:    []string{"*"},
	},
Sietse Ringers's avatar
Sietse Ringers committed
108
	Requestors: map[string]requestorserver.Requestor{
Sietse Ringers's avatar
Sietse Ringers committed
109
		"requestor1": {
Sietse Ringers's avatar
Sietse Ringers committed
110
			AuthenticationMethod:  requestorserver.AuthenticationMethodPublicKey,
111
			AuthenticationKeyFile: filepath.Join(testdata, "jwtkeys", "requestor1.pem"),
112
113
		},
		"requestor2": {
Sietse Ringers's avatar
Sietse Ringers committed
114
			AuthenticationMethod: requestorserver.AuthenticationMethodToken,
115
116
			AuthenticationKey:    "xa6=*&9?8jeUu5>.f-%rVg`f63pHim",
		},
117
		"requestor3": {
Sietse Ringers's avatar
Sietse Ringers committed
118
			AuthenticationMethod: requestorserver.AuthenticationMethodHmac,
119
120
			AuthenticationKey:    "eGE2PSomOT84amVVdTU+LmYtJXJWZ2BmNjNwSGltCg==",
		},
121
	},
122
123
124
125
126
127
128
129
130
131
132
133
134
	StaticSessions: map[string]interface{}{
		"staticsession": irma.ServiceProviderRequest{
			RequestorBaseRequest: irma.RequestorBaseRequest{
				CallbackUrl: "http://localhost:48685",
			},
			Request: &irma.DisclosureRequest{
				BaseRequest: irma.BaseRequest{LDContext: irma.LDContextDisclosureRequest},
				Disclose: irma.AttributeConDisCon{
					{{irma.NewAttributeRequest("irma-demo.RU.studentCard.level")}},
				},
			},
		},
	},
135
	JwtPrivateKeyFile: filepath.Join(testdata, "jwtkeys", "sk.pem"),
136
}