Skip to content

Commit

Permalink
Changes the login with token method to a flag for auth login
Browse files Browse the repository at this point in the history
  • Loading branch information
groulot committed Nov 27, 2017
1 parent 326289b commit 9c01dc2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 32 deletions.
27 changes: 11 additions & 16 deletions auth/auth.go
Expand Up @@ -26,7 +26,8 @@ const (
// An entry will be created in ~/.netrc on successful login.
func Login() error {
// Create a function to be overriden in tests
email, password, err := getCredentials()
email := getEmail()
password, err := getPassword("Enter password (will be hidden): ")
if err != nil {
return err
}
Expand All @@ -47,9 +48,9 @@ func Login() error {

// Login with the user email and API token
// An entry will be created in ~/.netrc on successful login.
func LoginWithAPIToken() error {
func LoginWithAPIToken(token string) (err error) {
// Create a function to be overriden in tests
email, token, err := getTokenCredentials()
email := getEmail()
if err != nil {
return err
}
Expand Down Expand Up @@ -81,26 +82,20 @@ func Logout() error {
}

// Lambda to be overriden in tests
var getCredentials = func() (email, password string, err error) {
var getEmail = func() (email string) {
fmt.Printf("Enter your email: ")
fmt.Scanf("%s", &email)
// NOTE: gopass doesn't support multi-byte chars on Windows
password, err = readPassword("Enter password (will be hidden): ")
if err != nil {
return "", "", err
}
return email, password, nil
return email
}

var getTokenCredentials = func() (email, token string, err error) {
fmt.Printf("Enter your email: ")
fmt.Scanf("%s", &email)
// Lambda to be overriden in tests
var getPassword = func(prompt string) (password string, err error) {
// NOTE: gopass doesn't support multi-byte chars on Windows
token, err = readPassword("Enter your token (API Key, it will be hidden) : ")
password, err = readPassword(prompt)
if err != nil {
return "", "", err
return "", err
}
return email, token, nil
return password, nil
}

func readPassword(prompt string) (password string, err error) {
Expand Down
7 changes: 5 additions & 2 deletions auth/auth_test.go
Expand Up @@ -70,8 +70,11 @@ func TestLogin(t *testing.T) {

api.APIImpl = api.NewAPIv1(ts.URL, "")
// don't try to use stdin
getCredentials = func() (email, password string, err error) {
return "batman@example.com", "secret123", nil
getEmail = func() (email string) {
return "batman@example.com"
}
getPassword = func(prompt string) (password string, err error) {
return "secret123", nil
}

netrcFile := bytes.NewBufferString("")
Expand Down
11 changes: 6 additions & 5 deletions commands/app.go
Expand Up @@ -63,13 +63,14 @@ func App() *cli.App {
{
Name: "login",
Usage: "Login",
Flags: []cli.Flag{
cli.StringFlag{
Name: "with-api-token",
Usage: "Log in with your API token (API key in the user profile)",
},
},
Action: Login,
},
{
Name: "with-api-token",
Usage: "Login with your API token (API Key in your profile page)",
Action: LoginWithAPIToken,
},
{
Name: "logout",
Usage: "Logout",
Expand Down
20 changes: 11 additions & 9 deletions commands/auth.go
Expand Up @@ -9,18 +9,20 @@ var login = func() error {
return auth.Login()
}

var login_with_api_token = func() error {
return auth.LoginWithAPIToken()
var login_with_api_token = func(api_token string) error {
return auth.LoginWithAPIToken(api_token)
}

// auth.Login wrapper with a cli.Content
func Login(ctx *cli.Context) error {
err := login()
return err
}

func LoginWithAPIToken(ctx *cli.Context) error {
err := login_with_api_token()
func Login(ctx *cli.Context) (err error) {
if ctx.IsSet("with-api-token") {
// log in with the provided token
api_token := ctx.String("with-api-token")
err = login_with_api_token(api_token)
} else {
// log in with the user and password
err = login()
}
return err
}

Expand Down

0 comments on commit 9c01dc2

Please sign in to comment.