logs.go 3.16 KB
Newer Older
1
package irmaclient
Sietse Ringers's avatar
Sietse Ringers committed
2
3
4
5

import (
	"time"

Tomas's avatar
Tomas committed
6
	"github.com/bwesterb/go-atum"
Sietse Ringers's avatar
Sietse Ringers committed
7
8
	"github.com/go-errors/errors"
	"github.com/mhe/gabi"
9
	"github.com/privacybydesign/irmago"
Sietse Ringers's avatar
Sietse Ringers committed
10
11
)

Sietse Ringers's avatar
Sietse Ringers committed
12
// LogEntry is a log entry of a past event.
Sietse Ringers's avatar
Sietse Ringers committed
13
type LogEntry struct {
Sietse Ringers's avatar
Sietse Ringers committed
14
	// General info
15
16
17
18
	Type    irma.Action
	Time    irma.Timestamp        // Time at which the session was completed
	Version *irma.ProtocolVersion `json:",omitempty"` // Protocol version that was used in the session
	Request irma.SessionRequest   `json:",omitempty"` // Message that started the session
Sietse Ringers's avatar
Sietse Ringers committed
19

Sietse Ringers's avatar
Sietse Ringers committed
20
	// Session type-specific info
Tomas's avatar
Tomas committed
21
22
23
	Removed       map[irma.CredentialTypeIdentifier][]irma.TranslatedString `json:",omitempty"` // In case of credential removal
	SignedMessage []byte                                                    `json:",omitempty"` // In case of signature sessions
	Timestamp     *atum.Timestamp                                           `json:",omitempty"` // In case of signature sessions
Sietse Ringers's avatar
Sietse Ringers committed
24

Tomas's avatar
Tomas committed
25
26
	IssueCommitment *gabi.IssueCommitmentMessage `json:",omitempty"`
	ProofList       gabi.ProofList               `json:",omitempty"`
Sietse Ringers's avatar
Sietse Ringers committed
27
28
}

29
const actionRemoval = irma.Action("removal")
30

Tomas's avatar
Tomas committed
31
// GetDisclosedCredentials gets the list of disclosed credentials for a log entry
32
func (entry *LogEntry) GetDisclosedCredentials(conf *irma.Configuration) ([]*irma.DisclosedAttribute, error) {
Tomas's avatar
Tomas committed
33
	if entry.Type == actionRemoval {
34
		return []*irma.DisclosedAttribute{}, nil
Tomas's avatar
Tomas committed
35
36
	}
	var proofs gabi.ProofList
37
	disjunctions := entry.Request.ToDisclose()
Tomas's avatar
Tomas committed
38
39
40
41
42
	if entry.Type == irma.ActionIssuing {
		proofs = entry.IssueCommitment.Proofs
	} else {
		proofs = entry.ProofList
	}
43
44
	_, attrs, err := irma.ProofList(proofs).DisclosedAttributes(conf, disjunctions)
	return attrs, err
Tomas's avatar
Tomas committed
45
46
47
48
49
50
51
}

// GetIssuedCredentials gets the list of issued credentials for a log entry
func (entry *LogEntry) GetIssuedCredentials(conf *irma.Configuration) (list irma.CredentialInfoList, err error) {
	if entry.Type != irma.ActionIssuing {
		return irma.CredentialInfoList{}, nil
	}
52
	return entry.Request.(*irma.IssuanceRequest).GetCredentialInfoList(conf, entry.Version)
Tomas's avatar
Tomas committed
53
54
55
}

// GetSignedMessage gets the signed for a log entry
56
func (entry *LogEntry) GetSignedMessage() (abs *irma.SignedMessage, err error) {
Tomas's avatar
Tomas committed
57
58
59
	if entry.Type != irma.ActionSigning {
		return nil, nil
	}
60
	request := entry.Request.(*irma.SignatureRequest)
61
	return &irma.SignedMessage{
Tomas's avatar
Tomas committed
62
		Signature: entry.ProofList,
63
64
		Nonce:     request.Nonce,
		Context:   request.Context,
Tomas's avatar
Tomas committed
65
66
67
68
69
		Message:   string(entry.SignedMessage),
		Timestamp: entry.Timestamp,
	}, nil
}

70
func (session *session) createLogEntry(response interface{}) (*LogEntry, error) {
Sietse Ringers's avatar
Sietse Ringers committed
71
	entry := &LogEntry{
72
73
74
75
		Type:    session.Action,
		Time:    irma.Timestamp(time.Now()),
		Version: session.Version,
		Request: session.request,
Sietse Ringers's avatar
Sietse Ringers committed
76
77
78
	}

	switch entry.Type {
Tomas's avatar
Tomas committed
79
80
	case actionRemoval:

81
	case irma.ActionSigning:
Tomas's avatar
Tomas committed
82
		// Get the signed message and timestamp
83
		request := session.request.(*irma.SignatureRequest)
Tomas's avatar
Tomas committed
84
85
86
		entry.SignedMessage = []byte(request.Message)
		entry.Timestamp = request.Timestamp

Sietse Ringers's avatar
Sietse Ringers committed
87
		fallthrough
88
	case irma.ActionDisclosing:
Tomas's avatar
Tomas committed
89
		entry.ProofList = response.(gabi.ProofList)
90
	case irma.ActionIssuing:
Tomas's avatar
Tomas committed
91
		entry.IssueCommitment = response.(*gabi.IssueCommitmentMessage)
Sietse Ringers's avatar
Sietse Ringers committed
92
93
94
95
96
97
	default:
		return nil, errors.New("Invalid log type")
	}

	return entry, nil
}