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

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

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

20
var (
21
22
23
24
25
	httpServer       *http.Server
	irmaServer       *irmaserver.Server
	revHttpServer    *http.Server
	revocationServer *irmaserver.Server
	requestorServer  *requestorserver.Server
26

27
28
29
	logger   = logrus.New()
	testdata = test.FindTestdataFolder(nil)
)
30

31
func init() {
32
	logger.Level = logrus.TraceLevel
33
34
35
36
37
38
	logger.Formatter = &prefixed.TextFormatter{
		ForceFormatting: true,
		ForceColors:     true,
		FullTimestamp:   true,
		TimestampFormat: "15:04:05.000000",
	}
39
}
40

Sietse Ringers's avatar
Sietse Ringers committed
41
func StartRequestorServer(configuration *requestorserver.Configuration) {
42
	go func() {
43
		var err error
Sietse Ringers's avatar
Sietse Ringers committed
44
		if requestorServer, err = requestorserver.New(configuration); err != nil {
45
46
			panic(err)
		}
Sietse Ringers's avatar
Sietse Ringers committed
47
		if err = requestorServer.Start(configuration); err != nil {
48
49
			panic("Starting server failed: " + err.Error())
		}
50
	}()
51
	time.Sleep(100 * time.Millisecond) // Give server time to start
52
53
}

Sietse Ringers's avatar
Sietse Ringers committed
54
func StopRequestorServer() {
55
	requestorServer.Stop()
56
57
}

58
59
func StartRevocationServer(t *testing.T) {
	var err error
60
61
62

	irma.Logger = logger
	dbstr := "host=127.0.0.1 port=5432 user=testuser dbname=test password='testpassword' sslmode=disable"
63
64
65
66
	cred := irma.NewCredentialTypeIdentifier("irma-demo.MijnOverheid.root")
	settings := map[irma.CredentialTypeIdentifier]*irma.RevocationSetting{
		cred: {Mode: irma.RevocationModeServer},
	}
67
	irmaconf, err := irma.NewConfiguration(filepath.Join(testdata, "irma_configuration"), irma.ConfigurationOptions{
68
69
		RevocationDB:       dbstr,
		RevocationSettings: settings,
70
71
72
73
	})
	require.NoError(t, err)
	require.NoError(t, irmaconf.ParseFolder())

74
	conf := &server.Configuration{
75
76
		Logger:               logger,
		DisableSchemesUpdate: true,
77
		SchemesPath:          filepath.Join(testdata, "irma_configuration"),
78
79
80
		RevocationSettings:   settings,
		IrmaConfiguration:    irmaconf,
		RevocationDB:         dbstr,
81
82
	}

83
84
	// Connect to database and clear records from previous test runs
	g, err := gorm.Open("postgres", conf.RevocationDB)
85
	require.NoError(t, err)
86
87
88
89
90
	require.NoError(t, g.DropTableIfExists((*irma.RevocationRecord)(nil)).Error)
	require.NoError(t, g.DropTableIfExists((*irma.IssuanceRecord)(nil)).Error)
	require.NoError(t, g.AutoMigrate((*irma.RevocationRecord)(nil)).Error)
	require.NoError(t, g.AutoMigrate((*irma.IssuanceRecord)(nil)).Error)
	require.NoError(t, g.Close())
91

92
	// Enable revocation for our credential type
93
94
95
	sk, err := irmaconf.RevocationStorage.Keys.PrivateKey(cred.IssuerIdentifier())
	require.NoError(t, err)
	require.NoError(t, irmaconf.RevocationStorage.EnableRevocation(cred, sk))
96
97
98
99

	// Start revocation server
	revocationServer, err = irmaserver.New(conf)
	require.NoError(t, err)
100
101
102
103
104
105
106
107
108
109
110
111
112
	mux := http.NewServeMux()
	mux.HandleFunc("/", revocationServer.HandlerFunc())
	revHttpServer = &http.Server{Addr: ":48683", Handler: mux}
	go func() {
		_ = revHttpServer.ListenAndServe()
	}()
}

func StopRevocationServer() {
	revocationServer.Stop()
	_ = revHttpServer.Close()
}

113
func StartIrmaServer(t *testing.T, updatedIrmaConf bool) {
114
	testdata := test.FindTestdataFolder(t)
115
116
117
118
	irmaconf := "irma_configuration"
	if updatedIrmaConf {
		irmaconf += "_updated"
	}
119
120

	var err error
121
	irmaServer, err = irmaserver.New(&server.Configuration{
122
123
124
125
		URL:                  "http://localhost:48680",
		Logger:               logger,
		DisableSchemesUpdate: true,
		SchemesPath:          filepath.Join(testdata, irmaconf),
126
127
128
129
130
	})

	require.NoError(t, err)

	mux := http.NewServeMux()
Sietse Ringers's avatar
Sietse Ringers committed
131
	mux.HandleFunc("/", irmaServer.HandlerFunc())
132
133
134
135
136
137
	httpServer = &http.Server{Addr: ":48680", Handler: mux}
	go func() {
		_ = httpServer.ListenAndServe()
	}()
}

Sietse Ringers's avatar
Sietse Ringers committed
138
func StopIrmaServer() {
139
	irmaServer.Stop()
140
	_ = httpServer.Close()
141
142
}

Sietse Ringers's avatar
Sietse Ringers committed
143
var IrmaServerConfiguration = &requestorserver.Configuration{
144
	Configuration: &server.Configuration{
Sietse Ringers's avatar
Sietse Ringers committed
145
		URL:                   "http://localhost:48682/irma",
146
		Logger:                logger,
147
		SchemesPath:           filepath.Join(testdata, "irma_configuration"),
148
149
		IssuerPrivateKeysPath: filepath.Join(testdata, "privatekeys"),
	},
150
	DisableRequestorAuthentication: true,
151
	Port: 48682,
152
153
}

Sietse Ringers's avatar
Sietse Ringers committed
154
var JwtServerConfiguration = &requestorserver.Configuration{
155
	Configuration: &server.Configuration{
Sietse Ringers's avatar
Sietse Ringers committed
156
		URL:                   "http://localhost:48682/irma",
157
		Logger:                logger,
158
		SchemesPath:           filepath.Join(testdata, "irma_configuration"),
159
160
		IssuerPrivateKeysPath: filepath.Join(testdata, "privatekeys"),
	},
161
	Port: 48682,
162
	DisableRequestorAuthentication: false,
163
	MaxRequestAge:                  3,
Sietse Ringers's avatar
Sietse Ringers committed
164
	Permissions: requestorserver.Permissions{
165
166
167
168
		Disclosing: []string{"*"},
		Signing:    []string{"*"},
		Issuing:    []string{"*"},
	},
Sietse Ringers's avatar
Sietse Ringers committed
169
	Requestors: map[string]requestorserver.Requestor{
Sietse Ringers's avatar
Sietse Ringers committed
170
		"requestor1": {
Sietse Ringers's avatar
Sietse Ringers committed
171
			AuthenticationMethod:  requestorserver.AuthenticationMethodPublicKey,
172
			AuthenticationKeyFile: filepath.Join(testdata, "jwtkeys", "requestor1.pem"),
173
174
		},
		"requestor2": {
Sietse Ringers's avatar
Sietse Ringers committed
175
			AuthenticationMethod: requestorserver.AuthenticationMethodToken,
176
177
			AuthenticationKey:    "xa6=*&9?8jeUu5>.f-%rVg`f63pHim",
		},
178
		"requestor3": {
Sietse Ringers's avatar
Sietse Ringers committed
179
			AuthenticationMethod: requestorserver.AuthenticationMethodHmac,
180
181
			AuthenticationKey:    "eGE2PSomOT84amVVdTU+LmYtJXJWZ2BmNjNwSGltCg==",
		},
182
	},
183
184
185
	StaticSessions: map[string]interface{}{
		"staticsession": irma.ServiceProviderRequest{
			RequestorBaseRequest: irma.RequestorBaseRequest{
186
				CallbackURL: "http://localhost:48685",
187
188
189
190
191
192
193
194
195
			},
			Request: &irma.DisclosureRequest{
				BaseRequest: irma.BaseRequest{LDContext: irma.LDContextDisclosureRequest},
				Disclose: irma.AttributeConDisCon{
					{{irma.NewAttributeRequest("irma-demo.RU.studentCard.level")}},
				},
			},
		},
	},
196
	JwtPrivateKeyFile: filepath.Join(testdata, "jwtkeys", "sk.pem"),
197
}