Skip to content

Commit ea039d2

Browse files
committed
Current development state.
web-api-key replaced to WEB-API-key globally
1 parent 8649581 commit ea039d2

21 files changed

+434
-90
lines changed

.openapi-generator/templates/api.mustache

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ use {{invokerPackage}}\ObjectSerializer;
102102
$this->headerSelector = $selector ?: new HeaderSelector();
103103
$this->hostIndex = $hostIndex;
104104
if (method_exists($this->client, 'getApiKey')) {
105-
$config['headers']['web-api-key'] = $this->client->getApiKey();
105+
$config['headers']['WEB-API-key'] = $this->client->getApiKey();
106106
$this->setApiKey($this->client->getApiKey());
107107
}
108108
if (method_exists($this->client, 'getAccessToken')) {
@@ -122,7 +122,7 @@ use {{invokerPackage}}\ObjectSerializer;
122122
*/
123123
public function setApiKey(string $apiKey): void
124124
{
125-
$this->config->setApiKey('web-api-key', $apiKey);
125+
$this->config->setApiKey('WEB-API-key', $apiKey);
126126
}
127127

128128
/**
@@ -909,7 +909,7 @@ use {{invokerPackage}}\ObjectSerializer;
909909
}
910910
}
911911

912-
$options['headers']['web-api-key'] = $this->config->getApiKey('web-api-key');
912+
$options['headers']['WEB-API-key'] = $this->config->getApiKey('WEB-API-key');
913913
$options['headers']['authorization'] = 'Bearer '.$this->config->getAccessToken();
914914

915915
return $options;

Examples/accounts.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
$apiInstance = new \SpojeNET\Csas\Accounts\DefaultApi(new SpojeNET\Csas\ApiClient(
2323
[
2424
'apikey' => Shr::cfg('API_KEY'),
25-
'token' => Shr::cfg('API_TOKEN'),
25+
'token' => Shr::cfg('ACCESS_TOKEN'),
2626
'debug' => Shr::cfg('API_DEBUG', false),
2727
'sandbox' => Shr::cfg('SANDBOX_MODE'),
2828
],

Examples/auth.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,22 @@
3535
'redirect_uri' => Shr::cfg('REDIRECT_URI'),
3636
'state' => Fnc::randomString(),
3737
'access_type' => 'offline',
38-
'scope' => implode('%20', [
39-
'siblings.accounts',
40-
// 'siblings.payments',
41-
// 'AISP',
42-
// 'PISP'
43-
]),
38+
// 'scope' => implode('%20', [
39+
// 'siblings.accounts',
40+
// // 'siblings.payments',
41+
// // 'AISP',
42+
// // 'PISP'
43+
// ]),
4444
];
4545

46+
session_start();
47+
$_SESSION['oauth2state'] = $idpParams['state'];
48+
4649
$idpUri = Fnc::addUrlParams($idpLink.'/auth', $idpParams);
4750

4851
if (\PHP_SAPI === 'cli') {
4952
echo $idpUri;
5053
} else {
5154
echo '<a href='.$idpUri.'>'.$idpUri.'</a>';
5255
}
56+

Examples/balance.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
$apiInstance = new \SpojeNET\Csas\Accounts\DefaultApi(new SpojeNET\Csas\ApiClient(
2323
[
2424
'apikey' => Shr::cfg('API_KEY'),
25-
'token' => Shr::cfg('API_TOKEN'),
25+
'token' => Shr::cfg('ACCESS_TOKEN'),
2626
'debug' => Shr::cfg('API_DEBUG', false),
2727
'sandbox' => Shr::cfg('SANDBOX_MODE'),
2828
],

Examples/redirectedFromBank.php

Lines changed: 92 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515

1616
use Ease\Shared as Shr;
17+
use League\OAuth2\Client\Provider\GenericProvider;
1718

1819
require_once \dirname(__DIR__).'/vendor/autoload.php';
1920

@@ -28,54 +29,102 @@
2829
$sandboxSite = 'https://webapi.developers.erstegroup.com/api/csas/sandbox/v1/sandbox-idp';
2930
$idpLink = Shr::cfg('SANDBOX_MODE', false) ? $sandboxSite : $productionSite;
3031
$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+
]);
3143

3244
// Start session
3345
session_start();
3446

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']) {
4168

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']);
7871
}
72+
73+
exit('Invalid state');
74+
7975
} 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+
81130
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of the CsasWebApi package
7+
*
8+
* https://github.com/Spoje-NET/php-csas-webapi
9+
*
10+
* (c) SpojeNetIT <http://spoje.net/>
11+
*
12+
* For the full copyright and license information, please view the LICENSE
13+
* file that was distributed with this source code.
14+
*/
15+
16+
use Ease\Shared as Shr;
17+
18+
require_once \dirname(__DIR__).'/vendor/autoload.php';
19+
20+
Shr::init(['CLIENT_ID', 'CLIENT_SECRET'], \dirname(__DIR__).'/.env');
21+
22+
// Use environment variables
23+
$clientId = Shr::cfg('CLIENT_ID');
24+
$clientSecret = Shr::cfg('CLIENT_SECRET');
25+
$redirectUri = Shr::cfg('REDIRECT_URI');
26+
27+
$productionSite = 'https://bezpecnost.csas.cz/api/psd2/fl/oidc/v1';
28+
$sandboxSite = 'https://webapi.developers.erstegroup.com/api/csas/sandbox/v1/sandbox-idp';
29+
$idpLink = Shr::cfg('SANDBOX_MODE', false) ? $sandboxSite : $productionSite;
30+
$tokenUrl = $idpLink.'/token';
31+
32+
// Start session
33+
session_start();
34+
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+
}
41+
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+
75+
echo '<h2> Výměna CODE za Access Token a Refresh Token </h2>';
76+
77+
curl_setopt($ch, \CURLOPT_URL, $idpLink.'/token');
78+
curl_setopt($ch, \CURLOPT_RETURNTRANSFER, true);
79+
curl_setopt($ch, \CURLOPT_HEADER, false);
80+
81+
curl_setopt($ch, \CURLOPT_POST, true);
82+
83+
$postFields = [
84+
'client_id' => $clientId,
85+
'client_secret' => $clientSecret,
86+
'grant_type' => 'refresh_token',
87+
'refresh_token' => $responseData['refresh_token'],
88+
];
89+
90+
echo '<h3>Token Request</h3>';
91+
var_dump($postFields);
92+
93+
curl_setopt($ch, \CURLOPT_POSTFIELDS, http_build_query($postFields));
94+
95+
curl_setopt($ch, \CURLOPT_HTTPHEADER, [
96+
'Content-Type: application/x-www-form-urlencoded',
97+
]);
98+
99+
$response = curl_exec($ch);
100+
101+
102+
echo '<h3>Token Response</h3>';
103+
var_dump(curl_getinfo($ch));
104+
105+
curl_close($ch);
106+
107+
$responseData = json_decode($response,true);
108+
109+
var_dump($responseData);
110+
111+
echo 'access token:<textarea>'.$responseData['access_token'].'</textarea>';
112+
113+
} else {
114+
echo 'Error obtaining access token!';
115+
116+
var_dump($info);
117+
}
118+
} else {
119+
echo 'Authorization code not found!';
120+
}

0 commit comments

Comments
 (0)