Skip to content

feat: link and sign in with Apple from the API - #558

Draft
simonhamp wants to merge 4 commits into
dev-null-sh:productionfrom
simonhamp:feat/api-sign-in-with-apple
Draft

simonhamp wants to merge 4 commits into
dev-null-sh:productionfrom
simonhamp:feat/api-sign-in-with-apple

Conversation

@simonhamp

Copy link
Copy Markdown
Contributor

Adds Sign in with Apple to the signed API, as a second way into an account made with GitHub. Apple never creates an account. It follows #130, which added the signed API, and is one of a few small PRs for the endpoints the app (NativePHP/dev-null-app) uses on top of it.

The App Store asks apps that offer a third-party sign-in like GitHub to offer Sign in with Apple too, which is why the app needs it.

Endpoints

Request Body Answer
PUT /api/v1/me/apple, signed {"identity_token", "nonce", "authorization_code"}, the code optional 200 with {"data": user, "apple_linked": true}
DELETE /api/v1/me/apple, signed none 200 with {"data": user, "apple_linked": false}
POST /api/v1/devices, signed with the new key {"apple_identity_token", "nonce", "device_name"} instead of {"code", "code_verifier"} 200 with the user, like GET /api/v1/me, once the key is approved as a device

New error codes: invalid_apple_token (422), apple_linked_elsewhere (409, the Apple ID is linked to another account) and apple_not_linked (404, signing in with an Apple ID nobody linked, with a message that says to sign in with GitHub first). If Apple can't be reached for its keys, the answer is a 503. GET /api/v1/me says apple_linked. Linking and unlinking are limited to 10 a minute per user, and signing in with Apple counts against the same limits as redeeming a code.

The user can also see and unlink Apple under Settings → Devices on the web.

How it works

  • AppleIdentityTokens checks the identity token without a new package. It verifies the RS256 JWT with phpseclib against Apple's JWKS, cached for a day. It checks the issuer, that the audience is one of APPLE_CLIENT_IDS, the expiry, and that the nonce claim is the SHA-256 of the raw nonce the app sends. The token's sub goes in a new nullable, unique users.apple_id. The token itself is never stored.
  • Code redemption and Apple sign-in both approve the key through a new UpdateOrCreateDevice action.
  • Apple asks apps to revoke a user's tokens when they unlink or delete their account. When an authorization_code comes with the link, AppleTokens swaps it for a refresh token, kept encrypted in a new users.apple_refresh_token. Unlinking, linking another Apple ID, and DeleteUser queue RevokeAppleToken. Linking never fails over the code: if Apple turns it down, the Apple ID is linked without a refresh token and the reason is logged.

Config

  • APPLE_CLIENT_IDS: the bundle IDs whose tokens are accepted. Defaults to com.simonhamp.devnull, the app's bundle ID.
  • APPLE_CLIENT_ID, APPLE_TEAM_ID, APPLE_KEY_ID and APPLE_PRIVATE_KEY (the .p8 key): only needed to swap and revoke tokens. Without them linking and sign-in still work, and nothing is swapped or revoked, which is logged once.

I haven't set up the App ID with Sign in with Apple or made the .p8 key yet, so this hasn't run against Apple. Until then the app ships with its Apple button off.

Worth checking

  • Two migrations add users.apple_id and users.apple_refresh_token.
  • Apple's sub is only the same across apps from one developer team, so links only keep working while the app ships under team J68WFCX458. The README says so.
  • feat: sign in to the API from a mobile app with a device key #130 moved authorization codes into the cache during review. This branch was written before that, so CreateDevice was reworked to keep the cache version and hand the approval to UpdateOrCreateDevice.

Testing

Everything CI runs passes on this branch locally: Pint, Rector, the JS lint, PHPStan, tsc, type coverage, and all 3,055 tests at 100% coverage.

Tests sign fake identity tokens with their own RSA key, served through a faked JWKS endpoint, and fake Apple's token and revoke endpoints with a P-256 key made for each test.

Apple becomes a second way into an account made with GitHub, never a way to make one. This commit is the link. The next one signs in with it.

PUT /api/v1/me/apple takes {"identity_token", "nonce"}, signed by an approved device. AppleIdentityTokens checks the token without any new package: an RS256 JWT verified with phpseclib against Apple's JWKS from https://appleid.apple.com/auth/keys, which is kept for a day and fetched again for a key ID it hasn't seen, at most once a minute. The issuer has to be https://appleid.apple.com, the audience exactly one of config('services.apple.client_ids') (APPLE_CLIENT_IDS, com.simonhamp.devnull by default), exp in the future, iat not in the future beyond a minute of skew, and the nonce claim hash('sha256', $nonce) of the raw nonce the app sends, in lowercase hex. The token's sub goes in a new nullable, unique users.apple_id, hidden from every serialized user. The token is never stored.

It answers 200 with {"data": user, "apple_linked": true}, also when the Apple ID is already linked to the user. A token that fails any check is a 422 with invalid_apple_token. An Apple ID linked to someone else is a 409 with apple_linked_elsewhere. DELETE /api/v1/me/apple unlinks it and answers with "apple_linked": false. Both are limited to 10 a minute per user. GET /api/v1/me says apple_linked. If Apple can't be reached for its keys, the answer is a 503.

Apple's sub is per developer team, so the README notes that links only survive while the app ships under team J68WFCX458.

Settings → Devices shows "Sign in with Apple: linked" with an Unlink button, so the link can be undone from the web too.

Tests sign fake tokens with their own RSA key, published through a faked JWKS endpoint.
POST /api/v1/devices now takes {"apple_identity_token", "nonce", "device_name"} as an alternative to {"code", "code_verifier"}, signed with the new key as before. The token goes through the same checks as linking. The key is approved as a device of the user who linked that Apple ID, and the answer is the user, like GET /api/v1/me, which now includes apple_linked here too.

Apple never makes an account. An Apple ID nobody linked is a 404 with apple_not_linked and "No /dev/null account is linked to this Apple ID. Sign in with GitHub, then link Apple in settings." A bad token is a 422 with invalid_apple_token. A sign-in that fails for any reason approves nothing, so it leaves no devices row behind, and a key approved before stays as it was. A request may send a code or an Apple token, not both. Apple sign-ins count against the same limits as redeeming a code: 12 a minute per key and 60 per address.

Approving a key moves into UpdateOrCreateDevice, which code redemption now uses as well.
…eted

Apple asks apps that offer Sign in with Apple to revoke the user's tokens when they unlink it or delete their account. Revoking takes a refresh token, so linking now takes an optional authorization_code next to the identity token, the one Apple hands the app at sign-in.

With the app's Sign in with Apple key configured (APPLE_CLIENT_ID, com.simonhamp.devnull by default, APPLE_TEAM_ID, APPLE_KEY_ID and the .p8 key in APPLE_PRIVATE_KEY), AppleTokens swaps the code for a refresh token at https://appleid.apple.com/auth/token. It signs the client secret itself, an ES256 JWT for the team and the app that lasts five minutes, with phpseclib. The refresh token goes in a new users.apple_refresh_token, encrypted and hidden. It is only kept when the ID token Apple hands back with it is for the Apple ID being linked.

Unlinking from the app or the web, linking another Apple ID, and DeleteUser queue RevokeAppleToken, which calls https://appleid.apple.com/auth/revoke. The job is encrypted on the queue, since the user no longer has the token by then, and tries 4 times over about an hour if Apple can't be reached.

Linking never fails over the code. If Apple turns it down or can't be reached, the Apple ID is linked without a refresh token and the reason is logged, so a retried link with a spent code still answers 200. Linking the same Apple ID again keeps the token it has unless Apple hands over a new one, and never revokes the old one, since that would revoke the new one too. Without the key, nothing is swapped or revoked, and that is logged once.

The new keys are in .env.example and config/services.php, all empty but the client ID. Tests sign with a P-256 key made for each test and fake Apple's endpoints.
Copilot AI balanced review requested due to automatic review settings October 19, 2026 06:37

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@simonhamp
simonhamp marked this pull request as ready for review October 19, 2026 08:47
@Gnative

Gnative commented Oct 19, 2026

Copy link
Copy Markdown

Hmm I understand Apples requirements auth for mobile apps - maybe we can sign in with apple but can't post without a github account ? readonly state.

@simonhamp

Copy link
Copy Markdown
Contributor Author

@Gnative thats exactly how this should be working

Copilot AI balanced review requested due to automatic review settings October 19, 2026 14:57

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@simonhamp
simonhamp marked this pull request as draft October 19, 2026 15:09
@mnapoli

mnapoli commented Oct 19, 2026

Copy link
Copy Markdown
Member

Adds Sign in with Apple to the signed API, as a second way into an account made with GitHub. Apple never creates an account.

@simonhamp to clarify, this means if someone signs in with Apple but has no GitHub account tied to the same email address, what happens?

(the premise here is that all user accounts = GitHub account, just want to make sure this is aligned)

@simonhamp

Copy link
Copy Markdown
Contributor Author

@mnapoli

(the premise here is that all user accounts = GitHub account, just want to make sure this is aligned)

Yes and this PR is trying to maintain that while meeting Apple's requirement to have "Sign in with Apple" when using any other third-party 'social auth' service.

if someone signs in with Apple but has no GitHub account tied to the same email address, what happens?

Hypothetically (more setup and testing required, hence why this PR is still draft), a new user who chooses "Sign in with Apple" would either:

  1. Immediately be presented with a /dev/null screen asking them to connect their GitHub account - it would be a weird double kind of sign-in; or,
  2. Have read-only access and be gently prompted to connect their GitHub account - doesn't solve (1) and is kinda half-hearted.

Honestly, I'm tempted to change the "Sign in with GitHub" to simply "Login" (and add "Sign up") as it all goes through dev-null.sh now - we don't take the user to github.com from the app during login at all - and then submit this for review and see what happens.

This may avoid Apple's lidless gaze on this area, pass review and remove this whole concern.

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