1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BPCI\SumUp\OAuth; |
4
|
|
|
|
5
|
|
|
use BPCI\SumUp\ContextInterface; |
6
|
|
|
use BPCI\SumUp\Exception\BadRequestException; |
7
|
|
|
use BPCI\SumUp\SumUp; |
8
|
|
|
|
9
|
|
|
class AuthenticationHelper |
10
|
|
|
{ |
11
|
|
|
const OAUTH_AUTHORIZATION = '/authorize'; |
12
|
|
|
const OAUTH_TOKEN = '/token'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Generate an url to merchant authorization. |
16
|
|
|
* |
17
|
|
|
* @param ContextInterface $context |
18
|
|
|
* @return string |
19
|
|
|
*/ |
20
|
|
|
public static function getAuthorizationURL(ContextInterface $context) |
21
|
|
|
{ |
22
|
|
|
$queryString = [ |
23
|
|
|
'client_id' => $context->getClientId(), |
24
|
|
|
'client_secret' => $context->getClientSecret(), |
25
|
|
|
'redirect_uri' => $context->getRedirectUri(), |
26
|
|
|
'response_type' => 'code', |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
return SumUp::ENTRYPOINT . self::OAUTH_AUTHORIZATION . '?' . http_build_query($queryString); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Request an acess token from sumup services. |
34
|
|
|
* |
35
|
|
|
* @param ContextInterface $context |
36
|
|
|
* @param array|null $scopes |
37
|
|
|
* @param array $options |
38
|
|
|
* @return \BPCI\SumUp\OAuth\AccessToken |
39
|
|
|
*/ |
40
|
|
|
public static function getAccessToken( |
41
|
|
|
ContextInterface $context, |
42
|
|
|
array $scopes = null, |
43
|
|
|
array $options = [] |
44
|
|
|
): AccessToken |
45
|
|
|
{ |
46
|
|
|
$formParams = [ |
47
|
|
|
'grant_type' => 'client_credentials', |
48
|
|
|
'client_id' => $context->getClientId(), |
49
|
|
|
'client_secret' => $context->getClientSecret(), |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
if ($scopes !== null) { |
53
|
|
|
$formParams['scope'] = implode(',', $scopes); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$client = SumUp::getClient($options); |
57
|
|
|
|
58
|
|
|
$response = $client->request( |
59
|
|
|
'POST', |
60
|
|
|
self::OAUTH_TOKEN, |
61
|
|
|
[ |
62
|
|
|
'form_params' => $formParams, |
63
|
|
|
]); |
64
|
|
|
|
65
|
|
|
$code = $response->getStatusCode(); |
66
|
|
|
if ($code !== 200) { |
67
|
|
|
$message = " Request code: $code \n Message: " . $response->getReasonPhrase(); |
68
|
|
|
throw new BadRequestException($message); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$body = json_decode($response->getBody()->getContents(), true); |
72
|
|
|
$token_params = [ |
73
|
|
|
$body['access_token'], |
74
|
|
|
$body['token_type'], |
75
|
|
|
$body['expires_in'], |
76
|
|
|
]; |
77
|
|
|
|
78
|
|
|
if (isset($body['scope'])) { |
79
|
|
|
$token_params[] = $body['scope']; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return new AccessToken(...$token_params); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* If available check token or generate a new token |
87
|
|
|
* |
88
|
|
|
* @param ContextInterface $context |
89
|
|
|
* @param AccessToken $token |
90
|
|
|
* @param array $scopes |
91
|
|
|
* @param array $options |
92
|
|
|
* |
93
|
|
|
* @return \BPCI\SumUp\OAuth\AccessToken |
94
|
|
|
*/ |
95
|
5 |
|
public static function getValidToken( |
96
|
|
|
ContextInterface $context, |
97
|
|
|
AccessToken $token = null, |
98
|
|
|
array $scopes = null, |
99
|
|
|
array $options = [] |
100
|
|
|
): AccessToken |
101
|
|
|
{ |
102
|
5 |
|
if ($token === null || !$token->isValid()) { |
103
|
|
|
$token = AuthenticationHelper::getAccessToken($context, $scopes, $options); |
104
|
|
|
} |
105
|
|
|
|
106
|
5 |
|
return $token; |
107
|
|
|
} |
108
|
|
|
|
109
|
5 |
|
public static function getOAuthHeader(AccessToken $token): array |
110
|
|
|
{ |
111
|
|
|
return [ |
112
|
|
|
'headers' => [ |
113
|
5 |
|
'Authorization' => $token->getType() . ' ' . $token->getToken(), |
114
|
|
|
], |
115
|
|
|
]; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.