-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcapabilities.go
194 lines (164 loc) · 5.29 KB
/
capabilities.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
package paymail
import (
"encoding/json"
"fmt"
"net/http"
"strings"
)
/*
Default:
{
"bsvalias": "1.0",
"capabilities": {
"6745385c3fc0": false,
"pki": "https://bsvalias.example.org/{alias}@{domain.tld}/id",
"paymentDestination": "https://bsvalias.example.org/{alias}@{domain.tld}/payment-destination"
}
}
*/
// CapabilitiesResponse is the full response returned
type CapabilitiesResponse struct {
StandardResponse
CapabilitiesPayload
}
// CapabilitiesPayload is the actual payload response
type CapabilitiesPayload struct {
BsvAlias string `json:"bsvalias"` // Version of the bsvalias
Capabilities map[string]interface{} `json:"capabilities"` // Raw list of the capabilities
Pike *PikeCapability `json:"pike,omitempty"`
}
// PikeCapability represents the structure of the PIKE capability
type PikeCapability struct {
Invite *string `json:"invite,omitempty"`
Outputs *string `json:"outputs,omitempty"`
}
// PikeOutputs represents the structure of the PIKE outputs
type PikeOutputs struct {
URL string `json:"url"`
}
// Has will check if a BRFC ID (or alternate) is found in the list of capabilities
//
// Alternate is used for example: "pki" is also BRFC "0c4339ef99c2"
func (c *CapabilitiesPayload) Has(brfcID, alternateID string) bool {
for key := range c.Capabilities {
if key == brfcID || (len(alternateID) > 0 && key == alternateID) {
return true
}
}
return false
}
// getValue will return the value (if found) from the capability (url or bool)
//
// Alternate is used for IE: pki (it breaks convention of using the BRFC ID)
func (c *CapabilitiesPayload) getValue(brfcID, alternateID string) (bool, interface{}) {
for key, val := range c.Capabilities {
if key == brfcID || (len(alternateID) > 0 && key == alternateID) {
return true, val
}
}
return false, nil
}
// GetString will perform getValue() but cast to a string if found
//
// Returns an empty string if not found
func (c *CapabilitiesPayload) GetString(brfcID, alternateID string) string {
if ok, val := c.getValue(brfcID, alternateID); ok {
return val.(string)
}
return ""
}
// GetBool will perform getValue() but cast to a bool if found
//
// Returns false if not found
func (c *CapabilitiesPayload) GetBool(brfcID, alternateID string) bool {
if ok, val := c.getValue(brfcID, alternateID); ok {
return val.(bool)
}
return false
}
// GetCapabilities will return a list of capabilities for a given domain & port
//
// Specs: http://bsvalias.org/02-02-capability-discovery.html
func (c *Client) GetCapabilities(target string, port int) (response *CapabilitiesResponse, err error) {
// Basic requirements for the request
if len(target) == 0 {
err = fmt.Errorf("missing target")
return
} else if port == 0 {
err = fmt.Errorf("missing port")
return
}
// Set the base url and path
// https://<host-discovery-target>:<host-discovery-port>/.well-known/bsvalias[network]
reqURL := fmt.Sprintf("https://%s:%d/.well-known/%s%s", target, port, DefaultServiceName, c.options.network.URLSuffix())
// Fire the GET request
var resp StandardResponse
if resp, err = c.getRequest(reqURL); err != nil {
return
}
// Start the response
response = &CapabilitiesResponse{StandardResponse: resp}
// Test the status code (200 or 304 is valid)
if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusNotModified {
serverError := &ServerError{}
if err = json.Unmarshal(resp.Body, serverError); err != nil {
return
}
err = fmt.Errorf("bad response from paymail provider: code %d, message: %s", response.StatusCode, serverError.Message)
return
}
// Decode the body of the response
if err = json.Unmarshal(resp.Body, &response); err != nil {
// Invalid character (sometimes quote related: U+0022 vs U+201C)
if strings.Contains(err.Error(), "invalid character") {
// Replace any invalid quotes
bodyString := strings.Replace(strings.Replace(string(resp.Body), `“`, `"`, -1), `”`, `"`, -1)
// Parse again after fixing quotes
if err = json.Unmarshal([]byte(bodyString), &response); err != nil {
return
}
}
// Still have an error?
if err != nil {
return
}
}
// Invalid version detected
if len(response.BsvAlias) == 0 {
err = fmt.Errorf("missing %s version", DefaultServiceName)
return
}
// Parse PIKE capability
if err = parsePikeCapability(response); err != nil {
return
}
return
}
// ExtractPikeOutputsURL extracts the outputs URL from the PIKE capability
func (c *CapabilitiesPayload) ExtractPikeOutputsURL() string {
if c.Pike != nil {
return *c.Pike.Outputs
}
return ""
}
// ExtractPikeInviteURL extracts the invite URL from the PIKE capability
func (c *CapabilitiesPayload) ExtractPikeInviteURL() string {
if c.Pike != nil {
return *c.Pike.Invite
}
return ""
}
// parsePikeCapability parses the PIKE capability from the capabilities response
func parsePikeCapability(response *CapabilitiesResponse) error {
if pike, ok := response.Capabilities[BRFCPike].(map[string]interface{}); ok {
response.Pike = &PikeCapability{}
if inviteStr, ok := pike["invite"].(string); ok {
response.Pike.Invite = &inviteStr
}
// TODO: Ensure the outputs field is required when the 'PIKE' transaction with linked keys is implemented.
if outputsStr, ok := pike["outputs"].(string); ok {
response.Pike.Outputs = &outputsStr
}
}
return nil
}