/
event.go
208 lines (187 loc) · 6.01 KB
/
event.go
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
61
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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
199
200
201
202
203
204
205
206
207
208
package fsm
import (
"encoding/json"
"github.com/Shopify/go-lua"
agency "github.com/findy-network/findy-common-go/grpc/agency/v1"
"github.com/golang/glog"
"github.com/lainio/err2"
"github.com/lainio/err2/assert"
"github.com/lainio/err2/try"
)
type NotificationType int32
type Event struct {
// TODO: questions could be protocols here, then TypeID would not be needed?
// we will continue with this when other protocol QAs will be implemented
// New! Hook now uses TypeID for hook name/ID
// These both are string versions to make writing the yaml fsm easier.
// There parser methdod, Initialize() that must be call to make the machine
// to work. It also make other syntax checks.
// NOTE. Don't use this here at code level, use ProtocolType!
Protocol string `json:"protocol"` // Note! See ProtocolType below
TypeID string `json:"type_id"` // Note! See NotificationType below
Rule string `json:"rule"`
Data string `json:"data,omitempty"`
// Deprecated: replaced by WantStatus, left to keep file format
NoStatus bool `json:"no_status,omitempty"`
// Tells that we want status updates about our sending, this is calculated
// automatically
WantStatus bool `json:"want_status,omitempty"`
*EventData `json:"event_data,omitempty"`
NoEcho bool `json:"no_echo,omitempty"`
ProtocolType agency.Protocol_Type `json:"-"`
NotificationType NotificationType `json:"-"`
// NotificationType agency.Notification_Type `json:"-"`
*agency.ProtocolStatus `json:"-"`
*Transition `json:"-"`
}
func (e *Event) filterEnvs() {
if e == nil {
glog.V(7).Infoln("no event data, in filter:", e.Protocol)
return
}
glog.V(10).Infoln("in filter:", e.Protocol)
switch e.ProtocolType {
case agency.Protocol_ISSUE_CREDENTIAL:
if e.EventData != nil && e.EventData.Issuing != nil {
e.EventData.Issuing.CredDefID = filterEnvs(e.EventData.Issuing.CredDefID)
}
e.Data = filterEnvs(e.Data)
case agency.Protocol_PRESENT_PROOF:
if e.EventData != nil && e.EventData.Proof != nil {
e.EventData.Proof.ProofJSON = filterEnvs(e.EventData.Proof.ProofJSON)
}
e.Data = filterEnvs(e.Data)
default:
glog.V(7).Infoln("wrong type, in filter:", e.ProtocolType)
}
}
func (e Event) TriggersByBackendData(data *BackendData) (ok bool, tgt string) {
if data == nil {
return true, ""
}
e.copyBackendDataValuesToMemory(data)
content := data.Content
switch e.Rule {
case TriggerTypeValidateInputNotEqual:
return e.Machine.Memory[e.Data] != content, ""
case TriggerTypeValidateInputEqual:
return e.Machine.Memory[e.Data] == content, ""
case TriggerTypeInputEqual:
return content == e.Data, ""
case TriggerTypeData, TriggerTypeUseInput, TriggerTypeUseInputSave,
TriggerTypeUseInputSaveConnID, TriggerTypeUseInputSaveSessionID:
return true, ""
case TriggerTypeLua:
_, target, ok := e.ExecLua(content)
return ok, target
}
return false, ""
}
func (e *Event) copyBackendDataValuesToMemory(data *BackendData) {
assert.NotNil(e.Transition)
glog.V(3).Infof("*** Data: '%v', Type: %v, SID:%v", e.Data, e.Machine.Type, data.SessionID)
if data.ConnID != "" {
e.Machine.Memory[LUA_CONN_ID] = data.ConnID
}
if data.Subject != "" {
e.Machine.Memory[LUA_SUBJECT] = data.Subject
}
if data.SessionID != "" {
e.Machine.Memory[LUA_SESSION_ID] = data.SessionID
}
}
func (e Event) TriggersByHook() bool {
return true
}
func (e Event) Triggers(status *agency.ProtocolStatus) (ok bool, tgt string) {
if status == nil {
return true, ""
}
switch status.GetState().ProtocolID.TypeID {
case agency.Protocol_ISSUE_CREDENTIAL, agency.Protocol_DIDEXCHANGE, agency.Protocol_PRESENT_PROOF:
return true, ""
case agency.Protocol_BASIC_MESSAGE:
content := status.GetBasicMessage().Content
switch e.Rule {
case TriggerTypeValidateInputNotEqual:
return e.Machine.Memory[e.Data] != content, ""
case TriggerTypeValidateInputEqual:
return e.Machine.Memory[e.Data] == content, ""
case TriggerTypeInputEqual:
return content == e.Data, ""
case TriggerTypeData, TriggerTypeUseInput,
TriggerTypeUseInputSave, TriggerTypeTransient,
TriggerTypeUseInputSaveConnID, TriggerTypeUseInputSaveSessionID:
return true, ""
case TriggerTypeLua:
_, target, ok := e.ExecLua(content)
return ok, target
}
}
return false, ""
}
func (e Event) ExecLua(content string, a ...string) (out, tgt string, ok bool) {
defer err2.Catch(err2.Err(func(err error) {
ok = false
}))
okStr := LUA_OK
if len(a) > 0 {
okStr = a[0]
}
e.Machine.Memory[LUA_INPUT] = content
luaScript := filterFilelink(e.Data)
try.To(lua.DoString(e.Machine.luaState, luaScript))
out, ok = e.Machine.Memory[LUA_OUTPUT]
if !ok {
glog.Warning("lua script: no output. Trying to get error")
errMsg := assert.MKeyExists(e.Machine.Memory, LUA_ERROR)
glog.Errorln("lua error:", errMsg)
}
tgt = e.Machine.Memory[LUA_TARGET]
if okStr == LUA_ALL_OK {
return out, tgt, true
}
ok = ok && out == okStr
return out, tgt, ok
}
func (e Event) Answers(status *agency.Question) bool {
switch status.TypeID {
case agency.Question_PING_WAITS:
case agency.Question_ISSUE_PROPOSE_WAITS:
case agency.Question_PROOF_PROPOSE_WAITS:
case agency.Question_PROOF_VERIFY_WAITS:
assert.Equal(e.ProtocolType, agency.Protocol_PRESENT_PROOF)
var attrValues []ProofAttr
try.To(json.Unmarshal([]byte(e.Data), &attrValues))
switch e.Rule {
case TriggerTypeNotAcceptValues:
if len(attrValues) != len(status.GetProofVerify().Attributes) {
return true
}
for _, attr := range status.GetProofVerify().Attributes {
for i, value := range attrValues {
if value.Name == attr.Name && value.CredDefID == attr.CredDefID {
attrValues[i].found = true
}
}
}
for _, value := range attrValues {
if !value.found {
return true
}
}
case TriggerTypeAcceptAndInputValues:
count := 0
for _, attr := range status.GetProofVerify().Attributes {
for _, value := range attrValues {
if value.Name == attr.Name {
e.Machine.Memory[value.Name] = attr.Value
count++
}
}
}
return count == len(status.GetProofVerify().Attributes)
}
}
return false
}