server_test.go 5.3 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
33
34
35
36
37
	logger.Formatter = &prefixed.TextFormatter{
		ForceFormatting: true,
		ForceColors:     true,
		FullTimestamp:   true,
		TimestampFormat: "15:04:05.000000",
	}
38
}
39

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

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

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

72
73
74
75
	sk, err := conf.PrivateKey(cred.IssuerIdentifier())
	require.NoError(t, err)
	require.NotNil(t, sk)
	revsk, err := sk.RevocationKey()
76
	require.NoError(t, err)
77
	db, err := conf.IrmaConfiguration.RevocationStorage.DB(cred)
78
	require.NoError(t, err)
79
	err = db.EnableRevocation(revsk)
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
	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()
}

95
func StartIrmaServer(t *testing.T, updatedIrmaConf bool) {
96
	testdata := test.FindTestdataFolder(t)
97
98
99
100
	irmaconf := "irma_configuration"
	if updatedIrmaConf {
		irmaconf += "_updated"
	}
101
102

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

	require.NoError(t, err)

	mux := http.NewServeMux()
Sietse Ringers's avatar
Sietse Ringers committed
114
	mux.HandleFunc("/", irmaServer.HandlerFunc())
115
116
117
118
119
120
	httpServer = &http.Server{Addr: ":48680", Handler: mux}
	go func() {
		_ = httpServer.ListenAndServe()
	}()
}

Sietse Ringers's avatar
Sietse Ringers committed
121
func StopIrmaServer() {
122
	irmaServer.Stop()
123
	_ = httpServer.Close()
124
125
}

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

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