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

import (
Sietse Ringers's avatar
Sietse Ringers committed
4
5
6
	"encoding/json"
	"time"

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

Sietse Ringers's avatar
Sietse Ringers committed
13
// LogEntry is a log entry of a past event.
Sietse Ringers's avatar
Sietse Ringers committed
14
type LogEntry struct {
Sietse Ringers's avatar
Sietse Ringers committed
15
	// General info
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
Sietse Ringers's avatar
Sietse Ringers committed
19
20
21

	Request json.RawMessage     `json:",omitempty"` // Message that started the session
	request irma.SessionRequest // cached parsed version of Request; get with LogEntry.SessionRequest()
Sietse Ringers's avatar
Sietse Ringers committed
22

Sietse Ringers's avatar
Sietse Ringers committed
23
	// Session type-specific info
Tomas's avatar
Tomas committed
24
25
26
	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
27

Tomas's avatar
Tomas committed
28
29
	IssueCommitment *gabi.IssueCommitmentMessage `json:",omitempty"`
	ProofList       gabi.ProofList               `json:",omitempty"`
Sietse Ringers's avatar
Sietse Ringers committed
30
31
}

32
const actionRemoval = irma.Action("removal")
33

Sietse Ringers's avatar
Sietse Ringers committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
func (entry *LogEntry) SessionRequest() (irma.SessionRequest, error) {
	if entry.request == nil {
		switch entry.Type {
		case irma.ActionDisclosing:
			entry.request = &irma.DisclosureRequest{}
		case irma.ActionSigning:
			entry.request = &irma.SignatureRequest{}
		case irma.ActionIssuing:
			entry.request = &irma.IssuanceRequest{}
		default:
			return nil, nil
		}
	}

	err := json.Unmarshal([]byte(entry.Request), entry.request)
	if err != nil {
		return nil, err
	}

	return entry.request, nil
}

func (entry *LogEntry) setSessionRequest() error {
	bts, err := json.Marshal(entry.request)
	if err != nil {
		return err
	}
	entry.Request = json.RawMessage(bts)
	return nil
}

Tomas's avatar
Tomas committed
65
// GetDisclosedCredentials gets the list of disclosed credentials for a log entry
66
func (entry *LogEntry) GetDisclosedCredentials(conf *irma.Configuration) ([]*irma.DisclosedAttribute, error) {
Sietse Ringers's avatar
Sietse Ringers committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
	if entry.Type == actionRemoval {
		return []*irma.DisclosedAttribute{}, nil
	}

	request, err := entry.SessionRequest()
	if err != nil {
		return nil, err
	}
	var proofs gabi.ProofList
	disjunctions := request.ToDisclose()
	if entry.Type == irma.ActionIssuing {
		proofs = entry.IssueCommitment.Proofs
	} else {
		proofs = entry.ProofList
	}
	_, attrs, err := irma.ProofList(proofs).DisclosedAttributes(conf, disjunctions)
	return attrs, err
Tomas's avatar
Tomas committed
84
85
86
87
}

// GetIssuedCredentials gets the list of issued credentials for a log entry
func (entry *LogEntry) GetIssuedCredentials(conf *irma.Configuration) (list irma.CredentialInfoList, err error) {
Sietse Ringers's avatar
Sietse Ringers committed
88
89
90
91
92
93
94
95
	if entry.Type != irma.ActionIssuing {
		return irma.CredentialInfoList{}, nil
	}
	request, err := entry.SessionRequest()
	if err != nil {
		return nil, err
	}
	return request.(*irma.IssuanceRequest).GetCredentialInfoList(conf, entry.Version)
Tomas's avatar
Tomas committed
96
97
98
}

// GetSignedMessage gets the signed for a log entry
99
func (entry *LogEntry) GetSignedMessage() (abs *irma.SignedMessage, err error) {
Sietse Ringers's avatar
Sietse Ringers committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
	if entry.Type != irma.ActionSigning {
		return nil, nil
	}
	request, err := entry.SessionRequest()
	if err != nil {
		return nil, err
	}
	sigrequest := request.(*irma.SignatureRequest)
	return &irma.SignedMessage{
		Signature: entry.ProofList,
		Nonce:     sigrequest.Nonce,
		Context:   sigrequest.Context,
		Message:   string(entry.SignedMessage),
		Timestamp: entry.Timestamp,
	}, nil
Tomas's avatar
Tomas committed
115
116
}

117
func (session *session) createLogEntry(response interface{}) (*LogEntry, error) {
Sietse Ringers's avatar
Sietse Ringers committed
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
	entry := &LogEntry{
		Type:    session.Action,
		Time:    irma.Timestamp(time.Now()),
		Version: session.Version,
		request: session.request,
	}

	if err := entry.setSessionRequest(); err != nil {
		return nil, err
	}

	switch entry.Type {
	case actionRemoval:

	case irma.ActionSigning:
		// Get the signed message and timestamp
		request := session.request.(*irma.SignatureRequest)
		entry.SignedMessage = []byte(request.Message)
		entry.Timestamp = request.Timestamp

		fallthrough
	case irma.ActionDisclosing:
		entry.ProofList = response.(gabi.ProofList)
	case irma.ActionIssuing:
		entry.IssueCommitment = response.(*gabi.IssueCommitmentMessage)
	default:
		return nil, errors.New("Invalid log type")
	}

	return entry, nil
Sietse Ringers's avatar
Sietse Ringers committed
148
}