logs.go 4.51 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
23
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
	SignedMessageLDContext string                                                    `json:",omitempty"` // In case of signature sessions
Sietse Ringers's avatar
Sietse Ringers committed
27

28
29
	IssueCommitment *irma.IssueCommitmentMessage `json:",omitempty"`
	Disclosure      *irma.Disclosure             `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
	if entry.Type == actionRemoval {
68
		return [][]*irma.DisclosedAttribute{}, nil
Sietse Ringers's avatar
Sietse Ringers committed
69
70
71
72
73
74
	}

	request, err := entry.SessionRequest()
	if err != nil {
		return nil, err
	}
75
	var disclosure *irma.Disclosure
76
	disjunctions := request.Disclosure()
Sietse Ringers's avatar
Sietse Ringers committed
77
	if entry.Type == irma.ActionIssuing {
78
		disclosure = entry.IssueCommitment.Disclosure()
Sietse Ringers's avatar
Sietse Ringers committed
79
	} else {
80
		disclosure = entry.Disclosure
Sietse Ringers's avatar
Sietse Ringers committed
81
	}
82
	_, attrs, err := disclosure.DisclosedAttributes(conf, disjunctions.Disclose)
Sietse Ringers's avatar
Sietse Ringers committed
83
	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
	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{
109
		LDContext: entry.SignedMessageLDContext,
110
		Signature: entry.Disclosure.Proofs,
Sietse Ringers's avatar
Sietse Ringers committed
111
		Nonce:     sigrequest.Nonce,
112
		Context:   sigrequest.GetContext(),
Sietse Ringers's avatar
Sietse Ringers committed
113
114
115
		Message:   string(entry.SignedMessage),
		Timestamp: entry.Timestamp,
	}, nil
Tomas's avatar
Tomas committed
116
117
}

118
func (session *session) createLogEntry(response interface{}) (*LogEntry, error) {
Sietse Ringers's avatar
Sietse Ringers committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
	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)
137
		entry.Timestamp = session.timestamp
138
		entry.SignedMessageLDContext = irma.LDContextSignedMessage
Sietse Ringers's avatar
Sietse Ringers committed
139
140
141

		fallthrough
	case irma.ActionDisclosing:
142
		entry.Disclosure = response.(*irma.Disclosure)
Sietse Ringers's avatar
Sietse Ringers committed
143
	case irma.ActionIssuing:
144
		entry.IssueCommitment = response.(*irma.IssueCommitmentMessage)
Sietse Ringers's avatar
Sietse Ringers committed
145
146
147
148
149
	default:
		return nil, errors.New("Invalid log type")
	}

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