| 53 | } |
| 54 | |
| 55 | func (q Quickstart) Download(ctx context.Context, downloadPath string, client *management.Client) error { |
| 56 | quickstartEndpoint := fmt.Sprintf("https://auth0.com%s", q.DownloadLink) |
| 57 | request, err := http.NewRequestWithContext(ctx, http.MethodGet, quickstartEndpoint, nil) |
| 58 | if err != nil { |
| 59 | return err |
| 60 | } |
| 61 | |
| 62 | params := request.URL.Query() |
| 63 | params.Add("org", quickstartsOrg) |
| 64 | params.Add("client_id", client.GetClientID()) |
| 65 | |
| 66 | // Callback URL, if not set, it will just take the default one. |
| 67 | callbackURL := quickstartsDefaultCallbackURL |
| 68 | if list := client.GetCallbacks(); len(list) > 0 { |
| 69 | callbackURL = list[0] |
| 70 | } |
| 71 | params.Add("callback_url", callbackURL) |
| 72 | |
| 73 | request.URL.RawQuery = params.Encode() |
| 74 | request.Header.Set("Content-Type", "application/json") |
| 75 | |
| 76 | userAgent := "Auth0 CLI" // Set User-Agent header using the standard CLI format. |
| 77 | request.Header.Set("User-Agent", fmt.Sprintf("%v/%v", userAgent, strings.TrimPrefix(buildinfo.Version, "v"))) |
| 78 | |
| 79 | response, err := quickstartHTTPClient.Do(request) |
| 80 | if err != nil { |
| 81 | return err |
| 82 | } |
| 83 | |
| 84 | defer func() { |
| 85 | _ = response.Body.Close() |
| 86 | }() |
| 87 | |
| 88 | if response.StatusCode != http.StatusOK { |
| 89 | return fmt.Errorf("expected status %d, got %d", http.StatusOK, response.StatusCode) |
| 90 | } |
| 91 | |
| 92 | // Check if we're getting a zip file or HTML response. |
| 93 | contentType := response.Header.Get("Content-Type") |
| 94 | if contentType != "" && !strings.Contains(contentType, "application/zip") && !strings.Contains(contentType, "application/octet-stream") { |
| 95 | return fmt.Errorf("expected zip file but got content-type: %s. The quickstart endpoint may have returned an error page", contentType) |
| 96 | } |
| 97 | |
| 98 | tmpFile, err := os.CreateTemp("", "auth0-quickstart*.zip") |
| 99 | if err != nil { |
| 100 | return err |
| 101 | } |
| 102 | |
| 103 | _, err = io.Copy(tmpFile, io.LimitReader(response.Body, maxDownloadSize)) |
| 104 | if err != nil { |
| 105 | return err |
| 106 | } |
| 107 | |
| 108 | if err = tmpFile.Close(); err != nil { |
| 109 | return err |
| 110 | } |
| 111 | defer func() { |
| 112 | _ = os.Remove(tmpFile.Name()) |