Skip to content

Commit 1786e98

Browse files
committed
Improve lcobucci/jwt usage in Apple provider
Using the Configuration class forces to know a signer and a key, which is irrelevant for the purpose of the provider, but it introduces some code that looks scary if used incorrectly. The irrelevant classes are removed, additional exception cases are now rethrowing as InvalidStateException. Conditions have been inverted to implement early exit.
1 parent 089b0ae commit 1786e98

File tree

3 files changed

+21
-83
lines changed

3 files changed

+21
-83
lines changed

src/Apple/AppleSignerInMemory.php

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/Apple/AppleSignerNone.php

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/Apple/Provider.php

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@
1010
use Illuminate\Support\Str;
1111
use Laravel\Socialite\Two\InvalidStateException;
1212
use Lcobucci\Clock\SystemClock;
13-
use Lcobucci\JWT\Configuration;
13+
use Lcobucci\JWT\Encoding\JoseEncoder;
14+
use Lcobucci\JWT\Exception;
15+
use Lcobucci\JWT\Signer\Key\InMemory;
1416
use Lcobucci\JWT\Signer\Rsa\Sha256;
17+
use Lcobucci\JWT\Token\Parser;
1518
use Lcobucci\JWT\Validation\Constraint\IssuedBy;
1619
use Lcobucci\JWT\Validation\Constraint\LooseValidAt;
1720
use Lcobucci\JWT\Validation\Constraint\SignedWith;
18-
use Lcobucci\JWT\Validation\RequiredConstraintsViolated;
21+
use Lcobucci\JWT\Validation\Validator;
1922
use Psr\Http\Message\ResponseInterface;
2023
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
2124
use SocialiteProviders\Manager\OAuth2\User;
@@ -119,11 +122,11 @@ public function userByIdentityToken(string $token): User
119122
*/
120123
public static function verify($jwt)
121124
{
122-
$jwtContainer = Configuration::forSymmetricSigner(
123-
new AppleSignerNone,
124-
AppleSignerInMemory::plainText('')
125-
);
126-
$token = $jwtContainer->parser()->parse($jwt);
125+
try {
126+
$token = (new Parser(new JoseEncoder()))->parse($jwt);
127+
} catch (Exception $e) {
128+
throw new InvalidStateException($e->getMessage());
129+
}
127130

128131
$data = Cache::remember('socialite:Apple-JWKSet', 5 * 60, function () {
129132
$response = (new Client)->get(self::URL.'/auth/keys');
@@ -134,24 +137,23 @@ public static function verify($jwt)
134137
$publicKeys = JWK::parseKeySet($data);
135138
$kid = $token->headers()->get('kid');
136139

137-
if (isset($publicKeys[$kid])) {
138-
$publicKey = openssl_pkey_get_details($publicKeys[$kid]->getKeyMaterial());
140+
if (!isset($publicKeys[$kid])) {
141+
throw new InvalidStateException('Invalid JWT Signature');
142+
}
143+
144+
$publicKey = openssl_pkey_get_details($publicKeys[$kid]->getKeyMaterial());
145+
try {
139146
$constraints = [
140-
new SignedWith(new Sha256, AppleSignerInMemory::plainText($publicKey['key'])),
147+
new SignedWith(new Sha256, InMemory::plainText($publicKey['key'])),
141148
new IssuedBy(self::URL),
142149
new LooseValidAt(SystemClock::fromSystemTimezone()),
143150
];
144151

145-
try {
146-
$jwtContainer->validator()->assert($token, ...$constraints);
147-
148-
return true;
149-
} catch (RequiredConstraintsViolated $e) {
150-
throw new InvalidStateException($e->getMessage());
151-
}
152+
(new Validator())->assert($token, ...$constraints);
153+
} catch (Exception $e) {
154+
throw new InvalidStateException($e->getMessage());
152155
}
153-
154-
throw new InvalidStateException('Invalid JWT Signature');
156+
return true;
155157
}
156158

157159
/**

0 commit comments

Comments
 (0)