server_test.go 4.8 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
func StartRevocationServer(t *testing.T) {
	var err error
	revocationServer, err = irmaserver.New(&server.Configuration{
		Logger:               logger,
		SchemesPath:          filepath.Join(testdata, "irma_configuration"),
		RevocationPath:       filepath.Join(testdata, "tmp", "issuer"),
		DisableSchemesUpdate: true,
		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
		URL:            "http://localhost:48680",
		Logger:         logger,
		SchemesPath:    filepath.Join(testdata, irmaconf),
90
		RevocationPath: filepath.Join(testdata, "tmp", "revocation"),
91
92
93
94
95
	})

	require.NoError(t, err)

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

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

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

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