-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
pki.go
100 lines (84 loc) · 2.59 KB
/
pki.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
package paymail
import (
"encoding/json"
"fmt"
"net/http"
"strings"
)
/*
Default Response:
{
"bsvalias": "1.0",
"handle": "<alias>@<domain>.<tld>",
"pubkey": "..."
}
*/
// PKIResponse is the result returned
type PKIResponse struct {
StandardResponse
PKIPayload
}
// PKIPayload is the payload from the response
type PKIPayload struct {
BsvAlias string `json:"bsvalias"` // Version of Paymail
Handle string `json:"handle"` // The <alias>@<domain>.<tld>
PubKey string `json:"pubkey"` // The related PubKey
}
// GetPKI will return a valid PKI response for a given alias@domain.tld
//
// Specs: http://bsvalias.org/03-public-key-infrastructure.html
func (c *Client) GetPKI(pkiURL, alias, domain string) (response *PKIResponse, err error) {
// Require a valid url
if len(pkiURL) == 0 || !strings.Contains(pkiURL, "https://") {
err = fmt.Errorf("invalid url: %s", pkiURL)
return
}
// Basic requirements for the request
if len(alias) == 0 {
err = fmt.Errorf("missing alias")
return
} else if len(domain) == 0 {
err = fmt.Errorf("missing domain")
return
}
// Set the base url and path, assuming the url is from the prior GetCapabilities() request
// https://<host-discovery-target>/{alias}@{domain.tld}/id
reqURL := replaceAliasDomain(pkiURL, alias, domain)
// Fire the GET request
var resp StandardResponse
if resp, err = c.getRequest(reqURL); err != nil {
return
}
// Start the response
response = &PKIResponse{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 {
return
}
// Invalid version detected
if len(response.BsvAlias) == 0 {
err = fmt.Errorf("missing bsvalias version")
return
}
// Check basic requirements (handle should match our alias@domain.tld)
if response.Handle != alias+"@"+domain {
err = fmt.Errorf("pki response handle %s does not match paymail address: %s", response.Handle, alias+"@"+domain)
return
}
// Check the PubKey length
if len(response.PubKey) == 0 {
err = fmt.Errorf("pki response is missing a PubKey value")
} else if len(response.PubKey) != PubKeyLength {
err = fmt.Errorf("returned pubkey is not the required length of %d, got: %d", PubKeyLength, len(response.PubKey))
}
return
}