-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
webhooks.go
155 lines (146 loc) · 5.7 KB
/
webhooks.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
package acapy
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
)
type WebhookHandlers struct {
ConnectionsEventHandler func(event Connection)
BasicMessagesEventHandler func(event BasicMessagesEvent)
ProblemReportEventHandler func(event ProblemReportEvent)
CredentialExchangeEventHandler func(event CredentialExchangeRecord)
CredentialExchangeV2EventHandler func(event CredentialExchangeRecordV2)
CredentialExchangeDIFEventHandler func(event CredentialExchangeDIF)
CredentialExchangeIndyEventHandler func(event CredentialExchangeIndy)
RevocationRegistryEventHandler func(event RevocationRegistry)
PresentationExchangeEventHandler func(event PresentationExchangeRecord)
CredentialRevocationEventHandler func(event CredentialRevocationRecord)
PingEventHandler func(event PingEvent)
OutOfBandEventHandler func(event OutOfBandEvent)
}
func CreateWebhooksHandler(handlers WebhookHandlers) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
path := strings.Split(strings.TrimSuffix(r.URL.Path, "/"), "/")
topic := path[len(path)-1]
defer r.Body.Close()
switch topic {
case "connections":
if handlers.ConnectionsEventHandler != nil {
var connectionsEvent Connection
json.NewDecoder(r.Body).Decode(&connectionsEvent)
handlers.ConnectionsEventHandler(connectionsEvent)
}
case "basicmessages":
if handlers.BasicMessagesEventHandler != nil {
var basicMessagesEvent BasicMessagesEvent
json.NewDecoder(r.Body).Decode(&basicMessagesEvent)
handlers.BasicMessagesEventHandler(basicMessagesEvent)
}
case "problem_report":
if handlers.ProblemReportEventHandler != nil {
var problemReportEvent ProblemReportEvent
json.NewDecoder(r.Body).Decode(&problemReportEvent)
handlers.ProblemReportEventHandler(problemReportEvent)
}
case "issue_credential":
if handlers.CredentialExchangeEventHandler != nil {
var credentialExchangeEvent CredentialExchangeRecord
json.NewDecoder(r.Body).Decode(&credentialExchangeEvent)
handlers.CredentialExchangeEventHandler(credentialExchangeEvent)
}
case "issuer_cred_rev":
if handlers.CredentialRevocationEventHandler != nil {
var credentialRevocationEvent CredentialRevocationRecord
json.NewDecoder(r.Body).Decode(&credentialRevocationEvent)
handlers.CredentialRevocationEventHandler(credentialRevocationEvent)
}
case "issue_credential_v2_0":
if handlers.CredentialExchangeV2EventHandler != nil {
var credentialExchangeV2Event CredentialExchangeRecordV2
json.NewDecoder(r.Body).Decode(&credentialExchangeV2Event)
handlers.CredentialExchangeV2EventHandler(credentialExchangeV2Event)
}
case "issue_credential_v2_0_dif":
body, _ := ioutil.ReadAll(r.Body)
fmt.Println(string(body))
if handlers.CredentialExchangeDIFEventHandler != nil {
var credentialExchangeDIFEvent CredentialExchangeDIF
if err := json.Unmarshal(body, &credentialExchangeDIFEvent); err != nil {
log.Fatal(err)
}
handlers.CredentialExchangeDIFEventHandler(credentialExchangeDIFEvent)
}
case "issue_credential_v2_0_indy":
if handlers.CredentialExchangeIndyEventHandler != nil {
var credentialExchangeIndyEvent CredentialExchangeIndy
json.NewDecoder(r.Body).Decode(&credentialExchangeIndyEvent)
handlers.CredentialExchangeIndyEventHandler(credentialExchangeIndyEvent)
}
case "revocation_registry":
if handlers.RevocationRegistryEventHandler != nil {
var revocationRegistryEvent RevocationRegistry
json.NewDecoder(r.Body).Decode(&revocationRegistryEvent)
handlers.RevocationRegistryEventHandler(revocationRegistryEvent)
}
case "oob_invitation":
if handlers.OutOfBandEventHandler != nil {
var outOfBandEvent OutOfBandEvent
json.NewDecoder(r.Body).Decode(&outOfBandEvent)
handlers.OutOfBandEventHandler(outOfBandEvent)
}
case "present_proof":
if handlers.PresentationExchangeEventHandler != nil {
var presentationExchangeEvent PresentationExchangeRecord
json.NewDecoder(r.Body).Decode(&presentationExchangeEvent)
handlers.PresentationExchangeEventHandler(presentationExchangeEvent)
}
case "ping":
if handlers.PingEventHandler != nil {
var pingEvent PingEvent
json.NewDecoder(r.Body).Decode(&pingEvent)
handlers.PingEventHandler(pingEvent)
}
default:
log.Printf("Webhook topic not supported: %q\n", topic)
w.WriteHeader(404)
return
}
w.WriteHeader(200)
}
}
type PingEvent struct {
Comment string `json:"comment"`
ConnectionID string `json:"connection_id"`
Responded bool `json:"responded"`
State string `json:"state"`
ThreadID string `json:"thread_id"`
}
type OutOfBandEvent struct {
InvitationID string `json:"invitation_id"`
InvitationMessageID string `json:"invi_msg_id"`
Invitation OutOfBandInvitation `json:"invitation"`
State string `json:"state"`
InvitationURL string `json:"invitation_url"`
UpdatedAt string `json:"updated_at"`
CreatedAt string `json:"created_at"`
AutoAccept bool `json:"auto_accept"`
MultiUse bool `json:"multi_use"`
Trace bool `json:"trace"`
}
type BasicMessagesEvent struct {
ConnectionID string `json:"connection_id"`
MessageID string `json:"message_id"`
State string `json:"state"`
Content string `json:"content"`
}
type ProblemReportEvent struct {
Type string `json:"@type"`
ID string `json:"@id"`
Thread struct {
Thid string `json:"thid"`
} `json:"~thread"`
ExplainLtxt string `json:"explain-ltxt"`
}