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\Events\CasUserLoginEvent; |
13
|
|
|
use Leo108\CAS\Events\CasUserLogoutEvent; |
14
|
|
|
use Leo108\CAS\Exceptions\CAS\CasException; |
15
|
|
|
use Illuminate\Http\Request; |
16
|
|
|
use Leo108\CAS\Repositories\ServiceRepository; |
17
|
|
|
use Leo108\CAS\Repositories\TicketRepository; |
18
|
|
|
|
19
|
|
|
class SecurityController extends Controller |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var ServiceRepository |
23
|
|
|
*/ |
24
|
|
|
protected $serviceRepository; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var TicketRepository |
28
|
|
|
*/ |
29
|
|
|
protected $ticketRepository; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var UserLogin |
33
|
|
|
*/ |
34
|
|
|
protected $loginInteraction; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* SecurityController constructor. |
38
|
|
|
* @param ServiceRepository $serviceRepository |
39
|
|
|
* @param TicketRepository $ticketRepository |
40
|
|
|
* @param UserLogin $loginInteraction |
41
|
|
|
*/ |
42
|
4 |
|
public function __construct( |
43
|
|
|
ServiceRepository $serviceRepository, |
44
|
|
|
TicketRepository $ticketRepository, |
45
|
|
|
UserLogin $loginInteraction |
46
|
|
|
) { |
47
|
4 |
|
$this->serviceRepository = $serviceRepository; |
48
|
4 |
|
$this->ticketRepository = $ticketRepository; |
49
|
4 |
|
$this->loginInteraction = $loginInteraction; |
50
|
4 |
|
} |
51
|
|
|
|
52
|
1 |
|
public function showLogin(Request $request) |
53
|
|
|
{ |
54
|
1 |
|
$service = $request->get('service', ''); |
55
|
1 |
|
$errors = []; |
56
|
1 |
|
if (!empty($service)) { |
57
|
|
|
//service not found in white list |
58
|
1 |
|
if (!$this->serviceRepository->isUrlValid($service)) { |
59
|
1 |
|
$errors[] = (new CasException(CasException::INVALID_SERVICE))->getCasMsg(); |
60
|
1 |
|
} |
61
|
1 |
|
} |
62
|
|
|
|
63
|
1 |
|
$user = $this->loginInteraction->getCurrentUser($request); |
64
|
|
|
//user already has sso session |
65
|
1 |
|
if ($user) { |
66
|
|
|
//has errors, should not be redirected to target url |
67
|
1 |
|
if (!empty($errors)) { |
68
|
|
|
return $this->loginInteraction->redirectToHome($errors); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
//must not be transparent |
72
|
1 |
|
if ($request->get('warn') === 'true' && !empty($service)) { |
73
|
|
|
$query = $request->query->all(); |
74
|
|
|
unset($query['warn']); |
75
|
|
|
$url = cas_route('login_page', $query); |
76
|
|
|
|
77
|
|
|
return $this->loginInteraction->showLoginWarnPage($request, $url, $service); |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
return $this->authenticated($request); |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
return $this->loginInteraction->showLoginPage($request, $errors); |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
public function login(Request $request) |
87
|
|
|
{ |
88
|
1 |
|
return $this->loginInteraction->login($request, array($this, 'authenticated')); |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
public function authenticated(Request $request) |
92
|
|
|
{ |
93
|
1 |
|
$user = $this->loginInteraction->getCurrentUser($request); |
94
|
1 |
|
event(new CasUserLoginEvent($request, $user)); |
|
|
|
|
95
|
1 |
|
$serviceUrl = $request->get('service', ''); |
96
|
1 |
|
if (!empty($serviceUrl)) { |
97
|
1 |
|
$query = parse_url($serviceUrl, PHP_URL_QUERY); |
98
|
|
|
try { |
99
|
1 |
|
$ticket = $this->ticketRepository->applyTicket($user, $serviceUrl); |
|
|
|
|
100
|
1 |
|
} catch (CasException $e) { |
101
|
1 |
|
return $this->loginInteraction->redirectToHome([$e->getCasMsg()]); |
102
|
|
|
} |
103
|
1 |
|
$finalUrl = $serviceUrl.($query ? '&' : '?').'ticket='.$ticket->ticket; |
|
|
|
|
104
|
|
|
|
105
|
1 |
|
return redirect($finalUrl); |
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
return $this->loginInteraction->redirectToHome(); |
109
|
|
|
} |
110
|
|
|
|
111
|
1 |
|
public function logout(Request $request) |
112
|
|
|
{ |
113
|
1 |
|
return $this->loginInteraction->logout( |
114
|
1 |
|
$request, |
115
|
1 |
|
function (Request $request) { |
116
|
1 |
|
event(new CasUserLogoutEvent($request, $this->loginInteraction->getCurrentUser($request))); |
|
|
|
|
117
|
1 |
|
} |
118
|
1 |
|
); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: