1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class AuthController extends PageController { |
|
|
|
|
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Page to show if user hasn't logged it but hit by auth filter |
7
|
|
|
*/ |
8
|
|
|
public function loginPage() { |
9
|
|
|
$this->layout->title = 'Login'; |
10
|
|
|
$this->layout->content = View::make('login'); |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Login with persona |
15
|
|
|
* |
16
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
17
|
|
|
*/ |
18
|
|
|
public function login() { |
19
|
|
|
/* Force logout */ |
20
|
|
|
if (! Auth::guest()) { |
21
|
|
|
Auth::logout(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
if (! $assertion = Input::get("assertion")) { |
25
|
|
|
App::abort(401, 'i don\'t even'); |
26
|
|
|
} |
27
|
|
|
// Check with persona |
28
|
|
|
try { |
29
|
|
|
$response = GuzzleHttp\post( |
30
|
|
|
'https://verifier.login.persona.org/verify', |
31
|
|
|
[ |
32
|
|
|
'body' => [ |
33
|
|
|
'assertion' => $assertion, |
34
|
|
|
'audience' => url(), |
35
|
|
|
], |
36
|
|
|
] |
37
|
|
|
); |
38
|
|
|
} catch (GuzzleHttp\Exception\BadResponseException $e) { |
39
|
|
|
App::abort(401, 'You are not authorized.'); |
40
|
|
|
|
41
|
|
|
return false; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$verification = $response->json(); |
45
|
|
|
if ($verification['status'] != "okay") { |
46
|
|
|
App::abort(401, 'You are not authorized.'); |
47
|
|
|
|
48
|
|
|
return false; |
49
|
|
|
} |
50
|
|
|
$email = $verification['email']; |
51
|
|
|
|
52
|
|
|
// Log in to laravel |
53
|
|
|
if (Auth::attempt(['email' => $email, 'password' => 'moz:persona'], true)) { |
54
|
|
|
if (Request::ajax()) { |
55
|
|
|
if(Session::has('url.intended')) { |
56
|
|
|
return Response::json(['redirect' => Session::get('url.intended')]); |
57
|
|
|
} else { |
58
|
|
|
return Response::json(['refresh' => true]); |
59
|
|
|
} |
60
|
|
|
} else { |
61
|
|
|
return Redirect::intended(); |
62
|
|
|
} |
63
|
|
|
} else { |
64
|
|
|
// Not an user |
65
|
|
|
Session::put('register_email', $email); |
66
|
|
|
if (Request::ajax()) { |
67
|
|
|
return Response::json(['redirect' => route('auth.register')]); |
68
|
|
|
} else { |
69
|
|
|
return Redirect::route('auth.register.form'); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Logout |
77
|
|
|
* |
78
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
79
|
|
|
*/ |
80
|
|
|
public function logout() { |
81
|
|
|
if (Session::has("register_email")) { |
82
|
|
|
Session::forget("register_email"); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
Auth::logout(); |
86
|
|
|
|
87
|
|
|
if (Request::ajax()) { |
88
|
|
|
return Response::json(["redirect" => url("/")]); |
89
|
|
|
} else { |
90
|
|
|
return Redirect::to("/"); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Registration validation rules |
96
|
|
|
* @var array |
97
|
|
|
*/ |
98
|
|
|
public $register_valid_rules = [ |
99
|
|
|
'username' => ['required', 'unique:users', 'min:2', 'max:16'], |
100
|
|
|
'displayname' => ['max:64'], |
101
|
|
|
]; |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Registration form |
105
|
|
|
* |
106
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View |
107
|
|
|
*/ |
108
|
|
|
public function registerForm() { |
109
|
|
|
if (! Session::has('register_email')) { |
110
|
|
|
return Redirect::home(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$this->layout->content = View::make("register", [ |
114
|
|
|
'validation_rules' => $this->register_valid_rules, |
115
|
|
|
'email' => Session::get('register_email'), |
116
|
|
|
]); |
117
|
|
|
|
118
|
|
|
return $this->layout; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Registration handler |
123
|
|
|
* |
124
|
|
|
* @return $this|\Illuminate\Http\RedirectResponse |
125
|
|
|
*/ |
126
|
|
|
public function register() { |
127
|
|
|
if (! Session::has('register_email')) { |
128
|
|
|
return Redirect::home(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
// Validate |
132
|
|
|
$validator = Validator::make(Input::all(), $this->register_valid_rules); |
133
|
|
|
if ($validator->fails()) { |
134
|
|
|
Notification::error("Something's wrong, check the fields bellow!"); |
135
|
|
|
|
136
|
|
|
return Redirect::back()->withInput()->withErrors($validator); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
// Create user |
140
|
|
|
$user = new User(); |
141
|
|
|
$user->username = Input::get('username'); |
142
|
|
|
$user->displayname = Input::get('displayname'); |
143
|
|
|
$user->email = Session::get('register_email'); |
144
|
|
|
|
145
|
|
|
// Save & Login |
146
|
|
|
if ($user->save()) { |
147
|
|
|
Auth::login($user, true); |
148
|
|
|
Session::forget('register_email'); |
149
|
|
|
|
150
|
|
|
Notification::success("Welcome {$user->name}!"); |
151
|
|
|
|
152
|
|
|
return Redirect::intended('/'); |
153
|
|
|
} else { |
154
|
|
|
Notification::error("Save errors :'("); |
155
|
|
|
|
156
|
|
|
return Redirect::to('register')->withInput(); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.