server_test.go 5.18 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
67
68
69
70
71
	}
	revocationServer, err = irmaserver.New(conf)
	require.NoError(t, err)

	sk, err := conf.IrmaConfiguration.RevocationStorage.PrivateKey(cred.IssuerIdentifier())
	require.NoError(t, err)
	db, err := conf.IrmaConfiguration.RevocationStorage.RevocationDB(cred)
	require.NoError(t, err)
	err = db.EnableRevocation(sk)
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
	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()
}

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

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

	require.NoError(t, err)

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

Sietse Ringers's avatar
Sietse Ringers committed
113
func StopIrmaServer() {
114
	irmaServer.Stop()
115
	_ = httpServer.Close()
116
117
}

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

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