Fix bug that can't authenticate user

This commit is contained in:
Jingwen Owen Ou 2013-05-30 06:58:19 -07:00
Родитель ea3f6c1451
Коммит a85616ad5b
4 изменённых файлов: 54 добавлений и 80 удалений

Просмотреть файл

@ -4,64 +4,73 @@ import (
"bufio"
"encoding/json"
"errors"
"fmt"
"github.com/howeyc/gopass"
"github.com/jingweno/gh/utils"
"os"
"path/filepath"
)
const GitHubHost string = "github.com"
type Config struct {
User string `json:"user"`
Token string `json:"token"`
}
func (c *Config) UserName() string {
if c.User == "" {
var user string
msg := fmt.Sprintf("%s username: ", GitHubHost)
fmt.Print(msg)
fmt.Scanln(&user)
c.User = user
}
return c.User
}
func (c *Config) Password() string {
msg := fmt.Sprintf("%s password for %s (never stored): ", GitHubHost, c.User)
fmt.Print(msg)
pass := gopass.GetPasswd()
if len(pass) == 0 {
utils.Check(errors.New("Password cannot be empty"))
}
return string(pass)
}
var DefaultFile string
func init() {
DefaultFile = filepath.Join(os.Getenv("HOME"), ".config", "gh")
}
func LoadAll() ([]*Config, error) {
configs, err := loadFrom(DefaultFile)
if err != nil {
return nil, err
}
return configs, nil
func Load() (Config, error) {
return loadFrom(DefaultFile)
}
func Load(user string) (*Config, error) {
configs, err := LoadAll()
if err != nil {
return nil, err
}
for _, c := range configs {
if c.User == user {
return c, nil
}
}
return nil, errors.New("There's no matching config for user: " + user)
}
func loadFrom(filename string) ([]*Config, error) {
func loadFrom(filename string) (Config, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
return Config{}, err
}
return doLoadFrom(f)
}
func doLoadFrom(f *os.File) ([]*Config, error) {
func doLoadFrom(f *os.File) (Config, error) {
defer f.Close()
reader := bufio.NewReader(f)
dec := json.NewDecoder(reader)
var c []*Config
var c Config
err := dec.Decode(&c)
if err != nil {
return nil, err
return Config{}, err
}
return c, nil
@ -72,8 +81,6 @@ func Save(config *Config) error {
}
func saveTo(filename string, config *Config) error {
configs, _ := loadFrom(filename)
err := os.MkdirAll(filepath.Dir(filename), 0771)
if err != nil {
return err
@ -84,25 +91,12 @@ func saveTo(filename string, config *Config) error {
return err
}
var foundConfig *Config
for _, c := range configs {
if c.User == config.User {
foundConfig = c
break
}
}
if foundConfig == nil {
configs = append(configs, config)
} else {
foundConfig.Token = config.Token
}
return doSaveTo(f, configs)
return doSaveTo(f, config)
}
func doSaveTo(f *os.File, configs []*Config) error {
func doSaveTo(f *os.File, config *Config) error {
defer f.Close()
enc := json.NewEncoder(f)
return enc.Encode(configs)
return enc.Encode(config)
}

Просмотреть файл

@ -15,21 +15,16 @@ func TestSave(t *testing.T) {
err := saveTo(file, &config)
assert.Equal(t, nil, err)
configs, err := loadFrom(file)
config, err = loadFrom(file)
assert.Equal(t, nil, err)
assert.Equal(t, 1, len(configs))
assert.Equal(t, "jingweno", configs[0].User)
assert.Equal(t, "123", configs[0].Token)
assert.Equal(t, "jingweno", config.User)
assert.Equal(t, "123", config.Token)
newConfig := Config{"foo", "456"}
err = saveTo(file, &newConfig)
assert.Equal(t, nil, err)
configs, err = loadFrom(file)
assert.Equal(t, nil, err)
assert.Equal(t, 2, len(configs))
assert.Equal(t, "jingweno", configs[0].User)
assert.Equal(t, "123", configs[0].Token)
assert.Equal(t, "foo", configs[1].User)
assert.Equal(t, "456", configs[1].Token)
config, err = loadFrom(file)
assert.Equal(t, "foo", config.User)
assert.Equal(t, "456", config.Token)
}

Просмотреть файл

@ -9,9 +9,9 @@ import (
)
const (
GitHubUrl string = "https://" + GitHubHost
GitHubHost string = "api.github.com"
OAuthAppUrl string = "http://owenou.com/gh"
GitHubApiUrl string = "https://" + GitHubApiHost
GitHubApiHost string = "api.github.com"
OAuthAppUrl string = "http://owenou.com/gh"
)
type GitHub struct {
@ -57,15 +57,9 @@ func hashAuth(u, p string) string {
func New() *GitHub {
project := CurrentProject()
configs, err := config.LoadAll()
var c *config.Config
if err == nil {
c = configs[0]
} else {
c = &config.Config{User: project.Owner}
}
c, _ := config.Load()
gh := GitHub{&http.Client{}, "", project, c}
gh := GitHub{&http.Client{}, "", project, &c}
if c.Token != "" {
gh.updateTokenAuth(c.Token)
}

Просмотреть файл

@ -5,7 +5,6 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/howeyc/gopass"
"io/ioutil"
"net/http"
)
@ -24,16 +23,8 @@ type GitHubErrors struct {
}
func performBasicAuth(gh *GitHub) error {
user := gh.project.Owner
msg := fmt.Sprintf("%s password for %s (never stored): ", GitHubHost, user)
fmt.Print(msg)
pass := gopass.GetPasswd()
if len(pass) == 0 {
return errors.New("Password cannot be empty")
}
password := string(pass)
user := gh.config.UserName()
password := gh.config.Password()
gh.updateBasicAuth(user, password)
return obtainOAuthTokenWithBasicAuth(gh)
@ -73,7 +64,7 @@ func obtainOAuthTokenWithBasicAuth(gh *GitHub) error {
}
func httpGet(gh *GitHub, uri string, extraHeaders map[string]string) (*http.Response, error) {
url := fmt.Sprintf("%s%s", GitHubUrl, uri)
url := fmt.Sprintf("%s%s", GitHubApiUrl, uri)
request, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
@ -89,7 +80,7 @@ func httpGet(gh *GitHub, uri string, extraHeaders map[string]string) (*http.Resp
}
func httpPost(gh *GitHub, uri string, extraHeaders map[string]string, content *bytes.Buffer) (*http.Response, error) {
url := fmt.Sprintf("%s%s", GitHubUrl, uri)
url := fmt.Sprintf("%s%s", GitHubApiUrl, uri)
request, err := http.NewRequest("POST", url, content)
if err != nil {
return nil, err