forked from tangled.org/core
this repo has no description
at master 2.8 kB view raw
1package signup 2 3// We have this extra code here for now since the xrpcclient package 4// only supports OAuth'd requests; these are unauthenticated or use PDS admin auth. 5 6import ( 7 "bytes" 8 "encoding/json" 9 "fmt" 10 "io" 11 "net/http" 12 "net/url" 13) 14 15// makePdsRequest is a helper method to make requests to the PDS service 16func (s *Signup) makePdsRequest(method, endpoint string, body interface{}, useAuth bool) (*http.Response, error) { 17 jsonData, err := json.Marshal(body) 18 if err != nil { 19 return nil, err 20 } 21 22 url := fmt.Sprintf("%s/xrpc/%s", s.config.Pds.Host, endpoint) 23 req, err := http.NewRequest(method, url, bytes.NewBuffer(jsonData)) 24 if err != nil { 25 return nil, err 26 } 27 28 req.Header.Set("Content-Type", "application/json") 29 30 if useAuth { 31 req.SetBasicAuth("admin", s.config.Pds.AdminSecret) 32 } 33 34 return http.DefaultClient.Do(req) 35} 36 37// handlePdsError processes error responses from the PDS service 38func (s *Signup) handlePdsError(resp *http.Response, action string) error { 39 var errorResp struct { 40 Error string `json:"error"` 41 Message string `json:"message"` 42 } 43 44 respBody, _ := io.ReadAll(resp.Body) 45 if err := json.Unmarshal(respBody, &errorResp); err == nil && errorResp.Message != "" { 46 return fmt.Errorf("Failed to %s: %s - %s.", action, errorResp.Error, errorResp.Message) 47 } 48 49 // Fallback if we couldn't parse the error 50 return fmt.Errorf("failed to %s, status code: %d", action, resp.StatusCode) 51} 52 53func (s *Signup) inviteCodeRequest() (string, error) { 54 body := map[string]any{"useCount": 1} 55 56 resp, err := s.makePdsRequest("POST", "com.atproto.server.createInviteCode", body, true) 57 if err != nil { 58 return "", err 59 } 60 defer resp.Body.Close() 61 62 if resp.StatusCode != http.StatusOK { 63 return "", s.handlePdsError(resp, "create invite code") 64 } 65 66 var result map[string]string 67 json.NewDecoder(resp.Body).Decode(&result) 68 return result["code"], nil 69} 70 71func (s *Signup) createAccountRequest(username, password, email, code string) (string, error) { 72 parsedURL, err := url.Parse(s.config.Pds.Host) 73 if err != nil { 74 return "", fmt.Errorf("invalid PDS host URL: %w", err) 75 } 76 77 pdsDomain := parsedURL.Hostname() 78 79 body := map[string]string{ 80 "email": email, 81 "handle": fmt.Sprintf("%s.%s", username, pdsDomain), 82 "password": password, 83 "inviteCode": code, 84 } 85 86 resp, err := s.makePdsRequest("POST", "com.atproto.server.createAccount", body, false) 87 if err != nil { 88 return "", err 89 } 90 defer resp.Body.Close() 91 92 if resp.StatusCode != http.StatusOK { 93 return "", s.handlePdsError(resp, "create account") 94 } 95 96 var result struct { 97 DID string `json:"did"` 98 } 99 if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { 100 return "", fmt.Errorf("failed to decode create account response: %w", err) 101 } 102 103 return result.DID, nil 104}