logs.go 6.07 KB
Newer Older
Sietse Ringers's avatar
Sietse Ringers committed
1
2
3
4
5
6
7
8
9
10
11
12
package irmago

import (
	"encoding/json"

	"time"

	"github.com/go-errors/errors"
	"github.com/mhe/gabi"
)

type LogEntry struct {
Sietse Ringers's avatar
Sietse Ringers committed
13
	// General info
Sietse Ringers's avatar
Sietse Ringers committed
14
15
16
17
	Type        Action
	Time        Timestamp    // Time at which the session was completed
	SessionInfo *SessionInfo // Message that started the session

Sietse Ringers's avatar
Sietse Ringers committed
18
19
20
21
22
23
	// Session type-specific info
	RemovedCredential CredentialTypeIdentifier                              // In case of credential removal
	Disclosed         map[CredentialTypeIdentifier]map[int]TranslatedString // Any session type
	Received          map[CredentialTypeIdentifier][]TranslatedString       // In case of issuance session
	SignedMessage     []byte                                                // In case of signature sessions
	SignedMessageType string                                                // In case of signature sessions
Sietse Ringers's avatar
Sietse Ringers committed
24

Sietse Ringers's avatar
Sietse Ringers committed
25
26
	response    interface{}     // Our response (ProofList or IssueCommitmentMessage)
	rawResponse json.RawMessage // Unparsed []byte version of response
Sietse Ringers's avatar
Sietse Ringers committed
27
28
}

Sietse Ringers's avatar
Sietse Ringers committed
29
func (session *session) createLogEntry(response interface{}) (*LogEntry, error) {
Sietse Ringers's avatar
Sietse Ringers committed
30
31
32
33
	entry := &LogEntry{
		Type:        session.Action,
		Time:        Timestamp(time.Now()),
		SessionInfo: session.info,
Sietse Ringers's avatar
Sietse Ringers committed
34
		response:    response,
Sietse Ringers's avatar
Sietse Ringers committed
35
36
	}

Sietse Ringers's avatar
Sietse Ringers committed
37
38
39
	// Populate session type-specific fields of the log entry (except for .Disclosed which is handled below)
	var prooflist gabi.ProofList
	var ok bool
Sietse Ringers's avatar
Sietse Ringers committed
40
	switch entry.Type {
Sietse Ringers's avatar
Sietse Ringers committed
41
42
43
44
	case ActionSigning:
		entry.SignedMessage = []byte(session.jwt.(*SignatureRequestorJwt).Request.Request.Message)
		entry.SignedMessageType = session.jwt.(*SignatureRequestorJwt).Request.Request.MessageType
		fallthrough
Sietse Ringers's avatar
Sietse Ringers committed
45
	case ActionDisclosing:
Sietse Ringers's avatar
Sietse Ringers committed
46
47
48
		if prooflist, ok = response.(gabi.ProofList); !ok {
			return nil, errors.New("Response was not a ProofList")
		}
Sietse Ringers's avatar
Sietse Ringers committed
49
	case ActionIssuing:
Sietse Ringers's avatar
Sietse Ringers committed
50
51
52
		if entry.Received == nil {
			entry.Received = map[CredentialTypeIdentifier][]TranslatedString{}
		}
Sietse Ringers's avatar
Sietse Ringers committed
53
		for _, req := range session.jwt.(*IdentityProviderJwt).Request.Request.Credentials {
54
			list, err := req.AttributeList(session.credManager.ConfigurationStore)
Sietse Ringers's avatar
Sietse Ringers committed
55
56
57
			if err != nil {
				continue // TODO?
			}
Sietse Ringers's avatar
Sietse Ringers committed
58
59
60
61
62
63
64
			entry.Received[list.CredentialType().Identifier()] = list.Strings()
		}
		var msg *gabi.IssueCommitmentMessage
		if msg, ok = response.(*gabi.IssueCommitmentMessage); ok {
			prooflist = msg.Proofs
		} else {
			return nil, errors.New("Response was not a *IssueCommitmentMessage")
Sietse Ringers's avatar
Sietse Ringers committed
65
66
67
68
69
		}
	default:
		return nil, errors.New("Invalid log type")
	}

Sietse Ringers's avatar
Sietse Ringers committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
	// Populate the list of disclosed attributes .Disclosed
	for _, proof := range prooflist {
		if proofd, isproofd := proof.(*gabi.ProofD); isproofd {
			if entry.Disclosed == nil {
				entry.Disclosed = map[CredentialTypeIdentifier]map[int]TranslatedString{}
			}
			meta := MetadataFromInt(proofd.ADisclosed[1], session.credManager.ConfigurationStore)
			id := meta.CredentialType().Identifier()
			entry.Disclosed[id] = map[int]TranslatedString{}
			for i, attr := range proofd.ADisclosed {
				if i == 1 {
					continue
				}
				val := string(attr.Bytes())
				entry.Disclosed[id][i] = TranslatedString{"en": val, "nl": val}
			}
		}
	}

Sietse Ringers's avatar
Sietse Ringers committed
89
90
91
92
93
94
95
96
	return entry, nil
}

func (entry *LogEntry) Jwt() (RequestorJwt, string, error) {
	return parseRequestorJwt(entry.Type, entry.SessionInfo.Jwt)
}

func (entry *LogEntry) GetResponse() (interface{}, error) {
Sietse Ringers's avatar
Sietse Ringers committed
97
	if entry.response == nil {
Sietse Ringers's avatar
Sietse Ringers committed
98
		switch entry.Type {
Sietse Ringers's avatar
Sietse Ringers committed
99
100
101
102
		case Action("removal"):
			return nil, nil
		case ActionSigning:
			fallthrough
Sietse Ringers's avatar
Sietse Ringers committed
103
		case ActionDisclosing:
Sietse Ringers's avatar
Sietse Ringers committed
104
			entry.response = []*gabi.ProofD{}
Sietse Ringers's avatar
Sietse Ringers committed
105
		case ActionIssuing:
Sietse Ringers's avatar
Sietse Ringers committed
106
			entry.response = &gabi.IssueCommitmentMessage{}
Sietse Ringers's avatar
Sietse Ringers committed
107
108
109
		default:
			return nil, errors.New("Invalid log type")
		}
Sietse Ringers's avatar
Sietse Ringers committed
110
		err := json.Unmarshal(entry.rawResponse, entry.response)
Sietse Ringers's avatar
Sietse Ringers committed
111
112
113
114
115
		if err != nil {
			return nil, err
		}
	}

Sietse Ringers's avatar
Sietse Ringers committed
116
	return entry.response, nil
Sietse Ringers's avatar
Sietse Ringers committed
117
118
119
120
121
122
}

type jsonLogEntry struct {
	Type        Action
	Time        Timestamp
	SessionInfo *logSessionInfo
Sietse Ringers's avatar
Sietse Ringers committed
123
124
125
126
127
128
129
130

	RemovedCredential string                                                `json:",omitempty"`
	Disclosed         map[CredentialTypeIdentifier]map[int]TranslatedString `json:",omitempty"`
	Received          map[CredentialTypeIdentifier][]TranslatedString       `json:",omitempty"`
	SignedMessage     []byte                                                `json:",omitempty"`
	SignedMessageType string                                                `json:",omitempty"`

	Response json.RawMessage
Sietse Ringers's avatar
Sietse Ringers committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
}

func (entry *LogEntry) UnmarshalJSON(bytes []byte) error {
	var err error
	temp := &jsonLogEntry{}
	if err = json.Unmarshal(bytes, temp); err != nil {
		return err
	}

	*entry = LogEntry{
		Type: temp.Type,
		Time: temp.Time,
		SessionInfo: &SessionInfo{
			Jwt:     temp.SessionInfo.Jwt,
			Nonce:   temp.SessionInfo.Nonce,
			Context: temp.SessionInfo.Context,
			Keys:    make(map[IssuerIdentifier]int),
		},
Sietse Ringers's avatar
Sietse Ringers committed
149
150
151
152
153
154
		RemovedCredential: NewCredentialTypeIdentifier(temp.RemovedCredential),
		Disclosed:         temp.Disclosed,
		Received:          temp.Received,
		SignedMessage:     temp.SignedMessage,
		SignedMessageType: temp.SignedMessageType,
		rawResponse:       temp.Response,
Sietse Ringers's avatar
Sietse Ringers committed
155
156
157
158
159
160
161
162
163
	}

	// TODO remove on protocol upgrade
	for iss, count := range temp.SessionInfo.Keys {
		entry.SessionInfo.Keys[NewIssuerIdentifier(iss)] = count
	}

	return nil
}
Sietse Ringers's avatar
Sietse Ringers committed
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198

func (entry *LogEntry) MarshalJSON() ([]byte, error) {
	// If the entry was created using createLogEntry(), then entry.rawResponse == nil
	if len(entry.rawResponse) == 0 && entry.response != nil {
		if bytes, err := json.Marshal(entry.response); err == nil {
			entry.rawResponse = json.RawMessage(bytes)
		} else {
			return nil, err
		}
	}

	temp := &jsonLogEntry{
		Type:     entry.Type,
		Time:     entry.Time,
		Response: entry.rawResponse,
		SessionInfo: &logSessionInfo{
			Jwt:     entry.SessionInfo.Jwt,
			Nonce:   entry.SessionInfo.Nonce,
			Context: entry.SessionInfo.Context,
			Keys:    make(map[string]int),
		},
		RemovedCredential: entry.RemovedCredential.String(),
		Disclosed:         entry.Disclosed,
		Received:          entry.Received,
		SignedMessage:     entry.SignedMessage,
		SignedMessageType: entry.SignedMessageType,
	}

	// TODO remove on protocol upgrade
	for iss, count := range entry.SessionInfo.Keys {
		temp.SessionInfo.Keys[iss.String()] = count
	}

	return json.Marshal(temp)
}