Change Client.AuthRefresh to take an Auth

This commit is contained in:
emersion 2017-08-22 12:23:58 +02:00
parent a6f6f5f8e2
commit 4785888590
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
3 changed files with 17 additions and 10 deletions

View File

@ -47,15 +47,11 @@ func main() {
var password string var password string
auth, err := readCachedAuth() auth, err := readCachedAuth()
if err == nil { if err == nil {
passwordMode := auth.PasswordMode
var err error var err error
auth, err = c.AuthRefresh(auth.UID, auth.RefreshToken) auth, err = c.AuthRefresh(auth)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
auth.PasswordMode = passwordMode
} else if os.IsNotExist(err) { } else if os.IsNotExist(err) {
fmt.Printf("Username: ") fmt.Printf("Username: ")
scanner.Scan() scanner.Scan()

View File

@ -5,6 +5,7 @@ import (
"errors" "errors"
"net/http" "net/http"
"strings" "strings"
"time"
"golang.org/x/crypto/openpgp" "golang.org/x/crypto/openpgp"
) )
@ -82,7 +83,7 @@ const (
) )
type Auth struct { type Auth struct {
ExpiresIn int ExpiresAt time.Time
Scope string Scope string
UID string `json:"Uid"` UID string `json:"Uid"`
RefreshToken string RefreshToken string
@ -97,6 +98,7 @@ type Auth struct {
type authResp struct { type authResp struct {
resp resp
Auth Auth
ExpiresIn int
AccessToken string AccessToken string
TokenType string TokenType string
ServerProof string ServerProof string
@ -106,6 +108,7 @@ type authResp struct {
func (resp *authResp) auth() *Auth { func (resp *authResp) auth() *Auth {
auth := &resp.Auth auth := &resp.Auth
auth.ExpiresAt = time.Now().Add(time.Duration(resp.ExpiresIn) * time.Second)
auth.accessToken = resp.AccessToken auth.accessToken = resp.AccessToken
auth.privateKey = resp.PrivateKey auth.privateKey = resp.PrivateKey
auth.keySalt = resp.KeySalt auth.keySalt = resp.KeySalt
@ -164,11 +167,11 @@ type authRefreshReq struct {
State string State string
} }
func (c *Client) AuthRefresh(uid, refreshToken string) (*Auth, error) { func (c *Client) AuthRefresh(expiredAuth *Auth) (*Auth, error) {
reqData := &authRefreshReq{ reqData := &authRefreshReq{
ClientID: c.ClientID, ClientID: c.ClientID,
UID: uid, UID: expiredAuth.UID,
RefreshToken: refreshToken, RefreshToken: expiredAuth.RefreshToken,
} }
req, err := c.newJSONRequest(http.MethodPost, "/auth/refresh", reqData) req, err := c.newJSONRequest(http.MethodPost, "/auth/refresh", reqData)
@ -181,7 +184,10 @@ func (c *Client) AuthRefresh(uid, refreshToken string) (*Auth, error) {
return nil, err return nil, err
} }
return respData.auth(), nil auth := respData.auth()
//auth.EventID = expiredAuth.EventID
auth.PasswordMode = expiredAuth.PasswordMode
return auth, nil
} }
func (c *Client) Unlock(auth *Auth, passphrase string) (openpgp.EntityList, error) { func (c *Client) Unlock(auth *Auth, passphrase string) (openpgp.EntityList, error) {

View File

@ -63,6 +63,11 @@ func (c *Client) newRequest(method, path string, body io.Reader) (*http.Request,
req.Header.Set("X-Pm-Appversion", c.AppVersion) req.Header.Set("X-Pm-Appversion", c.AppVersion)
req.Header.Set(headerAPIVersion, strconv.Itoa(Version)) req.Header.Set(headerAPIVersion, strconv.Itoa(Version))
if c.uid != "" && c.accessToken != "" {
req.Header.Set("X-Pm-Uid", c.uid)
req.Header.Set("Authorization", "Bearer " + c.accessToken)
}
return req, nil return req, nil
} }