server_test.go 4.9 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
54
55
56
func StartRevocationServer(t *testing.T) {
	var err error
	revocationServer, err = irmaserver.New(&server.Configuration{
		Logger:               logger,
		DisableSchemesUpdate: true,
57
58
		SchemesPath:          filepath.Join(testdata, "irma_configuration"),
		RevocationPath:       filepath.Join(testdata, "tmp", "issuer"), // todo rename this path to revocation?
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
		RevocationServers: map[irma.CredentialTypeIdentifier]server.RevocationServer{
			irma.NewCredentialTypeIdentifier("irma-demo.MijnOverheid.root"): {},
		},
	})
	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()
}

78
func StartIrmaServer(t *testing.T, updatedIrmaConf bool) {
79
	testdata := test.FindTestdataFolder(t)
80
81
82
83
	irmaconf := "irma_configuration"
	if updatedIrmaConf {
		irmaconf += "_updated"
	}
84
85

	var err error
86
	irmaServer, err = irmaserver.New(&server.Configuration{
87
88
89
90
91
		URL:                  "http://localhost:48680",
		Logger:               logger,
		DisableSchemesUpdate: true,
		SchemesPath:          filepath.Join(testdata, irmaconf),
		RevocationPath:       filepath.Join(testdata, "tmp", "revocation"),
92
93
94
95
96
	})

	require.NoError(t, err)

	mux := http.NewServeMux()
Sietse Ringers's avatar
Sietse Ringers committed
97
	mux.HandleFunc("/", irmaServer.HandlerFunc())
98
99
100
101
102
103
	httpServer = &http.Server{Addr: ":48680", Handler: mux}
	go func() {
		_ = httpServer.ListenAndServe()
	}()
}

Sietse Ringers's avatar
Sietse Ringers committed
104
func StopIrmaServer() {
105
	irmaServer.Stop()
106
	_ = httpServer.Close()
107
108
}

Sietse Ringers's avatar
Sietse Ringers committed
109
var IrmaServerConfiguration = &requestorserver.Configuration{
110
	Configuration: &server.Configuration{
Sietse Ringers's avatar
Sietse Ringers committed
111
		URL:                   "http://localhost:48682/irma",
112
		Logger:                logger,
113
		SchemesPath:           filepath.Join(testdata, "irma_configuration"),
114
		IssuerPrivateKeysPath: filepath.Join(testdata, "privatekeys"),
115
		RevocationPath:        filepath.Join(testdata, "tmp", "revocation"),
116
	},
117
	DisableRequestorAuthentication: true,
118
	Port: 48682,
119
120
}

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