11
11
12
12
class GooglePublicKey
13
13
{
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 ' ;
15
17
16
18
private $ guzzle ;
19
+ private $ rsa ;
17
20
18
- public function __construct (Client $ guzzle )
21
+ public function __construct (Client $ guzzle, RSA $ rsa )
19
22
{
20
23
$ this ->guzzle = $ guzzle ;
24
+ $ this ->rsa = $ rsa ;
21
25
}
22
26
23
27
public function get ($ kid = null )
24
28
{
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
+ });
31
32
32
33
$ cert = $ kid ? collect ($ v3Certs )->firstWhere ('kid ' , '= ' , $ kid ) : $ v3Certs [0 ];
33
34
@@ -36,61 +37,37 @@ function () {
36
37
37
38
private function getv3Certs ()
38
39
{
39
- $ jwksUri = $ this ->getJwksUri ( );
40
+ $ jwksUri = $ this ->callApiAndReturnValue ( self :: URL_OPENID_CONFIG , ' jwks_uri ' );
40
41
41
- return $ this ->getCertificateKeys ($ jwksUri );
42
+ return $ this ->callApiAndReturnValue ($ jwksUri, ' keys ' );
42
43
}
43
44
44
45
private function extractPublicKeyFromCertificate ($ certificate )
45
46
{
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 ) ;
48
49
49
- $ rsa = app ( RSA ::class );
50
+ $ this -> rsa -> loadKey ( compact ( ' modulus ' , ' exponent ' ) );
50
51
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 ();
72
53
}
73
54
74
- private function getCertificateKeys ( $ jwksUri )
55
+ public function getKid ( $ openIdToken )
75
56
{
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 ' );
81
58
}
82
59
83
- public function getKid ( $ openIdToken )
60
+ private function callApiAndReturnValue ( $ url , $ value )
84
61
{
85
- $ response = $ this ->guzzle ->get (' https://www.googleapis.com/oauth2/v3/tokeninfo?id_token= ' . $ openIdToken );
62
+ $ response = $ this ->guzzle ->get ($ url );
86
63
87
- $ tokenInfo = json_decode ($ response ->getBody (), true );
64
+ $ data = json_decode ($ response ->getBody (), true );
88
65
89
- return Arr::get ($ tokenInfo , ' kid ' );
66
+ return Arr::get ($ data , $ value );
90
67
}
91
68
92
69
public function isCached ()
93
70
{
94
- return Cache::has (self ::CACHE_KEY );
71
+ return Cache::has (self ::V3_CERTS );
95
72
}
96
73
}
0 commit comments