Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions login.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"errors"
"fmt"
"net/url"
"os"
"time"

Expand All @@ -16,7 +17,6 @@ var loginTopic = &Topic{

var loginCmd = &Command{
Topic: "login",
Hidden: true,
Description: "Login with your Heroku credentials.",
Run: func(ctx *Context) {
login()
Expand All @@ -37,7 +37,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
Expand Down Expand Up @@ -80,6 +82,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 res.StatusCode == 403 {
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"
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func init() {
authTopic,
twoFactorTopic,
twoFactorTopicAlias,
//loginTopic,
loginTopic,
}
cli.Commands = []*Command{
commandsListCmd,
Expand All @@ -50,7 +50,7 @@ func init() {
pluginsInstallCmd,
pluginsUninstallCmd,
whoamiCmd,
//loginCmd,
loginCmd,
authLoginCmd,
twoFactorCmd,
twoFactorCmdAlias,
Expand Down
9 changes: 7 additions & 2 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
}
Expand All @@ -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")
}
Expand Down