forked from vienthuong/shopware-php-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
authentication.php
93 lines (71 loc) · 3.13 KB
/
authentication.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
require __DIR__ . '/../vendor/autoload.php';
use \Vin\ShopwareSdk\Client\GrantType\PasswordGrantType;
use \Vin\ShopwareSdk\Client\GrantType\ClientCredentialsGrantType;
use \Vin\ShopwareSdk\Client\GrantType\RefreshTokenGrantType;
use \Vin\ShopwareSdk\Client\AdminAuthenticator;
use \Vin\ShopwareSdk\Data\AccessToken;
use \Vin\ShopwareSdk\Client\GrantType\GrantType;
class AuthenticationExample {
public function exampleUsingPasswordGrantType(): AccessToken
{
$config = $this->getConfig();
$grantType = new PasswordGrantType($config['username'], $config['password']);
$adminClient = new AdminAuthenticator($grantType, $config['shop_url']);
$accessToken = $adminClient->fetchAccessToken();
dump("============================================");
dump("Fetch access token using password grant type:");
dump($accessToken);
return $accessToken;
}
public function exampleUsingClientCredentials(): AccessToken
{
$config = $this->getConfig();
$grantType = new ClientCredentialsGrantType($config['client_id'], $config['client_secret']);
$adminClient = new AdminAuthenticator($grantType, $config['shop_url']);
$accessToken = $adminClient->fetchAccessToken();
dump("============================================");
dump("Fetch access token using client credential grant type: \n");
dump($accessToken);
return $accessToken;
}
public function exampleUsingRefreshToken(): AccessToken
{
$config = $this->getConfig();
$grantType = new PasswordGrantType($config['username'], $config['password']);
$adminClient = new AdminAuthenticator($grantType, $config['shop_url']);
$passwordAccessToken = $adminClient->fetchAccessToken();
$grantType = new RefreshTokenGrantType($passwordAccessToken->refreshToken);
$adminClient = new AdminAuthenticator($grantType, $config['shop_url']);
$refreshToken = $adminClient->fetchAccessToken();
dump("============================================");
dump("Fetch access token using refresh grant type:");
dump($refreshToken);
return $refreshToken;
}
public function exampleUsingConfig(): AccessToken
{
$config = $this->getConfig();
$config['grant_type'] = 'password';
$grantType = GrantType::createFromConfig($config);
$adminClient = new AdminAuthenticator($grantType, $config['shop_url']);
$passwordAccessToken = $adminClient->fetchAccessToken();
dump("============================================");
dump("Fetch access token using array config:");
dump($passwordAccessToken);
return $passwordAccessToken;
}
public function execute(): void
{
$this->exampleUsingPasswordGrantType();
$this->exampleUsingClientCredentials();
$this->exampleUsingRefreshToken();
$this->exampleUsingConfig();
}
private function getConfig(): array
{
return json_decode(file_get_contents(__DIR__ . '/auth-config.json'), true);
}
}
$authExample = new AuthenticationExample();
$authExample->execute();