[**Documentation**](../../../README)

***

[Documentation](../../../README) / [@zerotal/auth](../README) / AppleDriver

# Class: AppleDriver

Defined in: [auth/src/social/drivers/AppleDriver.ts:31](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/auth/src/social/drivers/AppleDriver.ts#L31)

Abstract base for every social-login provider driver.

A driver turns a provider's OAuth2 authorization-code flow into a normalized
[SocialUser](../interfaces/SocialUser). The two calls an app makes are [redirect](OAuth2Driver#redirect) (send the
user to the provider) and [user](OAuth2Driver#user) (handle the callback); everything in
between — CSRF `state`, PKCE, token exchange, profile fetch — is handled here.

## Remarks

**PKCE (RFC 7636, S256) is on by default** ([usesPKCE](OAuth2Driver#usespkce)). [redirect](OAuth2Driver#redirect)
generates a `code_verifier`, stores it in the session, and sends only its S256
`code_challenge` to the provider; [user](OAuth2Driver#user) replays the verifier during token
exchange. A stolen authorization code is therefore useless without the
session-bound verifier. Providers that don't support PKCE ignore the extra
params.

**CSRF `state`** is a random UUID written to the session by [redirect](OAuth2Driver#redirect) and
verified (constant-time) against the callback's `state` by [user](OAuth2Driver#user). Both the
state and the PKCE verifier are single-use: [user](OAuth2Driver#user) consumes and forgets
them from the session so they can never be replayed on a later callback.

**`redirect_uri` is fixed** from `config.redirectUrl` — it is never taken from
the request, so it can't be tampered with.

Both [redirect](OAuth2Driver#redirect) and [user](OAuth2Driver#user) read the current `HttpContext` from
async-local storage; nothing needs to be passed explicitly. For flows with no
request (mobile/SPA), use [stateless](OAuth2Driver#stateless) + `user(rawCode)` or
[userFromToken](OAuth2Driver#userfromtoken).

Concrete drivers implement the provider endpoints and profile mapping:
[authUrl](OAuth2Driver#authurl), [tokenUrl](OAuth2Driver#tokenurl), [userUrl](OAuth2Driver#userurl), [normalise](OAuth2Driver#normalise),
[defaultScopes](OAuth2Driver#defaultscopes), with optional hooks [extraAuthParams](OAuth2Driver#extraauthparams),
[afterNormalise](OAuth2Driver#afternormalise), [\_doUser](OAuth2Driver#_douser), [\_extractCodeAndState](OAuth2Driver#_extractcodeandstate).

## Example

```ts
import { Social } from "@zerotal/auth";

// Step 1 — send the user to GitHub (stores state + PKCE verifier in session):
async redirect() {
  return Social.driver("github").redirect();
}

// Step 2 — handle the callback (verifies state, exchanges the code):
async callback() {
  const socialUser = await Social.driver("github").user();
  // socialUser.id, .name, .email, .avatar, .token …
  // find-or-create your app user, then log them in.
}
```

## Extends

- [`OAuth2Driver`](OAuth2Driver)

## Constructors

### Constructor

> **new AppleDriver**(`config`): `AppleDriver`

Defined in: [auth/src/social/drivers/AppleDriver.ts:34](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/auth/src/social/drivers/AppleDriver.ts#L34)

#### Parameters

##### config

[`AppleOAuth2Config`](../interfaces/AppleOAuth2Config)

#### Returns

`AppleDriver`

#### Overrides

[`OAuth2Driver`](OAuth2Driver).[`constructor`](OAuth2Driver#constructor)

## Callback

### userFromToken()

> **userFromToken**(`token`): `Promise`\<[`SocialUser`](../interfaces/SocialUser)\>

Defined in: [auth/src/social/drivers/OAuth2Driver.ts:186](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/auth/src/social/drivers/OAuth2Driver.ts#L186)

Retrieve a user's profile from an access token you already hold — e.g. a
native/mobile app that obtained the token via its own SDK. Skips the
code-exchange step entirely; the returned user carries no refresh token or
expiry (those come from exchange).

```ts
const socialUser = await Social.driver('github').userFromToken(accessToken);
```

#### Parameters

##### token

`string`

An access token already obtained out-of-band (e.g. a mobile SDK).

#### Returns

`Promise`\<[`SocialUser`](../interfaces/SocialUser)\>

The normalized profile for that token.

#### Throws

If the provider's user-info request fails.

#### Inherited from

[`OAuth2Driver`](OAuth2Driver).[`userFromToken`](OAuth2Driver#userfromtoken)

***

### user()

> **user**(`code?`): `Promise`\<[`SocialUser`](../interfaces/SocialUser)\>

Defined in: [auth/src/social/drivers/OAuth2Driver.ts:386](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/auth/src/social/drivers/OAuth2Driver.ts#L386)

Handle the OAuth2 callback, or exchange a raw code in stateless mode.

**Stateful (session-based):** call with no arguments — the driver reads the
current `HttpContext` from async-local storage, extracts `code` + `state`,
verifies state against the session, then exchanges the code for a profile:

```ts
async callback({ params }: HttpContext) {
  const socialUser = await Social.driver(params.provider).user();
}
```

**Stateless (SPA / mobile):** call with the raw code — skips session/state
verification entirely:

```ts
const socialUser = await Social.driver('github').stateless().user(rawCode);
```

Throws `Error('invalid_state')` or `Error('missing_code')` on validation
failure — catch in your controller and redirect accordingly.

#### Parameters

##### code?

`string`

Optional raw authorization code for stateless flows; omit for
  the stateful session-based callback.

#### Returns

`Promise`\<[`SocialUser`](../interfaces/SocialUser)\>

The normalized [SocialUser](../interfaces/SocialUser) for the authenticated account.

#### Throws

When the callback `state` is missing or mismatched.

#### Throws

When no authorization code is present.

#### Throws

When the provider's token exchange fails.

#### Inherited from

[`OAuth2Driver`](OAuth2Driver).[`user`](OAuth2Driver#user)

## Configuration

### stateless()

> **stateless**(): `this`

Defined in: [auth/src/social/drivers/OAuth2Driver.ts:208](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/auth/src/social/drivers/OAuth2Driver.ts#L208)

Return a shallow copy of this driver with CSRF state verification disabled.

Use this for stateless API/mobile endpoints that receive a raw `code`
directly (no session, no state param):

```ts
const socialUser = await Social.driver('google').stateless().user(rawCode);
```

A copy is returned so the registered singleton is never mutated.

#### Returns

`this`

#### Inherited from

[`OAuth2Driver`](OAuth2Driver).[`stateless`](OAuth2Driver#stateless)

***

### scopes()

> **scopes**(`scopes`): `this`

Defined in: [auth/src/social/drivers/OAuth2Driver.ts:224](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/auth/src/social/drivers/OAuth2Driver.ts#L224)

Add scopes to the authorization request, merged with the default/configured
scopes. Returns a copy so the registered singleton is never mutated.

```ts
Social.driver('github').scopes(['read:user', 'public_repo']).redirect();
```

#### Parameters

##### scopes

`string`[]

#### Returns

`this`

#### Inherited from

[`OAuth2Driver`](OAuth2Driver).[`scopes`](OAuth2Driver#scopes)

***

### setScopes()

> **setScopes**(`scopes`): `this`

Defined in: [auth/src/social/drivers/OAuth2Driver.ts:236](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/auth/src/social/drivers/OAuth2Driver.ts#L236)

Replace all scopes on the authorization request. Returns a copy so the
registered singleton is never mutated.

#### Parameters

##### scopes

`string`[]

#### Returns

`this`

#### Inherited from

[`OAuth2Driver`](OAuth2Driver).[`setScopes`](OAuth2Driver#setscopes)

***

### with()

> **with**(`params`): `this`

Defined in: [auth/src/social/drivers/OAuth2Driver.ts:259](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/auth/src/social/drivers/OAuth2Driver.ts#L259)

Append optional parameters to the authorization redirect URL. Useful for
provider-specific options such as Google's
`access_type=offline` + `prompt=consent` (required to receive a refresh
token) or `hd` for hosted-domain restriction.

Do not pass reserved keys (`client_id`, `redirect_uri`, `scope`, `state`,
`response_type`) — those are managed by the driver. Returns a copy.

```ts
Social.driver('google')
  .with({ access_type: 'offline', prompt: 'consent' })
  .redirect();
```

#### Parameters

##### params

`Record`\<`string`, `string`\>

#### Returns

`this`

#### Inherited from

[`OAuth2Driver`](OAuth2Driver).[`with`](OAuth2Driver#with)

## Other

### config

> `protected` **config**: [`AppleOAuth2Config`](../interfaces/AppleOAuth2Config)

Defined in: [auth/src/social/drivers/AppleDriver.ts:32](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/auth/src/social/drivers/AppleDriver.ts#L32)

#### Overrides

[`OAuth2Driver`](OAuth2Driver).[`config`](OAuth2Driver#config)

***

### authUrl()

> `protected` **authUrl**(): `string`

Defined in: [auth/src/social/drivers/AppleDriver.ts:38](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/auth/src/social/drivers/AppleDriver.ts#L38)

#### Returns

`string`

#### Overrides

[`OAuth2Driver`](OAuth2Driver).[`authUrl`](OAuth2Driver#authurl)

***

### tokenUrl()

> `protected` **tokenUrl**(): `string`

Defined in: [auth/src/social/drivers/AppleDriver.ts:41](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/auth/src/social/drivers/AppleDriver.ts#L41)

#### Returns

`string`

#### Overrides

[`OAuth2Driver`](OAuth2Driver).[`tokenUrl`](OAuth2Driver#tokenurl)

***

### userUrl()

> `protected` **userUrl**(): `string`

Defined in: [auth/src/social/drivers/AppleDriver.ts:45](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/auth/src/social/drivers/AppleDriver.ts#L45)

Apple returns the profile in the `id_token` JWT — no user-info endpoint.

#### Returns

`string`

#### Overrides

[`OAuth2Driver`](OAuth2Driver).[`userUrl`](OAuth2Driver#userurl)

***

### defaultScopes()

> `protected` **defaultScopes**(): `string`[]

Defined in: [auth/src/social/drivers/AppleDriver.ts:49](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/auth/src/social/drivers/AppleDriver.ts#L49)

#### Returns

`string`[]

#### Overrides

[`OAuth2Driver`](OAuth2Driver).[`defaultScopes`](OAuth2Driver#defaultscopes)

***

### scopeSeparator()

> `protected` **scopeSeparator**(): `string`

Defined in: [auth/src/social/drivers/AppleDriver.ts:52](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/auth/src/social/drivers/AppleDriver.ts#L52)

Separator used to join scope values. Default: space.

#### Returns

`string`

#### Overrides

[`OAuth2Driver`](OAuth2Driver).[`scopeSeparator`](OAuth2Driver#scopeseparator)

***

### extraAuthParams()

> `protected` **extraAuthParams**(): `Record`\<`string`, `string`\>

Defined in: [auth/src/social/drivers/AppleDriver.ts:56](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/auth/src/social/drivers/AppleDriver.ts#L56)

Extra query parameters appended to the authorization redirect URL.

#### Returns

`Record`\<`string`, `string`\>

#### Overrides

[`OAuth2Driver`](OAuth2Driver).[`extraAuthParams`](OAuth2Driver#extraauthparams)

***

### normalise()

> `protected` **normalise**(`raw`, `token`): [`SocialUser`](../interfaces/SocialUser)

Defined in: [auth/src/social/drivers/AppleDriver.ts:60](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/auth/src/social/drivers/AppleDriver.ts#L60)

#### Parameters

##### raw

`Record`\<`string`, `unknown`\>

##### token

`string`

#### Returns

[`SocialUser`](../interfaces/SocialUser)

#### Overrides

[`OAuth2Driver`](OAuth2Driver).[`normalise`](OAuth2Driver#normalise)

***

### \_extractCodeAndState()

> `protected` **\_extractCodeAndState**(`ctx`): `Promise`\<\{ `code`: `string` \| `null`; `state`: `string` \| `null`; \}\>

Defined in: [auth/src/social/drivers/AppleDriver.ts:77](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/auth/src/social/drivers/AppleDriver.ts#L77)

Override to change how `code` and `state` are extracted from the incoming
request.  Default: reads `?code=&state=` from the query string.

This is an internal hook called by `user()` — it receives the `HttpContext`
resolved from async-local storage.  Apple overrides this to read from a
POST form body instead.

#### Parameters

##### ctx

`SocialHttpContext`

#### Returns

`Promise`\<\{ `code`: `string` \| `null`; `state`: `string` \| `null`; \}\>

#### Overrides

[`OAuth2Driver`](OAuth2Driver).[`_extractCodeAndState`](OAuth2Driver#_extractcodeandstate)

***

### \_doUser()

> `protected` **\_doUser**(`code`, `codeVerifier?`): `Promise`\<[`SocialUser`](../interfaces/SocialUser)\>

Defined in: [auth/src/social/drivers/AppleDriver.ts:94](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/auth/src/social/drivers/AppleDriver.ts#L94)

Override to change the full code→SocialUser pipeline.
Apple overrides this to decode an `id_token` JWT instead of calling a
user-info endpoint.

#### Parameters

##### code

`string`

##### codeVerifier?

`string`

#### Returns

`Promise`\<[`SocialUser`](../interfaces/SocialUser)\>

#### Overrides

[`OAuth2Driver`](OAuth2Driver).[`_doUser`](OAuth2Driver#_douser)

***

### includeResponseType()

> `protected` **includeResponseType**(): `boolean`

Defined in: [auth/src/social/drivers/OAuth2Driver.ts:119](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/auth/src/social/drivers/OAuth2Driver.ts#L119)

Whether to include `response_type=code` in the authorization URL.
Most providers require it; GitHub does not accept it and returns 404.
Override to return `false` for providers that omit it.

#### Returns

`boolean`

#### Inherited from

[`OAuth2Driver`](OAuth2Driver).[`includeResponseType`](OAuth2Driver#includeresponsetype)

***

### usesPKCE()

> `protected` **usesPKCE**(): `boolean`

Defined in: [auth/src/social/drivers/OAuth2Driver.ts:130](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/auth/src/social/drivers/OAuth2Driver.ts#L130)

Whether to use PKCE (RFC 7636, S256) on the authorization-code flow.
Default `true`: a stolen authorization code cannot be exchanged without
the per-flow `code_verifier` held in the session, and providers that do
not support PKCE simply ignore the extra parameters. Override to return
`false` for a provider that rejects unknown params.

#### Returns

`boolean`

#### Inherited from

[`OAuth2Driver`](OAuth2Driver).[`usesPKCE`](OAuth2Driver#usespkce)

***

### afterNormalise()

> `protected` **afterNormalise**(`user`, `_token`): `Promise`\<[`SocialUser`](../interfaces/SocialUser)\>

Defined in: [auth/src/social/drivers/OAuth2Driver.ts:138](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/auth/src/social/drivers/OAuth2Driver.ts#L138)

Called after `normalise()` — override to enrich the user object (e.g. fetch
a secondary endpoint).  Default: returns the user unchanged.

#### Parameters

##### user

[`SocialUser`](../interfaces/SocialUser)

##### \_token

`string`

#### Returns

`Promise`\<[`SocialUser`](../interfaces/SocialUser)\>

#### Inherited from

[`OAuth2Driver`](OAuth2Driver).[`afterNormalise`](OAuth2Driver#afternormalise)

***

### \_exchangeCode()

> `protected` **\_exchangeCode**(`code`, `codeVerifier?`): `Promise`\<[`TokenBundle`](../interfaces/TokenBundle)\>

Defined in: [auth/src/social/drivers/OAuth2Driver.ts:434](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/auth/src/social/drivers/OAuth2Driver.ts#L434)

#### Parameters

##### code

`string`

##### codeVerifier?

`string`

#### Returns

`Promise`\<[`TokenBundle`](../interfaces/TokenBundle)\>

#### Inherited from

[`OAuth2Driver`](OAuth2Driver).[`_exchangeCode`](OAuth2Driver#_exchangecode)

***

### \_fetchRaw()

> `protected` **\_fetchRaw**(`token`): `Promise`\<`Record`\<`string`, `unknown`\>\>

Defined in: [auth/src/social/drivers/OAuth2Driver.ts:475](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/auth/src/social/drivers/OAuth2Driver.ts#L475)

#### Parameters

##### token

`string`

#### Returns

`Promise`\<`Record`\<`string`, `unknown`\>\>

#### Inherited from

[`OAuth2Driver`](OAuth2Driver).[`_fetchRaw`](OAuth2Driver#_fetchraw)

## Redirect

### redirectUrl()

> **redirectUrl**(`state`, `codeVerifier?`): `string`

Defined in: [auth/src/social/drivers/OAuth2Driver.ts:293](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/auth/src/social/drivers/OAuth2Driver.ts#L293)

Build the full authorization URL (for low-level use or testing).
Prefer `redirect()` in controllers.

When a PKCE `codeVerifier` is supplied, its S256 challenge is sent as
`code_challenge` + `code_challenge_method` (RFC 7636 §4.3).

#### Parameters

##### state

`string`

The CSRF state token to embed in the URL.

##### codeVerifier?

`string`

Optional PKCE verifier; when present its S256 challenge is sent.

#### Returns

`string`

The fully-built authorization URL.

#### Inherited from

[`OAuth2Driver`](OAuth2Driver).[`redirectUrl`](OAuth2Driver#redirecturl)

***

### redirect()

> **redirect**(): `void`

Defined in: [auth/src/social/drivers/OAuth2Driver.ts:336](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/auth/src/social/drivers/OAuth2Driver.ts#L336)

Generate a CSRF state token, store it in the session, and redirect the
user to the provider's authorization page.

The current HTTP context is read automatically from async-local storage —
no need to pass it explicitly:

```ts
async redirect({ params }: HttpContext) {
  return Social.driver(params.provider).redirect();
}
```

#### Returns

`void`

#### Throws

If called outside an HTTP request; use
  `.stateless().user(code)` for request-less flows.

#### Inherited from

[`OAuth2Driver`](OAuth2Driver).[`redirect`](OAuth2Driver#redirect)
