Skip to content

Commit a750d03

Browse files
committed
oAuth example works now
1 parent 9d3cd39 commit a750d03

File tree

3 files changed

+65
-13
lines changed

3 files changed

+65
-13
lines changed

Examples/auth.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
4+
use Ease\Shared as Shr;
5+
use Ease\Functions as Fnc;
6+
7+
require_once dirname(__DIR__) . '/vendor/autoload.php';
8+
9+
Shr::init([], dirname(__DIR__) . '/.env');
10+
11+
$productionSite = 'https://bezpecnost.csas.cz/api/psd2/fl/oidc/v1';
12+
$sandboxSite = 'https://webapi.developers.erstegroup.com/api/csas/sandbox/v1/sandbox-idp';
13+
$idpLink = (strtolower(Shr::cfg('API_ENVIRONMENT', 'production')) === 'sandbox') ? $sandboxSite : $productionSite;
14+
15+
/**
16+
* @link https://developers.erstegroup.com/docs/tutorial/csas-how-to-call-api Authentization & Authorization
17+
* @var array<string,string> Authorization link parameters
18+
*/
19+
$idpParams = [
20+
'client_id' => Shr::cfg('CLIENT_ID'),
21+
'response_type' => 'code',
22+
'prompt'=>'consent',
23+
'redirect_uri' => Shr::cfg('REDIRECT_URI'),
24+
'state' => Fnc::randomString(),
25+
'scope' => implode('%20', [
26+
'siblings.accounts',
27+
// 'siblings.payments',
28+
// 'AISP',
29+
// 'PISP'
30+
])
31+
];
32+
33+
$idpUri = Fnc::addUrlParams($idpLink.'/auth', $idpParams);
34+
35+
if(PHP_SAPI == 'cli'){
36+
echo $idpUri;
37+
} else {
38+
echo '<a href='.$idpUri.'>'.$idpUri.'</a>';
39+
}

Examples/balance.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// This is optional, `GuzzleHttp\Client` will be used as default.
88
new GuzzleHttp\Client()
99
);
10-
$id = 'id_example'; // string | Opaque system ID of the account
10+
$id = 'CZ1208000000000259459101'; // string | Opaque system ID of the account
1111

1212
try {
1313
$result = $apiInstance->getAccountBalance($id);

Examples/redirectedFromBank.php

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,35 @@
11
<?php
22

3-
// redirectedFromBank.php
3+
use Ease\Shared as Shr;
4+
use Ease\Functions as Fnc;
45

5-
// Load Ease Framework
6-
require_once __DIR__ . '/vendor/autoload.php';
6+
require_once dirname(__DIR__) . '/vendor/autoload.php';
77

8-
// Initialize Ease Framework and load .env file
9-
\Ease\Shared::init(['.env'], __DIR__);
8+
Shr::init(['CLIENT_ID', 'CLIENT_SECRET'], dirname(__DIR__) . '/.env');
109

1110
// Use environment variables
12-
$clientId = \Ease\Functions::cfg('CLIENT_ID');
13-
$clientSecret = \Ease\Functions::cfg('API_KEY');
14-
$redirectUri = 'http://localhost/php-csas-webapi/Examples/redirectedFromBank.php';
15-
$tokenUrl = 'https://sandbox.csas.cz/sandbox/api/token';
11+
$clientId = Shr::cfg('CLIENT_ID');
12+
$clientSecret = Shr::cfg('CLIENT_SECRET');
13+
$redirectUri = Shr::cfg('REDIRECT_URI');
14+
15+
$productionSite = 'https://bezpecnost.csas.cz/api/psd2/fl/oidc/v1';
16+
$sandboxSite = 'https://webapi.developers.erstegroup.com/api/csas/sandbox/v1/sandbox-idp';
17+
$idpLink = (strtolower(Shr::cfg('API_ENVIRONMENT', 'production')) === 'sandbox') ? $sandboxSite : $productionSite;
18+
19+
$tokenUrl = $idpLink . '/token';
1620

1721
// Start session
1822
session_start();
1923

20-
// Check if the authorization code is set
21-
if (isset($_GET['code'])) {
22-
$code = $_GET['code'];
24+
if (PHP_SAPI == 'cli') {
25+
parse_str($argv[1], $params);
26+
$code = array_key_exists('code', $params) ? $params['code'] : '';
27+
} else {
28+
$code = array_key_exists('code', $_GET) ? $_GET['code'] : '';
29+
}
2330

31+
// Check if the authorization code is set
32+
if ($code) {
2433
// Prepare the POST request to exchange the authorization code for an access token
2534
$postFields = [
2635
'grant_type' => 'authorization_code',
@@ -35,8 +44,10 @@
3544
curl_setopt($ch, CURLOPT_POST, true);
3645
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postFields));
3746
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
47+
curl_setopt($ch, CURLOPT_VERBOSE, 1);
3848

3949
$response = curl_exec($ch);
50+
$info = curl_getinfo($ch);
4051
curl_close($ch);
4152

4253
$responseData = json_decode($response, true);
@@ -47,6 +58,8 @@
4758
echo 'Access token obtained successfully!';
4859
} else {
4960
echo 'Error obtaining access token!';
61+
62+
var_dump($info);
5063
}
5164
} else {
5265
echo 'Authorization code not found!';

0 commit comments

Comments
 (0)