main.go 1.34 KB
Newer Older
1
2
3
4
5
6
7
package irmarequestor

import (
	"io/ioutil"
	"net/http"

	"github.com/privacybydesign/irmago"
Sietse Ringers's avatar
Sietse Ringers committed
8
9
	"github.com/privacybydesign/irmago/server"
	"github.com/privacybydesign/irmago/server/backend"
10
11
)

Sietse Ringers's avatar
Sietse Ringers committed
12
type SessionHandler func(*server.SessionResult)
13

Sietse Ringers's avatar
Sietse Ringers committed
14
var handlers = make(map[string]SessionHandler)
15

Sietse Ringers's avatar
Sietse Ringers committed
16
func Initialize(configuration *server.Configuration) error {
17
18
19
20
21
22
23
24
25
	return backend.Initialize(configuration)
}

func StartSession(request irma.SessionRequest, handler SessionHandler) (*irma.Qr, string, error) {
	qr, token, err := backend.StartSession(request)
	if err != nil {
		return nil, "", err
	}
	if handler != nil {
Sietse Ringers's avatar
Sietse Ringers committed
26
		handlers[token] = handler
27
28
29
30
	}
	return qr, token, nil
}

Sietse Ringers's avatar
Sietse Ringers committed
31
func GetSessionResult(token string) *server.SessionResult {
Sietse Ringers's avatar
Sietse Ringers committed
32
33
34
	return backend.GetSessionResult(token)
}

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
func HttpHandlerFunc(prefix string) http.HandlerFunc {
	if len(prefix) != 0 && prefix[0] != '/' {
		prefix = "/" + prefix
	}
	return func(w http.ResponseWriter, r *http.Request) {
		var message []byte
		message, err := ioutil.ReadAll(r.Body)
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		path := r.URL.Path[len(prefix):]
		status, response, result := backend.HandleProtocolMessage(path, r.Method, r.Header, message)
		w.WriteHeader(status)
		w.Write(response)
		if result != nil {
Sietse Ringers's avatar
Sietse Ringers committed
51
			if handler, ok := handlers[result.Token]; ok {
52
53
54
55
56
				go handler(result)
			}
		}
	}
}