forked from privacybydesign/irmago
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logs.go
158 lines (135 loc) · 4.35 KB
/
logs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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
148
149
150
151
152
153
154
155
156
157
158
package irmaclient
import (
"encoding/json"
"time"
"github.com/bwesterb/go-atum"
"github.com/go-errors/errors"
"github.com/privacybydesign/irmago"
)
// LogEntry is a log entry of a past event.
type LogEntry struct {
// General info
ID uint64
Type irma.Action
Time irma.Timestamp // Time at which the session was completed
// Credential removal
Removed map[irma.CredentialTypeIdentifier][]irma.TranslatedString `json:",omitempty"`
// Signature sessions
SignedMessage []byte `json:",omitempty"`
Timestamp *atum.Timestamp `json:",omitempty"`
SignedMessageLDContext string `json:",omitempty"`
// Issuance sessions
IssueCommitment *irma.IssueCommitmentMessage `json:",omitempty"`
// All session types
ServerName irma.TranslatedString `json:",omitempty"`
Version *irma.ProtocolVersion `json:",omitempty"`
Disclosure *irma.Disclosure `json:",omitempty"`
Request json.RawMessage `json:",omitempty"` // Message that started the session
request irma.SessionRequest // cached parsed version of Request; get with LogEntry.SessionRequest()
}
const ActionRemoval = irma.Action("removal")
func (entry *LogEntry) SessionRequest() (irma.SessionRequest, error) {
if entry.request != nil {
return 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
}
// GetDisclosedCredentials gets the list of disclosed credentials for a log entry
func (entry *LogEntry) GetDisclosedCredentials(conf *irma.Configuration) ([][]*irma.DisclosedAttribute, error) {
if entry.Type == ActionRemoval {
return [][]*irma.DisclosedAttribute{}, nil
}
request, err := entry.SessionRequest()
if err != nil {
return nil, err
}
var disclosure *irma.Disclosure
disjunctions := request.Disclosure()
if entry.Type == irma.ActionIssuing {
disclosure = entry.IssueCommitment.Disclosure()
} else {
disclosure = entry.Disclosure
}
_, attrs, err := disclosure.DisclosedAttributes(conf, disjunctions.Disclose)
return attrs, err
}
// 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
}
request, err := entry.SessionRequest()
if err != nil {
return nil, err
}
return request.(*irma.IssuanceRequest).GetCredentialInfoList(conf, entry.Version)
}
// GetSignedMessage gets the signed for a log entry
func (entry *LogEntry) GetSignedMessage() (abs *irma.SignedMessage, err error) {
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{
LDContext: entry.SignedMessageLDContext,
Signature: entry.Disclosure.Proofs,
Nonce: sigrequest.Nonce,
Context: sigrequest.GetContext(),
Message: string(entry.SignedMessage),
Timestamp: entry.Timestamp,
}, nil
}
func (session *session) createLogEntry(response interface{}) (*LogEntry, error) {
entry := &LogEntry{
Type: session.Action,
Time: irma.Timestamp(time.Now()),
ServerName: session.ServerName,
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
entry.SignedMessage = []byte(session.request.(*irma.SignatureRequest).Message)
entry.Timestamp = session.timestamp
entry.SignedMessageLDContext = irma.LDContextSignedMessage
fallthrough
case irma.ActionDisclosing:
entry.Disclosure = response.(*irma.Disclosure)
case irma.ActionIssuing:
entry.IssueCommitment = response.(*irma.IssueCommitmentMessage)
default:
return nil, errors.New("Invalid log type")
}
return entry, nil
}