server_test.go 5.73 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
63
64
65
66
67
68

	irma.Logger = logger
	dbstr := "host=127.0.0.1 port=5432 user=testuser dbname=test password='testpassword' sslmode=disable"
	irmaconf, err := irma.NewConfiguration(filepath.Join(testdata, "irma_configuration"), irma.ConfigurationOptions{
		RevocationDB: dbstr,
	})
	require.NoError(t, err)
	require.NoError(t, irmaconf.ParseFolder())

69
70
	cred := irma.NewCredentialTypeIdentifier("irma-demo.MijnOverheid.root")
	conf := &server.Configuration{
71
72
		Logger:               logger,
		DisableSchemesUpdate: true,
73
		SchemesPath:          filepath.Join(testdata, "irma_configuration"),
74
75
		RevocationSettings: map[irma.CredentialTypeIdentifier]*irma.RevocationSetting{
			cred: {Mode: irma.RevocationModeServer},
76
		},
77
78
		IrmaConfiguration: irmaconf,
		RevocationDB:      dbstr,
79
80
	}

81
82
	// Connect to database and clear records from previous test runs
	g, err := gorm.Open("postgres", conf.RevocationDB)
83
	require.NoError(t, err)
84
85
86
87
88
	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())
89

90
91
92
93
94
95
	// Enable revocation for our credential type
	require.NoError(t, irmaconf.RevocationStorage.EnableRevocation(cred))

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

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

109
func StartIrmaServer(t *testing.T, updatedIrmaConf bool) {
110
	testdata := test.FindTestdataFolder(t)
111
112
113
114
	irmaconf := "irma_configuration"
	if updatedIrmaConf {
		irmaconf += "_updated"
	}
115
116

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

	require.NoError(t, err)

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

Sietse Ringers's avatar
Sietse Ringers committed
134
func StopIrmaServer() {
135
	irmaServer.Stop()
136
	_ = httpServer.Close()
137
138
}

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

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