-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogin.php
More file actions
171 lines (149 loc) · 4.78 KB
/
Login.php
File metadata and controls
171 lines (149 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
namespace Neuron\Cms\Controllers\Auth;
use Neuron\Cms\Auth\SessionManager;
use Neuron\Cms\Controllers\Content;
use Neuron\Cms\Enums\FlashMessageType;
use Neuron\Cms\Services\Auth\IAuthenticationService;
use Neuron\Core\Exceptions\NotFound;
use Neuron\Data\Settings\SettingManager;
use Neuron\Mvc\IMvcApplication;
use Neuron\Mvc\Responses\HttpResponseStatus;
use Neuron\Mvc\Requests\Request;
use Neuron\Routing\Attributes\Get;
use Neuron\Routing\Attributes\Post;
/**
* Login controller.
*
* Handles user authentication (login/logout).
*
* @package Neuron\Cms\Controllers\Auth
*/
class Login extends Content
{
private IAuthenticationService $_authentication;
/**
* @param IMvcApplication $app
* @param IAuthenticationService $authentication
* @param SettingManager $settings
* @param SessionManager $sessionManager
* @throws \Exception
*/
public function __construct(
IMvcApplication $app,
IAuthenticationService $authentication,
SettingManager $settings,
SessionManager $sessionManager
)
{
parent::__construct( $app, $settings, $sessionManager );
$this->_authentication = $authentication;
}
/**
* Show login form
*
* @param Request $request
* @return string
* @throws NotFound
*/
#[Get('/login', name: 'login')]
public function showLoginForm( Request $request ): string
{
// If already logged in, redirect to the dashboard
if( $this->_authentication->check() )
{
$this->redirect( 'admin_dashboard' );
}
$this->initializeCsrfToken();
// Get redirect parameter from URL or default to admin dashboard
$defaultRedirect = $this->urlFor( 'admin_dashboard', [], '/admin/dashboard' ) ?? '/admin/dashboard';
$requestedRedirect = $request->get( 'redirect', $defaultRedirect ) ?? $defaultRedirect;
// Validate and use requested redirect, fallback to default if invalid
$redirectUrl = $this->isValidRedirectUrl( $requestedRedirect )
? $requestedRedirect
: $defaultRedirect;
return $this->view()
->title( 'Login' )
->description( 'Login to ' . $this->getName() )
->withCsrfToken()
->with( FlashMessageType::ERROR->viewKey(), $this->getSessionManager()->getFlash( FlashMessageType::ERROR->value ) )
->with( FlashMessageType::SUCCESS->viewKey(), $this->getSessionManager()->getFlash( FlashMessageType::SUCCESS->value ) )
->with( 'RedirectUrl', $redirectUrl )
->render( 'login', 'auth' );
}
/**
* Process login
*
* @param Request $request
* @return never
*/
#[Post('/login', name: 'login_post', filters: ['csrf'])]
public function login( Request $request ): never
{
// Create and validate DTO
$dto = $this->createDto( 'auth/login-request.yaml' );
$this->mapRequestToDto( $dto, $request );
// Validate DTO
if( !$dto->validate() )
{
$errors = implode( ', ', $dto->getErrors() );
$this->redirect( 'login', [], [FlashMessageType::ERROR->value, $errors] );
}
// Attempt authentication
if( !$this->_authentication->attempt( $dto->username, $dto->password, $dto->remember ?? false ) )
{
$this->redirect( 'login', [], [FlashMessageType::ERROR->value, 'Invalid username or password.'] );
}
// Successful login - redirect to intended URL or dashboard
$defaultRedirect = $this->urlFor( 'admin_dashboard', [], '/admin/dashboard' ) ?? '/admin/dashboard';
$requestedRedirect = $dto->redirect_url ?? $defaultRedirect;
// Validate and use requested redirect, fallback to default if invalid
$redirectUrl = $this->isValidRedirectUrl( $requestedRedirect )
? $requestedRedirect
: $defaultRedirect;
$this->redirectToUrl( $redirectUrl, [ FlashMessageType::SUCCESS->value, 'Welcome back!' ] );
}
/**
* Process logout
* @param Request $request
* @return never
*/
#[Post('/logout', name: 'logout', filters: ['auth', 'csrf'])]
public function logout( Request $request ): never
{
$this->_authentication->logout();
$this->redirect( 'home', [], [FlashMessageType::SUCCESS->value, 'You have been logged out successfully.'] );
}
/**
* Validate if a redirect URL is safe to use.
* Only allows relative URLs (starting with /) to prevent open redirect vulnerabilities.
*
* @param string $url The URL to validate
* @return bool True if the URL is safe, false otherwise
*/
private function isValidRedirectUrl( string $url ): bool
{
// Empty URLs are not valid
if( $url === '' )
{
return false;
}
// Only allow relative URLs that start with /
if( $url[0] !== '/' )
{
return false;
}
// Prevent protocol-relative URLs (//example.com)
if( strlen( $url ) > 1 && $url[1] === '/' )
{
return false;
}
// Check for malicious patterns
// Prevent URLs with @ symbol (could be used for phishing: /path@evil.com)
// Prevent URLs with backslashes (could bypass filters: /\evil.com)
if( str_contains( $url, '@' ) || str_contains( $url, '\\' ) )
{
return false;
}
return true;
}
}