Skip to content

feat: sign in to the API from a mobile app with a device key - #130

Merged
xHeaven merged 14 commits into
dev-null-sh:productionfrom
simonhamp:feat/mobile-api
Oct 19, 2026
Merged

xHeaven merged 14 commits into
dev-null-sh:productionfrom
simonhamp:feat/mobile-api

Conversation

@simonhamp

@simonhamp simonhamp commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

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:

  1. The app opens GET /mobile/authorize in a secure browser sheet, with the key's fingerprint, a device_name, an S256 code_challenge, a state and a redirect_uri that must start with devnull://.
  2. You sign in with GitHub as usual if you aren't already. The page asks "Approve /dev/null on iPhone?" and shows the key's fingerprint.
  3. Approve redirects to 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 with error=access_denied.
  4. The app redeems it with 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) and X-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 the devnull-api namespace. It's the same format ssh-keygen -Y sign produces.

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-commit namespace, through the same SshSignatures checks. 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/v1

The 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_muting and achievements.

Signed only:

POST /devices redeem a sign-in code
GET /me you, with your unread notification count
GET /log/{following,latest,trending} the three logs, ranked and paginated like the web, with ?since= for new commits
GET /commits/{commit}/replies replies
POST /commits, DELETE /commits/{commit} commit, reply and delete, through the web's validation, moderation and limits
POST, DELETE /users/{username}/follow follow and unfollow
GET /notifications?type= all, none, or a comma-separated list of replies, quotes, stars, mentions, follows and moderation, grouped like the web
GET /notifications/{notification}/group one group expanded

Errors carry a code next to message: invalid_signature, device_not_approved, invalid_grant, user_blocked, user_unavailable and rate_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.

devices is the only new table. Every row goes when you revoke the device or delete your account, since it cascades on user_id. Devices are in the account export and covered by DeleteUserTest.

Column Why the feature needs it Who sees it
id names the device when you revoke it you, on Settings → Devices
user_id a signed request acts as this user nobody, it's never serialized
fingerprint finds the device for each signed request you, to tell keys apart
name the only way to tell your devices apart, e.g. "devnull on iPhone" you
approved_at shown on Devices. It resets when a key is approved again, so created_at can't stand in you
last_used_at shown on Devices so you can spot a phone you lost and revoke it. Written at most once a minute you
created_at, updated_at Laravel's timestamps, as on upstream's other tables. Nothing reads them nobody

Things to look at

  • PRODUCT.md lists "a write API or API tokens" as deliberately excluded. This is a write API without tokens, so I narrowed that line to "API tokens" and described the signed API. That's your call.
  • Upstream's API code. The three public controllers gained a #[CurrentUser] ?User and an early branch for signed requests. CommitResource uses whenCounted and whenLoaded so the signed views that don't load everything don't lazy-load.
  • Shared queries. The profile's commits, the pinned commit and the commit page's query moved into UserCommitsQuery, PinnedCommitQuery and CommitPageQuery, and take a ?User viewer, so the web and the API can't drift.
  • Threads (feat: read a thread an author writes as replies to themselves in order #449). CommitPageQuery also loads withContinuationId(), 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 send continuation_id yet, so the app can't show "Show thread" until a follow-up adds it.
  • app/Exceptions. The error codes use an ApiException there, the folder Pest's arch preset expects.
  • Guests on the authorize page land on home, now the Trending log, sign in there and come back. Sending them straight to GitHub would be a small follow-up.
  • The signed log stays signed-only, even though guests can now read Latest and Trending on the web. Opening those to unsigned requests could be a follow-up.
  • CommitRepliesQuery didn't load stars, so a reply you'd starred showed as unstarred on the web too. It now uses withStarsFor() like the log.
  • The signature covers the request URI exactly as received, so a proxy that rewrites the path or query would break every signature.

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.

@nunomaduro

Copy link
Copy Markdown
Member

fix the conflicts

@kadirtikil

Copy link
Copy Markdown

@simonhamp is there already a repo for the nativephp app? would love to collab on it

@simonhamp

Copy link
Copy Markdown
Contributor Author

@kadirtikil yep: https://github.com/nativephp/dev-null-app

@simonhamp
simonhamp marked this pull request as ready for review October 16, 2026 00:11
@simonhamp simonhamp changed the title feat: JSON API for a mobile app, signed with your GitHub SSH key feat: sign in to the API with your GitHub SSH key, for a mobile app Oct 16, 2026
@simonhamp
simonhamp force-pushed the feat/mobile-api branch 2 times, most recently from 9281801 to f94dd9b Compare October 16, 2026 11:16
@brendt
brendt requested a review from mnapoli October 16, 2026 12:13
@simonhamp simonhamp changed the title feat: sign in to the API with your GitHub SSH key, for a mobile app feat: sign in to the API from a mobile app with a device key Oct 16, 2026
@xHeaven

xHeaven commented Oct 18, 2026

Copy link
Copy Markdown
Member

@nunomaduro This PR touches database migrations, new services, authentication / permissions, and configuration. Could you take a look?

@simonhamp

Copy link
Copy Markdown
Contributor Author

Resolving conflicts now

@simonhamp

Copy link
Copy Markdown
Contributor Author

Abiding the new data guidelines

Copilot AI balanced review requested due to automatic review settings October 18, 2026 17:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread bootstrap/app.php

// 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);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread app/Http/Resources/CommitResource.php Resolved
Comment thread app/Queries/ReplyPreviewsQuery.php Resolved
Comment thread resources/js/pages/device/index.tsx Resolved
Copilot AI balanced review requested due to automatic review settings October 18, 2026 17:45

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread app/Services/SignedRequests.php Resolved
Comment thread app/Services/SshSignatures.php Resolved
Copilot AI balanced review requested due to automatic review settings October 18, 2026 18:46
simonhampel added a commit to hampel/zero-dev-null that referenced this pull request Oct 19, 2026
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]>
@simonhamp
simonhamp deleted the feat/mobile-api branch October 19, 2026 23:12
simonhamp pushed a commit to NativePHP/dev-null-app that referenced this pull request Oct 30, 2026
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants