1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace STS\SocialiteAuth; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Contracts\Auth\Authenticatable; |
7
|
|
|
use Laravel\Socialite\Contracts\User; |
8
|
|
|
|
9
|
|
|
class SocialiteAuth |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
protected $config; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var \Closure |
18
|
|
|
*/ |
19
|
|
|
protected $verifyUser = null; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \Closure |
23
|
|
|
*/ |
24
|
|
|
protected $handleNewUser = null; |
25
|
|
|
|
26
|
|
|
public function __construct(array $config) |
27
|
|
|
{ |
28
|
|
|
$this->config = $config; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Provide custom logic for verifying a user before login. |
33
|
|
|
* |
34
|
|
|
* @param \Closure |
35
|
|
|
*/ |
36
|
|
|
public function beforeLogin(Closure $beforeLogin) |
37
|
|
|
{ |
38
|
|
|
$this->verifyUser = $beforeLogin; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Provide custom handler for setting up a new user. |
43
|
|
|
* |
44
|
|
|
* @param Closure $handleNewUser |
45
|
|
|
*/ |
46
|
|
|
public function newUser(Closure $handleNewUser) |
47
|
|
|
{ |
48
|
|
|
$this->handleNewUser = $handleNewUser; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Verify that the user meets login requirements |
53
|
|
|
* according to the custom logic provided. |
54
|
|
|
* |
55
|
|
|
* @param Authenticatable |
56
|
|
|
* |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
public function verifyBeforeLogin(Authenticatable $user): bool |
60
|
|
|
{ |
61
|
|
|
return $this->verifyUser |
62
|
|
|
? call_user_func($this->verifyUser, $user) |
63
|
|
|
: true; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param $user |
68
|
|
|
* |
69
|
|
|
* @return bool|Authenticatable |
70
|
|
|
*/ |
71
|
|
|
public function handleNewUser(User $user) |
72
|
|
|
{ |
73
|
|
|
if ($this->handleNewUser) { |
74
|
|
|
return call_user_func($this->handleNewUser, $user); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if ($this->config['match'] == false) { |
78
|
|
|
$user = new Identity($user); |
79
|
|
|
session()->put($this->sessionKeyFor($user->getAuthIdentifier()), $user); |
80
|
|
|
|
81
|
|
|
return $user; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function sessionKeyFor($identifier) |
86
|
|
|
{ |
87
|
|
|
return 'socialite-auth.' . str_replace('.','-',$identifier); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|