1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* To change this license header, choose License Headers in Project Properties. |
5
|
|
|
* To change this template file, choose Tools | Templates |
6
|
|
|
* and open the template in the editor. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace OBRSDK\HttpClient; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Description of OAuth2Cliente |
13
|
|
|
* |
14
|
|
|
* @author Antonio |
15
|
|
|
*/ |
16
|
|
|
class OAuth2Cliente extends Nucleo\Instancia { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Testa se o atual accesstoken esta autenticando com sucesso na api |
20
|
|
|
*/ |
21
|
|
|
public function testarAutenticacao() { |
22
|
|
|
$this->apiCliente->addAuthorization()->get('auth/ping')->getRespostaObject(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Faz autenticacao na API com as suas credenciais de 'appId' e 'appSecret' |
27
|
|
|
* Neste caso você consegue acessar seus dados |
28
|
|
|
* |
29
|
|
|
* @throws \OBRSDK\Exceptions\RespostaException |
30
|
|
|
* @throws \OBRSDK\Exceptions\AutenticacaoException |
31
|
|
|
*/ |
32
|
|
|
public function autenticarComCredenciais() { |
33
|
|
|
$this->oauth([ |
34
|
|
|
'grant_type' => 'client_credentials', |
35
|
|
|
'scope' => '*' |
36
|
|
|
]); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Faz autenticacao na API em nome de outro usuário utilizando um codigo de |
41
|
|
|
* autorização que esse usuário permite em ser utilizado por você |
42
|
|
|
* |
43
|
|
|
* @param string $code |
44
|
|
|
* @return mixed |
45
|
|
|
* @throws OBRSDK\Exceptions\AutorizationException |
46
|
|
|
* @throws \OBRSDK\Exceptions\RespostaException |
47
|
|
|
*/ |
48
|
|
|
public function autenticarComAutorizacao($code = null) { |
49
|
|
|
$error = filter_input(INPUT_GET, 'error'); |
50
|
|
|
if ($error != null && $error != '') { |
51
|
|
|
throw new \OBRSDK\Exceptions\AutorizationException($error, filter_input(INPUT_GET, 'error_description')); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$authorizationCode = $code === null ? filter_input(INPUT_GET, 'code') : $code; |
55
|
|
|
$this->oauth([ |
56
|
|
|
'grant_type' => 'authorization_code', |
57
|
|
|
'code' => $authorizationCode |
58
|
|
|
]); |
59
|
|
|
|
60
|
|
|
return filter_input(INPUT_GET, 'state'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Se um access_token com permissoes de um usuário foi expirado e você tiver |
65
|
|
|
* um reflesh token do mesmo, você pode resolicitar uma nova autenticacao |
66
|
|
|
* utilizando o reflesh token |
67
|
|
|
* |
68
|
|
|
* @param string $refreshToken |
69
|
|
|
* @throws \OBRSDK\Exceptions\RespostaException |
70
|
|
|
* @throws \OBRSDK\Exceptions\AutenticacaoException |
71
|
|
|
* @return \OBRSDK\ObjetoBoletosRegistrados |
72
|
|
|
*/ |
73
|
|
|
public function autenticarComRefreshToken($refreshToken) { |
74
|
|
|
$this->oauth([ |
75
|
|
|
'grant_type' => 'refresh_token', |
76
|
|
|
'refresh_token' => $refreshToken |
77
|
|
|
]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* |
82
|
|
|
* @param array $config |
83
|
|
|
* @return \OBRSDK\ObjetoBoletosRegistrados |
84
|
|
|
* @throws \OBRSDK\Exceptions\AutenticacaoException |
85
|
|
|
* @throws \OBRSDK\Exceptions\RespostaException |
86
|
|
|
*/ |
87
|
|
|
private function oauth(array $config) { |
88
|
|
|
try { |
89
|
|
|
$resposta = $this->apiCliente |
90
|
|
|
->postParam('auth/token', $this->getParametroOauthToken($config)) |
91
|
|
|
->getRespostaArray(); |
92
|
|
|
|
93
|
|
|
$this->getInstancia()->setObjAccessToken($this->entidadeAccessToken($resposta)); |
94
|
|
|
} catch (\OBRSDK\Exceptions\RespostaException $ex) { |
95
|
|
|
if (isset($ex->getError()->OAuth2->erro_description)) { |
96
|
|
|
throw new \OBRSDK\Exceptions\AutenticacaoException($ex); |
97
|
|
|
} else { |
98
|
|
|
throw $ex; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* |
105
|
|
|
* @param array $config |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
|
private function getParametroOauthToken(array $config) { |
109
|
|
|
return array_merge($config, [ |
110
|
|
|
'client_id' => $this->getInstancia()->getAppId(), |
111
|
|
|
'client_secret' => $this->getInstancia()->getAppSecret() |
112
|
|
|
]); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* |
117
|
|
|
* @param array $respostaOauth |
118
|
|
|
* @return \OBRSDK\Entidades\AccessToken |
119
|
|
|
*/ |
120
|
|
|
private function entidadeAccessToken(array $respostaOauth) { |
121
|
|
|
if (!isset($respostaOauth['refresh_token'])) { |
122
|
|
|
$respostaOauth['refresh_token'] = null; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$respostaOauth['data_token'] = time(); |
126
|
|
|
|
127
|
|
|
$entidadeAccessToken = new \OBRSDK\Entidades\AccessToken(); |
128
|
|
|
$entidadeAccessToken->setAtributos($respostaOauth); |
129
|
|
|
|
130
|
|
|
return $entidadeAccessToken; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
} |
134
|
|
|
|