vestibule_apple
Apple Sign In strategy for vestibule.
vestibule_apple
Apple Sign In strategy for vestibule.
Apple’s OAuth implementation has several key differences from standard providers:
- No userinfo endpoint: User info is extracted from the
id_tokenJWT in the token response rather than from a separate API call. response_mode=form_post: Apple sends the callback as a POST with form data. The vestibule core handles extracting callback parameters, so this strategy just adds the parameter to the authorization URL.- Client secret is a JWT: Apple requires the client_secret to be a signed
JWT. Build it with
build_client_secret, then provide it asconfig.client_secret_auth(jwt). - User info only on first auth: Apple only sends the full user object
(with name) on the first authorization. Subsequent authorizations only
include
subandemailin the ID token. - JWT signature verification: ID tokens are verified against Apple’s published JWKS keys using RS256.
Setup
Call vestibule_apple.initialize() once per VM at application startup to
initialize caches, then use vestibule_apple.strategy() to create the
strategy. Handle the error, or assert at the top level of your
application where failing to start is the right outcome:
let assert Ok(apple) = vestibule_apple.initialize()let strategy = vestibule_apple.strategy(apple)Types
AppleCache
Holds the JWKS cache used for Apple ID token signature verification.
Returned by initialize() and required by strategy(). Construction and
field access are intentionally opaque so the cache backing store can change
without breaking consumers.
pub type AppleCacheAppleInitializationError
Errors returned by checked Apple cache initialization.
pub type AppleInitializationError { JwksCacheInitializationFailed(jwks.JwksCacheError)}ClientSecretError
Errors returned when building an Apple client-secret JWT.
pub type ClientSecretError { InvalidTeamId InvalidClientId InvalidKeyId InvalidPrivateKey InvalidTtl}Functions
build_authorization_code_request
Build Apple’s authorization-code token request without sending it.
pub fn build_authorization_code_request( config.ClientConfig, String, option.Option(String)) -> Result(request.Request(String), error.AuthError(a))build_client_secret
Build the ES256 client-secret JWT required by Apple.
team_id and key_id must be 10-character Apple identifiers. client_id
must contain only ASCII letters, digits, dots, and hyphens. p8_pem must
contain one unencrypted PKCS#8 P-256 private key. ttl is in seconds and
must be between 1 and Apple’s limit of 15,777,000 seconds.
pub fn build_client_secret( team_id: String, client_id: String, key_id: String, p8_pem: String, ttl: Int) -> Result(String, ClientSecretError)build_refresh_token_request
Build Apple’s refresh-token request without sending it.
pub fn build_refresh_token_request( config.ClientConfig, String) -> Result(request.Request(String), error.AuthError(a))initialize
Initialize the Apple strategy’s caches.
Must be called once per VM at application startup before handling any
authentication flows. Returns the cache handle needed by strategy(),
or an error if the underlying cache tables already exist or cannot be
created.
Example
let assert Ok(apple) = vestibule_apple.initialize()let strategy = vestibule_apple.strategy(apple)pub fn initialize() -> Result(AppleCache, AppleInitializationError)initialize_named
Initialize named Apple strategy caches. Useful for tests that need isolated cache tables.
pub fn initialize_named(String) -> Result(AppleCache, AppleInitializationError)parse_authorization_code_response
Parse Apple’s authorization-code HTTP response without performing I/O.
pub fn parse_authorization_code_response(response.Response(String)) -> Result(strategy.ExchangeResult, error.AuthError(a))parse_refresh_token_response
Parse Apple’s refresh-token HTTP response without performing I/O.
pub fn parse_refresh_token_response(response.Response(String)) -> Result(credential.Credentials, error.AuthError(a))parse_token_response
Parse Apple token response JSON.
Apple’s token response includes an id_token JWT containing user claims.
This function parses the response and returns standard credentials plus
provider-specific artifacts such as the raw id_token.
pub fn parse_token_response(String) -> Result(strategy.ExchangeResult, error.AuthError(a))strategy
Create an Apple Sign In authentication strategy.
Requires the cache handle from initialize(). Apple requires
response_mode=form_post for the authorization URL, which is added
automatically. The config.client_secret_auth(jwt) value must be a signed JWT
from build_client_secret or an equivalent signer.
ID tokens are verified against Apple’s published JWKS keys with claim validation for issuer, audience, and expiration.
pub fn strategy(AppleCache) -> strategy.Strategy(a)strategy_with_sender
Create an Apple strategy with a custom HTTP sender.
pub fn strategy_with_sender( AppleCache, fn(request.Request(String)) -> Result(response.Response(String), a)) -> strategy.Strategy(b)verify_id_token
Verify and decode an Apple ID token JWT.
Fetches Apple’s JWKS public keys (cached), verifies the JWT signature
using RS256, and validates the iss, aud, and exp claims.
Returns the user’s sub (user ID) and UserInfo on success.
pub fn verify_id_token( jwt: String, keys: List(verify_key.VerifyKey), client_id: String) -> Result(#(String, user_info.UserInfo), error.AuthError(a))