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
auth, err := readCachedAuth()
if err == nil {
passwordMode := auth.PasswordMode
var err error
auth, err = c.AuthRefresh(auth.UID, auth.RefreshToken)
auth, err = c.AuthRefresh(auth)
if err != nil {
log.Fatal(err)
}
auth.PasswordMode = passwordMode
} else if os.IsNotExist(err) {
fmt.Printf("Username: ")
scanner.Scan()

View File

@ -5,6 +5,7 @@ import (
"errors"
"net/http"
"strings"
"time"
"golang.org/x/crypto/openpgp"
)
@ -82,7 +83,7 @@ const (
)
type Auth struct {
ExpiresIn int
ExpiresAt time.Time
Scope string
UID string `json:"Uid"`
RefreshToken string
@ -97,6 +98,7 @@ type Auth struct {
type authResp struct {
resp
Auth
ExpiresIn int
AccessToken string
TokenType string
ServerProof string
@ -106,6 +108,7 @@ type authResp struct {
func (resp *authResp) auth() *Auth {
auth := &resp.Auth
auth.ExpiresAt = time.Now().Add(time.Duration(resp.ExpiresIn) * time.Second)
auth.accessToken = resp.AccessToken
auth.privateKey = resp.PrivateKey
auth.keySalt = resp.KeySalt
@ -164,11 +167,11 @@ type authRefreshReq struct {
State string
}
func (c *Client) AuthRefresh(uid, refreshToken string) (*Auth, error) {
func (c *Client) AuthRefresh(expiredAuth *Auth) (*Auth, error) {
reqData := &authRefreshReq{
ClientID: c.ClientID,
UID: uid,
RefreshToken: refreshToken,
UID: expiredAuth.UID,
RefreshToken: expiredAuth.RefreshToken,
}
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 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) {

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(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
}