Skip to content
This repository was archived by the owner on Jan 16, 2021. It is now read-only.

Commit 453fa27

Browse files
committed
Merge pull request #11 from pavanka/access_tokens
refactor configure token command to not ask for email
2 parents f12a828 + 0362634 commit 453fa27

File tree

6 files changed

+26
-34
lines changed

6 files changed

+26
-34
lines changed

apps.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ func getAuthHeaders(credentials *credentials, headers http.Header) http.Header {
8181
if headers == nil {
8282
headers = make(http.Header)
8383
}
84-
headers.Add("X-Parse-Email", credentials.email)
8584
if credentials.token != "" {
8685
headers.Add("X-Parse-Account-Key", credentials.token)
8786
} else {
87+
headers.Add("X-Parse-Email", credentials.email)
8888
headers.Add("X-Parse-Password", credentials.password)
8989
}
9090
return headers

apps_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func newAppHarness(t testing.TB) (*Harness, []*app) {
5353
email := r.Header.Get("X-Parse-Email")
5454
password := r.Header.Get("X-Parse-Password")
5555
token := r.Header.Get("X-Parse-Account-Key")
56-
if !((email == "email" && password == "password") || (email == "email" && token == "token")) {
56+
if !((email == "email" && password == "password") || (token == "token")) {
5757
return &http.Response{
5858
StatusCode: http.StatusUnauthorized,
5959
Body: ioutil.NopCloser(strings.NewReader(`{"error": "incorrect credentials"}`)),

configure_cmd.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,22 @@ type configureCmd struct {
1313

1414
func (c *configureCmd) accessToken(e *env) error {
1515
fmt.Fprintf(e.Out,
16-
`Please enter the email id you used to register with Parse
17-
and an access token if you already generated it.
16+
`Please enter an access token if you already generated it.
1817
If you do not have an access token or would like to generate a new one,
1918
please type: "y" to open the browser or "n" to continue: `,
2019
)
2120

2221
c.login.helpCreateToken(e)
2322

2423
var credentials credentials
25-
fmt.Fprintf(e.Out, "Email: ")
26-
fmt.Fscanf(e.In, "%s\n", &credentials.email)
27-
fmt.Fprintf(e.Out, "Account Key: ")
24+
fmt.Fprintf(e.Out, "Access Token: ")
2825
fmt.Fscanf(e.In, "%s\n", &credentials.token)
2926

3027
_, err := (&apps{login: login{credentials: credentials}}).restFetchApps(e)
3128
if err != nil {
3229
if err == errAuth {
3330
fmt.Fprintf(e.Err,
34-
`Sorry, we do not have a user with this email and access token.
31+
`Sorry, the access token you provided is not valid.
3532
Please follow instructions at %s to generate a new access token.
3633
`,
3734
keysURL,

configure_cmd_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,21 @@ func TestConfigureAcessToken(t *testing.T) {
1616
defer h.Stop()
1717

1818
c := configureCmd{login: login{tokenReader: strings.NewReader("")}}
19-
h.env.In = ioutil.NopCloser(strings.NewReader("n\nemail\ntoken\n"))
19+
h.env.In = ioutil.NopCloser(strings.NewReader("n\ntoken\n"))
2020
ensure.Nil(t, c.accessToken(h.env))
2121
ensure.DeepEqual(t,
2222
h.Out.String(),
23-
`Please enter the email id you used to register with Parse
24-
and an access token if you already generated it.
23+
`Please enter an access token if you already generated it.
2524
If you do not have an access token or would like to generate a new one,
26-
please type: "y" to open the browser or "n" to continue: Email: Account Key: Successfully stored credentials.
27-
`)
28-
25+
please type: "y" to open the browser or "n" to continue: Access Token: Successfully stored credentials.
26+
`,
27+
)
2928
h.env.In = ioutil.NopCloser(strings.NewReader("n\nemail\ninvalid\n"))
3029
ensure.Err(t, c.accessToken(h.env), regexp.MustCompile("Please try again"))
3130
ensure.DeepEqual(t,
3231
h.Err.String(),
33-
`Sorry, we do not have a user with this email and access token.
32+
`Sorry, the access token you provided is not valid.
3433
Please follow instructions at https://www.parse.com/account_keys to generate a new access token.
35-
`)
34+
`,
35+
)
3636
}

login.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ func (l *login) getTokenCredentials(e *env) (*credentials, error) {
9999
return nil, stackerr.Newf("could not find token for %s", server)
100100
}
101101
return &credentials{
102-
email: machine.Login,
103102
token: machine.Password,
104103
}, nil
105104
}
@@ -120,9 +119,8 @@ func (l *login) updatedNetrcContent(
120119
}
121120
machine := tokens.FindMachine(server)
122121
if machine == nil {
123-
machine = tokens.NewMachine(server, credentials.email, credentials.token, "")
122+
machine = tokens.NewMachine(server, "default", credentials.token, "")
124123
} else {
125-
machine.UpdateLogin(credentials.email)
126124
machine.UpdatePassword(credentials.token)
127125
}
128126

@@ -169,12 +167,10 @@ func (l *login) authUserWithToken(e *env) error {
169167
apps := &apps{login: login{credentials: *tokenCredentials}}
170168
_, err = apps.restFetchApps(e)
171169
if err == errAuth {
172-
fmt.Fprintf(e.Err,
170+
fmt.Fprintln(e.Err,
173171
`Sorry, you have an invalid token associated with the email: %q.
174172
To avoid typing the email and password everytime,
175-
please type "parse login" and provide a valid token for the email.
176-
`,
177-
tokenCredentials.email,
173+
please type "parse configure token" and provide a valid access token.`,
178174
)
179175
}
180176
if err != nil {

login_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,12 @@ func TestGetTokenCredentials(t *testing.T) {
3232

3333
l.tokenReader = strings.NewReader(
3434
`machine api.example.com
35-
login email
35+
login default
3636
password token
3737
`,
3838
)
3939
credentials, err := l.getTokenCredentials(h.env)
4040
ensure.Nil(t, err)
41-
ensure.DeepEqual(t, credentials.email, "email")
4241
ensure.DeepEqual(t, credentials.token, "token")
4342

4443
h.env.Server = "http://api.parse.com"
@@ -76,26 +75,26 @@ func TestUpdatedNetrcContent(t *testing.T) {
7675
updated, err := l.updatedNetrcContent(h.env,
7776
strings.NewReader(
7877
`machine api.example.com
79-
login email
78+
login default
8079
password token0
8180
8281
machine api.example.org
83-
login email
82+
login default
8483
password token
8584
`,
8685
),
87-
&credentials{email: "email", token: "token"},
86+
&credentials{token: "token"},
8887
)
8988

9089
ensure.Nil(t, err)
9190
ensure.DeepEqual(t,
9291
string(updated),
9392
`machine api.example.com
94-
login email
93+
login default
9594
password token
9695
9796
machine api.example.org
98-
login email
97+
login default
9998
password token
10099
`,
101100
)
@@ -104,22 +103,22 @@ machine api.example.org
104103
updated, err = l.updatedNetrcContent(h.env,
105104
strings.NewReader(
106105
`machine api.example.com
107-
login email
106+
login default
108107
password token
109108
`,
110109
),
111-
&credentials{email: "email", token: "token"},
110+
&credentials{token: "token"},
112111
)
113112

114113
ensure.Nil(t, err)
115114
ensure.DeepEqual(t,
116115
string(updated),
117116
`machine api.example.com
118-
login email
117+
login default
119118
password token
120119
121120
machine api.example.org
122-
login email
121+
login default
123122
password token`,
124123
)
125124
}

0 commit comments

Comments
 (0)