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

import (
	"time"

Tomas's avatar
Tomas committed
6
	"github.com/bwesterb/go-atum"
Sietse Ringers's avatar
Sietse Ringers committed
7
8
	"github.com/go-errors/errors"
	"github.com/mhe/gabi"
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
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
	Request irma.SessionRequest   `json:",omitempty"` // Message that started the session
Sietse Ringers's avatar
Sietse Ringers committed
19

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

Tomas's avatar
Tomas committed
25
26
	IssueCommitment *gabi.IssueCommitmentMessage `json:",omitempty"`
	ProofList       gabi.ProofList               `json:",omitempty"`
Sietse Ringers's avatar
Sietse Ringers committed
27
28
}

29
const actionRemoval = irma.Action("removal")
30

Tomas's avatar
Tomas committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// GetDisclosedCredentials gets the list of disclosed credentials for a log entry
func (entry *LogEntry) GetDisclosedCredentials(conf *irma.Configuration) (irma.DisclosedCredentialList, error) {
	if entry.Type == actionRemoval {
		return irma.DisclosedCredentialList{}, nil
	}
	var proofs gabi.ProofList
	if entry.Type == irma.ActionIssuing {
		proofs = entry.IssueCommitment.Proofs
	} else {
		proofs = entry.ProofList
	}
	return irma.ExtractDisclosedCredentials(conf, proofs)
}

// 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
	}
50
	return entry.Request.(*irma.IssuanceRequest).GetCredentialInfoList(conf, entry.Version)
Tomas's avatar
Tomas committed
51
52
53
}

// GetSignedMessage gets the signed for a log entry
54
func (entry *LogEntry) GetSignedMessage() (abs *irma.SignedMessage, err error) {
Tomas's avatar
Tomas committed
55
56
57
	if entry.Type != irma.ActionSigning {
		return nil, nil
	}
58
	request := entry.Request.(*irma.SignatureRequest)
59
	return &irma.SignedMessage{
Tomas's avatar
Tomas committed
60
		Signature: entry.ProofList,
61
62
		Nonce:     request.Nonce,
		Context:   request.Context,
Tomas's avatar
Tomas committed
63
64
65
66
67
		Message:   string(entry.SignedMessage),
		Timestamp: entry.Timestamp,
	}, nil
}

68
func (session *session) createLogEntry(response interface{}) (*LogEntry, error) {
Sietse Ringers's avatar
Sietse Ringers committed
69
	entry := &LogEntry{
70
71
72
73
		Type:    session.Action,
		Time:    irma.Timestamp(time.Now()),
		Version: session.Version,
		Request: session.request,
Sietse Ringers's avatar
Sietse Ringers committed
74
75
76
	}

	switch entry.Type {
Tomas's avatar
Tomas committed
77
78
	case actionRemoval:

79
	case irma.ActionSigning:
Tomas's avatar
Tomas committed
80
		// Get the signed message and timestamp
81
		request := session.request.(*irma.SignatureRequest)
Tomas's avatar
Tomas committed
82
83
84
		entry.SignedMessage = []byte(request.Message)
		entry.Timestamp = request.Timestamp

Sietse Ringers's avatar
Sietse Ringers committed
85
		fallthrough
86
	case irma.ActionDisclosing:
Tomas's avatar
Tomas committed
87
		entry.ProofList = response.(gabi.ProofList)
88
	case irma.ActionIssuing:
Tomas's avatar
Tomas committed
89
		entry.IssueCommitment = response.(*gabi.IssueCommitmentMessage)
Sietse Ringers's avatar
Sietse Ringers committed
90
91
92
93
94
95
	default:
		return nil, errors.New("Invalid log type")
	}

	return entry, nil
}