From 7a787c71bb5d205610cdf8bcdd89c84dad02c101 Mon Sep 17 00:00:00 2001 From: dickeyxxx Date: Mon, 14 Sep 2015 14:29:18 -0700 Subject: [PATCH 1/3] use v2 for login --- login.go | 28 +++++++++++++++++++++++++++- request.go | 9 +++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/login.go b/login.go index b16f2048e9..e681c8f0c2 100644 --- a/login.go +++ b/login.go @@ -3,6 +3,7 @@ package main import ( "errors" "fmt" + "net/url" "os" "time" @@ -37,7 +38,9 @@ func login() { email := getString("Email: ") password := getPassword() - token, err := createOauthToken(email, password, "") + token, err := v2login(email, password, "") + // TODO: use createOauthToken (v3 API) + // token, err := createOauthToken(email, password, "") if err != nil { PrintError(err) return @@ -80,6 +83,29 @@ func getPassword() string { return password } +func v2login(email, password, secondFactor string) (string, error) { + req := apiRequestBase("") + req.Method = "POST" + req.Uri = req.Uri + "/login?username=" + url.QueryEscape(email) + "&password=" + url.QueryEscape(password) + if secondFactor != "" { + req.AddHeader("Heroku-Two-Factor-Code", secondFactor) + } + res, err := req.Do() + ExitIfError(err) + type Doc struct { + APIKey string `json:"api_key"` + } + var doc Doc + res.Body.FromJsonTo(&doc) + //if doc.ID == "two_factor" { + //return v2login(email, password, getString("Two-factor code: ")) + //} + if res.StatusCode != 200 { + return "", errors.New("Authentication failure.") + } + return doc.APIKey, nil +} + func createOauthToken(email, password, secondFactor string) (string, error) { req := apiRequest("") req.Method = "POST" diff --git a/request.go b/request.go index 802701a186..7d557d9d9f 100644 --- a/request.go +++ b/request.go @@ -20,10 +20,9 @@ func init() { goreq.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{RootCAs: getCACerts()} } -func apiRequest(authToken string) *goreq.Request { +func apiRequestBase(authToken string) *goreq.Request { req := goreq.Request{ Uri: "https://" + apiHost(), - Accept: "application/vnd.heroku+json; version=3", ShowDebug: debugging, Insecure: !shouldVerifyHost(apiHost()), } @@ -40,6 +39,12 @@ func apiRequest(authToken string) *goreq.Request { return &req } +func apiRequest(authToken string) *goreq.Request { + req := apiRequestBase(authToken) + req.AddHeader("Accept", "application/vnd.heroku+json; version=3") + return req +} + func shouldVerifyHost(host string) bool { return !strings.HasSuffix(host, "herokudev.com") } From 0dcdc03937225eeea2d5e2d862e29ded6d85acc2 Mon Sep 17 00:00:00 2001 From: dickeyxxx Date: Mon, 14 Sep 2015 14:32:12 -0700 Subject: [PATCH 2/3] use v4 cli for login --- login.go | 1 - main.go | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/login.go b/login.go index e681c8f0c2..96211008e0 100644 --- a/login.go +++ b/login.go @@ -17,7 +17,6 @@ var loginTopic = &Topic{ var loginCmd = &Command{ Topic: "login", - Hidden: true, Description: "Login with your Heroku credentials.", Run: func(ctx *Context) { login() diff --git a/main.go b/main.go index d18c0ecef7..2968266c06 100644 --- a/main.go +++ b/main.go @@ -39,7 +39,7 @@ func init() { authTopic, twoFactorTopic, twoFactorTopicAlias, - //loginTopic, + loginTopic, } cli.Commands = []*Command{ commandsListCmd, @@ -50,7 +50,7 @@ func init() { pluginsInstallCmd, pluginsUninstallCmd, whoamiCmd, - //loginCmd, + loginCmd, authLoginCmd, twoFactorCmd, twoFactorCmdAlias, From 32e02c7e330e634ea57d483949a39334f56ed2a7 Mon Sep 17 00:00:00 2001 From: dickeyxxx Date: Mon, 14 Sep 2015 14:36:07 -0700 Subject: [PATCH 3/3] fix 2fa login --- login.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/login.go b/login.go index 96211008e0..54a33ac06f 100644 --- a/login.go +++ b/login.go @@ -96,9 +96,9 @@ func v2login(email, password, secondFactor string) (string, error) { } var doc Doc res.Body.FromJsonTo(&doc) - //if doc.ID == "two_factor" { - //return v2login(email, password, getString("Two-factor code: ")) - //} + if res.StatusCode == 403 { + return v2login(email, password, getString("Two-factor code: ")) + } if res.StatusCode != 200 { return "", errors.New("Authentication failure.") }