提交 865b3f34 编写于 作者: M Mislav Marohnić

Merge branch 'llimllib-token-instead-of-password'

Closes #1740
......@@ -37,6 +37,54 @@ Feature: OAuth authentication
And the file "../home/.config/hub" should contain "oauth_token: OTOKEN"
And the file "../home/.config/hub" should have mode "0600"
Scenario: Prompt for username & password, receive personal access token
Given the GitHub API server:
"""
get('/user') {
halt 401 unless request.env['HTTP_AUTHORIZATION'] == 'token 0123456789012345678901234567890123456789'
json :login => 'llIMLLib'
}
post('/user/repos') {
halt 401 unless request.env['HTTP_AUTHORIZATION'] == 'token 0123456789012345678901234567890123456789'
status 201
json :full_name => 'llimllib/dotfiles'
}
"""
When I run `hub create` interactively
When I type "llimllib"
And I type "0123456789012345678901234567890123456789"
And the exit status should be 0
And the file "../home/.config/hub" should contain "user: llIMLLib"
And the file "../home/.config/hub" should contain:
"""
oauth_token: "0123456789012345678901234567890123456789"
"""
Scenario: Ask for username & password, receive password that looks like a token
Given the GitHub API server:
"""
post('/authorizations') {
assert_basic_auth 'llimllib', '0123456789012345678901234567890123456789'
status 201
json :token => 'OTOKEN'
}
get('/user') {
halt 401 unless request.env['HTTP_AUTHORIZATION'] == 'token OTOKEN'
json :login => 'llIMLLib'
}
post('/user/repos') {
halt 401 unless request.env['HTTP_AUTHORIZATION'] == 'token OTOKEN'
status 201
json :full_name => 'llimllib/dotfiles'
}
"""
When I run `hub create` interactively
When I type "llimllib"
And I type "0123456789012345678901234567890123456789"
And the exit status should be 0
And the file "../home/.config/hub" should contain "user: llIMLLib"
And the file "../home/.config/hub" should contain "oauth_token: OTOKEN"
Scenario: Rename & retry creating authorization if there's a token name collision
Given the GitHub API server:
"""
......
......@@ -683,13 +683,30 @@ type AuthorizationEntry struct {
Token string `json:"token"`
}
func isToken(api *simpleClient, password string) bool {
api.PrepareRequest = func(req *http.Request) {
req.Header.Set("Authorization", "token "+password)
}
res, _ := api.Get("user")
if res != nil && res.StatusCode == 200 {
return true
}
return false
}
func (client *Client) FindOrCreateToken(user, password, twoFactorCode string) (token string, err error) {
api := client.apiClient()
if len(password) >= 40 && isToken(api, password) {
return password, nil
}
params := map[string]interface{}{
"scopes": []string{"repo"},
"note_url": OAuthAppURL,
}
api := client.apiClient()
api.PrepareRequest = func(req *http.Request) {
req.SetBasicAuth(user, password)
if twoFactorCode != "" {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册