22
33namespace RapidWeb \GoogleOAuth2Handler ;
44
5- use GuzzleHttp \Psr7 \Request ;
5+ use InvalidArgumentException ;
6+ use RuntimeException ;
67
78class GoogleOAuth2Handler
89{
910 private $ clientId ;
1011 private $ clientSecret ;
1112 private $ scopes ;
1213 private $ refreshToken ;
14+ private $ redirectUri ;
1315 private $ client ;
1416
1517 public $ authUrl ;
1618
17- public function __construct ($ clientId , $ clientSecret , $ scopes , $ refreshToken = '' )
19+ public function __construct ($ clientId , $ clientSecret , $ scopes , $ refreshToken = '' , $ redirectUri = null )
1820 {
1921 $ this ->clientId = $ clientId ;
2022 $ this ->clientSecret = $ clientSecret ;
2123 $ this ->scopes = $ scopes ;
2224 $ this ->refreshToken = $ refreshToken ;
25+ $ this ->redirectUri = $ redirectUri ;
26+
27+ if (!$ this ->refreshToken && !$ this ->redirectUri ) {
28+ throw new InvalidArgumentException ('A redirect URI is required when requesting a new Google authorization code. ' );
29+ }
2330
2431 $ this ->setupClient ();
2532 }
@@ -30,11 +37,17 @@ private function setupClient()
3037
3138 $ this ->client ->setClientId ($ this ->clientId );
3239 $ this ->client ->setClientSecret ($ this ->clientSecret );
33- $ this ->client ->setRedirectUri ('urn:ietf:wg:oauth:2.0:oob ' );
40+ if ($ this ->redirectUri ) {
41+ $ this ->client ->setRedirectUri ($ this ->redirectUri );
42+ }
3443 $ this ->client ->setAccessType ('offline ' );
35- $ this ->client ->setApprovalPrompt ('force ' );
44+ if (method_exists ($ this ->client , 'setPrompt ' )) {
45+ $ this ->client ->setPrompt ('consent ' );
46+ } else {
47+ $ this ->client ->setApprovalPrompt ('force ' );
48+ }
3649
37- foreach ($ this ->scopes as $ scope ) {
50+ foreach ($ this ->scopes as $ scope ) {
3851 $ this ->client ->addScope ($ scope );
3952 }
4053
@@ -47,17 +60,27 @@ private function setupClient()
4760
4861 public function getRefreshToken ($ authCode )
4962 {
50- $ this ->client ->authenticate ($ authCode );
51- $ accessToken = $ this ->client ->getAccessToken ();
63+ $ accessToken = $ this ->client ->fetchAccessTokenWithAuthCode ($ authCode );
64+
65+ if (isset ($ accessToken ['error ' ])) {
66+ throw new RuntimeException ('Google rejected the authorization code: ' .($ accessToken ['error_description ' ] ?? $ accessToken ['error ' ]));
67+ }
68+
69+ if (empty ($ accessToken ['refresh_token ' ])) {
70+ throw new RuntimeException ('Google did not return a refresh token. Revoke the existing grant and authorize again with consent. ' );
71+ }
72+
5273 return $ accessToken ['refresh_token ' ];
5374 }
5475
55- public function performRequest ($ method , $ url , $ body = null )
76+ public function performRequest ($ method , $ url , $ body = null , array $ options = [] )
5677 {
5778 $ httpClient = $ this ->client ->authorize ();
58- $ request = new Request ($ method , $ url , [], $ body );
59- $ response = $ httpClient ->send ($ request );
60- return $ response ;
61- }
6279
63- }
80+ if ($ body !== null && !array_key_exists ('body ' , $ options )) {
81+ $ options ['body ' ] = $ body ;
82+ }
83+
84+ return $ httpClient ->request ($ method , $ url , $ options );
85+ }
86+ }
0 commit comments