handle.go 2.92 KB
Newer Older
1
2
3
4
5
6
package backend

import (
	"encoding/json"
	"net/http"
	"runtime/debug"
7
	"time"
8
9
10
11
12
13
14
15

	"github.com/mhe/gabi"
	"github.com/privacybydesign/irmago"
	"github.com/privacybydesign/irmago/irmaserver"
)

var conf *irmaserver.Configuration

16
func (session *session) handleDelete() {
17
	if session.finished() {
18
		return
19
	}
20
21
22
	session.markAlive()
	// TODO const ProofStatusCancelled = irma.ProofStatus("CANCELLED") ?
	session.result = &irmaserver.SessionResult{Token: session.token}
23
24
25
	session.status = irmaserver.StatusCancelled
}

26
func (session *session) handleGetRequest(min, max *irma.ProtocolVersion) (irma.SessionRequest, *irma.RemoteError) {
27
28
	var err error
	if session.version, err = chooseProtocolVersion(min, max); err != nil {
29
		return nil, session.fail(irmaserver.ErrorProtocolVersion, "")
30
	}
31
32
	session.markAlive()
	session.status = irmaserver.StatusConnected
33
	session.request.SetVersion(session.version)
34
	return session.request, nil
35
36
}

37
38
func handleGetStatus(session *session) irmaserver.Status {
	return session.status
39
40
}

41
func (session *session) handlePostSignature(signature *irma.SignedMessage) (irma.ProofStatus, *irma.RemoteError) {
42
43
44
45
	session.markAlive()
	session.result.Signature = signature
	session.result.Disclosed, session.result.Status = signature.Verify(
		conf.IrmaConfiguration, session.request.(*irma.SignatureRequest))
46
	session.finish()
47
	return session.result.Status, nil
48
}
49

50
func (session *session) handlePostProofs(proofs gabi.ProofList) (irma.ProofStatus, *irma.RemoteError) {
51
52
53
	session.markAlive()
	session.result.Disclosed, session.result.Status = irma.ProofList(proofs).Verify(
		conf.IrmaConfiguration, session.request.(*irma.DisclosureRequest))
54
	session.finish()
55
	return session.result.Status, nil
56
57
}

58
// Session helpers
59

60
61
func (session *session) finished() bool {
	return session.status == irmaserver.StatusDone || session.status == irmaserver.StatusCancelled
62
63
}

64
func (session *session) finish() {
65
	session.status = irmaserver.StatusDone
66
67
68
69
}

func (session *session) markAlive() {
	session.lastActive = time.Now()
70
71
}

72
73
74
75
76
77
78
79
80
81
82
83
84
func (session *session) fail(err irmaserver.Error, message string) *irma.RemoteError {
	rerr := getError(err, message)
	session.status = irmaserver.StatusCancelled
	session.result = &irmaserver.SessionResult{Err: rerr, Token: session.token}
	return rerr
}

// Output helpers

func getError(err irmaserver.Error, message string) *irma.RemoteError {
	stack := string(debug.Stack())
	conf.Logger.Errorf("Error: %d %s %s\n%s", err.Status, err.Type, message, stack)
	return &irma.RemoteError{
85
86
87
88
		Status:      err.Status,
		Description: err.Description,
		ErrorName:   string(err.Type),
		Message:     message,
89
		Stacktrace:  stack,
90
	}
91
}
92

93
94
95
96
97
98
99
100
101
102
103
func responseJson(v interface{}, err *irma.RemoteError) (int, []byte) {
	msg := v
	status := http.StatusOK
	if err != nil {
		msg = err
		status = err.Status
	}
	b, e := json.Marshal(msg)
	if e != nil {
		conf.Logger.Error("Failed to serialize response:", e.Error())
		return http.StatusInternalServerError, nil
104
	}
105
	return status, b
106
}