server_test.go 5.22 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 (
20
21
22
23
24
	httpServer       *http.Server
	irmaServer       *irmaserver.Server
	revHttpServer    *http.Server
	revocationServer *irmaserver.Server
	requestorServer  *requestorserver.Server
25

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

30
func init() {
31
	logger.Level = logrus.TraceLevel
32
	logger.Formatter = &prefixed.TextFormatter{ForceFormatting: true, ForceColors: true}
33
}
34

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

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

52
53
func StartRevocationServer(t *testing.T) {
	var err error
54
55
	cred := irma.NewCredentialTypeIdentifier("irma-demo.MijnOverheid.root")
	conf := &server.Configuration{
56
57
		Logger:               logger,
		DisableSchemesUpdate: true,
58
59
		SchemesPath:          filepath.Join(testdata, "irma_configuration"),
		RevocationPath:       filepath.Join(testdata, "tmp", "issuer"), // todo rename this path to revocation?
60
		RevocationServers: map[irma.CredentialTypeIdentifier]server.RevocationServer{
61
			cred: {},
62
		},
63
64
65
66
	}
	revocationServer, err = irmaserver.New(conf)
	require.NoError(t, err)

67
68
69
70
	sk, err := conf.PrivateKey(cred.IssuerIdentifier())
	require.NoError(t, err)
	require.NotNil(t, sk)
	revsk, err := sk.RevocationKey()
71
	require.NoError(t, err)
72
	db, err := conf.IrmaConfiguration.RevocationStorage.DB(cred)
73
	require.NoError(t, err)
74
	err = db.EnableRevocation(revsk)
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
	require.NoError(t, err)

	mux := http.NewServeMux()
	mux.HandleFunc("/", revocationServer.HandlerFunc())
	revHttpServer = &http.Server{Addr: ":48683", Handler: mux}
	go func() {
		_ = revHttpServer.ListenAndServe()
	}()
}

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

90
func StartIrmaServer(t *testing.T, updatedIrmaConf bool) {
91
	testdata := test.FindTestdataFolder(t)
92
93
94
95
	irmaconf := "irma_configuration"
	if updatedIrmaConf {
		irmaconf += "_updated"
	}
96
97

	var err error
98
	irmaServer, err = irmaserver.New(&server.Configuration{
99
100
101
102
103
		URL:                  "http://localhost:48680",
		Logger:               logger,
		DisableSchemesUpdate: true,
		SchemesPath:          filepath.Join(testdata, irmaconf),
		RevocationPath:       filepath.Join(testdata, "tmp", "revocation"),
104
105
106
107
108
	})

	require.NoError(t, err)

	mux := http.NewServeMux()
Sietse Ringers's avatar
Sietse Ringers committed
109
	mux.HandleFunc("/", irmaServer.HandlerFunc())
110
111
112
113
114
115
	httpServer = &http.Server{Addr: ":48680", Handler: mux}
	go func() {
		_ = httpServer.ListenAndServe()
	}()
}

Sietse Ringers's avatar
Sietse Ringers committed
116
func StopIrmaServer() {
117
	irmaServer.Stop()
118
	_ = httpServer.Close()
119
120
}

Sietse Ringers's avatar
Sietse Ringers committed
121
var IrmaServerConfiguration = &requestorserver.Configuration{
122
	Configuration: &server.Configuration{
Sietse Ringers's avatar
Sietse Ringers committed
123
		URL:                   "http://localhost:48682/irma",
124
		Logger:                logger,
125
		SchemesPath:           filepath.Join(testdata, "irma_configuration"),
126
		IssuerPrivateKeysPath: filepath.Join(testdata, "privatekeys"),
127
		RevocationPath:        filepath.Join(testdata, "tmp", "revocation"),
128
	},
129
	DisableRequestorAuthentication: true,
130
	Port: 48682,
131
132
}

Sietse Ringers's avatar
Sietse Ringers committed
133
var JwtServerConfiguration = &requestorserver.Configuration{
134
	Configuration: &server.Configuration{
Sietse Ringers's avatar
Sietse Ringers committed
135
		URL:                   "http://localhost:48682/irma",
136
		Logger:                logger,
137
		SchemesPath:           filepath.Join(testdata, "irma_configuration"),
138
		IssuerPrivateKeysPath: filepath.Join(testdata, "privatekeys"),
139
		RevocationPath:        filepath.Join(testdata, "tmp", "revocation"),
140
	},
141
	Port: 48682,
142
	DisableRequestorAuthentication: false,
143
	MaxRequestAge: 3,
Sietse Ringers's avatar
Sietse Ringers committed
144
	Permissions: requestorserver.Permissions{
145
146
147
148
		Disclosing: []string{"*"},
		Signing:    []string{"*"},
		Issuing:    []string{"*"},
	},
Sietse Ringers's avatar
Sietse Ringers committed
149
	Requestors: map[string]requestorserver.Requestor{
Sietse Ringers's avatar
Sietse Ringers committed
150
		"requestor1": {
Sietse Ringers's avatar
Sietse Ringers committed
151
			AuthenticationMethod:  requestorserver.AuthenticationMethodPublicKey,
152
			AuthenticationKeyFile: filepath.Join(testdata, "jwtkeys", "requestor1.pem"),
153
154
		},
		"requestor2": {
Sietse Ringers's avatar
Sietse Ringers committed
155
			AuthenticationMethod: requestorserver.AuthenticationMethodToken,
156
157
			AuthenticationKey:    "xa6=*&9?8jeUu5>.f-%rVg`f63pHim",
		},
158
		"requestor3": {
Sietse Ringers's avatar
Sietse Ringers committed
159
			AuthenticationMethod: requestorserver.AuthenticationMethodHmac,
160
161
			AuthenticationKey:    "eGE2PSomOT84amVVdTU+LmYtJXJWZ2BmNjNwSGltCg==",
		},
162
	},
163
164
165
	StaticSessions: map[string]interface{}{
		"staticsession": irma.ServiceProviderRequest{
			RequestorBaseRequest: irma.RequestorBaseRequest{
166
				CallbackURL: "http://localhost:48685",
167
168
169
170
171
172
173
174
175
			},
			Request: &irma.DisclosureRequest{
				BaseRequest: irma.BaseRequest{LDContext: irma.LDContextDisclosureRequest},
				Disclose: irma.AttributeConDisCon{
					{{irma.NewAttributeRequest("irma-demo.RU.studentCard.level")}},
				},
			},
		},
	},
176
	JwtPrivateKeyFile: filepath.Join(testdata, "jwtkeys", "sk.pem"),
177
}