forked from tangled.org/core
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, dev bool) (*SignedClient, error) { 37 client := &http.Client{ 38 Timeout: 5 * time.Second, 39 Transport: SignerTransport{ 40 Secret: secret, 41 }, 42 } 43 44 uri := "https" 45 if dev { 46 uri = "http" 47 } 48 url, err := url.Parse(fmt.Sprintf("%s://%s", uri, domain)) 49 if err != nil { 50 return nil, err 51 } 52 53 signedClient := &SignedClient{ 54 Secret: secret, 55 client: client, 56 Url: url, 57 } 58 59 return signedClient, nil 60} 61 62func (s *SignedClient) newRequest(method, endpoint string, body []byte) (*http.Request, error) { 63 return http.NewRequest(method, s.Url.JoinPath(endpoint).String(), bytes.NewReader(body)) 64} 65 66func (s *SignedClient) Init(did string) (*http.Response, error) { 67 const ( 68 Method = "POST" 69 Endpoint = "/init" 70 ) 71 72 body, _ := json.Marshal(map[string]interface{}{ 73 "did": did, 74 }) 75 76 req, err := s.newRequest(Method, Endpoint, body) 77 if err != nil { 78 return nil, err 79 } 80 81 return s.client.Do(req) 82} 83 84func (s *SignedClient) NewRepo(did, repoName, defaultBranch string) (*http.Response, error) { 85 const ( 86 Method = "PUT" 87 Endpoint = "/repo/new" 88 ) 89 90 body, _ := json.Marshal(map[string]interface{}{ 91 "did": did, 92 "name": repoName, 93 "default_branch": defaultBranch, 94 }) 95 96 fmt.Println(body) 97 98 req, err := s.newRequest(Method, Endpoint, body) 99 if err != nil { 100 return nil, err 101 } 102 103 return s.client.Do(req) 104} 105 106func (s *SignedClient) AddMember(did string) (*http.Response, error) { 107 const ( 108 Method = "PUT" 109 Endpoint = "/member/add" 110 ) 111 112 body, _ := json.Marshal(map[string]interface{}{ 113 "did": did, 114 }) 115 116 req, err := s.newRequest(Method, Endpoint, body) 117 if err != nil { 118 return nil, err 119 } 120 121 return s.client.Do(req) 122} 123 124func (s *SignedClient) AddCollaborator(ownerDid, repoName, memberDid string) (*http.Response, error) { 125 const ( 126 Method = "POST" 127 ) 128 endpoint := fmt.Sprintf("/%s/%s/collaborator/add", ownerDid, repoName) 129 130 body, _ := json.Marshal(map[string]interface{}{ 131 "did": memberDid, 132 }) 133 134 req, err := s.newRequest(Method, endpoint, body) 135 if err != nil { 136 return nil, err 137 } 138 139 return s.client.Do(req) 140}