Skip to content

Commit 750c1f6

Browse files
committed
Refactor and clean up GooglePublicKey class
1 parent da1635e commit 750c1f6

File tree

2 files changed

+23
-46
lines changed

2 files changed

+23
-46
lines changed

src/GooglePublicKey.php

Lines changed: 22 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,24 @@
1111

1212
class GooglePublicKey
1313
{
14-
private const CACHE_KEY = 'GooglePublicKey';
14+
private const V3_CERTS = 'GOOGLE_V3_CERTS';
15+
private const URL_OPENID_CONFIG = 'https://accounts.google.com/.well-known/openid-configuration';
16+
private const URL_TOKEN_INFO = 'https://www.googleapis.com/oauth2/v3/tokeninfo';
1517

1618
private $guzzle;
19+
private $rsa;
1720

18-
public function __construct(Client $guzzle)
21+
public function __construct(Client $guzzle, RSA $rsa)
1922
{
2023
$this->guzzle = $guzzle;
24+
$this->rsa = $rsa;
2125
}
2226

2327
public function get($kid = null)
2428
{
25-
$v3Certs = Cache::rememberForever(
26-
self::CACHE_KEY,
27-
function () {
28-
return $this->getv3Certs();
29-
}
30-
);
29+
$v3Certs = Cache::rememberForever(self::V3_CERTS, function () {
30+
return $this->getv3Certs();
31+
});
3132

3233
$cert = $kid ? collect($v3Certs)->firstWhere('kid', '=', $kid) : $v3Certs[0];
3334

@@ -36,61 +37,37 @@ function () {
3637

3738
private function getv3Certs()
3839
{
39-
$jwksUri = $this->getJwksUri();
40+
$jwksUri = $this->callApiAndReturnValue(self::URL_OPENID_CONFIG, 'jwks_uri');
4041

41-
return $this->getCertificateKeys($jwksUri);
42+
return $this->callApiAndReturnValue($jwksUri, 'keys');
4243
}
4344

4445
private function extractPublicKeyFromCertificate($certificate)
4546
{
46-
$modulus = $certificate['n'];
47-
$exponent = $certificate['e'];
47+
$modulus = new BigInteger(JWT::urlsafeB64Decode($certificate['n']), 256);
48+
$exponent = new BigInteger(JWT::urlsafeB64Decode($certificate['e']), 256);
4849

49-
$rsa = app(RSA::class);
50+
$this->rsa->loadKey(compact('modulus', 'exponent'));
5051

51-
$modulus = new BigInteger(JWT::urlsafeB64Decode($modulus), 256);
52-
$exponent = new BigInteger(JWT::urlsafeB64Decode($exponent), 256);
53-
54-
$rsa->loadKey([
55-
'n' => $modulus,
56-
'e' => $exponent
57-
]);
58-
$rsa->setPublicKey();
59-
60-
return $rsa->getPublicKey();
61-
}
62-
63-
private function getJwksUri()
64-
{
65-
$discoveryEndpoint = 'https://accounts.google.com/.well-known/openid-configuration';
66-
67-
$configurationJson = $this->guzzle->get($discoveryEndpoint);
68-
69-
$configurations = json_decode($configurationJson->getBody(), true);
70-
71-
return Arr::get($configurations, 'jwks_uri');
52+
return $this->rsa->getPublicKey();
7253
}
7354

74-
private function getCertificateKeys($jwksUri)
55+
public function getKid($openIdToken)
7556
{
76-
$json = $this->guzzle->get($jwksUri);
77-
78-
$certificates = json_decode($json->getBody(), true);
79-
80-
return Arr::get($certificates, 'keys');
57+
return $this->callApiAndReturnValue(self::URL_TOKEN_INFO . '?id_token=' . $openIdToken, 'kid');
8158
}
8259

83-
public function getKid($openIdToken)
60+
private function callApiAndReturnValue($url, $value)
8461
{
85-
$response = $this->guzzle->get('https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=' . $openIdToken);
62+
$response = $this->guzzle->get($url);
8663

87-
$tokenInfo = json_decode($response->getBody(), true);
64+
$data = json_decode($response->getBody(), true);
8865

89-
return Arr::get($tokenInfo, 'kid');
66+
return Arr::get($data, $value);
9067
}
9168

9269
public function isCached()
9370
{
94-
return Cache::has(self::CACHE_KEY);
71+
return Cache::has(self::V3_CERTS);
9572
}
9673
}

tests/GooglePublicKeyTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ protected function setUp(): void
2626

2727
$this->guzzle = Mockery::mock(new Client());
2828

29-
$this->publicKey = new GooglePublicKey($this->guzzle);
29+
$this->publicKey = new GooglePublicKey($this->guzzle, new RSA());
3030
}
3131

3232
/** @test */

0 commit comments

Comments
 (0)