1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: chenyihong |
5
|
|
|
* Date: 16/8/1 |
6
|
|
|
* Time: 14:50 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Leo108\CAS\Http\Controllers; |
10
|
|
|
|
11
|
|
|
use Leo108\CAS\Contracts\Interactions\UserLogin; |
12
|
|
|
use Leo108\CAS\Contracts\Models\UserModel; |
13
|
|
|
use Leo108\CAS\Events\CasUserLoginEvent; |
14
|
|
|
use Leo108\CAS\Events\CasUserLogoutEvent; |
15
|
|
|
use Leo108\CAS\Exceptions\CAS\CasException; |
16
|
|
|
use Illuminate\Http\Request; |
17
|
|
|
use Leo108\CAS\Repositories\PGTicketRepository; |
18
|
|
|
use Leo108\CAS\Repositories\ServiceRepository; |
19
|
|
|
use Leo108\CAS\Repositories\TicketRepository; |
20
|
|
|
use function Leo108\CAS\cas_route; |
21
|
|
|
|
22
|
|
|
class SecurityController extends Controller |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var ServiceRepository |
26
|
|
|
*/ |
27
|
|
|
protected $serviceRepository; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var TicketRepository |
31
|
|
|
*/ |
32
|
|
|
protected $ticketRepository; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var PGTicketRepository |
36
|
|
|
*/ |
37
|
|
|
protected $pgTicketRepository; |
38
|
|
|
/** |
39
|
|
|
* @var UserLogin |
40
|
|
|
*/ |
41
|
|
|
protected $loginInteraction; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* SecurityController constructor. |
45
|
|
|
* @param ServiceRepository $serviceRepository |
46
|
|
|
* @param TicketRepository $ticketRepository |
47
|
|
|
* @param PGTicketRepository $pgTicketRepository |
48
|
|
|
* @param UserLogin $loginInteraction |
49
|
|
|
*/ |
50
|
11 |
|
public function __construct( |
51
|
|
|
ServiceRepository $serviceRepository, |
52
|
|
|
TicketRepository $ticketRepository, |
53
|
|
|
PGTicketRepository $pgTicketRepository, |
54
|
|
|
UserLogin $loginInteraction |
55
|
|
|
) { |
56
|
11 |
|
$this->serviceRepository = $serviceRepository; |
57
|
11 |
|
$this->ticketRepository = $ticketRepository; |
58
|
11 |
|
$this->loginInteraction = $loginInteraction; |
59
|
11 |
|
$this->pgTicketRepository = $pgTicketRepository; |
60
|
11 |
|
} |
61
|
|
|
|
62
|
5 |
|
public function showLogin(Request $request) |
63
|
|
|
{ |
64
|
5 |
|
$service = $request->get('service', ''); |
65
|
5 |
|
$errors = []; |
66
|
5 |
|
if (!empty($service)) { |
67
|
|
|
//service not found in white list |
68
|
5 |
|
if (!$this->serviceRepository->isUrlValid($service)) { |
69
|
2 |
|
$errors[] = (new CasException(CasException::INVALID_SERVICE))->getCasMsg(); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
5 |
|
$user = $this->loginInteraction->getCurrentUser($request); |
74
|
|
|
//user already has sso session |
75
|
5 |
|
if ($user) { |
76
|
|
|
//has errors, should not be redirected to target url |
77
|
3 |
|
if (!empty($errors)) { |
78
|
1 |
|
return $this->loginInteraction->redirectToHome($errors); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
//must not be transparent |
82
|
2 |
|
if ($request->get('warn') === 'true' && !empty($service)) { |
83
|
1 |
|
$query = $request->query->all(); |
84
|
1 |
|
unset($query['warn']); |
85
|
1 |
|
$url = cas_route('login_page', $query); |
86
|
|
|
|
87
|
1 |
|
return $this->loginInteraction->showLoginWarnPage($request, $url, $service); |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
return $this->authenticated($request, $user); |
91
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
2 |
|
return $this->loginInteraction->showLoginPage($request, $errors); |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
public function login(Request $request) |
98
|
|
|
{ |
99
|
1 |
|
$user = $this->loginInteraction->login($request); |
100
|
1 |
|
if (is_null($user)) { |
101
|
1 |
|
return $this->loginInteraction->showAuthenticateFailed($request); |
102
|
|
|
} |
103
|
|
|
|
104
|
1 |
|
return $this->authenticated($request, $user); |
105
|
|
|
} |
106
|
|
|
|
107
|
2 |
|
public function authenticated(Request $request, UserModel $user) |
108
|
|
|
{ |
109
|
2 |
|
event(new CasUserLoginEvent($request, $user)); |
110
|
2 |
|
$serviceUrl = $request->get('service', ''); |
111
|
2 |
|
if (!empty($serviceUrl)) { |
112
|
1 |
|
$query = parse_url($serviceUrl, PHP_URL_QUERY); |
113
|
|
|
try { |
114
|
1 |
|
$ticket = $this->ticketRepository->applyTicket($user, $serviceUrl); |
115
|
1 |
|
} catch (CasException $e) { |
116
|
1 |
|
return $this->loginInteraction->redirectToHome([$e->getCasMsg()]); |
117
|
|
|
} |
118
|
1 |
|
$finalUrl = $serviceUrl.($query ? '&' : '?').'ticket='.$ticket->ticket; |
119
|
|
|
|
120
|
1 |
|
return redirect($finalUrl); |
121
|
|
|
} |
122
|
|
|
|
123
|
1 |
|
return $this->loginInteraction->redirectToHome(); |
124
|
|
|
} |
125
|
|
|
|
126
|
3 |
|
public function logout(Request $request) |
127
|
|
|
{ |
128
|
3 |
|
$user = $this->loginInteraction->getCurrentUser($request); |
129
|
3 |
|
if ($user) { |
130
|
2 |
|
$this->loginInteraction->logout($request); |
131
|
2 |
|
$this->pgTicketRepository->invalidTicketByUser($user); |
132
|
2 |
|
event(new CasUserLogoutEvent($request, $user)); |
133
|
|
|
} |
134
|
3 |
|
$service = $request->get('service'); |
135
|
3 |
|
if ($service && $this->serviceRepository->isUrlValid($service)) { |
136
|
1 |
|
return redirect($service); |
137
|
|
|
} |
138
|
|
|
|
139
|
2 |
|
return $this->loginInteraction->showLoggedOut($request); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|