Skip to content
Merged

sso #148

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
5 changes: 5 additions & 0 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Godeps/_workspace/src/github.com/toqueteos/webbrowser/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions Godeps/_workspace/src/github.com/toqueteos/webbrowser/LICENSE.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions Godeps/_workspace/src/github.com/toqueteos/webbrowser/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Godeps/_workspace/src/github.com/toqueteos/webbrowser/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

111 changes: 111 additions & 0 deletions Godeps/_workspace/src/github.com/toqueteos/webbrowser/webbrowser.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func getNetrc() *netrc.Netrc {
func auth() (password string) {
token := apiToken()
if token == "" {
login()
interactiveLogin()
return auth()
}
return token
Expand Down
66 changes: 58 additions & 8 deletions login.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/dickeyxxx/speakeasy"
"github.com/toqueteos/webbrowser"
)

var loginTopic = &Topic{
Expand All @@ -19,24 +20,73 @@ var loginTopic = &Topic{
var loginCmd = &Command{
Topic: "login",
Description: "Login with your Heroku credentials.",
Run: func(ctx *Context) {
login()
Flags: []Flag{
{Name: "sso", Description: "login for enterprise users under SSO"},
},
Run: login,
}

var authLoginCmd = &Command{
Topic: "auth",
Command: "login",
Description: "Login with your Heroku credentials.",
Run: func(ctx *Context) {
login()
Flags: []Flag{
{Name: "sso", Description: "login for enterprise users under SSO"},
},
Run: login,
}

func login(ctx *Context) {
if ctx.Flags["sso"] == true {
ssoLogin()
} else {
interactiveLogin()
}
}

func ssoLogin() {
url := os.Getenv("SSO_URL")
if url == "" {
org := os.Getenv("HEROKU_ORGANIZATION")
for org == "" {
org = getString("Enter your organization name: ")
}
url = "https://sso.heroku.com/saml/" + org + "/init?cli=true"
}
Err("Opening browser for login...")
err := webbrowser.Open(url)
if err != nil {
Errln(" " + err.Error() + ".\nNavigate to " + cyan(url))
} else {
Errln(" done")
}
token := getPassword("Enter your access token (typing will be hidden): ")
user := getUserFromToken(token)
if user == "" {
ExitIfError(errors.New("Access token invalid."))
}
saveOauthToken(user, token)
Println("Logged in as " + cyan(user))
}

func getUserFromToken(token string) string {
req := apiRequest(token)
req.Method = "GET"
req.Uri = req.Uri + "/account"
res, err := req.Do()
ExitIfError(err)
if res.StatusCode != 200 {
return ""
}
var doc map[string]interface{}
res.Body.FromJsonTo(&doc)
return doc["email"].(string)
}

func login() {
func interactiveLogin() {
Println("Enter your Heroku credentials.")
email := getString("Email: ")
password := getPassword()
password := getPassword("Password (typing will be hidden): ")

token, err := v2login(email, password, "")
// TODO: use createOauthToken (v3 API)
Expand Down Expand Up @@ -71,8 +121,8 @@ func getString(prompt string) string {
return s
}

func getPassword() string {
password, err := speakeasy.Ask("Password (typing will be hidden): ")
func getPassword(prompt string) string {
password, err := speakeasy.Ask(prompt)
if err != nil {
if err.Error() == "The handle is invalid." {
Errln(`Login is currently incompatible with git bash/cygwin
Expand Down
4 changes: 2 additions & 2 deletions two_factor.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func twoFactorGenerateRun(ctx *Context) {
req := apiRequest(ctx.APIToken)
req.Method = "POST"
req.Uri = req.Uri + "/account/recovery-codes"
req.AddHeader("Heroku-Password", getPassword())
req.AddHeader("Heroku-Password", getPassword("Password (typing will be hidden): "))
req.AddHeader("Heroku-Two-Factor-Code", getString("Two-factor code: "))
res, err := req.Do()
ExitIfError(err)
Expand Down Expand Up @@ -91,7 +91,7 @@ func twoFactorDisableRun(ctx *Context) {
req.Uri = req.Uri + "/account/"
req.Body = map[string]interface{}{
"two_factor_authentication": "false",
"password": getPassword(),
"password": getPassword("Password (typing will be hidden):"),
}
res, err := req.Do()
ExitIfError(err)
Expand Down
12 changes: 3 additions & 9 deletions whoami.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,11 @@ var whoamiCmd = &Command{
Exit(100)
}

req := apiRequest(ctx.APIToken)
req.Method = "GET"
req.Uri = req.Uri + "/account"
res, err := req.Do()
ExitIfError(err)
if res.StatusCode != 200 {
user := getUserFromToken(ctx.APIToken)
if user == "" {
Println("not logged in")
Exit(100)
}
var doc map[string]interface{}
res.Body.FromJsonTo(&doc)
Println(doc["email"])
Println(user)
},
}