server.go 14.4 KB
Newer Older
Sietse Ringers's avatar
Sietse Ringers committed
1
// Package requestorserver is a server allowing IRMA verifiers, issuers or attribute-based signature
Sietse Ringers's avatar
Sietse Ringers committed
2 3 4
// 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
5
package requestorserver
6 7

import (
8
	"context"
Sietse Ringers's avatar
Sietse Ringers committed
9
	"crypto/tls"
10
	"crypto/x509"
Sietse Ringers's avatar
Sietse Ringers committed
11
	"encoding/json"
12
	"encoding/pem"
13
	"fmt"
14
	"io/ioutil"
15
	"log"
16
	"net/http"
17
	"time"
18

19
	"github.com/dgrijalva/jwt-go"
20
	"github.com/go-chi/chi"
21
	"github.com/go-chi/chi/middleware"
Sietse Ringers's avatar
Sietse Ringers committed
22
	"github.com/go-chi/cors"
23
	"github.com/go-errors/errors"
24
	"github.com/privacybydesign/irmago"
Sietse Ringers's avatar
Sietse Ringers committed
25
	"github.com/privacybydesign/irmago/server"
26
	"github.com/privacybydesign/irmago/server/irmaserver"
27
	"github.com/sirupsen/logrus"
28 29
)

Sietse Ringers's avatar
Sietse Ringers committed
30
// Server is a requestor server instance.
31
type Server struct {
32 33 34
	conf     *Configuration
	irmaserv *irmaserver.Server
	stop     chan struct{}
35
}
36

37
// Start the server. If successful then it will not return until Stop() is called.
38 39 40
func (s *Server) Start(config *Configuration) error {
	if s.conf.LogJSON {
		s.conf.Logger.WithField("configuration", s.conf).Debug("Configuration")
41
	} else {
42 43
		bts, _ := json.MarshalIndent(s.conf, "", "   ")
		s.conf.Logger.Debug("Configuration: ", string(bts), "\n")
44
	}
45

46 47 48 49 50 51 52 53 54
	// We start either one or two servers, depending on whether a separate client server is enabled, such that:
	// - if any of them returns, the other is also stopped (neither of them is of use without the other)
	// - if any of them returns an unexpected error (ie. other than http.ErrServerClosed), the error is logged and returned
	// - we have a way of stopping all servers from outside (with Stop())
	// - the function returns only after all servers have been stopped
	// - any unexpected error is dealt with here instead of when stopping using Stop().
	// Inspired by https://dave.cheney.net/practical-go/presentations/qcon-china.html#_never_start_a_goroutine_without_when_it_will_stop

	count := 1
55
	if s.conf.separateClientServer() {
56
		count = 2
57
	}
58 59
	done := make(chan error, count)
	s.stop = make(chan struct{})
60

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
	if s.conf.separateClientServer() {
		go func() {
			done <- s.startClientServer()
		}()
	}
	go func() {
		done <- s.startRequestorServer()
	}()

	var stopped bool
	var err error
	for i := 0; i < cap(done); i++ {
		if err = <-done; err != nil {
			_ = server.LogError(err)
		}
		if !stopped {
			stopped = true
			close(s.stop)
		}
	}

	return err
83 84
}

85
func (s *Server) startRequestorServer() error {
86
	tlsConf, _ := s.conf.tlsConfig()
87
	return s.startServer(s.Handler(), "Server", s.conf.ListenAddress, s.conf.Port, tlsConf)
88 89
}

90
func (s *Server) startClientServer() error {
91
	tlsConf, _ := s.conf.clientTlsConfig()
92
	return s.startServer(s.ClientHandler(), "Client server", s.conf.ClientListenAddress, s.conf.ClientPort, tlsConf)
93 94
}

95
func (s *Server) startServer(handler http.Handler, name, addr string, port int, tlsConf *tls.Config) error {
96
	fulladdr := fmt.Sprintf("%s:%d", addr, port)
97
	s.conf.Logger.Info(name, " listening at ", fulladdr)
98 99 100 101 102 103 104 105 106 107 108 109 110 111

	serv := &http.Server{
		Addr:      fulladdr,
		Handler:   handler,
		TLSConfig: tlsConf,
	}

	go func() {
		<-s.stop
		if err := serv.Shutdown(context.Background()); err != nil {
			_ = server.LogError(err)
		}
	}()

Sietse Ringers's avatar
Sietse Ringers committed
112
	if tlsConf != nil {
113
		// Disable HTTP/2 (see package documentation of http): it breaks server side events :(
114 115
		serv.TLSNextProto = make(map[string]func(*http.Server, *tls.Conn, http.Handler))
		s.conf.Logger.Info(name, " TLS enabled")
116
		return filterStopError(serv.ListenAndServeTLS("", ""))
Sietse Ringers's avatar
Sietse Ringers committed
117
	} else {
118
		return filterStopError(serv.ListenAndServe())
119 120 121
	}
}

122 123 124
func filterStopError(err error) error {
	if err == http.ErrServerClosed {
		return nil
125
	}
126 127
	return err
}
128

129 130 131
func (s *Server) Stop() {
	s.irmaserv.Stop()
	s.stop <- struct{}{}
132 133
}

134
func New(config *Configuration) (*Server, error) {
135
	irmaserv, err := irmaserver.New(config.Configuration)
136 137
	if err != nil {
		return nil, err
138
	}
139 140
	if err := config.initialize(); err != nil {
		return nil, err
141
	}
142 143 144 145
	return &Server{
		conf:     config,
		irmaserv: irmaserv,
	}, nil
146
}
147

148 149 150 151 152 153
var corsOptions = cors.Options{
	AllowedOrigins: []string{"*"},
	AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "Cache-Control"},
	AllowedMethods: []string{http.MethodGet, http.MethodPost, http.MethodDelete},
}

154
func (s *Server) ClientHandler() http.Handler {
155
	router := chi.NewRouter()
156
	router.Use(cors.New(corsOptions).Handler)
Sietse Ringers's avatar
Sietse Ringers committed
157

Sietse Ringers's avatar
Sietse Ringers committed
158
	router.Mount("/irma/", s.irmaserv.HandlerFunc())
159
	if s.conf.StaticPath != "" {
160
		router.Mount(s.conf.StaticPrefix, s.StaticFilesHandler())
161 162
	}

163 164 165 166 167
	return router
}

// Handler returns a http.Handler that handles all IRMA requestor messages
// and IRMA client messages.
168
func (s *Server) Handler() http.Handler {
169
	router := chi.NewRouter()
170
	router.Use(cors.New(corsOptions).Handler)
171

172
	if !s.conf.separateClientServer() {
173
		// Mount server for irmaclient
Sietse Ringers's avatar
Sietse Ringers committed
174
		router.Mount("/irma/", s.irmaserv.HandlerFunc())
175 176 177
		if s.conf.StaticPath != "" {
			router.Mount(s.conf.StaticPrefix, s.StaticFilesHandler())
		}
178
	}
179

180
	// Server routes
181 182 183 184 185
	router.Post("/session", s.handleCreate)
	router.Delete("/session/{token}", s.handleDelete)
	router.Get("/session/{token}/status", s.handleStatus)
	router.Get("/session/{token}/statusevents", s.handleStatusEvents)
	router.Get("/session/{token}/result", s.handleResult)
Sietse Ringers's avatar
Sietse Ringers committed
186 187

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

191
	router.Get("/publickey", s.handlePublicKey)
192

193
	return router
194 195
}

196
func (s *Server) StaticFilesHandler() http.Handler {
197 198 199 200 201 202
	if len(s.conf.URL) > 6 {
		url := s.conf.URL[:len(s.conf.URL)-6] + s.conf.StaticPrefix
		s.conf.Logger.Infof("Hosting files at %s under %s", s.conf.StaticPath, url)
	} else { // URL not known, don't log it but otherwise continue
		s.conf.Logger.Infof("Hosting files at %s", s.conf.StaticPath)
	}
203 204 205 206 207 208 209 210
	// Hook up chi middleware logger with our own logger
	middleware.DefaultLogger = middleware.RequestLogger(&middleware.DefaultLogFormatter{
		Logger:  log.New(s.conf.Logger.WriterLevel(logrus.TraceLevel), "static: ", 0),
		NoColor: true,
	})
	return http.StripPrefix(s.conf.StaticPrefix, middleware.Logger(http.FileServer(http.Dir(s.conf.StaticPath))))
}

211
func (s *Server) handleCreate(w http.ResponseWriter, r *http.Request) {
212 213
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
214
		s.conf.Logger.Error("Could not read session request HTTP POST body")
215
		_ = server.LogError(err)
Sietse Ringers's avatar
Sietse Ringers committed
216
		server.WriteError(w, server.ErrorInvalidRequest, err.Error())
217 218
		return
	}
219

Sietse Ringers's avatar
Sietse Ringers committed
220 221 222
	// 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.
223
	var (
224
		rrequest  irma.RequestorRequest
225 226 227 228 229
		request   irma.SessionRequest
		requestor string
		rerr      *irma.RemoteError
		applies   bool
	)
Sietse Ringers's avatar
Sietse Ringers committed
230
	for _, authenticator := range authenticators { // rrequest abbreviates "requestor request"
231
		applies, rrequest, requestor, rerr = authenticator.Authenticate(r.Header, body)
232 233 234 235 236
		if applies || rerr != nil {
			break
		}
	}
	if rerr != nil {
237
		_ = server.LogError(rerr)
238 239 240 241
		server.WriteResponse(w, nil, rerr)
		return
	}
	if !applies {
242
		s.conf.Logger.Warnf("Session request uses unknown authentication method, HTTP headers: %s, HTTP POST body: %s",
243
			server.ToJson(r.Header), string(body))
Sietse Ringers's avatar
Sietse Ringers committed
244
		server.WriteError(w, server.ErrorInvalidRequest, "Request could not be authorized")
245 246 247
		return
	}

248 249
	// Authorize request: check if the requestor is allowed to verify or issue
	// the requested attributes or credentials
250
	request = rrequest.SessionRequest()
251
	if request.Action() == irma.ActionIssuing {
252
		allowed, reason := s.conf.CanIssue(requestor, request.(*irma.IssuanceRequest).Credentials)
253
		if !allowed {
254
			s.conf.Logger.WithFields(logrus.Fields{"requestor": requestor, "id": reason}).
255
				Warn("Requestor not authorized to issue credential; full request: ", server.ToJson(request))
256 257 258 259
			server.WriteError(w, server.ErrorUnauthorized, reason)
			return
		}
	}
Sietse Ringers's avatar
Sietse Ringers committed
260
	disjunctions := request.ToDisclose()
261
	if len(disjunctions) > 0 {
262
		allowed, reason := s.conf.CanVerifyOrSign(requestor, request.Action(), disjunctions)
263
		if !allowed {
264
			s.conf.Logger.WithFields(logrus.Fields{"requestor": requestor, "id": reason}).
265
				Warn("Requestor not authorized to verify attribute; full request: ", server.ToJson(request))
266 267 268 269
			server.WriteError(w, server.ErrorUnauthorized, reason)
			return
		}
	}
270 271
	if rrequest.Base().CallbackUrl != "" && s.conf.jwtPrivateKey == nil {
		s.conf.Logger.WithFields(logrus.Fields{"requestor": requestor}).Warn("Requestor provided callbackUrl but no JWT private key is installed")
272 273 274
		server.WriteError(w, server.ErrorUnsupported, "")
		return
	}
275 276

	// Everything is authenticated and parsed, we're good to go!
277
	qr, token, err := s.irmaserv.StartSession(rrequest, s.doResultCallback)
278
	if err != nil {
Sietse Ringers's avatar
Sietse Ringers committed
279
		server.WriteError(w, server.ErrorInvalidRequest, err.Error())
280 281 282
		return
	}

283 284 285 286
	server.WriteJson(w, server.SessionPackage{
		SessionPtr: qr,
		Token:      token,
	})
287 288
}

289 290
func (s *Server) handleStatus(w http.ResponseWriter, r *http.Request) {
	res := s.irmaserv.GetSessionResult(chi.URLParam(r, "token"))
291
	if res == nil {
Sietse Ringers's avatar
Sietse Ringers committed
292
		server.WriteError(w, server.ErrorSessionUnknown, "")
293 294
		return
	}
Sietse Ringers's avatar
Sietse Ringers committed
295
	server.WriteJson(w, res.Status)
296 297
}

298
func (s *Server) handleStatusEvents(w http.ResponseWriter, r *http.Request) {
299
	token := chi.URLParam(r, "token")
300
	s.conf.Logger.WithFields(logrus.Fields{"session": token}).Debug("new client subscribed to server sent events")
301
	if err := s.irmaserv.SubscribeServerSentEvents(w, r, token, true); err != nil {
302 303 304 305 306
		server.WriteResponse(w, nil, &irma.RemoteError{
			Status:      server.ErrorUnsupported.Status,
			ErrorName:   string(server.ErrorUnsupported.Type),
			Description: server.ErrorUnsupported.Description,
		})
307 308 309
	}
}

310 311
func (s *Server) handleDelete(w http.ResponseWriter, r *http.Request) {
	err := s.irmaserv.CancelSession(chi.URLParam(r, "token"))
312 313 314 315 316
	if err != nil {
		server.WriteError(w, server.ErrorSessionUnknown, "")
	}
}

317 318
func (s *Server) handleResult(w http.ResponseWriter, r *http.Request) {
	res := s.irmaserv.GetSessionResult(chi.URLParam(r, "token"))
319
	if res == nil {
Sietse Ringers's avatar
Sietse Ringers committed
320
		server.WriteError(w, server.ErrorSessionUnknown, "")
321 322
		return
	}
Sietse Ringers's avatar
Sietse Ringers committed
323
	server.WriteJson(w, res)
324 325
}

326 327 328
func (s *Server) handleJwtResult(w http.ResponseWriter, r *http.Request) {
	if s.conf.jwtPrivateKey == nil {
		s.conf.Logger.Warn("Session result JWT requested but no JWT private key is configured")
329 330
		server.WriteError(w, server.ErrorUnknown, "JWT signing not supported")
		return
331
	}
332

333
	sessiontoken := chi.URLParam(r, "token")
334
	res := s.irmaserv.GetSessionResult(sessiontoken)
335 336 337
	if res == nil {
		server.WriteError(w, server.ErrorSessionUnknown, "")
		return
338
	}
339

340
	j, err := s.resultJwt(res)
341
	if err != nil {
342
		s.conf.Logger.Error("Failed to sign session result JWT")
343
		_ = server.LogError(err)
344 345 346
		server.WriteError(w, server.ErrorUnknown, err.Error())
		return
	}
347
	server.WriteString(w, j)
348 349
}

350 351 352
func (s *Server) handleJwtProofs(w http.ResponseWriter, r *http.Request) {
	if s.conf.jwtPrivateKey == nil {
		s.conf.Logger.Warn("Session result JWT requested but no JWT private key is configured")
353 354 355 356
		server.WriteError(w, server.ErrorUnknown, "JWT signing not supported")
		return
	}

357
	sessiontoken := chi.URLParam(r, "token")
358
	res := s.irmaserv.GetSessionResult(sessiontoken)
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
	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()
379 380
	if s.conf.JwtIssuer != "" {
		claims["iss"] = s.conf.JwtIssuer
381 382
	}
	claims["status"] = res.Status
383
	validity := s.irmaserv.GetRequest(sessiontoken).Base().ResultJwtValidity
384 385 386
	if validity != 0 {
		claims["exp"] = time.Now().Unix() + int64(validity)
	}
387 388 389 390 391 392 393 394 395 396 397 398 399

	// 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)
400
	resultJwt, err := token.SignedString(s.conf.jwtPrivateKey)
401
	if err != nil {
402
		s.conf.Logger.Error("Failed to sign session result JWT")
403
		_ = server.LogError(err)
404 405
		server.WriteError(w, server.ErrorUnknown, err.Error())
		return
406
	}
407
	server.WriteString(w, resultJwt)
408
}
409

410 411
func (s *Server) handlePublicKey(w http.ResponseWriter, r *http.Request) {
	if s.conf.jwtPrivateKey == nil {
412 413 414 415
		server.WriteError(w, server.ErrorUnsupported, "")
		return
	}

416
	bts, err := x509.MarshalPKIXPublicKey(&s.conf.jwtPrivateKey.PublicKey)
417 418 419 420 421 422 423 424 425 426
	if err != nil {
		server.WriteError(w, server.ErrorUnknown, err.Error())
		return
	}
	pubBytes := pem.EncodeToMemory(&pem.Block{
		Type:  "PUBLIC KEY",
		Bytes: bts,
	})
	_, _ = w.Write(pubBytes)
}
427

428
func (s *Server) resultJwt(sessionresult *server.SessionResult) (string, error) {
429 430 431 432 433
	claims := struct {
		jwt.StandardClaims
		*server.SessionResult
	}{
		StandardClaims: jwt.StandardClaims{
434
			Issuer:   s.conf.JwtIssuer,
435 436 437 438 439
			IssuedAt: time.Now().Unix(),
			Subject:  string(sessionresult.Type) + "_result",
		},
		SessionResult: sessionresult,
	}
440
	validity := s.irmaserv.GetRequest(sessionresult.Token).Base().ResultJwtValidity
441 442 443 444 445 446
	if validity != 0 {
		claims.ExpiresAt = time.Now().Unix() + int64(validity)
	}

	// Sign the jwt and return it
	token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
447
	return token.SignedString(s.conf.jwtPrivateKey)
448 449
}

450 451 452
func (s *Server) doResultCallback(result *server.SessionResult) {
	callbackUrl := s.irmaserv.GetRequest(result.Token).Base().CallbackUrl
	if callbackUrl == "" || s.conf.jwtPrivateKey == nil {
453 454
		return
	}
455
	s.conf.Logger.WithFields(logrus.Fields{"session": result.Token, "callbackUrl": callbackUrl}).Debug("POSTing session result")
456

457
	j, err := s.resultJwt(result)
458 459 460 461 462 463 464 465
	if err != nil {
		_ = server.LogError(errors.WrapPrefix(err, "Failed to create JWT for result callback", 0))
		return
	}

	var x string // dummy for the server's return value that we don't care about
	if err := irma.NewHTTPTransport(callbackUrl).Post("", &x, j); err != nil {
		// not our problem, log it and go on
466
		s.conf.Logger.Warn(errors.WrapPrefix(err, "Failed to POST session result to callback URL", 0))
467 468
	}
}