build / windows (push) Skipped
build / other_os (push) Skipped
build / mac_amd64 (push) Skipped
build / mac_arm64 (push) Skipped
build / linux (push) Skipped
build / go1.26 (push) Skipped
build / linux_386 (push) Skipped
build / lint (push) Skipped
build / android-all (push) Skipped
Build & Push Docker Images / Build Docker Image for linux/386 (push) Skipped
Build & Push Docker Images / Build Docker Image for linux/amd64 (push) Skipped
Build & Push Docker Images / Build Docker Image for linux/arm/v6 (push) Skipped
Build & Push Docker Images / Build Docker Image for linux/arm/v7 (push) Skipped
Build & Push Docker Images / Build Docker Image for linux/arm64 (push) Skipped
Build & Push Docker Images / Merge & Push Final Docker Image (push) Waiting for needed jobs
Replace Proton-API-Bridge and go-proton-api with a client in backend/protondrive written against Proton's current Drive API, following the behaviour of Proton's official Drive SDK: - volume-based and v2 routes only - upload block verification, signed manifests and extended attributes - current content encryption (v4 node keys, v6 PKESK, SEIPDv2) - browser (session fork) sign-in, with username and password still supported - the volume's change events, used to keep the metadata cache current and for ChangeNotify, polled at most every 30 s - an in-memory fake Proton server (protontest) so the tests run without an account Uses ProtonMail/gopenpgp/v3 and ProtonMail/go-srp directly; no cgo. Written with heavy AI assistance (Claude Code). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
140 lines
5.4 KiB
Go
140 lines
5.4 KiB
Go
package protontest
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/ProtonMail/go-srp"
|
|
"github.com/pquerna/otp/totp"
|
|
"github.com/rclone/rclone/backend/protondrive/api"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// refresh calls POST /auth/v4/refresh for uid with refreshToken.
|
|
func refresh(t *testing.T, s *Server, uid, refreshToken string) (*http.Response, []byte) {
|
|
t.Helper()
|
|
body, err := json.Marshal(&api.AuthRefreshRequest{ResponseType: "token", GrantType: "refresh_token", RefreshToken: refreshToken})
|
|
require.NoError(t, err)
|
|
req, err := http.NewRequest("POST", s.URL()+"/auth/v4/refresh", bytes.NewReader(body))
|
|
require.NoError(t, err)
|
|
req.Header.Set("x-pm-appversion", testAppVersion)
|
|
req.Header.Set("x-pm-uid", uid)
|
|
resp, err := s.Client().Do(req)
|
|
require.NoError(t, err)
|
|
defer func() { _ = resp.Body.Close() }()
|
|
out, err := io.ReadAll(resp.Body)
|
|
require.NoError(t, err)
|
|
return resp, out
|
|
}
|
|
|
|
func authed(t *testing.T, s *Server, sess Session) int {
|
|
t.Helper()
|
|
resp, _ := do(t, s, "GET", "/tests/auth", map[string]string{
|
|
"x-pm-appversion": testAppVersion,
|
|
"x-pm-uid": sess.UID,
|
|
"Authorization": "Bearer " + sess.AccessToken,
|
|
})
|
|
return resp.StatusCode
|
|
}
|
|
|
|
func TestAuth(t *testing.T) {
|
|
s := New(t)
|
|
u := s.AddUser("user", "pass")
|
|
assert.Equal(t, "pass", u.MailboxPassword)
|
|
sess := s.NewSession(u)
|
|
|
|
assert.Equal(t, http.StatusOK, authed(t, s, sess))
|
|
assert.Equal(t, http.StatusUnauthorized, authed(t, s, Session{UID: sess.UID, AccessToken: "wrong"}))
|
|
assert.Equal(t, http.StatusUnauthorized, authed(t, s, Session{UID: "unknown", AccessToken: sess.AccessToken}))
|
|
|
|
s.ExpireAccessToken(sess.UID)
|
|
assert.Equal(t, http.StatusUnauthorized, authed(t, s, sess))
|
|
|
|
resp, body := refresh(t, s, sess.UID, sess.RefreshToken)
|
|
require.Equal(t, http.StatusOK, resp.StatusCode, "body: %s", body)
|
|
var out api.AuthRefreshResponse
|
|
require.NoError(t, json.Unmarshal(body, &out))
|
|
assert.Equal(t, sess.UID, out.UID)
|
|
next := Session{UID: out.UID, AccessToken: out.AccessToken, RefreshToken: out.RefreshToken}
|
|
assert.Equal(t, http.StatusOK, authed(t, s, next))
|
|
assert.Equal(t, http.StatusUnauthorized, authed(t, s, sess), "old access token")
|
|
|
|
// Refresh tokens are single use.
|
|
resp, body = refresh(t, s, sess.UID, sess.RefreshToken)
|
|
assert.Equal(t, http.StatusUnprocessableEntity, resp.StatusCode)
|
|
assert.Equal(t, api.CodeRefreshTokenInvalid, decodeError(t, body).Code)
|
|
|
|
s.RevokeSession(sess.UID)
|
|
assert.Equal(t, http.StatusUnauthorized, authed(t, s, next))
|
|
resp, body = refresh(t, s, next.UID, next.RefreshToken)
|
|
assert.Equal(t, http.StatusUnprocessableEntity, resp.StatusCode)
|
|
assert.Equal(t, api.CodeRefreshTokenInvalid, decodeError(t, body).Code)
|
|
}
|
|
|
|
// postJSON sends in as JSON to path with the given extra headers and
|
|
// decodes the response into out, returning the HTTP status.
|
|
func postJSON(t *testing.T, s *Server, path string, header map[string]string, in, out any) int {
|
|
t.Helper()
|
|
body, err := json.Marshal(in)
|
|
require.NoError(t, err)
|
|
req, err := http.NewRequest("POST", s.URL()+path, bytes.NewReader(body))
|
|
require.NoError(t, err)
|
|
req.Header.Set("x-pm-appversion", testAppVersion)
|
|
for k, v := range header {
|
|
req.Header.Set(k, v)
|
|
}
|
|
resp, err := s.Client().Do(req)
|
|
require.NoError(t, err)
|
|
defer func() { _ = resp.Body.Close() }()
|
|
require.NoError(t, json.NewDecoder(resp.Body).Decode(out))
|
|
return resp.StatusCode
|
|
}
|
|
|
|
func TestLoginTOTPLocksSession(t *testing.T) {
|
|
s := New(t)
|
|
u := s.AddUser("user", "pass")
|
|
secret := u.EnableTOTP()
|
|
|
|
var info api.AuthInfoResponse
|
|
require.Equal(t, http.StatusOK, postJSON(t, s, "/core/v4/auth/info", nil, &api.AuthInfoRequest{Intent: "Proton", Username: "user"}, &info))
|
|
auth, err := srp.NewAuth(info.Version, "user", []byte("pass"), info.Salt, info.Modulus, info.ServerEphemeral)
|
|
require.NoError(t, err)
|
|
proofs, err := auth.GenerateProofs(srpBits)
|
|
require.NoError(t, err)
|
|
var out api.AuthResponse
|
|
require.Equal(t, http.StatusOK, postJSON(t, s, "/core/v4/auth", nil, &api.AuthRequest{
|
|
Username: "user",
|
|
ClientEphemeral: base64.StdEncoding.EncodeToString(proofs.ClientEphemeral),
|
|
ClientProof: base64.StdEncoding.EncodeToString(proofs.ClientProof),
|
|
SRPSession: info.SRPSession,
|
|
}, &out))
|
|
assert.Equal(t, base64.StdEncoding.EncodeToString(proofs.ExpectedServerProof), out.ServerProof)
|
|
assert.Equal(t, api.TwoFATOTP, out.TwoFA.Enabled)
|
|
sess := Session{UID: out.UID, AccessToken: out.AccessToken, RefreshToken: out.RefreshToken}
|
|
assert.Equal(t, http.StatusForbidden, authed(t, s, sess))
|
|
|
|
headers := map[string]string{"x-pm-uid": sess.UID, "Authorization": "Bearer " + sess.AccessToken}
|
|
var res api.Error
|
|
assert.Equal(t, http.StatusUnprocessableEntity, postJSON(t, s, "/core/v4/auth/2fa", headers, &api.Auth2FARequest{TwoFactorCode: "000000x"}, &res))
|
|
assert.Equal(t, http.StatusForbidden, authed(t, s, sess))
|
|
|
|
code, err := totp.GenerateCode(secret, time.Now())
|
|
require.NoError(t, err)
|
|
assert.Equal(t, http.StatusOK, postJSON(t, s, "/core/v4/auth/2fa", headers, &api.Auth2FARequest{TwoFactorCode: code}, &res))
|
|
assert.Equal(t, http.StatusOK, authed(t, s, sess))
|
|
|
|
// SRP challenges are single use.
|
|
assert.Equal(t, http.StatusUnprocessableEntity, postJSON(t, s, "/core/v4/auth", nil, &api.AuthRequest{
|
|
Username: "user",
|
|
ClientEphemeral: base64.StdEncoding.EncodeToString(proofs.ClientEphemeral),
|
|
ClientProof: base64.StdEncoding.EncodeToString(proofs.ClientProof),
|
|
SRPSession: info.SRPSession,
|
|
}, &res))
|
|
}
|