|
14 | 14 | */
|
15 | 15 |
|
16 | 16 | use Ease\Shared as Shr;
|
| 17 | +use League\OAuth2\Client\Provider\GenericProvider; |
17 | 18 |
|
18 | 19 | require_once \dirname(__DIR__).'/vendor/autoload.php';
|
19 | 20 |
|
|
28 | 29 | $sandboxSite = 'https://webapi.developers.erstegroup.com/api/csas/sandbox/v1/sandbox-idp';
|
29 | 30 | $idpLink = Shr::cfg('SANDBOX_MODE', false) ? $sandboxSite : $productionSite;
|
30 | 31 | $tokenUrl = $idpLink.'/token';
|
| 32 | +$authorizeUrl = $idpLink.'/authorize'; |
| 33 | +$resourceUrl = $idpLink.'/resource'; |
| 34 | + |
| 35 | +$provider = new GenericProvider([ |
| 36 | + 'clientId' => $clientId, |
| 37 | + 'clientSecret' => $clientSecret, |
| 38 | + 'redirectUri' => $redirectUri, |
| 39 | + 'urlAuthorize' => $authorizeUrl, |
| 40 | + 'urlAccessToken' => $tokenUrl, |
| 41 | + 'urlResourceOwnerDetails' => $resourceUrl |
| 42 | +]); |
31 | 43 |
|
32 | 44 | // Start session
|
33 | 45 | session_start();
|
34 | 46 |
|
35 |
| -if (\PHP_SAPI === 'cli') { |
36 |
| - parse_str($argv[1], $params); |
37 |
| - $code = \array_key_exists('code', $params) ? $params['code'] : ''; |
38 |
| -} else { |
39 |
| - $code = \array_key_exists('code', $_GET) ? $_GET['code'] : ''; |
40 |
| -} |
| 47 | +// If we don't have an authorization code then get one |
| 48 | +if (!isset($_GET['code'])) { |
| 49 | + |
| 50 | + // Fetch the authorization URL from the provider; this returns the |
| 51 | + // urlAuthorize option and generates and applies any necessary parameters |
| 52 | + // (e.g. state). |
| 53 | + $authorizationUrl = $provider->getAuthorizationUrl(); |
| 54 | + |
| 55 | + // Get the state generated for you and store it to the session. |
| 56 | + $_SESSION['oauth2state'] = $provider->getState(); |
| 57 | + |
| 58 | + // Optional, only required when PKCE is enabled. |
| 59 | + // Get the PKCE code generated for you and store it to the session. |
| 60 | + $_SESSION['oauth2pkceCode'] = $provider->getPkceCode(); |
| 61 | + |
| 62 | + // Redirect the user to the authorization URL. |
| 63 | + header('Location: ' . $authorizationUrl); |
| 64 | + exit; |
| 65 | + |
| 66 | +// Check given state against previously stored one to mitigate CSRF attack |
| 67 | +} elseif (empty($_GET['state']) || empty($_SESSION['oauth2state']) || $_GET['state'] !== $_SESSION['oauth2state']) { |
41 | 68 |
|
42 |
| -// Check if the authorization code is set |
43 |
| -if ($code) { |
44 |
| - // Prepare the POST request to exchange the authorization code for an access token |
45 |
| - $postFields = [ |
46 |
| - 'grant_type' => 'authorization_code', |
47 |
| - 'code' => $code, |
48 |
| - 'redirect_uri' => $redirectUri, |
49 |
| - 'client_id' => $clientId, |
50 |
| - 'client_secret' => $clientSecret, |
51 |
| - ]; |
52 |
| - |
53 |
| - $ch = curl_init(); |
54 |
| - curl_setopt($ch, \CURLOPT_URL, $tokenUrl); |
55 |
| - curl_setopt($ch, \CURLOPT_POST, true); |
56 |
| - curl_setopt($ch, \CURLOPT_POSTFIELDS, http_build_query($postFields)); |
57 |
| - curl_setopt($ch, \CURLOPT_RETURNTRANSFER, true); |
58 |
| - curl_setopt($ch, \CURLOPT_VERBOSE, 1); |
59 |
| - |
60 |
| - $response = curl_exec($ch); |
61 |
| - $info = curl_getinfo($ch); |
62 |
| - curl_close($ch); |
63 |
| - |
64 |
| - $responseData = json_decode($response, true); |
65 |
| - |
66 |
| - if (isset($responseData['access_token'])) { |
67 |
| - // Store the access token in the session |
68 |
| - $_SESSION['access_token'] = $responseData['access_token']; |
69 |
| - echo '<h2>Access token obtained successfully!</h2>'; |
70 |
| - |
71 |
| - echo 'access token:<textarea>'.$responseData['access_token'].'</textarea>'; |
72 |
| - echo 'refresh token:<textarea>'.$responseData['refresh_token'].'</textarea>'; |
73 |
| - var_dump($responseData); |
74 |
| - } else { |
75 |
| - echo 'Error obtaining access token!'; |
76 |
| - |
77 |
| - var_dump($info); |
| 69 | + if (isset($_SESSION['oauth2state'])) { |
| 70 | + unset($_SESSION['oauth2state']); |
78 | 71 | }
|
| 72 | + |
| 73 | + exit('Invalid state'); |
| 74 | + |
79 | 75 | } else {
|
80 |
| - echo 'Authorization code not found!'; |
| 76 | + |
| 77 | + try { |
| 78 | + |
| 79 | + // Optional, only required when PKCE is enabled. |
| 80 | + // Restore the PKCE code stored in the session. |
| 81 | + $provider->setPkceCode($_SESSION['oauth2pkceCode']); |
| 82 | + |
| 83 | + // Try to get an access token using the authorization code grant. |
| 84 | + $tokens = $provider->getAccessToken('authorization_code', [ |
| 85 | + 'code' => $_GET['code'] |
| 86 | + ]); |
| 87 | + |
| 88 | + // We have an access token, which we may use in authenticated |
| 89 | + // requests against the service provider's API. |
| 90 | + echo 'Access Token: ' . $tokens->getToken() . "<br>"; |
| 91 | + echo 'Refresh Token: ' . $tokens->getRefreshToken() . "<br>"; |
| 92 | + echo 'Expired in: ' . $tokens->getExpires() . "<br>"; |
| 93 | + echo 'Already expired? ' . ($tokens->hasExpired() ? 'expired' : 'not expired') . "<br>"; |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + $tokens = $provider->getAccessToken('refresh_token', [ |
| 100 | + 'refresh_token' => $tokens->getRefreshToken() |
| 101 | + ]); |
| 102 | + |
| 103 | + echo 'access token:<textarea>'.$tokens->getToken().'</textarea>'; |
| 104 | +// echo 'refresh token:<textarea>'.$tokens->getRefreshToken().'</textarea>'; |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | +// // Using the access token, we may look up details about the |
| 109 | +// // resource owner. |
| 110 | +// $resourceOwner = $provider->getResourceOwner($tokens); |
| 111 | +// |
| 112 | +// var_export($resourceOwner->toArray()); |
| 113 | +// |
| 114 | +// // The provider provides a way to get an authenticated API request for |
| 115 | +// // the service, using the access token; it returns an object conforming |
| 116 | +// // to Psr\Http\Message\RequestInterface. |
| 117 | +// $request = $provider->getAuthenticatedRequest( |
| 118 | +// 'GET', |
| 119 | +// 'https://service.example.com/resource', |
| 120 | +// $tokens |
| 121 | +// ); |
| 122 | + |
| 123 | + } catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) { |
| 124 | + |
| 125 | + // Failed to get the access token or user details. |
| 126 | + exit($e->getMessage()); |
| 127 | + |
| 128 | + } |
| 129 | + |
81 | 130 | }
|
0 commit comments