зеркало из https://github.com/mislav/hub.git
Implement two factor authentication
This commit is contained in:
Родитель
ebc91a30f9
Коммит
6ff2b3b729
|
@ -9,6 +9,7 @@ import (
|
|||
"github.com/jingweno/gh/utils"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
|
@ -40,6 +41,14 @@ func (c *Config) FetchPassword() string {
|
|||
return string(pass)
|
||||
}
|
||||
|
||||
func (c *Config) FetchTwoFactorCode() string {
|
||||
var code string
|
||||
fmt.Print("two-factor authentication code: ")
|
||||
fmt.Scanln(&code)
|
||||
|
||||
return code
|
||||
}
|
||||
|
||||
func (c *Config) FetchCredentials() {
|
||||
var changed bool
|
||||
if c.User == "" {
|
||||
|
@ -49,7 +58,16 @@ func (c *Config) FetchCredentials() {
|
|||
|
||||
if c.Token == "" {
|
||||
password := c.FetchPassword()
|
||||
token, err := findOrCreateToken(c.User, password)
|
||||
token, err := findOrCreateToken(c.User, password, "")
|
||||
// TODO: return an two factor auth failure error
|
||||
if err != nil {
|
||||
re := regexp.MustCompile("two-factor authentication OTP code")
|
||||
if re.MatchString(fmt.Sprintf("%s", err)) {
|
||||
code := c.FetchTwoFactorCode()
|
||||
token, err = findOrCreateToken(c.User, password, code)
|
||||
}
|
||||
}
|
||||
|
||||
utils.Check(err)
|
||||
|
||||
c.Token = token
|
||||
|
|
|
@ -127,9 +127,15 @@ func (gh *GitHub) repo() octokat.Repo {
|
|||
return octokat.Repo{Name: project.Name, UserName: project.Owner}
|
||||
}
|
||||
|
||||
func findOrCreateToken(user, password string) (string, error) {
|
||||
func findOrCreateToken(user, password, twoFactorCode string) (string, error) {
|
||||
client := octokat.NewClient().WithLogin(user, password)
|
||||
auths, err := client.Authorizations(nil)
|
||||
options := &octokat.Options{}
|
||||
if twoFactorCode != "" {
|
||||
headers := octokat.Headers{"X-GitHub-OTP": twoFactorCode}
|
||||
options.Headers = headers
|
||||
}
|
||||
|
||||
auths, err := client.Authorizations(options)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
@ -147,9 +153,9 @@ func findOrCreateToken(user, password string) (string, error) {
|
|||
authParam.Scopes = append(authParam.Scopes, "repo")
|
||||
authParam.Note = "gh"
|
||||
authParam.NoteURL = OAuthAppURL
|
||||
options := octokat.Options{Params: authParam}
|
||||
options.Params = authParam
|
||||
|
||||
auth, err := client.CreateAuthorization(&options)
|
||||
auth, err := client.CreateAuthorization(options)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче