|
| 1 | +# Authentication Testing Setup |
| 2 | + |
| 3 | +This repo can now support a full local auth test loop: |
| 4 | + |
| 5 | +- email/password login |
| 6 | +- refresh and logout flows |
| 7 | +- verification and password-reset emails |
| 8 | +- PowerSync token minting |
| 9 | +- Google OAuth browser flow after you add Google credentials |
| 10 | + |
| 11 | +## Local Setup |
| 12 | + |
| 13 | +1. Start local dependencies: |
| 14 | + |
| 15 | +```bash |
| 16 | +docker compose up -d database mailpit |
| 17 | +``` |
| 18 | + |
| 19 | +1. Generate local PowerSync signing keys: |
| 20 | + |
| 21 | +```bash |
| 22 | +./scripts/generate_dev_powersync_keys.sh |
| 23 | +``` |
| 24 | + |
| 25 | +1. Make sure `.env` contains the auth values shown in `.env.example`. |
| 26 | + |
| 27 | +Recommended local values when the API runs on the host with `uvicorn`: |
| 28 | + |
| 29 | +```dotenv |
| 30 | +PUBLIC_BASE_URL=http://localhost:8080 |
| 31 | +EMAIL_DELIVERY_ENABLED=true |
| 32 | +SMTP_HOST=127.0.0.1 |
| 33 | +SMTP_PORT=1025 |
| 34 | +SMTP_USE_TLS=false |
| 35 | +SMTP_USE_SSL=false |
| 36 | +SMTP_FROM_EMAIL=noreply@papyrus.local |
| 37 | +SMTP_FROM_NAME=Papyrus |
| 38 | +POWERSYNC_JWT_PRIVATE_KEY_FILE=.local/powersync/private.pem |
| 39 | +POWERSYNC_JWT_PUBLIC_KEY_FILE=.local/powersync/public.pem |
| 40 | +POWERSYNC_JWT_KEY_ID=papyrus-powersync-dev |
| 41 | +POWERSYNC_JWT_AUDIENCE=powersync-dev |
| 42 | +``` |
| 43 | + |
| 44 | +If the API runs inside Docker instead of on the host, set `SMTP_HOST=mailpit` and `POSTGRES_HOST=database`. |
| 45 | + |
| 46 | +1. Apply migrations and run the API: |
| 47 | + |
| 48 | +```bash |
| 49 | +uv run alembic upgrade head |
| 50 | +uv run uvicorn papyrus.main:app --reload |
| 51 | +``` |
| 52 | + |
| 53 | +## Useful Local Pages |
| 54 | + |
| 55 | +- API index: `http://localhost:8080/` |
| 56 | +- Swagger UI: `http://localhost:8080/docs` |
| 57 | +- ReDoc: `http://localhost:8080/redoc` |
| 58 | +- Dev auth sandbox: `http://localhost:8080/__dev/auth-sandbox` |
| 59 | +- Mailpit inbox UI: `http://localhost:8025` |
| 60 | + |
| 61 | +## SMTP End-to-End Testing |
| 62 | + |
| 63 | +Mailpit is a local SMTP sink. No real mailbox is needed. |
| 64 | + |
| 65 | +- Trigger `forgot password` or `resend verification` from the sandbox or API. |
| 66 | +- Open `http://localhost:8025` to inspect the delivered messages. |
| 67 | +- For the opt-in smoke test, use any recipient address: |
| 68 | + |
| 69 | +```bash |
| 70 | +RUN_SMTP_SMOKE_TEST=true \ |
| 71 | +AUTH_SMOKE_EMAIL_RECIPIENT=smoke@papyrus.local \ |
| 72 | +uv run pytest tests/integration/test_auth_smoke.py -m auth_smoke -q |
| 73 | +``` |
| 74 | + |
| 75 | +## Google OAuth Setup |
| 76 | + |
| 77 | +Papyrus uses a server-owned browser OAuth flow. The Flutter app opens: |
| 78 | + |
| 79 | +- `GET /v1/auth/oauth/google/start` |
| 80 | + |
| 81 | +Google redirects back to the server callback: |
| 82 | + |
| 83 | +- `GET /v1/auth/oauth/google/callback` |
| 84 | + |
| 85 | +The server then redirects to your app callback URI with a one-time Papyrus exchange code. |
| 86 | + |
| 87 | +### What To Create In Google Cloud |
| 88 | + |
| 89 | +Create an OAuth client with: |
| 90 | + |
| 91 | +- Client type: `Web application` |
| 92 | +- Redirect URI: |
| 93 | + - local desktop testing: `http://localhost:8080/v1/auth/oauth/google/callback` |
| 94 | + - public tunnel/device testing: `https://<your-public-host>/v1/auth/oauth/google/callback` |
| 95 | + |
| 96 | +Authorized JavaScript origins are not required for this backend-owned redirect flow. If the Google UI requires one for localhost testing, use: |
| 97 | + |
| 98 | +- `http://localhost:8080` |
| 99 | + |
| 100 | +Set the resulting values in `.env`: |
| 101 | + |
| 102 | +```dotenv |
| 103 | +GOOGLE_OAUTH_CLIENT_ID=... |
| 104 | +GOOGLE_OAUTH_CLIENT_SECRET=... |
| 105 | +PUBLIC_BASE_URL=http://localhost:8080 |
| 106 | +``` |
| 107 | + |
| 108 | +For mobile-device testing or any device where the browser cannot reach your workstation as `localhost`, use a public HTTPS base URL and set `PUBLIC_BASE_URL` to that exact value. |
| 109 | + |
| 110 | +### Localhost vs Public Testing |
| 111 | + |
| 112 | +- Desktop same-machine testing: |
| 113 | + - `PUBLIC_BASE_URL=http://localhost:8080` |
| 114 | + - Google redirect URI: `http://localhost:8080/v1/auth/oauth/google/callback` |
| 115 | +- Mobile emulator, physical phone, or shared test device: |
| 116 | + - expose the backend through a public HTTPS URL |
| 117 | + - set `PUBLIC_BASE_URL=https://<your-public-host>` |
| 118 | + - Google redirect URI: `https://<your-public-host>/v1/auth/oauth/google/callback` |
| 119 | + |
| 120 | +The callback URI must match Google exactly, including scheme, host, port, path, and trailing slash behavior. |
| 121 | + |
| 122 | +### OAuth Consent Screen Notes |
| 123 | + |
| 124 | +For development: |
| 125 | + |
| 126 | +- keep the app in testing mode |
| 127 | +- add your Google account under test users if Google requires it |
| 128 | + |
| 129 | +Papyrus only requests basic identity scopes: |
| 130 | + |
| 131 | +- `openid` |
| 132 | +- `email` |
| 133 | +- `profile` |
| 134 | + |
| 135 | +## Google Smoke Test |
| 136 | + |
| 137 | +The Google smoke test now validates a live Papyrus session produced by a successful Google browser login. |
| 138 | + |
| 139 | +Recommended workflow: |
| 140 | + |
| 141 | +1. Complete a real Google login in the auth sandbox. |
| 142 | +2. Copy the access token or refresh token from the sandbox. |
| 143 | +3. Run the smoke test against the running server. |
| 144 | + |
| 145 | +Access-token-only mode: |
| 146 | + |
| 147 | +```bash |
| 148 | +RUN_GOOGLE_SMOKE_TEST=true \ |
| 149 | +AUTH_SMOKE_SERVER_BASE_URL=http://localhost:8080 \ |
| 150 | +AUTH_SMOKE_GOOGLE_ACCESS_TOKEN=<access-token-from-sandbox> \ |
| 151 | +uv run pytest tests/integration/test_auth_smoke.py -m auth_smoke -q |
| 152 | +``` |
| 153 | + |
| 154 | +Refresh-token mode is more durable and also validates token rotation: |
| 155 | + |
| 156 | +```bash |
| 157 | +RUN_GOOGLE_SMOKE_TEST=true \ |
| 158 | +AUTH_SMOKE_SERVER_BASE_URL=http://localhost:8080 \ |
| 159 | +AUTH_SMOKE_GOOGLE_REFRESH_TOKEN=<refresh-token-from-sandbox> \ |
| 160 | +uv run pytest tests/integration/test_auth_smoke.py -m auth_smoke -q |
| 161 | +``` |
| 162 | + |
| 163 | +If both are provided, the test tries the access token first and falls back to refresh if the access token is expired. |
| 164 | + |
| 165 | +Notes: |
| 166 | + |
| 167 | +- refresh-token mode rotates the provided refresh token, so the old token will stop working after the test |
| 168 | +- on success, the test prints `AUTH_SMOKE_ROTATED_REFRESH_TOKEN=...`; use that value for the next manual run |
| 169 | +- if you only provide an access token, the test is non-destructive but depends on that token still being unexpired |
| 170 | +- `AUTH_SMOKE_SERVER_BASE_URL` defaults to `PUBLIC_BASE_URL` if omitted |
0 commit comments