Skip to content

Commit f8ac8d0

Browse files
authored
fix: #1354 - allow customisable leeway for JWT issued time in Apple provider … (#1439)
1 parent d11aa71 commit f8ac8d0

2 files changed

Lines changed: 25 additions & 11 deletions

File tree

src/Apple/Provider.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ class Provider extends AbstractProvider
5959
*/
6060
protected function getAuthUrl($state): string
6161
{
62-
return $this->buildAuthUrlFromBase(self::URL.'/auth/authorize', $state);
62+
return $this->buildAuthUrlFromBase(self::URL . '/auth/authorize', $state);
6363
}
6464

6565
protected function getTokenUrl(): string
6666
{
67-
return self::URL.'/auth/token';
67+
return self::URL . '/auth/token';
6868
}
6969

7070
/**
@@ -82,7 +82,7 @@ protected function getCodeFields($state = null)
8282

8383
if ($this->usesState()) {
8484
$fields['state'] = $state;
85-
$fields['nonce'] = Str::uuid().'.'.$state;
85+
$fields['nonce'] = Str::uuid() . '.' . $state;
8686
}
8787

8888
return array_merge($fields, $this->parameters);
@@ -94,7 +94,7 @@ protected function getCodeFields($state = null)
9494
public function getAccessTokenResponse($code)
9595
{
9696
$response = $this->getHttpClient()->post($this->getTokenUrl(), [
97-
RequestOptions::HEADERS => ['Authorization' => 'Basic '.base64_encode($this->clientId.':'.$this->getClientSecret())],
97+
RequestOptions::HEADERS => ['Authorization' => 'Basic ' . base64_encode($this->clientId . ':' . $this->getClientSecret())],
9898
RequestOptions::FORM_PARAMS => $this->getTokenFields($code),
9999
]);
100100

@@ -182,7 +182,7 @@ public function checkToken($jwt)
182182
$token = $this->getJwtConfig()->parser()->parse($jwt);
183183

184184
$data = Cache::remember('socialite:Apple-JWKSet', 5 * 60, function () {
185-
$response = (new Client)->get(self::URL.'/auth/keys');
185+
$response = (new Client)->get(self::URL . '/auth/keys');
186186

187187
return json_decode((string) $response->getBody(), true);
188188
});
@@ -196,7 +196,7 @@ public function checkToken($jwt)
196196
new SignedWith(new Sha256, AppleSignerInMemory::plainText($publicKey['key'])),
197197
new IssuedBy(self::URL),
198198
// fix for #1354
199-
new LooseValidAt(SystemClock::fromSystemTimezone(), new DateInterval('PT3S')),
199+
new LooseValidAt(SystemClock::fromSystemTimezone(), new DateInterval($this->getConfig('jwt_issued_time_leeway', 'PT3S'))),
200200
];
201201

202202
try {
@@ -278,8 +278,8 @@ protected function mapUserToObject(array $user)
278278
$user['name'] = $userRequest['name'];
279279
$fullName = trim(
280280
($user['name']['firstName'] ?? '')
281-
.' '
282-
.($user['name']['lastName'] ?? '')
281+
. ' '
282+
. ($user['name']['lastName'] ?? '')
283283
);
284284
}
285285

@@ -314,7 +314,7 @@ private function getUserRequest(): array
314314
*/
315315
protected function getRevokeUrl(): string
316316
{
317-
return self::URL.'/auth/revoke';
317+
return self::URL . '/auth/revoke';
318318
}
319319

320320
/**
@@ -365,6 +365,6 @@ public function refreshToken($refreshToken): ResponseInterface
365365
*/
366366
public static function additionalConfigKeys()
367367
{
368-
return ['private_key', 'passphrase', 'signer'];
368+
return ['private_key', 'passphrase', 'signer', 'jwt_issued_time_leeway'];
369369
}
370370
}

src/Apple/README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ Add lines to the configuration as follows:
3434
'private_key' => env('APPLE_PRIVATE_KEY'), // Required. Must be absolute path, e.g. /var/www/cert/AuthKey_XYZ.p8
3535
'passphrase' => env('APPLE_PASSPHRASE'), // Optional. Set if your private key have a passphrase.
3636
'signer' => env('APPLE_SIGNER'), // Optional. Signer used for Configuration::forSymmetricSigner(). Default: \Lcobucci\JWT\Signer\Ecdsa\Sha256
37-
'redirect' => env('APPLE_REDIRECT_URI') // Required.
37+
'redirect' => env('APPLE_REDIRECT_URI'), // Required.
38+
39+
'jwt_issued_time_leeway' => env('APPLE_JWT_ISSUED_TIME_LEEWAY'), // Optional. Set this to add a leeway to your JWT issued_time value. See section below
3840
],
3941
```
4042

@@ -86,6 +88,18 @@ return Socialite::driver('apple')->redirect();
8688
- ``name``
8789
- ``email``
8890

91+
### Known Issues
92+
93+
#### JWT Issued_at
94+
Sometimes the plugin may throw an exception due to a mismatch in time - See #1354. Use `config('services.apple.jwt_issued_time_leeway')` to 'rewind' the time. Default value is 3 seconds (PT3S).
95+
96+
Examples of possible values are PT3S -> 3 seconds, PT1M -> 1 Minute etc ...
97+
98+
The thrown exception may look like this:
99+
```
100+
[object] (Laravel\\Socialite\\Two\\InvalidStateException(code: 0): The token violates some mandatory constraints, details: - The token was issued in the future at /vendor/socialiteproviders/apple/Provider.php:207) [stacktrace]
101+
```
102+
89103
### Reference
90104

91105
- [Apple API Reference](https://developer.apple.com/documentation/signinwithapplerestapi/)

0 commit comments

Comments
 (0)