logs.go 4.25 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"
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
	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
18
19
20

	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
21

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

27
28
	IssueCommitment *irma.IssueCommitmentMessage `json:",omitempty"`
	Disclosure      *irma.Disclosure             `json:",omitempty"`
Sietse Ringers's avatar
Sietse Ringers committed
29
30
}

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

Sietse Ringers's avatar
Sietse Ringers committed
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
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
64
// GetDisclosedCredentials gets the list of disclosed credentials for a log entry
65
func (entry *LogEntry) GetDisclosedCredentials(conf *irma.Configuration) ([][]*irma.DisclosedAttribute, error) {
Sietse Ringers's avatar
Sietse Ringers committed
66
	if entry.Type == actionRemoval {
67
		return [][]*irma.DisclosedAttribute{}, nil
Sietse Ringers's avatar
Sietse Ringers committed
68
69
70
71
72
73
	}

	request, err := entry.SessionRequest()
	if err != nil {
		return nil, err
	}
74
	var disclosure *irma.Disclosure
75
	disjunctions := request.Disclosure()
Sietse Ringers's avatar
Sietse Ringers committed
76
	if entry.Type == irma.ActionIssuing {
77
		disclosure = entry.IssueCommitment.Disclosure()
Sietse Ringers's avatar
Sietse Ringers committed
78
	} else {
79
		disclosure = entry.Disclosure
Sietse Ringers's avatar
Sietse Ringers committed
80
	}
81
	_, attrs, err := disclosure.DisclosedAttributes(conf, disjunctions.Disclose)
Sietse Ringers's avatar
Sietse Ringers committed
82
	return attrs, err
Tomas's avatar
Tomas committed
83
84
85
86
}

// 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
87
88
89
90
91
92
93
94
	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
95
96
97
}

// GetSignedMessage gets the signed for a log entry
98
func (entry *LogEntry) GetSignedMessage() (abs *irma.SignedMessage, err error) {
Sietse Ringers's avatar
Sietse Ringers committed
99
100
101
102
103
104
105
106
107
	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{
108
		Signature: entry.Disclosure.Proofs,
Sietse Ringers's avatar
Sietse Ringers committed
109
		Nonce:     sigrequest.Nonce,
110
		Context:   sigrequest.GetContext(),
Sietse Ringers's avatar
Sietse Ringers committed
111
112
113
		Message:   string(entry.SignedMessage),
		Timestamp: entry.Timestamp,
	}, nil
Tomas's avatar
Tomas committed
114
115
}

116
func (session *session) createLogEntry(response interface{}) (*LogEntry, error) {
Sietse Ringers's avatar
Sietse Ringers committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
	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:
139
		entry.Disclosure = response.(*irma.Disclosure)
Sietse Ringers's avatar
Sietse Ringers committed
140
	case irma.ActionIssuing:
141
		entry.IssueCommitment = response.(*irma.IssueCommitmentMessage)
Sietse Ringers's avatar
Sietse Ringers committed
142
143
144
145
146
	default:
		return nil, errors.New("Invalid log type")
	}

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