handlers.go 3.68 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package irmaclient

import (
	"github.com/go-errors/errors"
	"github.com/privacybydesign/irmago"
)

// keyshareEnrollmentHandler handles the keyshare attribute issuance session
// after registering to a new keyshare server.
type keyshareEnrollmentHandler struct {
	pin    string
	client *Client
	kss    *keyshareServer
}

// Force keyshareEnrollmentHandler to implement the Handler interface
var _ Handler = (*keyshareEnrollmentHandler)(nil)

// Session handlers in the order they are called

21
func (h *keyshareEnrollmentHandler) RequestIssuancePermission(request *irma.IssuanceRequest, candidates [][][]*irma.AttributeIdentifier, ServerName irma.TranslatedString, callback PermissionHandler) {
22
	// Fetch the username from the credential request and save it along with the scheme manager
23
24
25
26
	for _, attr := range request.Credentials[0].Attributes {
		h.kss.Username = attr
		break
	}
27
28

	// Do the issuance
29
30
31
32
33
34
35
36
37
38
39
	callback(true, nil)
}

func (h *keyshareEnrollmentHandler) RequestPin(remainingAttempts int, callback PinHandler) {
	if remainingAttempts == -1 { // -1 signifies that this is the first attempt
		callback(true, h.pin)
	} else {
		h.fail(errors.New("PIN incorrect"))
	}
}

40
func (h *keyshareEnrollmentHandler) Success(result string) {
41
42
43
44
	_ = h.client.storage.StoreKeyshareServers(h.client.keyshareServers) // TODO handle err?
	h.client.handler.EnrollmentSuccess(h.kss.SchemeManagerIdentifier)
}

45
func (h *keyshareEnrollmentHandler) Failure(err *irma.SessionError) {
46
47
48
49
50
51
	h.fail(err)
}

// fail is a helper to ensure the kss is removed from the client in case of any problem
func (h *keyshareEnrollmentHandler) fail(err error) {
	delete(h.client.keyshareServers, h.kss.SchemeManagerIdentifier)
Tomas's avatar
Tomas committed
52
	h.client.handler.EnrollmentFailure(h.kss.SchemeManagerIdentifier, err)
53
54
55
56
57
58
}

// Not interested, ingore
func (h *keyshareEnrollmentHandler) StatusUpdate(action irma.Action, status irma.Status) {}

// The methods below should never be called, so we let each of them fail the session
59
func (h *keyshareEnrollmentHandler) RequestVerificationPermission(request *irma.DisclosureRequest, candidates [][][]*irma.AttributeIdentifier, ServerName irma.TranslatedString, callback PermissionHandler) {
60
61
	callback(false, nil)
}
62
func (h *keyshareEnrollmentHandler) RequestSignaturePermission(request *irma.SignatureRequest, candidates [][][]*irma.AttributeIdentifier, ServerName irma.TranslatedString, callback PermissionHandler) {
63
64
65
66
67
	callback(false, nil)
}
func (h *keyshareEnrollmentHandler) RequestSchemeManagerPermission(manager *irma.SchemeManager, callback func(proceed bool)) {
	callback(false)
}
68
func (h *keyshareEnrollmentHandler) Cancelled() {
69
70
71
72
73
74
75
76
	h.fail(errors.New("Keyshare enrollment session unexpectedly cancelled"))
}
func (h *keyshareEnrollmentHandler) KeyshareBlocked(manager irma.SchemeManagerIdentifier, duration int) {
	h.fail(errors.New("Keyshare enrollment failed: blocked"))
}
func (h *keyshareEnrollmentHandler) KeyshareEnrollmentIncomplete(manager irma.SchemeManagerIdentifier) {
	h.fail(errors.New("Keyshare enrollment failed: registration incomplete"))
}
77
78
79
func (h *keyshareEnrollmentHandler) KeyshareEnrollmentDeleted(manager irma.SchemeManagerIdentifier) {
	h.fail(errors.New("Keyshare enrollment failed: not enrolled"))
}
80
81
82
func (h *keyshareEnrollmentHandler) KeyshareEnrollmentMissing(manager irma.SchemeManagerIdentifier) {
	h.fail(errors.New("Keyshare enrollment failed: unenrolled"))
}
83
func (h *keyshareEnrollmentHandler) UnsatisfiableRequest(request irma.SessionRequest, ServerName irma.TranslatedString, missing MissingAttributes) {
84
85
	h.fail(errors.New("Keyshare enrollment failed: unsatisfiable"))
}
86
87
88
func (h *keyshareEnrollmentHandler) ClientReturnURLFound(clientReturnURL string) {
	h.fail(errors.New("Keyshare enrollment session unexpectedly found an external return url"))
}