api.go 13.7 KB
Newer Older
Sietse Ringers's avatar
Sietse Ringers committed
1
// Package servercore is the core of the IRMA server library, allowing IRMA verifiers, issuers
2 3
// or attribute-based signature applications to perform IRMA sessions with irmaclient instances
// (i.e. the IRMA app). It exposes a small interface to expose to other programming languages
Sietse Ringers's avatar
Sietse Ringers committed
4
// through cgo. It is used by the irmaserver package but otherwise not meant for use in Go.
5
package servercore
6 7 8

import (
	"encoding/json"
9
	"io/ioutil"
10
	"net/http"
11
	"path/filepath"
12
	"regexp"
13
	"strings"
14
	"time"
15 16

	"github.com/go-errors/errors"
17
	"github.com/jasonlvhit/gocron"
18 19
	"github.com/privacybydesign/gabi"
	"github.com/privacybydesign/gabi/big"
20
	"github.com/privacybydesign/irmago"
21
	"github.com/privacybydesign/irmago/internal/fs"
Sietse Ringers's avatar
Sietse Ringers committed
22
	"github.com/privacybydesign/irmago/server"
23
	"github.com/sirupsen/logrus"
24 25
)

26
type Server struct {
27 28 29 30
	conf          *server.Configuration
	sessions      sessionStore
	scheduler     *gocron.Scheduler
	stopScheduler chan bool
31 32 33 34 35 36 37
}

func New(conf *server.Configuration) (*Server, error) {
	s := &Server{
		conf:      conf,
		scheduler: gocron.NewScheduler(),
		sessions: &memorySessionStore{
38 39 40
			requestor: make(map[string]*session),
			client:    make(map[string]*session),
			conf:      conf,
41 42 43 44 45
		},
	}
	s.scheduler.Every(10).Seconds().Do(func() {
		s.sessions.deleteExpired()
	})
46
	s.stopScheduler = s.scheduler.Start()
47 48 49

	return s, s.verifyConfiguration(s.conf)
}
50

51 52 53 54 55
func (s *Server) Stop() {
	s.stopScheduler <- true
	s.sessions.stop()
}

56 57
func (s *Server) verifyConfiguration(configuration *server.Configuration) error {
	if s.conf.Logger == nil {
58
		s.conf.Logger = server.NewLogger(s.conf.Verbose, s.conf.Quiet, s.conf.LogJSON)
59
	}
60 61
	server.Logger = s.conf.Logger
	irma.Logger = s.conf.Logger
62

63
	if s.conf.IrmaConfiguration == nil {
64 65 66 67 68 69 70 71 72 73 74
		var (
			err    error
			exists bool
		)
		if s.conf.SchemesPath == "" {
			s.conf.SchemesPath = server.DefaultSchemesPath() // Returns an existing path
		}
		if exists, err = fs.PathExists(s.conf.SchemesPath); err != nil {
			return server.LogError(err)
		}
		if !exists {
75
			return server.LogError(errors.Errorf("Nonexisting schemes_path provided: %s", s.conf.SchemesPath))
76
		}
77
		s.conf.Logger.WithField("schemes_path", s.conf.SchemesPath).Info("Determined schemes path")
78 79
		if s.conf.SchemesAssetsPath == "" {
			s.conf.IrmaConfiguration, err = irma.NewConfiguration(s.conf.SchemesPath)
80
		} else {
81
			s.conf.IrmaConfiguration, err = irma.NewConfigurationFromAssets(s.conf.SchemesPath, s.conf.SchemesAssetsPath)
82
		}
83
		if err != nil {
84
			return server.LogError(err)
85
		}
86
		if err = s.conf.IrmaConfiguration.ParseFolder(); err != nil {
87
			return server.LogError(err)
88 89 90
		}
	}

91
	if len(s.conf.IrmaConfiguration.SchemeManagers) == 0 {
92 93 94
		s.conf.Logger.Infof("No schemes found in %s, downloading default (irma-demo and pbdf)", s.conf.SchemesPath)
		if err := s.conf.IrmaConfiguration.DownloadDefaultSchemes(); err != nil {
			return server.LogError(err)
95
		}
96
	}
97 98 99 100 101
	if s.conf.SchemesUpdateInterval == 0 {
		s.conf.SchemesUpdateInterval = 60
	}
	if !s.conf.DisableSchemesUpdate {
		s.conf.IrmaConfiguration.AutoUpdateSchemes(uint(s.conf.SchemesUpdateInterval))
Sietse Ringers's avatar
Sietse Ringers committed
102 103
	}

104 105
	if s.conf.IssuerPrivateKeys == nil {
		s.conf.IssuerPrivateKeys = make(map[irma.IssuerIdentifier]*gabi.PrivateKey)
106
	}
107 108
	if s.conf.IssuerPrivateKeysPath != "" {
		files, err := ioutil.ReadDir(s.conf.IssuerPrivateKeysPath)
109
		if err != nil {
110
			return server.LogError(err)
111 112 113
		}
		for _, file := range files {
			filename := file.Name()
114 115
			if filepath.Ext(filename) != ".xml" || filename[0] == '.' || strings.Count(filename, ".") != 2 {
				s.conf.Logger.WithField("file", filename).Infof("Skipping non-private key file encountered in private keys path")
116 117
				continue
			}
118
			issid := irma.NewIssuerIdentifier(strings.TrimSuffix(filename, filepath.Ext(filename))) // strip .xml
119
			if _, ok := s.conf.IrmaConfiguration.Issuers[issid]; !ok {
120
				return server.LogError(errors.Errorf("Private key %s belongs to an unknown issuer", filename))
121
			}
122
			sk, err := gabi.NewPrivateKeyFromFile(filepath.Join(s.conf.IssuerPrivateKeysPath, filename))
123
			if err != nil {
124
				return server.LogError(err)
125
			}
126
			s.conf.IssuerPrivateKeys[issid] = sk
127 128
		}
	}
129 130
	for issid, sk := range s.conf.IssuerPrivateKeys {
		pk, err := s.conf.IrmaConfiguration.PublicKey(issid, int(sk.Counter))
131
		if err != nil {
132
			return server.LogError(err)
133 134
		}
		if pk == nil {
135
			return server.LogError(errors.Errorf("Missing public key belonging to private key %s-%d", issid.String(), sk.Counter))
136 137
		}
		if new(big.Int).Mul(sk.P, sk.Q).Cmp(pk.N) != 0 {
138
			return server.LogError(errors.Errorf("Private key %s-%d does not belong to corresponding public key", issid.String(), sk.Counter))
139 140 141
		}
	}

142 143 144
	if s.conf.URL != "" {
		if !strings.HasSuffix(s.conf.URL, "/") {
			s.conf.URL = s.conf.URL + "/"
145
		}
146 147 148 149 150 151 152 153 154 155
		if !strings.HasPrefix(s.conf.URL, "https://") {
			if !s.conf.Production || s.conf.DisableTLS {
				s.conf.DisableTLS = true
				s.conf.Logger.Warnf("TLS is not enabled on the url \"%s\" to which the IRMA app will connect. "+
					"Ensure that attributes are encrypted in transit by either enabling TLS or adding TLS in a reverse proxy.", s.conf.URL)
			} else {
				return server.LogError(errors.Errorf("Running without TLS in production mode is unsafe without a reverse proxy. " +
					"Either use a https:// URL or explicitly disable TLS."))
			}
		}
156
	} else {
157
		s.conf.Logger.Warn("No url parameter specified in configuration; unless an url is elsewhere prepended in the QR, the IRMA client will not be able to connect")
158 159
	}

Sietse Ringers's avatar
Sietse Ringers committed
160 161 162 163 164
	if s.conf.Email != "" {
		// Very basic sanity checks
		if !strings.Contains(s.conf.Email, "@") || strings.Contains(s.conf.Email, "\n") {
			return server.LogError(errors.New("Invalid email address specified"))
		}
Sietse Ringers's avatar
Sietse Ringers committed
165
		t := irma.NewHTTPTransport("https://metrics.privacybydesign.foundation/history")
Sietse Ringers's avatar
Sietse Ringers committed
166 167
		t.SetHeader("User-Agent", "irmaserver")
		var x string
Sietse Ringers's avatar
Sietse Ringers committed
168
		_ = t.Post("email", &x, s.conf.Email)
Sietse Ringers's avatar
Sietse Ringers committed
169 170
	}

171 172 173
	return nil
}

174
func (s *Server) validateRequest(request irma.SessionRequest) error {
175 176 177 178
	if _, err := s.conf.IrmaConfiguration.Download(request); err != nil {
		return err
	}
	return request.Disclosure().Disclose.Validate(s.conf.IrmaConfiguration)
179 180
}

181
func (s *Server) StartSession(req interface{}) (*irma.Qr, string, error) {
182 183
	rrequest, err := server.ParseSessionRequest(req)
	if err != nil {
184
		return nil, "", err
185
	}
186 187 188

	request := rrequest.SessionRequest()
	action := request.Action()
189 190 191 192 193

	if err := s.validateRequest(request); err != nil {
		return nil, "", err
	}

194
	if action == irma.ActionIssuing {
195
		if err := s.validateIssuanceRequest(request.(*irma.IssuanceRequest)); err != nil {
196
			return nil, "", err
197 198 199
		}
	}

200 201 202 203
	session := s.newSession(action, rrequest)
	s.conf.Logger.WithFields(logrus.Fields{"action": action, "session": session.token}).Infof("Session started")
	if s.conf.Logger.IsLevelEnabled(logrus.DebugLevel) {
		s.conf.Logger.WithFields(logrus.Fields{"session": session.token}).Info("Session request: ", server.ToJson(rrequest))
204
	} else {
205
		s.conf.Logger.WithFields(logrus.Fields{"session": session.token}).Info("Session request (purged of attribute values): ", server.ToJson(purgeRequest(rrequest)))
206
	}
207 208
	return &irma.Qr{
		Type: action,
209
		URL:  s.conf.URL + session.clientToken,
210 211 212
	}, session.token, nil
}

213 214
func (s *Server) GetSessionResult(token string) *server.SessionResult {
	session := s.sessions.get(token)
215
	if session == nil {
216
		s.conf.Logger.Warn("Session result requested of unknown session ", token)
Sietse Ringers's avatar
Sietse Ringers committed
217 218 219 220 221
		return nil
	}
	return session.result
}

222 223
func (s *Server) GetRequest(token string) irma.RequestorRequest {
	session := s.sessions.get(token)
224
	if session == nil {
225
		s.conf.Logger.Warn("Session request requested of unknown session ", token)
226 227 228 229 230
		return nil
	}
	return session.rrequest
}

231 232
func (s *Server) CancelSession(token string) error {
	session := s.sessions.get(token)
233
	if session == nil {
234
		return server.LogError(errors.Errorf("can't cancel unknown session %s", token))
235 236 237 238 239
	}
	session.handleDelete()
	return nil
}

240 241 242 243 244 245 246 247 248
func ParsePath(path string) (string, string, error) {
	pattern := regexp.MustCompile("(\\w+)/?(|commitments|proofs|status|statusevents)$")
	matches := pattern.FindStringSubmatch(path)
	if len(matches) != 3 {
		return "", "", server.LogWarning(errors.Errorf("Invalid URL: %s", path))
	}
	return matches[1], matches[2], nil
}

249
func (s *Server) SubscribeServerSentEvents(w http.ResponseWriter, r *http.Request, token string, requestor bool) error {
250 251 252 253
	if !s.conf.EnableSSE {
		return errors.New("Server sent events disabled")
	}

254 255 256 257 258 259
	var session *session
	if requestor {
		session = s.sessions.get(token)
	} else {
		session = s.sessions.clientGet(token)
	}
260 261 262 263 264 265 266 267 268
	if session == nil {
		return server.LogError(errors.Errorf("can't subscribe to server sent events of unknown session %s", token))
	}
	if session.status.Finished() {
		return server.LogError(errors.Errorf("can't subscribe to server sent events of finished session %s", token))
	}

	session.Lock()
	defer session.Unlock()
269 270 271 272 273 274 275 276 277 278 279 280 281

	// The EventSource.onopen Javascript callback is not consistently called across browsers (Chrome yes, Firefox+Safari no).
	// However, when the SSE connection has been opened the webclient needs some signal so that it can early detect SSE failures.
	// So we manually send an "open" event. Unfortunately:
	// - we need to give the webclient that connected just now some time, otherwise it will miss the "open" event
	// - the "open" event also goes to all other webclients currently listening, as we have no way to send this
	//   event to just the webclient currently listening. (Thus the handler of this "open" event must be idempotent.)
	evtSource := session.eventSource()
	go func() {
		time.Sleep(200 * time.Millisecond)
		evtSource.SendEventMessage("", "open", "")
	}()
	evtSource.ServeHTTP(w, r)
282 283 284
	return nil
}

285
func (s *Server) HandleProtocolMessage(
286 287 288 289
	path string,
	method string,
	headers map[string][]string,
	message []byte,
290 291 292 293
) (int, []byte, *server.SessionResult) {
	var start time.Time
	if s.conf.Verbose >= 2 {
		start = time.Now()
294
		server.LogRequest(method, path, "", "", http.Header(headers), message)
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
	}

	status, output, result := s.handleProtocolMessage(path, method, headers, message)

	if s.conf.Verbose >= 2 {
		server.LogResponse(status, time.Now().Sub(start), output)
	}

	return status, output, result
}

func (s *Server) handleProtocolMessage(
	path string,
	method string,
	headers map[string][]string,
	message []byte,
Sietse Ringers's avatar
Sietse Ringers committed
311
) (status int, output []byte, result *server.SessionResult) {
312 313 314 315 316 317 318 319 320
	// Parse path into session and action
	if len(path) > 0 { // Remove any starting and trailing slash
		if path[0] == '/' {
			path = path[1:]
		}
		if path[len(path)-1] == '/' {
			path = path[:len(path)-1]
		}
	}
321

322 323 324
	token, noun, err := ParsePath(path)
	if err != nil {
		status, output = server.JsonResponse(nil, server.RemoteError(server.ErrorUnsupported, ""))
325
		return
326 327
	}

Sietse Ringers's avatar
Sietse Ringers committed
328
	// Fetch the session
329
	session := s.sessions.clientGet(token)
330
	if session == nil {
331
		s.conf.Logger.WithField("clientToken", token).Warn("Session not found")
Sietse Ringers's avatar
Sietse Ringers committed
332
		status, output = server.JsonResponse(nil, server.RemoteError(server.ErrorSessionUnknown, ""))
333
		return
334
	}
335 336
	session.Lock()
	defer session.Unlock()
337

338 339
	// However we return, if the session status has been updated
	// then we should inform the user by returning a SessionResult
340
	defer func() {
341 342
		if session.status != session.prevStatus {
			session.prevStatus = session.status
343 344 345 346
			result = session.result
		}
	}()

347
	// Route to handler
348
	switch len(noun) {
349
	case 0:
350
		if method == http.MethodDelete {
351 352 353
			session.handleDelete()
			status = http.StatusOK
			return
354
		}
355
		if method == http.MethodGet {
356 357 358 359
			h := http.Header(headers)
			min := &irma.ProtocolVersion{}
			max := &irma.ProtocolVersion{}
			if err := json.Unmarshal([]byte(h.Get(irma.MinVersionHeader)), min); err != nil {
Sietse Ringers's avatar
Sietse Ringers committed
360
				status, output = server.JsonResponse(nil, session.fail(server.ErrorMalformedInput, err.Error()))
361
				return
362 363
			}
			if err := json.Unmarshal([]byte(h.Get(irma.MaxVersionHeader)), max); err != nil {
Sietse Ringers's avatar
Sietse Ringers committed
364
				status, output = server.JsonResponse(nil, session.fail(server.ErrorMalformedInput, err.Error()))
365
				return
366
			}
Sietse Ringers's avatar
Sietse Ringers committed
367
			status, output = server.JsonResponse(session.handleGetRequest(min, max))
368
			return
369
		}
Sietse Ringers's avatar
Sietse Ringers committed
370
		status, output = server.JsonResponse(nil, session.fail(server.ErrorInvalidRequest, ""))
371
		return
372
	default:
373 374 375 376 377 378
		if noun == "statusevents" {
			err := server.RemoteError(server.ErrorInvalidRequest, "server sent events not supported by this server")
			status, output = server.JsonResponse(nil, err)
			return
		}

379 380
		if method == http.MethodGet && noun == "status" {
			status, output = server.JsonResponse(session.handleGetStatus())
Sietse Ringers's avatar
Sietse Ringers committed
381
			return
382 383 384
		}

		// Below are only POST enpoints
385
		if method != http.MethodPost {
Sietse Ringers's avatar
Sietse Ringers committed
386
			status, output = server.JsonResponse(nil, session.fail(server.ErrorInvalidRequest, ""))
Sietse Ringers's avatar
Sietse Ringers committed
387 388 389
			return
		}

390
		if noun == "commitments" && session.action == irma.ActionIssuing {
Sietse Ringers's avatar
Sietse Ringers committed
391
			commitments := &irma.IssueCommitmentMessage{}
Sietse Ringers's avatar
Sietse Ringers committed
392
			if err := irma.UnmarshalValidate(message, commitments); err != nil {
Sietse Ringers's avatar
Sietse Ringers committed
393
				status, output = server.JsonResponse(nil, session.fail(server.ErrorMalformedInput, ""))
394
				return
395
			}
Sietse Ringers's avatar
Sietse Ringers committed
396
			status, output = server.JsonResponse(session.handlePostCommitments(commitments))
Sietse Ringers's avatar
Sietse Ringers committed
397 398
			return
		}
399
		if noun == "proofs" && session.action == irma.ActionDisclosing {
Sietse Ringers's avatar
Sietse Ringers committed
400 401
			disclosure := irma.Disclosure{}
			if err := irma.UnmarshalValidate(message, &disclosure); err != nil {
Sietse Ringers's avatar
Sietse Ringers committed
402
				status, output = server.JsonResponse(nil, session.fail(server.ErrorMalformedInput, ""))
403
				return
404
			}
Sietse Ringers's avatar
Sietse Ringers committed
405
			status, output = server.JsonResponse(session.handlePostDisclosure(disclosure))
Sietse Ringers's avatar
Sietse Ringers committed
406 407
			return
		}
408
		if noun == "proofs" && session.action == irma.ActionSigning {
Sietse Ringers's avatar
Sietse Ringers committed
409 410
			signature := &irma.SignedMessage{}
			if err := irma.UnmarshalValidate(message, signature); err != nil {
Sietse Ringers's avatar
Sietse Ringers committed
411
				status, output = server.JsonResponse(nil, session.fail(server.ErrorMalformedInput, ""))
412
				return
413
			}
Sietse Ringers's avatar
Sietse Ringers committed
414
			status, output = server.JsonResponse(session.handlePostSignature(signature))
415
			return
416
		}
Sietse Ringers's avatar
Sietse Ringers committed
417

Sietse Ringers's avatar
Sietse Ringers committed
418
		status, output = server.JsonResponse(nil, session.fail(server.ErrorInvalidRequest, ""))
419
		return
420 421
	}
}