logs.go 3.19 KB
Newer Older
Sietse Ringers's avatar
Sietse Ringers committed
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
package irmago

import (
	"encoding/json"

	"time"

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

type LogEntry struct {
	Type        Action
	Time        Timestamp    // Time at which the session was completed
	SessionInfo *SessionInfo // Message that started the session
	Response    interface{}  // Session-type specific info, parsed on-demand, use .GetResponse()

	raw json.RawMessage
}

type RemovalLog struct {
	Credential CredentialTypeIdentifier
}

type VerificationLog struct {
	Proofs []*gabi.ProofD
}

type IssuanceLog struct {
	Proofs        []*gabi.ProofD
	AttributeList []*AttributeList
}

type SigningLog struct {
	Proofs      []*gabi.ProofD
	Message     []byte
	MessageType string
}

func (session *session) createLogEntry(response gabi.ProofList) (*LogEntry, error) {
	entry := &LogEntry{
		Type:        session.Action,
		Time:        Timestamp(time.Now()),
		SessionInfo: session.info,
	}

	proofs := []*gabi.ProofD{}
	for _, proof := range response {
		if proofd, isproofd := proof.(*gabi.ProofD); isproofd {
			proofs = append(proofs, proofd)
		}
	}

	switch entry.Type {
	case ActionDisclosing:
		item := &VerificationLog{Proofs: proofs}
		entry.Response = item
	case ActionIssuing:
		item := &IssuanceLog{Proofs: proofs}
		for _, req := range session.jwt.(*IdentityProviderJwt).Request.Request.Credentials {
61
			list, err := req.AttributeList(session.credManager.ConfigurationStore)
Sietse Ringers's avatar
Sietse Ringers committed
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
			if err != nil {
				continue // TODO?
			}
			item.AttributeList = append(item.AttributeList, list)
		}
		entry.Response = item
	case ActionSigning:
		item := SigningLog{Proofs: proofs}
		item.Message = []byte(session.jwt.(*SignatureRequestorJwt).Request.Request.Message)
		item.MessageType = session.jwt.(*SignatureRequestorJwt).Request.Request.MessageType
		entry.Response = item
	default:
		return nil, errors.New("Invalid log type")
	}

	return entry, nil
}

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

func (entry *LogEntry) GetResponse() (interface{}, error) {
	if entry.Response == nil {
		switch entry.Type {
		case ActionDisclosing:
			entry.Response = &VerificationLog{}
		case ActionIssuing:
			entry.Response = &IssuanceLog{}
		case ActionSigning:
			entry.Response = &SigningLog{}
		case Action("removal"):
			entry.Response = &RemovalLog{}
		default:
			return nil, errors.New("Invalid log type")
		}
		err := json.Unmarshal(entry.raw, entry.Response)
		if err != nil {
			return nil, err
		}
	}

	return entry.Response, nil
}

type jsonLogEntry struct {
	Type        Action
	Time        Timestamp
	SessionInfo *logSessionInfo
	Response    json.RawMessage
}

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),
		},
		raw: temp.Response,
	}

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

	return nil
}