server_test.go 3.97 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 (
Sietse Ringers's avatar
Sietse Ringers committed
20
21
22
	httpServer      *http.Server
	irmaServer      *irmaserver.Server
	requestorServer *requestorserver.Server
23

24
25
26
	logger   = logrus.New()
	testdata = test.FindTestdataFolder(nil)
)
27

28
func init() {
29
	logger.Level = logrus.ErrorLevel
30
	logger.Formatter = &prefixed.TextFormatter{ForceFormatting: true, ForceColors: true}
31
}
32

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

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

50
func StartIrmaServer(t *testing.T, updatedIrmaConf bool) {
51
	testdata := test.FindTestdataFolder(t)
52
53
54
55
	irmaconf := "irma_configuration"
	if updatedIrmaConf {
		irmaconf += "_updated"
	}
56
57

	var err error
58
	irmaServer, err = irmaserver.New(&server.Configuration{
59
60
61
62
		URL:            "http://localhost:48680",
		Logger:         logger,
		SchemesPath:    filepath.Join(testdata, irmaconf),
		RevocationPath: filepath.Join(testdata, "storage", "revocation"),
63
64
65
66
67
	})

	require.NoError(t, err)

	mux := http.NewServeMux()
Sietse Ringers's avatar
Sietse Ringers committed
68
	mux.HandleFunc("/", irmaServer.HandlerFunc())
69
70
71
72
73
74
	httpServer = &http.Server{Addr: ":48680", Handler: mux}
	go func() {
		_ = httpServer.ListenAndServe()
	}()
}

Sietse Ringers's avatar
Sietse Ringers committed
75
func StopIrmaServer() {
76
	irmaServer.Stop()
77
	_ = httpServer.Close()
78
79
}

Sietse Ringers's avatar
Sietse Ringers committed
80
var IrmaServerConfiguration = &requestorserver.Configuration{
81
	Configuration: &server.Configuration{
Sietse Ringers's avatar
Sietse Ringers committed
82
		URL:                   "http://localhost:48682/irma",
83
		Logger:                logger,
84
		SchemesPath:           filepath.Join(testdata, "irma_configuration"),
85
		IssuerPrivateKeysPath: filepath.Join(testdata, "privatekeys"),
86
		RevocationPath:        filepath.Join(testdata, "storage", "revocation"),
87
	},
88
	DisableRequestorAuthentication: true,
89
	Port: 48682,
90
91
}

Sietse Ringers's avatar
Sietse Ringers committed
92
var JwtServerConfiguration = &requestorserver.Configuration{
93
	Configuration: &server.Configuration{
Sietse Ringers's avatar
Sietse Ringers committed
94
		URL:                   "http://localhost:48682/irma",
95
		Logger:                logger,
96
		SchemesPath:           filepath.Join(testdata, "irma_configuration"),
97
		IssuerPrivateKeysPath: filepath.Join(testdata, "privatekeys"),
98
		RevocationPath:        filepath.Join(testdata, "storage", "revocation"),
99
	},
100
	Port: 48682,
101
	DisableRequestorAuthentication: false,
102
	MaxRequestAge: 3,
Sietse Ringers's avatar
Sietse Ringers committed
103
	Permissions: requestorserver.Permissions{
104
105
106
107
		Disclosing: []string{"*"},
		Signing:    []string{"*"},
		Issuing:    []string{"*"},
	},
Sietse Ringers's avatar
Sietse Ringers committed
108
	Requestors: map[string]requestorserver.Requestor{
Sietse Ringers's avatar
Sietse Ringers committed
109
		"requestor1": {
Sietse Ringers's avatar
Sietse Ringers committed
110
			AuthenticationMethod:  requestorserver.AuthenticationMethodPublicKey,
111
			AuthenticationKeyFile: filepath.Join(testdata, "jwtkeys", "requestor1.pem"),
112
113
		},
		"requestor2": {
Sietse Ringers's avatar
Sietse Ringers committed
114
			AuthenticationMethod: requestorserver.AuthenticationMethodToken,
115
116
			AuthenticationKey:    "xa6=*&9?8jeUu5>.f-%rVg`f63pHim",
		},
117
		"requestor3": {
Sietse Ringers's avatar
Sietse Ringers committed
118
			AuthenticationMethod: requestorserver.AuthenticationMethodHmac,
119
120
			AuthenticationKey:    "eGE2PSomOT84amVVdTU+LmYtJXJWZ2BmNjNwSGltCg==",
		},
121
	},
122
123
124
	StaticSessions: map[string]interface{}{
		"staticsession": irma.ServiceProviderRequest{
			RequestorBaseRequest: irma.RequestorBaseRequest{
125
				CallbackURL: "http://localhost:48685",
126
127
128
129
130
131
132
133
134
			},
			Request: &irma.DisclosureRequest{
				BaseRequest: irma.BaseRequest{LDContext: irma.LDContextDisclosureRequest},
				Disclose: irma.AttributeConDisCon{
					{{irma.NewAttributeRequest("irma-demo.RU.studentCard.level")}},
				},
			},
		},
	},
135
	JwtPrivateKeyFile: filepath.Join(testdata, "jwtkeys", "sk.pem"),
136
}