this repo has no description
1package state 2 3import ( 4 "bytes" 5 "crypto/hmac" 6 "crypto/sha256" 7 "encoding/hex" 8 "encoding/json" 9 "fmt" 10 "net/http" 11 "net/url" 12 "time" 13) 14 15type SignerTransport struct { 16 Secret string 17} 18 19func (s SignerTransport) RoundTrip(req *http.Request) (*http.Response, error) { 20 timestamp := time.Now().Format(time.RFC3339) 21 mac := hmac.New(sha256.New, []byte(s.Secret)) 22 message := req.Method + req.URL.Path + timestamp 23 mac.Write([]byte(message)) 24 signature := hex.EncodeToString(mac.Sum(nil)) 25 req.Header.Set("X-Signature", signature) 26 req.Header.Set("X-Timestamp", timestamp) 27 return http.DefaultTransport.RoundTrip(req) 28} 29 30type SignedClient struct { 31 Secret string 32 Url *url.URL 33 client *http.Client 34} 35 36func NewSignedClient(domain, secret string) (*SignedClient, error) { 37 client := &http.Client{ 38 Timeout: 5 * time.Second, 39 Transport: SignerTransport{ 40 Secret: secret, 41 }, 42 } 43 44 url, err := url.Parse(fmt.Sprintf("http://%s", domain)) 45 if err != nil { 46 return nil, err 47 } 48 49 signedClient := &SignedClient{ 50 Secret: secret, 51 client: client, 52 Url: url, 53 } 54 55 return signedClient, nil 56} 57 58func (s *SignedClient) newRequest(method, endpoint string, body []byte) (*http.Request, error) { 59 return http.NewRequest(method, s.Url.JoinPath(endpoint).String(), bytes.NewReader(body)) 60} 61 62func (s *SignedClient) Init(did string, keys []string) (*http.Response, error) { 63 const ( 64 Method = "POST" 65 Endpoint = "/init" 66 ) 67 68 body, _ := json.Marshal(map[string]interface{}{ 69 "did": did, 70 "keys": keys, 71 }) 72 73 req, err := s.newRequest(Method, Endpoint, body) 74 if err != nil { 75 return nil, err 76 } 77 78 return s.client.Do(req) 79} 80 81func (s *SignedClient) NewRepo(did, repoName string) (*http.Response, error) { 82 const ( 83 Method = "PUT" 84 Endpoint = "/repo/new" 85 ) 86 87 body, _ := json.Marshal(map[string]interface{}{ 88 "did": did, 89 "name": repoName, 90 }) 91 92 req, err := s.newRequest(Method, Endpoint, body) 93 if err != nil { 94 return nil, err 95 } 96 97 return s.client.Do(req) 98} 99 100func (s *SignedClient) AddMember(did string, keys []string) (*http.Response, error) { 101 const ( 102 Method = "PUT" 103 Endpoint = "/member/add" 104 ) 105 106 body, _ := json.Marshal(map[string]interface{}{ 107 "did": did, 108 "keys": keys, 109 }) 110 111 req, err := s.newRequest(Method, Endpoint, body) 112 if err != nil { 113 return nil, err 114 } 115 116 return s.client.Do(req) 117}