| 1 |
package proton |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
"encoding/base64" |
| 7 |
"errors" |
| 8 |
|
| 9 |
"github.com/ProtonMail/go-srp" |
| 10 |
"github.com/ProtonMail/gopenpgp/v2/crypto" |
| 11 |
"github.com/go-resty/resty/v2" |
| 12 |
) |
| 13 |
|
| 14 |
var ErrInvalidProof = errors.New("unexpected server proof") |
| 15 |
|
| 16 |
func (m *Manager) NewClient(uid, acc, ref string) *Client { |
| 17 |
return newClient(m, uid).withAuth(acc, ref) |
| 18 |
} |
| 19 |
|
| 20 |
func (m *Manager) NewClientWithRefresh(ctx context.Context, uid, ref string) (*Client, Auth, error) { |
| 21 |
c := newClient(m, uid) |
| 22 |
|
| 23 |
auth, err := m.authRefresh(ctx, uid, ref) |
| 24 |
if err != nil { |
| 25 |
return nil, Auth{}, err |
| 26 |
} |
| 27 |
|
| 28 |
return c.withAuth(auth.AccessToken, auth.RefreshToken), auth, nil |
| 29 |
} |
| 30 |
|
| 31 |
func (m *Manager) NewClientWithLogin(ctx context.Context, username string, password []byte) (*Client, Auth, error) { |
| 32 |
info, err := m.AuthInfo(ctx, AuthInfoReq{Username: username}) |
| 33 |
if err != nil { |
| 34 |
return nil, Auth{}, err |
| 35 |
} |
| 36 |
|
| 37 |
srpAuth, err := srp.NewAuth(info.Version, username, password, info.Salt, info.Modulus, info.ServerEphemeral) |
| 38 |
if err != nil { |
| 39 |
return nil, Auth{}, err |
| 40 |
} |
| 41 |
|
| 42 |
proofs, err := srpAuth.GenerateProofs(2048) |
| 43 |
if err != nil { |
| 44 |
return nil, Auth{}, err |
| 45 |
} |
| 46 |
|
| 47 |
auth, err := m.auth(ctx, AuthReq{ |
| 48 |
Username: username, |
| 49 |
ClientProof: base64.StdEncoding.EncodeToString(proofs.ClientProof), |
| 50 |
ClientEphemeral: base64.StdEncoding.EncodeToString(proofs.ClientEphemeral), |
| 51 |
SRPSession: info.SRPSession, |
| 52 |
}) |
| 53 |
if err != nil { |
| 54 |
return nil, Auth{}, err |
| 55 |
} |
| 56 |
|
| 57 |
serverProof, err := base64.StdEncoding.DecodeString(auth.ServerProof) |
| 58 |
if err != nil { |
| 59 |
return nil, Auth{}, err |
| 60 |
} |
| 61 |
|
| 62 |
if m.verifyProofs { |
| 63 |
if !bytes.Equal(serverProof, proofs.ExpectedServerProof) { |
| 64 |
return nil, Auth{}, ErrInvalidProof |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
return newClient(m, auth.UID).withAuth(auth.AccessToken, auth.RefreshToken), auth, nil |
| 69 |
} |
| 70 |
|
| 71 |
func (m *Manager) AuthInfo(ctx context.Context, req AuthInfoReq) (AuthInfo, error) { |
| 72 |
var res struct { |
| 73 |
AuthInfo |
| 74 |
} |
| 75 |
|
| 76 |
if _, err := m.r(ctx).SetBody(req).SetResult(&res).Post("/auth/v4/info"); err != nil { |
| 77 |
return AuthInfo{}, err |
| 78 |
} |
| 79 |
|
| 80 |
return res.AuthInfo, nil |
| 81 |
} |
| 82 |
|
| 83 |
func (m *Manager) AuthModulus(ctx context.Context) (AuthModulus, error) { |
| 84 |
var res AuthModulus |
| 85 |
|
| 86 |
if _, err := m.r(ctx).SetResult(&res).Get("/auth/v4/modulus"); err != nil { |
| 87 |
return AuthModulus{}, err |
| 88 |
} |
| 89 |
|
| 90 |
return res, nil |
| 91 |
} |
| 92 |
|
| 93 |
func (m *Manager) auth(ctx context.Context, req AuthReq) (Auth, error) { |
| 94 |
var res struct { |
| 95 |
Auth |
| 96 |
} |
| 97 |
|
| 98 |
if _, err := m.r(ctx).SetBody(req).SetResult(&res).Post("/auth/v4"); err != nil { |
| 99 |
return Auth{}, err |
| 100 |
} |
| 101 |
|
| 102 |
return res.Auth, nil |
| 103 |
} |
| 104 |
|
| 105 |
func (m *Manager) authRefresh(ctx context.Context, uid, ref string) (Auth, error) { |
| 106 |
state, err := crypto.RandomToken(32) |
| 107 |
if err != nil { |
| 108 |
return Auth{}, err |
| 109 |
} |
| 110 |
|
| 111 |
req := AuthRefreshReq{ |
| 112 |
UID: uid, |
| 113 |
RefreshToken: ref, |
| 114 |
ResponseType: "token", |
| 115 |
GrantType: "refresh_token", |
| 116 |
RedirectURI: "https://protonmail.ch", |
| 117 |
State: string(state), |
| 118 |
} |
| 119 |
|
| 120 |
var res struct { |
| 121 |
Auth |
| 122 |
} |
| 123 |
|
| 124 |
if resp, err := m.r(ctx).SetBody(req).SetResult(&res).Post("/auth/v4/refresh"); err != nil { |
| 125 |
if resp != nil { |
| 126 |
return Auth{}, &resty.ResponseError{Response: resp, Err: err} |
| 127 |
} |
| 128 |
|
| 129 |
return Auth{}, err |
| 130 |
} |
| 131 |
|
| 132 |
return res.Auth, nil |
| 133 |
} |