Skip to content

Commit

Permalink
[DXCDT-19] Use GET instead of POST when downloading quickstarts (#391)
Browse files Browse the repository at this point in the history
* Update quickstarts.json using json-generator branch

* Use GET instead of POST when downloading quickstarts
  • Loading branch information
sergiught authored Jan 28, 2022
1 parent 57001ee commit fd0ce1d
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 91 deletions.
72 changes: 38 additions & 34 deletions internal/cli/data/quickstarts.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"branch": "master"
},
{
"name": "Go",
"name": "Go API",
"path": "golang",
"samples": [
"01-Authorization-RS256"
Expand Down Expand Up @@ -73,7 +73,9 @@
{
"name": "PHP API",
"path": "php",
"samples": ["app"],
"samples": [
"app"
],
"org": "auth0-samples",
"repo": "auth0-php-api-samples",
"branch": "main"
Expand All @@ -99,17 +101,7 @@
"branch": "master"
},
{
"name": "Spring Security 4 Java API",
"path": "java-spring-security",
"samples": [
"01-Authorization"
],
"org": "auth0-samples",
"repo": "auth0-spring-security-api-sample",
"branch": "master"
},
{
"name": "Spring Security 5 Java API",
"name": "Spring Boot API",
"path": "java-spring-security5",
"samples": [
"01-Authorization-MVC"
Expand Down Expand Up @@ -151,14 +143,34 @@
"branch": "master"
},
{
"name": "Ionic 4",
"path": "ionic4",
"name": "Ionic & Capacitor (Angular)",
"path": "ionic-angular",
"samples": [
"01-Login"
"angular"
],
"org": "auth0-samples",
"repo": "auth0-ionic4-samples",
"branch": "master"
"repo": "auth0-ionic-samples",
"branch": "main"
},
{
"name": "Ionic & Capacitor (React)",
"path": "ionic-react",
"samples": [
"react"
],
"org": "auth0-samples",
"repo": "auth0-ionic-samples",
"branch": "main"
},
{
"name": "iOS / macOS",
"path": "swift-beta",
"samples": [
"Sample-01"
],
"org": "auth0-samples",
"repo": "auth0-ios-swift-sample",
"branch": "beta"
},
{
"name": "iOS Swift",
Expand Down Expand Up @@ -217,7 +229,9 @@
"org": "auth0-samples",
"repo": "auth0-uwp-oidc-samples",
"branch": "master"
},
}
],
"spa": [
{
"name": "WPF / Winforms",
"path": "wpf-winforms",
Expand Down Expand Up @@ -297,12 +311,10 @@
"branch": "master"
},
{
"name": "ASP.NET Core",
"name": "ASP.NET Core MVC",
"path": "aspnet-core",
"samples": [
"Quickstart/01-Login",
"Quickstart/02-User-Profile",
"Quickstart/03-Authorization"
"Quickstart/Sample"
],
"org": "auth0-samples",
"repo": "auth0-aspnetcore-mvc-samples",
Expand Down Expand Up @@ -390,20 +402,12 @@
"repo": "auth0-nextjs-samples",
"branch": "main"
},
{
"name": "Node.js",
"path": "nodejs",
"samples": [
"01-Login"
],
"org": "auth0-samples",
"repo": "auth0-nodejs-webapp-sample",
"branch": "master"
},
{
"name": "PHP",
"path": "php",
"samples": ["app"],
"samples": [
"app"
],
"org": "auth0-samples",
"repo": "auth0-php-web-app",
"branch": "main"
Expand Down
93 changes: 36 additions & 57 deletions internal/cli/quickstarts.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cli

import (
"bytes"
"context"
_ "embed"
"encoding/json"
Expand All @@ -16,12 +15,13 @@ import (
"regexp"
"strings"

"github.com/auth0/auth0-cli/internal/ansi"
"github.com/auth0/auth0-cli/internal/auth0"
"github.com/auth0/auth0-cli/internal/prompt"
"github.com/mholt/archiver/v3"
"github.com/spf13/cobra"
"gopkg.in/auth0.v5/management"

"github.com/auth0/auth0-cli/internal/ansi"
"github.com/auth0/auth0-cli/internal/auth0"
"github.com/auth0/auth0-cli/internal/prompt"
)

// QuickStart app types and defaults
Expand Down Expand Up @@ -160,13 +160,13 @@ auth0 qs download --stack <stack>`,
}
}

q, err := getQuickstart(client.GetAppType(), inputs.Stack)
quickstart, err := getQuickstart(client.GetAppType(), inputs.Stack)
if err != nil {
return fmt.Errorf("An unexpected error occurred with the specified stack %v: %v", inputs.Stack, err)
}

err = ansi.Waiting(func() error {
return downloadQuickStart(cmd.Context(), cli, client, target, q)
return downloadQuickStart(cmd.Context(), client, target, quickstart)
})

if err != nil {
Expand All @@ -180,7 +180,7 @@ auth0 qs download --stack <stack>`,
return err
}

qsSamplePath := path.Join(target, q.Samples[0])
qsSamplePath := path.Join(target, quickstart.Samples[0])
readme, err := loadQuickstartSampleReadme(qsSamplePath) // Some QS have non-markdown READMEs (eg auth0-python uses rst)

if err == nil {
Expand All @@ -206,86 +206,65 @@ auth0 qs download --stack <stack>`,
return cmd
}

func downloadQuickStart(ctx context.Context, cli *cli, client *management.Client, target string, q auth0.Quickstart) error {
var payload struct {
Branch string `json:"branch"`
Org string `json:"org"`
Repo string `json:"repo"`
Path string `json:"path"`
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
CallbackURL string `json:"callback_url"`
Domain string `json:"domain"`
Tenant string `json:"tenant"`
}

ten, err := cli.getTenant()
func downloadQuickStart(ctx context.Context, client *management.Client, target string, q auth0.Quickstart) error {
request, err := http.NewRequestWithContext(ctx, http.MethodGet, quickstartEndpoint, nil)
if err != nil {
return fmt.Errorf("Unable to get tenant: %v", err)
return unexpectedError(err)
}

payload.Tenant = ten.Name
payload.Domain = ten.Domain
params := request.URL.Query()

// FIXME(copland): Default to first item from list of samples.
// Eventually we should add a forced survey for user to select one if
// there are multiple.
payload.Branch = q.Branch
payload.Repo = q.Repo
payload.Path = q.Samples[0]

// These appear to be largely constant and refers to the github
// username they're under.
payload.Org = quickstartOrg
payload.ClientID = client.GetClientID()
payload.ClientSecret = client.GetClientSecret()

// Callback URL, if not set, will just take the default one.
payload.CallbackURL = quickstartDefaultCallbackURL
// Eventually we should add a forced survey for
// user to select one if there are multiple.
params.Add("branch", q.Branch)
params.Add("repo", q.Repo)
params.Add("path", q.Samples[0])

// These appear to be largely constant and refers
// to the GitHub username they're under.
params.Add("org", quickstartOrg)
params.Add("client_id", client.GetClientID())

// Callback URL, if not set, it will just take the default one.
callbackURL := quickstartDefaultCallbackURL
if list := urlsFor(client.Callbacks); len(list) > 0 {
payload.CallbackURL = list[0]
}

buf := &bytes.Buffer{}
if err := json.NewEncoder(buf).Encode(payload); err != nil {
return unexpectedError(err)
callbackURL = list[0]
}
params.Add("callback_url", callbackURL)

req, err := http.NewRequest("POST", quickstartEndpoint, buf)
if err != nil {
return unexpectedError(err)
}
req.Header.Set("Content-Type", quickstartContentType)
request.URL.RawQuery = params.Encode()
request.Header.Set("Content-Type", quickstartContentType)

res, err := http.DefaultClient.Do(req)
response, err := http.DefaultClient.Do(request)
if err != nil {
return unexpectedError(err)
}

if res.StatusCode != http.StatusOK {
return fmt.Errorf("Expected status %d, got %d", http.StatusOK, res.StatusCode)
if response.StatusCode != http.StatusOK {
return fmt.Errorf("Expected status %d, got %d", http.StatusOK, response.StatusCode)
}

tmpfile, err := ioutil.TempFile("", "auth0-quickstart*.zip")
tmpFile, err := ioutil.TempFile("", "auth0-quickstart*.zip")
if err != nil {
return unexpectedError(err)
}

_, err = io.Copy(tmpfile, res.Body)
_, err = io.Copy(tmpFile, response.Body)
if err != nil {
return unexpectedError(err)
}

if err := tmpfile.Close(); err != nil {
if err := tmpFile.Close(); err != nil {
return unexpectedError(err)
}
defer os.Remove(tmpfile.Name())
defer os.Remove(tmpFile.Name())

if err := os.RemoveAll(target); err != nil {
return unexpectedError(err)
}

if err := archiver.Unarchive(tmpfile.Name(), target); err != nil {
if err := archiver.Unarchive(tmpFile.Name(), target); err != nil {
return unexpectedError(err)
}

Expand Down

0 comments on commit fd0ce1d

Please sign in to comment.