1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace BristolSU\Service\Typeform\Connectors; |
4
|
|
|
|
5
|
|
|
use BristolSU\Service\Typeform\Fields\TypeformAuthCode; |
6
|
|
|
use BristolSU\Service\Typeform\Models\TypeformAuthCode as AuthModel; |
7
|
|
|
use BristolSU\Support\Connection\Contracts\Connector; |
8
|
|
|
use Carbon\Carbon; |
9
|
|
|
use FormSchema\Generator\Field; |
10
|
|
|
use FormSchema\Schema\Form; |
11
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
12
|
|
|
|
13
|
|
|
class OAuth extends Connector |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
|
|
|
|
17
|
|
|
* @inheritDoc |
18
|
|
|
*/ |
|
|
|
|
19
|
6 |
|
public function request($method, $uri, array $options = []) |
20
|
|
|
{ |
21
|
6 |
|
$options['base_uri'] = config('typeform_service.base_uri'); |
22
|
6 |
|
$headers = ((isset($options['headers']) && is_array($options['headers']))?$options['headers']:[]); |
23
|
6 |
|
$headers['Authorization'] = 'Bearer ' . $this->getAccessToken(); |
24
|
5 |
|
$options['headers'] = $headers; |
25
|
5 |
|
return $this->client->request($method, $uri, $options); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
|
|
|
|
29
|
|
|
* @inheritDoc |
30
|
|
|
*/ |
|
|
|
|
31
|
2 |
|
public function test(): bool |
32
|
|
|
{ |
33
|
|
|
try { |
34
|
2 |
|
$this->request('get', '/me'); |
35
|
1 |
|
return true; |
36
|
1 |
|
} catch (GuzzleException $e) { |
37
|
1 |
|
return false; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
|
|
|
|
42
|
|
|
* @inheritDoc |
43
|
|
|
*/ |
|
|
|
|
44
|
1 |
|
static public function settingsSchema(): Form |
45
|
|
|
{ |
46
|
1 |
|
return \FormSchema\Generator\Form::make()->withField( |
47
|
1 |
|
Field::make(TypeformAuthCode::class, 'auth_code_id')->label('Log Into Typeform')->required(true) |
48
|
1 |
|
)->getSchema(); |
49
|
|
|
} |
50
|
|
|
|
51
|
6 |
|
private function getAccessToken($refreshable = true): string |
|
|
|
|
52
|
|
|
{ |
53
|
6 |
|
$authCode = AuthModel::findOrFail($this->getSetting('auth_code_id')); |
54
|
6 |
|
if($authCode->isValid()) { |
|
|
|
|
55
|
5 |
|
return $authCode->auth_code; |
56
|
|
|
} |
57
|
2 |
|
if($refreshable && $this->refreshAccessToken($authCode)) { |
|
|
|
|
58
|
1 |
|
return $this->getAccessToken(false); |
59
|
|
|
} else { |
60
|
|
|
// TODO Throw special error to send email to people |
61
|
1 |
|
throw new \Exception('Access token could not be refreshed'); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
private function refreshAccessToken(AuthModel $authCode) |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
try { |
68
|
2 |
|
$response = $this->client->request('post', config('typeform_service.urlAccessToken'), [ |
|
|
|
|
69
|
|
|
'form_params' => [ |
70
|
2 |
|
'grant_type' => 'refresh_token', |
71
|
2 |
|
'refresh_token' => $authCode->refresh_token, |
72
|
2 |
|
'client_id' => config('typeform_service.client_id'), |
73
|
2 |
|
'client_secret' => config('typeform_service.client_secret'), |
74
|
2 |
|
'scope' => 'offline accounts:read responses:read webhooks:read webhooks:write forms:read' |
75
|
|
|
], |
76
|
|
|
]); |
|
|
|
|
77
|
1 |
|
} catch (GuzzleException $e) { |
78
|
1 |
|
return false; |
79
|
|
|
} |
80
|
1 |
|
$token = json_decode($response->getBody()->getContents(), true); |
81
|
1 |
|
$authCode->auth_code = $token['access_token']; |
82
|
1 |
|
$authCode->refresh_token = $token['refresh_token']; |
83
|
1 |
|
$authCode->expires_at = Carbon::now()->addSeconds($token['expires_in']); |
84
|
1 |
|
return $authCode->save(); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|