handle.go 2.85 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package backend

import (
	"encoding/json"
	"net/http"
	"runtime/debug"

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

var conf *irmaserver.Configuration

15
16
17
func (session *session) handleDelete() {
	if !session.alive() {
		return
18
	}
19
	session.result = &irmaserver.SessionResult{Token: session.token} // TODO what to return here?
20
21
22
	session.status = irmaserver.StatusCancelled
}

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

33
34
func handleGetStatus(session *session) irmaserver.Status {
	return session.status
35
36
}

37
func (session *session) handlePostSignature(signature *irma.SignedMessage) (irma.ProofStatus, *irma.RemoteError) {
38
39
	session.signature = signature
	session.disclosed, session.proofStatus = signature.Verify(conf.IrmaConfiguration, session.request.(*irma.SignatureRequest))
40
41
	session.finish()
	return session.proofStatus, nil
42
}
43

44
func (session *session) handlePostProofs(proofs gabi.ProofList) (irma.ProofStatus, *irma.RemoteError) {
45
	session.disclosed, session.proofStatus = irma.ProofList(proofs).Verify(conf.IrmaConfiguration, session.request.(*irma.DisclosureRequest))
46
47
	session.finish()
	return session.proofStatus, nil
48
49
}

50
// Session helpers
51
52
53
54
55

func (session *session) alive() bool {
	return session.status != irmaserver.StatusDone && session.status != irmaserver.StatusCancelled
}

56
func (session *session) finish() {
57
	session.status = irmaserver.StatusDone
58
	session.result = &irmaserver.SessionResult{
59
60
61
62
63
64
65
		Token:     session.token,
		Status:    session.proofStatus,
		Disclosed: session.disclosed,
		Signature: session.signature,
	}
}

66
67
68
69
70
71
72
73
74
75
76
77
78
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{
79
80
81
82
		Status:      err.Status,
		Description: err.Description,
		ErrorName:   string(err.Type),
		Message:     message,
83
		Stacktrace:  stack,
84
	}
85
}
86

87
88
89
90
91
92
93
94
95
96
97
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
98
	}
99
	return status, b
100
}