server.go 10.4 KB
Newer Older
1
// Package irmaserver is a server allowing IRMA verifiers, issuers or attribute-based signature applications (the requestor) to perform IRMA sessions with irmaclient instances (i.e. the IRMA app). It exposes a RESTful protocol with which the requestor can start and manage the session as well as HTTP endpoints for the irmaclient.
Sietse Ringers's avatar
Sietse Ringers committed
2
package irmaserver
3
4

import (
Sietse Ringers's avatar
Sietse Ringers committed
5
	"crypto/tls"
6
	"crypto/x509"
Sietse Ringers's avatar
Sietse Ringers committed
7
	"encoding/json"
8
	"encoding/pem"
9
	"fmt"
10
11
	"io/ioutil"
	"net/http"
12
	"time"
13

14
	"github.com/dgrijalva/jwt-go"
15
	"github.com/go-chi/chi"
Sietse Ringers's avatar
Sietse Ringers committed
16
	"github.com/go-chi/cors"
17
	"github.com/privacybydesign/irmago"
Sietse Ringers's avatar
Sietse Ringers committed
18
19
	"github.com/privacybydesign/irmago/server"
	"github.com/privacybydesign/irmago/server/irmarequestor"
20
21
)

22
var (
23
24
	serv, clientserv *http.Server
	conf             *Configuration
25
)
26

27
// Start the server. If successful then it will not return until Stop() is called.
28
func Start(config *Configuration) error {
29
	if err := Initialize(config); err != nil {
30
31
32
		return err
	}

33
34
35
	// Start server(s)
	if conf.separateClientServer() {
		go startClientServer()
36
	}
37
	startRequestorServer()
38

39
	return nil
40
41
}

42
43
func startRequestorServer() {
	serv = &http.Server{}
Sietse Ringers's avatar
Sietse Ringers committed
44
45
	tlsConf, _ := conf.tlsConfig()
	startServer(serv, Handler(), "Server", conf.ListenAddress, conf.Port, tlsConf)
46
47
}

48
49
func startClientServer() {
	clientserv = &http.Server{}
Sietse Ringers's avatar
Sietse Ringers committed
50
51
	tlsConf, _ := conf.clientTlsConfig()
	startServer(clientserv, ClientHandler(), "Client server", conf.ClientListenAddress, conf.ClientPort, tlsConf)
52
53
}

Sietse Ringers's avatar
Sietse Ringers committed
54
func startServer(s *http.Server, handler http.Handler, name, addr string, port int, tlsConf *tls.Config) {
55
56
57
58
	fulladdr := fmt.Sprintf("%s:%d", addr, port)
	conf.Logger.Info(name, " listening at ", fulladdr)
	s.Addr = fulladdr
	s.Handler = handler
Sietse Ringers's avatar
Sietse Ringers committed
59
60
61
62
63
64
65
66
67
	var err error
	if tlsConf != nil {
		conf.Logger.Info(name, " TLS enabled")
		s.TLSConfig = tlsConf
		err = s.ListenAndServeTLS("", "")
	} else {
		err = s.ListenAndServe()
	}
	if err != http.ErrServerClosed {
68
69
70
71
72
73
74
75
76
77
78
79
80
81
		_ = server.LogFatal(err)
	}
}

func Stop() error {
	var err1, err2 error

	// Even if closing serv fails, we want to try closing clientserv
	err1 = serv.Close()
	if clientserv != nil {
		err2 = clientserv.Close()
	}

	// Now check errors
82
	if err1 != nil && err1 != http.ErrServerClosed {
83
84
		return err1
	}
85
	if err2 != nil && err2 != http.ErrServerClosed {
86
87
88
89
90
91
		return err2
	}
	return nil
}

func Initialize(config *Configuration) error {
92
	conf = config
93
	if err := irmarequestor.Initialize(conf.Configuration); err != nil {
94
		return err
95
	}
96
	if err := conf.initialize(); err != nil {
97
		return err
98
	}
Sietse Ringers's avatar
Sietse Ringers committed
99
100
	bts, _ := json.MarshalIndent(config, "", "   ")
	config.Logger.Debug("Configuration: ", string(bts), "\n")
101
102
	return nil
}
103

104
105
106
107
108
109
var corsOptions = cors.Options{
	AllowedOrigins: []string{"*"},
	AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "Cache-Control"},
	AllowedMethods: []string{http.MethodGet, http.MethodPost, http.MethodDelete},
}

110
func ClientHandler() http.Handler {
111
	router := chi.NewRouter()
112
	router.Use(cors.New(corsOptions).Handler)
Sietse Ringers's avatar
Sietse Ringers committed
113

114
	router.Mount("/irma/", irmarequestor.HttpHandlerFunc())
115
116
117
118
119
120
121
	return router
}

// Handler returns a http.Handler that handles all IRMA requestor messages
// and IRMA client messages.
func Handler() http.Handler {
	router := chi.NewRouter()
122
	router.Use(cors.New(corsOptions).Handler)
123
124
125
126
127

	if !conf.separateClientServer() {
		// Mount server for irmaclient
		router.Mount("/irma/", irmarequestor.HttpHandlerFunc())
	}
128

129
	// Server routes
130
	router.Post("/session", handleCreate)
131
	router.Delete("/session/{token}", handleDelete)
132
	router.Get("/session/{token}/status", handleStatus)
133
	router.Get("/session/{token}/statusevents", handleStatusEvents)
134
	router.Get("/session/{token}/result", handleResult)
Sietse Ringers's avatar
Sietse Ringers committed
135
136

	// Routes for getting signed JWTs containing the session result. Only work if configuration has a private key
137
138
	router.Get("/session/{token}/result-jwt", handleJwtResult)
	router.Get("/session/{token}/getproof", handleJwtProofs) // irma_api_server-compatible JWT
139

140
141
	router.Get("/publickey", handlePublicKey)

142
	return router
143
144
}

145
146
147
func handleCreate(w http.ResponseWriter, r *http.Request) {
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
148
149
		conf.Logger.Error("Could not read session request HTTP POST body")
		_ = server.LogError(err)
Sietse Ringers's avatar
Sietse Ringers committed
150
		server.WriteError(w, server.ErrorInvalidRequest, err.Error())
151
152
		return
	}
153

Sietse Ringers's avatar
Sietse Ringers committed
154
155
156
	// Authenticate request: check if the requestor is known and allowed to submit requests.
	// We do this by feeding the HTTP POST details to all known authenticators, and see if
	// one of them is applicable and able to authenticate the request.
157
	var (
158
		rrequest  irma.RequestorRequest
159
160
161
162
163
164
		request   irma.SessionRequest
		requestor string
		rerr      *irma.RemoteError
		applies   bool
	)
	for _, authenticator := range authenticators {
165
		applies, rrequest, requestor, rerr = authenticator.Authenticate(r.Header, body)
166
167
168
169
170
		if applies || rerr != nil {
			break
		}
	}
	if rerr != nil {
171
		_ = server.LogError(rerr)
172
173
174
175
		server.WriteResponse(w, nil, rerr)
		return
	}
	if !applies {
176
177
		conf.Logger.Warnf("Session request uses unknown authentication method, HTTP headers: %s, HTTP POST body: %s",
			server.ToJson(r.Header), string(body))
Sietse Ringers's avatar
Sietse Ringers committed
178
		server.WriteError(w, server.ErrorInvalidRequest, "Request could not be authorized")
179
180
181
		return
	}

182
183
	// Authorize request: check if the requestor is allowed to verify or issue
	// the requested attributes or credentials
184
	request = rrequest.SessionRequest()
185
186
187
	if request.Action() == irma.ActionIssuing {
		allowed, reason := conf.CanIssue(requestor, request.(*irma.IssuanceRequest).Credentials)
		if !allowed {
188
189
			conf.Logger.Warn("Requestor %s tried to issue credential %s but it is not authorized to; full request: %s",
				requestor, reason, server.ToJson(request))
190
191
192
193
			server.WriteError(w, server.ErrorUnauthorized, reason)
			return
		}
	}
Sietse Ringers's avatar
Sietse Ringers committed
194
	disjunctions := request.ToDisclose()
195
196
197
	if len(disjunctions) > 0 {
		allowed, reason := conf.CanVerifyOrSign(requestor, request.Action(), disjunctions)
		if !allowed {
198
199
			conf.Logger.Warn("Requestor %s tried to verify attribute %s but it is not authorized to; full request: %s",
				requestor, reason, server.ToJson(request))
200
201
202
203
204
205
			server.WriteError(w, server.ErrorUnauthorized, reason)
			return
		}
	}

	// Everything is authenticated and parsed, we're good to go!
206
	qr, _, err := irmarequestor.StartSession(rrequest, nil)
207
	if err != nil {
Sietse Ringers's avatar
Sietse Ringers committed
208
		server.WriteError(w, server.ErrorInvalidRequest, err.Error())
209
210
211
		return
	}

Sietse Ringers's avatar
Sietse Ringers committed
212
	server.WriteJson(w, qr)
213
214
215
216
217
}

func handleStatus(w http.ResponseWriter, r *http.Request) {
	res := irmarequestor.GetSessionResult(chi.URLParam(r, "token"))
	if res == nil {
Sietse Ringers's avatar
Sietse Ringers committed
218
		server.WriteError(w, server.ErrorSessionUnknown, "")
219
220
		return
	}
Sietse Ringers's avatar
Sietse Ringers committed
221
	server.WriteJson(w, res.Status)
222
223
}

224
225
226
227
228
229
230
231
func handleStatusEvents(w http.ResponseWriter, r *http.Request) {
	token := chi.URLParam(r, "token")
	conf.Logger.Debug("new client subscribed to server sent events of session " + token)
	if err := irmarequestor.SubscribeServerSentEvents(w, r, token); err != nil {
		server.WriteError(w, server.ErrorUnexpectedRequest, err.Error())
	}
}

232
233
234
235
236
237
238
func handleDelete(w http.ResponseWriter, r *http.Request) {
	err := irmarequestor.CancelSession(chi.URLParam(r, "token"))
	if err != nil {
		server.WriteError(w, server.ErrorSessionUnknown, "")
	}
}

239
240
241
func handleResult(w http.ResponseWriter, r *http.Request) {
	res := irmarequestor.GetSessionResult(chi.URLParam(r, "token"))
	if res == nil {
Sietse Ringers's avatar
Sietse Ringers committed
242
		server.WriteError(w, server.ErrorSessionUnknown, "")
243
244
		return
	}
Sietse Ringers's avatar
Sietse Ringers committed
245
	server.WriteJson(w, res)
246
247
}

248
func handleJwtResult(w http.ResponseWriter, r *http.Request) {
249
	if conf.jwtPrivateKey == nil {
250
		conf.Logger.Warn("Session result JWT requested but no JWT private key is configured")
251
252
		server.WriteError(w, server.ErrorUnknown, "JWT signing not supported")
		return
253
	}
254

255
256
	sessiontoken := chi.URLParam(r, "token")
	res := irmarequestor.GetSessionResult(sessiontoken)
257
258
259
	if res == nil {
		server.WriteError(w, server.ErrorSessionUnknown, "")
		return
260
	}
261
262
263
264
265
266
267
268
269
270

	claims := struct {
		jwt.StandardClaims
		*server.SessionResult
	}{
		SessionResult: res,
	}
	claims.Issuer = conf.JwtIssuer
	claims.IssuedAt = time.Now().Unix()
	claims.Subject = string(res.Type) + "_result"
271
272
273
274
	validity := irmarequestor.GetRequest(sessiontoken).Base().ResultJwtValidity
	if validity != 0 {
		claims.ExpiresAt = time.Now().Unix() + int64(validity)
	}
275
276
277

	// Sign the jwt and return it
	token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
278
	resultJwt, err := token.SignedString(conf.jwtPrivateKey)
279
	if err != nil {
280
281
		conf.Logger.Error("Failed to sign session result JWT")
		_ = server.LogError(err)
282
283
284
285
286
287
288
		server.WriteError(w, server.ErrorUnknown, err.Error())
		return
	}
	server.WriteString(w, resultJwt)
}

func handleJwtProofs(w http.ResponseWriter, r *http.Request) {
289
	if conf.jwtPrivateKey == nil {
290
		conf.Logger.Warn("Session result JWT requested but no JWT private key is configured")
291
292
293
294
		server.WriteError(w, server.ErrorUnknown, "JWT signing not supported")
		return
	}

295
296
	sessiontoken := chi.URLParam(r, "token")
	res := irmarequestor.GetSessionResult(sessiontoken)
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
	if res == nil {
		server.WriteError(w, server.ErrorSessionUnknown, "")
		return
	}

	claims := jwt.MapClaims{}

	// Fill standard claims
	switch res.Type {
	case irma.ActionDisclosing:
		claims["subject"] = "verification_result"
	case irma.ActionSigning:
		claims["subject"] = "abs_result"
	default:
		if res == nil {
			server.WriteError(w, server.ErrorInvalidRequest, "")
			return
		}
	}
	claims["iat"] = time.Now().Unix()
	if conf.JwtIssuer != "" {
		claims["iss"] = conf.JwtIssuer
	}
	claims["status"] = res.Status
321
322
323
324
	validity := irmarequestor.GetRequest(sessiontoken).Base().ResultJwtValidity
	if validity != 0 {
		claims["exp"] = time.Now().Unix() + int64(validity)
	}
325
326
327
328
329
330
331
332
333
334
335
336
337

	// Disclosed credentials and possibly signature
	m := make(map[irma.AttributeTypeIdentifier]string, len(res.Disclosed))
	for _, attr := range res.Disclosed {
		m[attr.Identifier] = attr.Value[""]
	}
	claims["attributes"] = m
	if res.Signature != nil {
		claims["signature"] = res.Signature
	}

	// Sign the jwt and return it
	token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
338
	resultJwt, err := token.SignedString(conf.jwtPrivateKey)
339
	if err != nil {
340
341
		conf.Logger.Error("Failed to sign session result JWT")
		_ = server.LogError(err)
342
343
		server.WriteError(w, server.ErrorUnknown, err.Error())
		return
344
	}
345
	server.WriteString(w, resultJwt)
346
}
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364

func handlePublicKey(w http.ResponseWriter, r *http.Request) {
	if conf.jwtPrivateKey == nil {
		server.WriteError(w, server.ErrorUnsupported, "")
		return
	}

	bts, err := x509.MarshalPKIXPublicKey(&conf.jwtPrivateKey.PublicKey)
	if err != nil {
		server.WriteError(w, server.ErrorUnknown, err.Error())
		return
	}
	pubBytes := pem.EncodeToMemory(&pem.Block{
		Type:  "PUBLIC KEY",
		Bytes: bts,
	})
	_, _ = w.Write(pubBytes)
}