1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace STS\StorageConnect\Drivers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Session\Store; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use SocialiteProviders\Manager\OAuth2\User; |
9
|
|
|
use StorageConnect; |
10
|
|
|
|
11
|
|
|
trait OAuthBehavior |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var User |
15
|
|
|
*/ |
16
|
|
|
protected $user; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $token; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* EnhancedProvider constructor. |
25
|
|
|
* |
26
|
|
|
* @param array $config |
27
|
|
|
*/ |
28
|
|
|
public function __construct(array $config) |
29
|
|
|
{ |
30
|
|
|
parent::__construct( |
31
|
|
|
app('request'), $config['client_id'], |
32
|
|
|
$config['client_secret'], $this->callbackUrl($config, app('request')), |
33
|
|
|
Arr::get($config, 'guzzle', []) |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
protected function callbackUrl($config, $request) |
41
|
|
|
{ |
42
|
|
|
return sprintf("https://%s/%s/callback/%s", |
43
|
|
|
Arr::get($config, 'callback_domain') ?: config('storage-connect.callback_domain') ?: $request->getHost(), |
44
|
|
|
config('storage-connect.path'), |
45
|
|
|
$this->name() |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return Store |
51
|
|
|
*/ |
52
|
|
|
public function session() |
53
|
|
|
{ |
54
|
|
|
return $this->request->session(); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
protected function getState() |
61
|
|
|
{ |
62
|
|
|
return base64_encode(json_encode(array_merge( |
63
|
|
|
['csrf' => Str::random(40)], |
64
|
|
|
StorageConnect::getState() |
65
|
|
|
))); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return User |
70
|
|
|
*/ |
71
|
|
|
public function user() |
72
|
|
|
{ |
73
|
|
|
if (!$this->user) { |
74
|
|
|
$this->user = parent::user(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this->user; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function name() |
84
|
|
|
{ |
85
|
|
|
return strtolower(self::IDENTIFIER); |
86
|
|
|
} |
87
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: