Repository navigation
feat: sign in to the API from a mobile app with a device key - #130
Conversation
|
fix the conflicts |
a78b94c to
88e37c8
Compare
|
@simonhamp is there already a repo for the nativephp app? would love to collab on it |
9281801 to
f94dd9b
Compare
f94dd9b to
3baea4c
Compare
3baea4c to
32b16a6
Compare
|
@nunomaduro This PR touches database migrations, new services, authentication / permissions, and configuration. Could you take a look? |
|
Resolving conflicts now |
|
Abiding the new data guidelines |
32b16a6 to
96e25bc
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Authentication failures bypass throttling, and unresolved image-access, reply-preview, and revocation-recovery issues affect clients.
5 open issues
What changed in this PR
Adds device-key authentication and signed read/write access to the existing /api/v1, while preserving unsigned public reads.
Changes:
- Adds browser approval with PKCE, signed requests, and device management.
- Reuses web queries and permissions for authenticated API responses and actions.
- Adds documentation and tests for authentication, visibility, limits, and device lifecycle.
| File | Description |
|---|---|
| tests/Unit/Services/SshSignaturesTest.php | Tests shared signature verification. |
| tests/Unit/Services/SignedRequestsTest.php | Tests request signing and replay rejection. |
| tests/Unit/Services/MentionsTest.php | Tests mention positions. |
| tests/Unit/Queries/UserCommitsQueryTest.php | Tests shared profile queries. |
| tests/Unit/Queries/ReplyPreviewsQueryTest.php | Tests preview loading. |
| tests/Unit/Queries/PinnedCommitQueryTest.php | Tests pinned-commit visibility. |
| tests/Unit/Queries/CommitPageQueryTest.php | Tests commit-page data. |
| tests/Unit/Providers/AppServiceProviderTest.php | Tests new rate limits. |
| tests/Unit/Models/UserTest.php | Tests device relationship. |
| tests/Unit/Models/DeviceTest.php | Tests device serialization and deletion. |
| tests/Unit/Dto/AuthorizationCodeTest.php | Tests code hashing and PKCE. |
| tests/Unit/Actions/UpdateDeviceLastUsedAtTest.php | Tests usage timestamp updates. |
| tests/Unit/Actions/DeleteUserTest.php | Tests device cleanup. |
| tests/Unit/Actions/DeleteDeviceTest.php | Tests revocation. |
| tests/Unit/Actions/CreateDeviceTest.php | Tests device approval. |
| tests/Unit/Actions/CreateAuthorizationCodeTest.php | Tests temporary authorization codes. |
| tests/Pest.php | Adds signed-request helpers. |
| tests/Feature/Controllers/MobileAuthorizationControllerTest.php | Tests browser approval flow. |
| tests/Feature/Controllers/ExportControllerTest.php | Tests device exports. |
| tests/Feature/Controllers/DeviceControllerTest.php | Tests device settings endpoints. |
| tests/Feature/Controllers/Api/UserFollowControllerTest.php | Tests signed follow actions. |
| tests/Feature/Controllers/Api/UserControllerTest.php | Tests authenticated profiles. |
| tests/Feature/Controllers/Api/UserCommitControllerTest.php | Tests authenticated profile feeds. |
| tests/Feature/Controllers/Api/LogControllerTest.php | Tests signed logs. |
| tests/Feature/Controllers/Api/DeviceControllerTest.php | Tests code redemption. |
| tests/Feature/Controllers/Api/CurrentUserControllerTest.php | Tests current-user responses. |
| tests/Feature/Controllers/Api/CommitReplyControllerTest.php | Tests signed replies. |
| tests/Feature/ApiSignatureTest.php | Tests API authentication boundaries. |
| routes/web.php | Registers approval and device settings. |
| routes/api.php | Registers signed API endpoints. |
| resources/js/types/index.ts | Exports device types. |
| resources/js/types/device.ts | Defines device data shape. |
| resources/js/pages/mobile-authorization/show.tsx | Adds approval screen. |
| resources/js/pages/device/index.tsx | Adds device management screen. |
| resources/js/layouts/settings/layout.tsx | Adds Devices tab. |
| README.md | Documents signing and approval. |
| PRODUCT.md | Updates signed API scope. |
| KNOWLEDGE_BASE.md | Documents devices and exports. |
| database/migrations/2026_10_16_170000_create_devices_table.php | Creates device storage. |
| database/factories/DeviceFactory.php | Adds device fixtures. |
| config/mobile.php | Configures callback prefixes. |
| bootstrap/app.php | Orders authentication and renders throttling errors. |
| app/Services/SshSignatures.php | Centralizes signature checks. |
| app/Services/SignedRequests.php | Verifies device-signed requests. |
| app/Services/SignedCommits.php | Reuses shared signature checks. |
| app/Services/Mentions.php | Exposes mention positions. |
| app/Queries/UserCommitsQuery.php | Shares profile feed queries. |
| app/Queries/ReplyPreviewsQuery.php | Loads API preview models. |
| app/Queries/PinnedCommitQuery.php | Shares pinned-commit lookup. |
| app/Queries/CommitPageQuery.php | Shares commit-page loading. |
| app/Providers/AppServiceProvider.php | Defines API and approval limits. |
| app/Models/User.php | Adds device relationship. |
| app/Models/Device.php | Defines approved devices. |
| app/Http/Resources/UserResource.php | Adds authenticated profile fields. |
| app/Http/Resources/PollResource.php | Serializes poll results. |
| app/Http/Resources/NotificationResource.php | Serializes notification groups. |
| app/Http/Resources/CommitResource.php | Adds authenticated commit data. |
| app/Http/Requests/ShowMobileAuthorizationRequest.php | Validates approval parameters. |
| app/Http/Requests/ShowApiUserRequest.php | Enforces profile block rules. |
| app/Http/Requests/ShowApiCommitRequest.php | Applies viewer visibility. |
| app/Http/Requests/ListApiUserCommitsRequest.php | Enforces profile-feed block rules. |
| app/Http/Requests/ListApiCommitRepliesRequest.php | Authorizes reply access. |
| app/Http/Requests/DeleteDeviceRequest.php | Authorizes device revocation. |
| app/Http/Requests/DeleteApiUserFollowRequest.php | Defines unfollow request. |
| app/Http/Requests/DeleteApiCommitRequest.php | Restricts deletion to authors. |
| app/Http/Requests/CreateMobileAuthorizationRequest.php | Validates approval submissions. |
| app/Http/Requests/CreateApiUserFollowRequest.php | Authorizes follows. |
| app/Http/Requests/CreateApiDeviceRequest.php | Verifies code redemption. |
| app/Http/Requests/CreateApiCommitRequest.php | Validates API commits. |
| app/Http/Middleware/ThrottleCommits.php | Adds signed API throttling errors. |
| app/Http/Middleware/AuthenticateSignedRequest.php | Authenticates device requests. |
| app/Http/Controllers/UserController.php | Uses shared profile queries. |
| app/Http/Controllers/MobileAuthorizationController.php | Handles browser approval. |
| app/Http/Controllers/DeviceController.php | Lists and revokes devices. |
| app/Http/Controllers/CommitController.php | Uses shared commit-page query. |
| app/Http/Controllers/Api/UserFollowController.php | Adds follow and unfollow actions. |
| app/Http/Controllers/Api/UserController.php | Adds authenticated profiles. |
| app/Http/Controllers/Api/UserCommitController.php | Adds authenticated profile feeds. |
| app/Http/Controllers/Api/NotificationGroupController.php | Exposes notification group members. |
| app/Http/Controllers/Api/NotificationController.php | Lists grouped notifications. |
| app/Http/Controllers/Api/LogController.php | Exposes authenticated logs. |
| app/Http/Controllers/Api/DeviceController.php | Redeems approval codes. |
| app/Http/Controllers/Api/CurrentUserController.php | Returns authenticated user details. |
| app/Http/Controllers/Api/CommitReplyController.php | Lists signed-request replies. |
| app/Http/Controllers/Api/CommitController.php | Adds authenticated reads and writes. |
| app/Exceptions/ApiException.php | Standardizes API errors. |
| app/Enums/ApiErrorCode.php | Defines stable error codes. |
| app/Dto/MobileAuthorization.php | Carries approval parameters. |
| app/Dto/AuthorizationCode.php | Represents cached authorization grants. |
| app/Actions/UpdateDeviceLastUsedAt.php | Records device usage. |
| app/Actions/DeleteDevice.php | Revokes device access. |
| app/Actions/CreateExport.php | Includes approved devices. |
| app/Actions/CreateDevice.php | Consumes codes and approves devices. |
| app/Actions/CreateAuthorizationCode.php | Issues temporary approval codes. |
Review effort: Balanced
|
|
||
| // Rate limits apply to the user authenticated by their SSH signature... | ||
| $middleware->prependToPriorityList(before: ThrottleRequests::class, prepend: AuthenticateSshSignature::class); | ||
| $middleware->prependToPriorityList(before: ThrottleRequests::class, prepend: AuthenticateSignedRequest::class); |
There was a problem hiding this comment.
Good catch. The per-user limiter needs the user, so it runs after the signature, and nothing counted the requests that failed before it. AuthenticateSignedRequest now counts each bad signature or unapproved key against the address, and after 60 failures in a minute the address gets a 429 rate_limited before any more of its signatures are checked. Requests that sign in fine don't count, so phones behind one carrier IP don't throttle each other. Fixed in 7c09ad8, with tests in ApiSignatureTest.
There was a problem hiding this comment.
Following up, since this still shows as open. The limit sits in AuthenticateSignedRequest (33c55fa after the latest rebase). Before it checks a signature, it looks at the address's count of failures, and each bad signature or unapproved key adds one. At 60 in a minute the address gets a 429 rate_limited and nothing after that runs: no signature check, no nonce write, no device lookup. I didn't cap every request from an address, because signed-in requests already get 120 a minute per user and many phones share a carrier IP. Counting only failures stops a self-made key without throttling the people behind that address.
96e25bc to
60d96d7
Compare
The site's signed API (dev-null-sh/dev-null.sh#130) takes requests signed in the devnull-api namespace by a key the user approved as a device, through the OAuth authorization code flow with PKCE built for its phone app. login makes a key in devnull's config directory, apart from ~/.ssh, prints the approval link, and redeems the code pasted back from the devnull:// redirect, which nothing on this machine opens. Proven against the site by redeeming a made-up code: it answered invalid_grant, which comes only after the signature verifies. Approving a real device is left to the user. Co-Authored-By: Claude Opus 5.5 <[email protected]>
* docs(readme): drop livestream styling from the roadmap Live users' avatars, their LIVE badge and the stream sheet shipped in #127, so styling for livestreams is no longer something left to do. 🤖 Generated with Claude Code * docs(readme): catch up with what has shipped - The API: dev-null-sh/dev-null.sh#130 was merged, and the app talks to production by default, so it no longer needs an instance running that branch. - Setup: since #75 the app tracks main of nativephp/mobile and nativephp/mobile-ui instead of the combined/dev-null-app branches. The ToastKit fork it does track wasn't mentioned. - Roadmap: Remotes already has voice notes (#128) and photos (#117). 🤖 Generated with Claude Code


This lets a mobile app sign in to /dev/null and use it, on top of the public read API from #98. I'm building a NativePHP app against it (NativePHP/dev-null-app).
There are no passwords or API tokens. The app makes its own ed25519 key and signs every request with it. You approve the phone once on dev-null.sh, the same way you'd approve any "Sign in with" request, and from then on its key is your device.
Signing in
It's a normal OAuth authorization code flow with PKCE, with dev-null.sh as the provider:
GET /mobile/authorizein a secure browser sheet, with the key'sfingerprint, adevice_name, an S256code_challenge, astateand aredirect_urithat must start withdevnull://.devnull://auth/callback?code=…&state=…. The code is single use, valid for 5 minutes, kept only in the cache under its hash, and bound to the key and the challenge. Cancel redirects witherror=access_denied.POST /api/v1/devices{code, code_verifier}, signed by the same key. That approves the device.Only the app that registered the
devnull://scheme ever receives the code, and only the key that started the flow can redeem it. So approving a link someone sent you doesn't hand them anything.Devices are listed under Settings → Devices, where you can revoke them.
How a request is signed
Each request sends
X-Devnull-Timestamp,X-Devnull-Nonce(32 hex characters, single use) andX-SSH-Signature(SSHSIG, base64 on one line). The signature covers the timestamp, the nonce, the method, the path with its query string exactly as sent, and the raw body, joined by\n, in thedevnull-apinamespace. It's the same formatssh-keygen -Y signproduces.A request is signed in when its key is an approved device. The key doesn't need to be on GitHub. The terminal script and the GitHub Action keep their own path: they still sign commits with a key that's on your GitHub account, in the
devnull-commitnamespace, through the sameSshSignatureschecks. So a CI key on its own still can't use the API, and the Terminal page's promise holds.A request with any of the three headers has to verify. A bad signature is a 401, never the public view.
One
/api/v1The three public endpoints from #98 are unchanged for unsigned requests, and so are their tests.
A signed request to the same URLs gets what the web shows that user: their own pending commits, blocks and mutes applied, and extra fields such as
starred,is_saved, polls with your vote, quotes, reply previews,is_following,follows_you,is_mutingand achievements.Signed only:
POST /devicesGET /meGET /log/{following,latest,trending}?since=for new commitsGET /commits/{commit}/repliesPOST /commits,DELETE /commits/{commit}POST,DELETE /users/{username}/followGET /notifications?type=all,none, or a comma-separated list ofreplies,quotes,stars,mentions,followsandmoderation, grouped like the webGET /notifications/{notification}/groupErrors carry a
codenext tomessage:invalid_signature,device_not_approved,invalid_grant,user_blocked,user_unavailableandrate_limited. Signed requests are limited to 120 a minute per user rather than per IP.Stored data
These are the answers the data guideline asks for. Sign-in codes don't get a table: they live in the cache for 5 minutes under their hash, and a redeemed code is marked used with
Cache::add, the same way signature nonces are spent.devicesis the only new table. Every row goes when you revoke the device or delete your account, since it cascades onuser_id. Devices are in the account export and covered byDeleteUserTest.iduser_idfingerprintnameapproved_atcreated_atcan't stand inlast_used_atcreated_at,updated_atThings to look at
#[CurrentUser] ?Userand an early branch for signed requests.CommitResourceuseswhenCountedandwhenLoadedso the signed views that don't load everything don't lazy-load.UserCommitsQuery,PinnedCommitQueryandCommitPageQuery, and take a?Userviewer, so the web and the API can't drift.CommitPageQueryalso loadswithContinuationId(), so the web's commit page is unchanged and the API's commit page counts replies the same way as its logs and profiles already do, without the author's own continuation. The API doesn't sendcontinuation_idyet, so the app can't show "Show thread" until a follow-up adds it.app/Exceptions. The error codes use anApiExceptionthere, the folder Pest's arch preset expects.CommitRepliesQuerydidn't load stars, so a reply you'd starred showed as unstarred on the web too. It now useswithStarsFor()like the log.Deploy
One migration,
devices. No new packages.All 2,910 tests pass on Postgres with pgvector, with 100% line and type coverage. KNOWLEDGE_BASE.md has a short section on the signed API and devices.
Star and save, and linking Sign in with Apple to an existing account, are ready on branches stacked on this one, for follow-up PRs once this shape is agreed.