Fix Client.AuthRefresh

This commit is contained in:
emersion 2017-08-22 12:07:31 +02:00
parent 8baa4a62f6
commit a6f6f5f8e2
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
2 changed files with 76 additions and 26 deletions

View File

@ -2,6 +2,7 @@ package main
import ( import (
"bufio" "bufio"
"encoding/json"
"fmt" "fmt"
"log" "log"
"os" "os"
@ -9,6 +10,30 @@ import (
"github.com/emersion/hydroxide/protonmail" "github.com/emersion/hydroxide/protonmail"
) )
const authFile = "auth.json"
func readCachedAuth() (*protonmail.Auth, error) {
f, err := os.Open(authFile)
if err != nil {
return nil, err
}
defer f.Close()
auth := new(protonmail.Auth)
err = json.NewDecoder(f).Decode(auth)
return auth, err
}
func saveAuth(auth *protonmail.Auth) error {
f, err := os.Create(authFile)
if err != nil {
return err
}
defer f.Close()
return json.NewEncoder(f).Encode(auth)
}
func main() { func main() {
c := &protonmail.Client{ c := &protonmail.Client{
RootURL: "https://dev.protonmail.com/api", RootURL: "https://dev.protonmail.com/api",
@ -19,13 +44,26 @@ func main() {
scanner := bufio.NewScanner(os.Stdin) scanner := bufio.NewScanner(os.Stdin)
var password string
auth, err := readCachedAuth()
if err == nil {
passwordMode := auth.PasswordMode
var err error
auth, err = c.AuthRefresh(auth.UID, auth.RefreshToken)
if err != nil {
log.Fatal(err)
}
auth.PasswordMode = passwordMode
} else if os.IsNotExist(err) {
fmt.Printf("Username: ") fmt.Printf("Username: ")
scanner.Scan() scanner.Scan()
username := scanner.Text() username := scanner.Text()
fmt.Printf("Password: ") fmt.Printf("Password: ")
scanner.Scan() scanner.Scan()
password := scanner.Text() password = scanner.Text()
authInfo, err := c.AuthInfo(username) authInfo, err := c.AuthInfo(username)
if err != nil { if err != nil {
@ -39,21 +77,29 @@ func main() {
twoFactorCode = scanner.Text() twoFactorCode = scanner.Text()
} }
auth, err := c.Auth(username, password, twoFactorCode, authInfo) auth, err = c.Auth(username, password, twoFactorCode, authInfo)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
} else {
log.Fatal(err)
}
log.Println(auth) if err := saveAuth(auth); err != nil {
log.Fatal(err)
}
var mailboxPassword string if auth.PasswordMode == protonmail.PasswordTwo || password == "" {
if auth.PasswordMode == protonmail.PasswordTwo { if auth.PasswordMode == protonmail.PasswordTwo {
fmt.Printf("Mailbox password: ") fmt.Printf("Mailbox password: ")
} else {
fmt.Printf("Password: ")
}
scanner.Scan() scanner.Scan()
mailboxPassword = scanner.Text() password = scanner.Text()
} }
_, err = c.Unlock(auth, mailboxPassword) _, err = c.Unlock(auth, password)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -154,15 +154,19 @@ func (c *Client) Auth(username, password, twoFactorCode string, info *AuthInfo)
type authRefreshReq struct { type authRefreshReq struct {
ClientID string ClientID string
ClientSecret string
UID string `json:"Uid"` UID string `json:"Uid"`
RefreshToken string RefreshToken string
// Unused but required
ResponseType string
GrantType string
RedirectURI string
State string
} }
func (c *Client) AuthRefresh(uid, refreshToken string) (*Auth, error) { func (c *Client) AuthRefresh(uid, refreshToken string) (*Auth, error) {
reqData := &authRefreshReq{ reqData := &authRefreshReq{
ClientID: c.ClientID, ClientID: c.ClientID,
ClientSecret: c.ClientSecret,
UID: uid, UID: uid,
RefreshToken: refreshToken, RefreshToken: refreshToken,
} }