1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NwLaravel\OAuth; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\Exception\RequestException; |
7
|
|
|
use GuzzleHttp\Psr7\Response as GuzzleResponse; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Proxy |
12
|
|
|
* |
13
|
|
|
* @package NwLaravel\OAuth |
14
|
|
|
*/ |
15
|
|
|
class OAuthProxy |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var OAuthClientEntity |
19
|
|
|
*/ |
20
|
|
|
protected $oauthClient; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Client |
24
|
|
|
*/ |
25
|
|
|
protected $clientHttp; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $proxyUrl; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Construct |
34
|
|
|
* |
35
|
|
|
* @param OAuthClientEntity $oauthClient OAuth Client Entity |
36
|
|
|
* @param Client $clientHttp Client Http |
37
|
|
|
*/ |
38
|
5 |
|
public function __construct(OAuthClientEntity $oauthClient, Client $clientHttp) |
39
|
|
|
{ |
40
|
5 |
|
$this->oauthClient = $oauthClient; |
41
|
5 |
|
$this->clientHttp = $clientHttp; |
42
|
5 |
|
$this->proxyUrl = sprintf('%s%s', config('app.url'), config('nwlaravel.oauth.urlToken', '/oauth/access-token')); |
43
|
5 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Attempt Login Password |
47
|
|
|
* |
48
|
|
|
* @param string $client_id Client Id |
49
|
|
|
* @param array $credentials Credentials |
50
|
|
|
* |
51
|
|
|
* @return Response |
52
|
|
|
*/ |
53
|
3 |
|
public function attemptLogin($client_id, array $credentials) |
54
|
|
|
{ |
55
|
3 |
|
return $this->proxy('password', $client_id, $credentials); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Attempt Refresh Token |
60
|
|
|
* |
61
|
|
|
* @param string $client_id Client Id |
62
|
|
|
* @param array $credentials Credentials |
63
|
|
|
* |
64
|
|
|
* @return Response |
65
|
|
|
*/ |
66
|
1 |
|
public function attemptRefresh($client_id, array $credentials) |
67
|
|
|
{ |
68
|
1 |
|
return $this->proxy('refresh_token', $client_id, $credentials); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Proxy |
73
|
|
|
* |
74
|
|
|
* @param string $grantType String Grant Type |
75
|
|
|
* @param string $client_id String Client Id |
76
|
|
|
* @param array $data Array Data |
77
|
|
|
* |
78
|
|
|
* @return Response |
79
|
|
|
*/ |
80
|
4 |
|
private function proxy($grantType, $client_id, array $data = array()) |
81
|
|
|
{ |
82
|
|
|
try { |
83
|
4 |
|
$data = array_merge([ |
84
|
4 |
|
'client_id' => $client_id, |
85
|
4 |
|
'client_secret' => $this->getClientSecret($client_id), |
86
|
|
|
'grant_type' => $grantType |
87
|
4 |
|
], $data); |
88
|
|
|
|
89
|
4 |
|
$guzzleResponse = $this->clientHttp->post($this->proxyUrl, ['json' => $data]); |
90
|
|
|
|
91
|
4 |
|
} catch (RequestException $e) { |
92
|
1 |
|
$body = sprintf('{"error": "%s", "error_description": "%s"}', get_class($e), $e->getMessage()); |
93
|
1 |
|
$guzzleResponse = $e->hasResponse() ? $e->getResponse() : new GuzzleResponse(500, [], $body); |
94
|
|
|
|
95
|
2 |
|
} catch (\Exception $e) { |
96
|
1 |
|
$body = sprintf('{"error": "%s", "error_description": "%s"}', get_class($e), $e->getMessage()); |
97
|
1 |
|
$guzzleResponse = new GuzzleResponse(500, [], $body); |
98
|
|
|
} |
99
|
|
|
|
100
|
4 |
|
return $this->parseResponse($guzzleResponse); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get Client Secret |
105
|
|
|
* |
106
|
|
|
* @param string $client_id Client Id |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
4 |
|
private function getClientSecret($client_id) |
110
|
|
|
{ |
111
|
4 |
|
$client_secret = ''; |
112
|
4 |
|
$client = $this->oauthClient->where('id', $client_id)->first(); |
113
|
4 |
|
if ($client) { |
114
|
4 |
|
$client_secret = $client->secret; |
115
|
4 |
|
} |
116
|
|
|
|
117
|
4 |
|
return $client_secret; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Parse Response |
122
|
|
|
* |
123
|
|
|
* @param ResponseInterface $guzzleResponse |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
4 |
|
private function parseResponse(ResponseInterface $guzzleResponse) |
127
|
|
|
{ |
128
|
4 |
|
$body = json_decode($guzzleResponse->getBody()); |
129
|
4 |
|
$response = response()->json($body); |
|
|
|
|
130
|
4 |
|
$response->setStatusCode($guzzleResponse->getStatusCode()); |
131
|
|
|
|
132
|
4 |
|
$headers = $guzzleResponse->getHeaders(); |
133
|
4 |
|
foreach ($headers as $headerType => $headerValue) { |
134
|
2 |
|
$response->header($headerType, $headerValue); |
135
|
4 |
|
} |
136
|
|
|
|
137
|
4 |
|
return $response; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: