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