|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "log" |
| 9 | + "net/http" |
| 10 | + "os" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +type PlatformAPI interface { |
| 15 | + CreateAuthRequest(ctx context.Context) (*AuthRequest, error) |
| 16 | + CheckAuthRequestConfirmed(ctx context.Context, id, exchangeToken string) (bool, error) |
| 17 | + ExchangeAuthRequest(ctx context.Context, id, exchangeToken string) (string, error) |
| 18 | + GetLicenseToken(ctx context.Context, bearerToken string) (string, error) |
| 19 | +} |
| 20 | + |
| 21 | +type AuthRequest struct { |
| 22 | + ID string `json:"id"` |
| 23 | + Code string `json:"code"` |
| 24 | + ExchangeToken string `json:"exchange_token"` |
| 25 | +} |
| 26 | + |
| 27 | +type authRequestStatus struct { |
| 28 | + Confirmed bool `json:"confirmed"` |
| 29 | +} |
| 30 | + |
| 31 | +type authTokenResponse struct { |
| 32 | + ID string `json:"id"` |
| 33 | + AuthToken string `json:"auth_token"` |
| 34 | +} |
| 35 | + |
| 36 | +type licenseTokenResponse struct { |
| 37 | + Token string `json:"token"` |
| 38 | +} |
| 39 | + |
| 40 | +type PlatformClient struct { |
| 41 | + baseURL string |
| 42 | + httpClient *http.Client |
| 43 | +} |
| 44 | + |
| 45 | +func NewPlatformClient() *PlatformClient { |
| 46 | + baseURL := os.Getenv("LOCALSTACK_API_ENDPOINT") |
| 47 | + if baseURL == "" { |
| 48 | + baseURL = "https://api.localstack.cloud" |
| 49 | + } |
| 50 | + return &PlatformClient{ |
| 51 | + baseURL: baseURL, |
| 52 | + httpClient: &http.Client{Timeout: 30 * time.Second}, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func (c *PlatformClient) CreateAuthRequest(ctx context.Context) (*AuthRequest, error) { |
| 57 | + req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.baseURL+"/v1/auth/request", nil) |
| 58 | + if err != nil { |
| 59 | + return nil, fmt.Errorf("failed to create request: %w", err) |
| 60 | + } |
| 61 | + |
| 62 | + resp, err := c.httpClient.Do(req) |
| 63 | + if err != nil { |
| 64 | + return nil, fmt.Errorf("failed to create auth request: %w", err) |
| 65 | + } |
| 66 | + defer func() { |
| 67 | + if err := resp.Body.Close(); err != nil { |
| 68 | + log.Printf("failed to close response body: %v", err) |
| 69 | + } |
| 70 | + }() |
| 71 | + |
| 72 | + if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated { |
| 73 | + return nil, fmt.Errorf("failed to create auth request: status %d", resp.StatusCode) |
| 74 | + } |
| 75 | + |
| 76 | + var authReq AuthRequest |
| 77 | + if err := json.NewDecoder(resp.Body).Decode(&authReq); err != nil { |
| 78 | + return nil, fmt.Errorf("failed to decode response: %w", err) |
| 79 | + } |
| 80 | + |
| 81 | + return &authReq, nil |
| 82 | +} |
| 83 | + |
| 84 | +func (c *PlatformClient) CheckAuthRequestConfirmed(ctx context.Context, id, exchangeToken string) (bool, error) { |
| 85 | + url := fmt.Sprintf("%s/v1/auth/request/%s?exchange_token=%s", c.baseURL, id, exchangeToken) |
| 86 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) |
| 87 | + if err != nil { |
| 88 | + return false, fmt.Errorf("failed to create request: %w", err) |
| 89 | + } |
| 90 | + |
| 91 | + resp, err := c.httpClient.Do(req) |
| 92 | + if err != nil { |
| 93 | + return false, fmt.Errorf("failed to check auth request: %w", err) |
| 94 | + } |
| 95 | + defer func() { |
| 96 | + if err := resp.Body.Close(); err != nil { |
| 97 | + log.Printf("failed to close response body: %v", err) |
| 98 | + } |
| 99 | + }() |
| 100 | + |
| 101 | + if resp.StatusCode != http.StatusOK { |
| 102 | + return false, fmt.Errorf("failed to check auth request: status %d", resp.StatusCode) |
| 103 | + } |
| 104 | + |
| 105 | + var status authRequestStatus |
| 106 | + if err := json.NewDecoder(resp.Body).Decode(&status); err != nil { |
| 107 | + return false, fmt.Errorf("failed to decode response: %w", err) |
| 108 | + } |
| 109 | + |
| 110 | + return status.Confirmed, nil |
| 111 | +} |
| 112 | + |
| 113 | +func (c *PlatformClient) ExchangeAuthRequest(ctx context.Context, id, exchangeToken string) (string, error) { |
| 114 | + url := fmt.Sprintf("%s/v1/auth/request/%s/exchange", c.baseURL, id) |
| 115 | + body, err := json.Marshal(map[string]string{"exchange_token": exchangeToken}) |
| 116 | + if err != nil { |
| 117 | + return "", fmt.Errorf("failed to marshal request: %w", err) |
| 118 | + } |
| 119 | + |
| 120 | + req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body)) |
| 121 | + if err != nil { |
| 122 | + return "", fmt.Errorf("failed to create request: %w", err) |
| 123 | + } |
| 124 | + req.Header.Set("Content-Type", "application/json") |
| 125 | + |
| 126 | + resp, err := c.httpClient.Do(req) |
| 127 | + if err != nil { |
| 128 | + return "", fmt.Errorf("failed to exchange auth request: %w", err) |
| 129 | + } |
| 130 | + defer func() { |
| 131 | + if err := resp.Body.Close(); err != nil { |
| 132 | + log.Printf("failed to close response body: %v", err) |
| 133 | + } |
| 134 | + }() |
| 135 | + |
| 136 | + if resp.StatusCode != http.StatusOK { |
| 137 | + return "", fmt.Errorf("failed to exchange auth request: status %d", resp.StatusCode) |
| 138 | + } |
| 139 | + |
| 140 | + var tokenResp authTokenResponse |
| 141 | + if err := json.NewDecoder(resp.Body).Decode(&tokenResp); err != nil { |
| 142 | + return "", fmt.Errorf("failed to decode response: %w", err) |
| 143 | + } |
| 144 | + |
| 145 | + return tokenResp.AuthToken, nil |
| 146 | +} |
| 147 | + |
| 148 | +func (c *PlatformClient) GetLicenseToken(ctx context.Context, bearerToken string) (string, error) { |
| 149 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.baseURL+"/v1/license/credentials", nil) |
| 150 | + if err != nil { |
| 151 | + return "", fmt.Errorf("failed to create request: %w", err) |
| 152 | + } |
| 153 | + req.Header.Set("Authorization", bearerToken) |
| 154 | + |
| 155 | + resp, err := c.httpClient.Do(req) |
| 156 | + if err != nil { |
| 157 | + return "", fmt.Errorf("failed to get license token: %w", err) |
| 158 | + } |
| 159 | + defer func() { |
| 160 | + if err := resp.Body.Close(); err != nil { |
| 161 | + log.Printf("failed to close response body: %v", err) |
| 162 | + } |
| 163 | + }() |
| 164 | + |
| 165 | + if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusAccepted { |
| 166 | + return "", fmt.Errorf("failed to get license token: status %d", resp.StatusCode) |
| 167 | + } |
| 168 | + |
| 169 | + var tokenResp licenseTokenResponse |
| 170 | + if err := json.NewDecoder(resp.Body).Decode(&tokenResp); err != nil { |
| 171 | + return "", fmt.Errorf("failed to decode response: %w", err) |
| 172 | + } |
| 173 | + |
| 174 | + return tokenResp.Token, nil |
| 175 | +} |
0 commit comments