-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
This reverts commit a0eb603.
- Loading branch information
Showing
16 changed files
with
1,355 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Binaryk\LaravelRestify\Http\Controllers; | ||
|
||
use Binaryk\LaravelRestify\Services\AuthService; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Routing\Controller; | ||
|
||
class AuthController extends Controller | ||
{ | ||
private AuthService $authService; | ||
|
||
public function __construct(AuthService $authService) | ||
{ | ||
$this->authService = $authService; | ||
} | ||
|
||
public function login(Request $request) | ||
{ | ||
return $this->authService->login($request); | ||
} | ||
|
||
public function register(Request $request) | ||
{ | ||
return $this->authService->register($request); | ||
} | ||
|
||
public function verify(Request $request, $id, $hash = null) | ||
{ | ||
return $this->authService->verify($request, $id, $hash); | ||
} | ||
|
||
public function forgotPassword(Request $request) | ||
{ | ||
return $this->authService->forgotPassword($request); | ||
} | ||
|
||
public function resetPassword(Request $request) | ||
{ | ||
return $this->authService->resetPassword($request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
|
||
namespace Binaryk\LaravelRestify\Services; | ||
|
||
use Binaryk\LaravelRestify\Contracts\Sanctumable; | ||
use Binaryk\LaravelRestify\Exceptions\Eloquent\EntityNotFoundException; | ||
use Binaryk\LaravelRestify\Exceptions\SanctumUserException; | ||
use Illuminate\Auth\Access\AuthorizationException; | ||
use Illuminate\Auth\Events\Verified; | ||
use Illuminate\Container\Container; | ||
use Illuminate\Contracts\Auth\Authenticatable; | ||
use Illuminate\Contracts\Auth\MustVerifyEmail; | ||
use Illuminate\Contracts\Auth\PasswordBroker; | ||
use Illuminate\Contracts\Container\BindingResolutionException; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Config; | ||
use Illuminate\Support\Facades\Password; | ||
use ReflectionException; | ||
|
||
class AuthService | ||
{ | ||
public function login(Request $request) | ||
{ | ||
if (config('restify.auth.provider') !== 'sanctum') { | ||
throw SanctumUserException::wrongConfiguration(); | ||
} | ||
|
||
$token = LoginService::make($request); | ||
|
||
return $token; | ||
} | ||
|
||
public function register(Request $request) | ||
{ | ||
return RegisterService::make($request, $this); | ||
} | ||
|
||
public function forgotPassword(Request $request, string $url = null) | ||
{ | ||
return ForgotPasswordService::make($request, $url); | ||
} | ||
|
||
/* | ||
* @param $id | ||
* @param null $hash | ||
* @return Builder|Builder[]|\Illuminate\Database\Eloquent\Collection|Model|null | ||
* @throws AuthorizationException | ||
* @throws EntityNotFoundException | ||
*/ | ||
public function verify(Request $request, $id, $hash = null) | ||
{ | ||
/** | ||
* @var Authenticatable | ||
*/ | ||
$user = $this->userQuery()->query()->findOrFail($id); | ||
|
||
if ($user instanceof Sanctumable && ! hash_equals((string) $hash, sha1($user->getEmailForVerification()))) { | ||
throw new AuthorizationException('Invalid hash'); | ||
} | ||
|
||
if ($user instanceof MustVerifyEmail && $user->markEmailAsVerified()) { | ||
event(new Verified($user)); | ||
} | ||
|
||
return $user; | ||
} | ||
|
||
public function resetPassword(Request $request) | ||
{ | ||
return ResetPasswordService::make($request, $this); | ||
} | ||
|
||
/** | ||
* @return PasswordBroker | ||
*/ | ||
public function broker() | ||
{ | ||
return Password::broker(); | ||
} | ||
|
||
/** | ||
* Returns query for User model and validate if it exists. | ||
* | ||
* @return Model | ||
* @throws SanctumUserException | ||
* @throws EntityNotFoundException | ||
*/ | ||
public function userQuery() | ||
{ | ||
$userClass = Config::get('auth.providers.users.model'); | ||
|
||
try { | ||
$container = Container::getInstance(); | ||
$userInstance = $container->make($userClass); | ||
$this->validateUserModel($userInstance); | ||
|
||
return $userInstance; | ||
} catch (BindingResolutionException $e) { | ||
throw new EntityNotFoundException("The model $userClass from he follow configuration -> 'auth.providers.users.model' cannot be instantiated (may be an abstract class).", $e->getCode(), $e); | ||
} catch (ReflectionException $e) { | ||
throw new EntityNotFoundException("The model from the follow configuration -> 'auth.providers.users.model' doesn't exists.", $e->getCode(), $e); | ||
} | ||
} | ||
|
||
/** | ||
* @param $userInstance | ||
* @throws SanctumUserException | ||
*/ | ||
public function validateUserModel($userInstance) | ||
{ | ||
if (config('restify.auth.provider') === 'sanctum' && false === $userInstance instanceof Sanctumable) { | ||
throw new SanctumUserException(__("User is not implementing Binaryk\LaravelRestify\Contracts\Sanctumable contract. User should use 'Laravel\Sanctum\HasApiTokens' trait to provide")); | ||
} | ||
} | ||
|
||
public function logout(Request $request) | ||
{ | ||
return LogoutService::make($request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
<?php | ||
|
||
namespace Binaryk\LaravelRestify\Services\Concerns; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Auth; | ||
use Illuminate\Validation\ValidationException; | ||
|
||
trait AuthenticatesUsers | ||
{ | ||
use ThrottlesLogins; | ||
|
||
/** | ||
* Get the post register / login redirect path. | ||
* | ||
* @return string | ||
*/ | ||
public function redirectPath() | ||
{ | ||
if (method_exists($this, 'redirectTo')) { | ||
return $this->redirectTo(); | ||
} | ||
|
||
return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home'; | ||
} | ||
|
||
/** | ||
* Handle a login request to the application. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response | ||
*/ | ||
public function login(Request $request) | ||
{ | ||
$this->validateLogin($request); | ||
|
||
// If the class is using the ThrottlesLogins trait, we can automatically throttle | ||
// the login attempts for this application. We'll key this by the username and | ||
// the IP address of the client making these requests into this application. | ||
if ($this->hasTooManyLoginAttempts($request)) { | ||
$this->fireLockoutEvent($request); | ||
|
||
return $this->sendLockoutResponse($request); | ||
} | ||
|
||
if ($this->attemptLogin($request)) { | ||
return $this->sendLoginResponse($request); | ||
} | ||
|
||
// If the login attempt was unsuccessful we will increment the number of attempts | ||
// to login and redirect the user back to the login form. Of course, when this | ||
// user surpasses their maximum number of attempts they will get locked out. | ||
$this->incrementLoginAttempts($request); | ||
|
||
return $this->sendFailedLoginResponse($request); | ||
} | ||
|
||
/** | ||
* Validate the user login request. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return void | ||
*/ | ||
protected function validateLogin(Request $request) | ||
{ | ||
$request->validate([ | ||
$this->username() => 'required|string', | ||
'password' => 'required|string', | ||
]); | ||
} | ||
|
||
/** | ||
* Attempt to log the user into the application. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return bool | ||
*/ | ||
protected function attemptLogin(Request $request) | ||
{ | ||
return $this->guard()->attempt( | ||
$this->credentials($request), | ||
$request->has('remember') | ||
); | ||
} | ||
|
||
/** | ||
* Get the needed authorization credentials from the request. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return array | ||
*/ | ||
protected function credentials(Request $request) | ||
{ | ||
return $request->only($this->username(), 'password'); | ||
} | ||
|
||
/** | ||
* Send the response after the user was authenticated. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
protected function sendLoginResponse(Request $request) | ||
{ | ||
$request->session()->regenerate(); | ||
|
||
$this->clearLoginAttempts($request); | ||
|
||
return $this->authenticated($request, $this->guard()->user()) | ||
?: redirect()->intended($this->redirectPath()); | ||
} | ||
|
||
/** | ||
* The user has been authenticated. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param mixed $user | ||
* @return mixed | ||
*/ | ||
protected function authenticated(Request $request, $user) | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Get the failed login response instance. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return \Illuminate\Http\RedirectResponse | ||
*/ | ||
protected function sendFailedLoginResponse(Request $request) | ||
{ | ||
throw ValidationException::withMessages([ | ||
$this->username() => [trans('auth.failed')], | ||
]); | ||
} | ||
|
||
/** | ||
* Get the login username to be used by the controller. | ||
* | ||
* @return string | ||
*/ | ||
public function username() | ||
{ | ||
return 'email'; | ||
} | ||
|
||
/** | ||
* Log the user out of the application. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function logout(Request $request) | ||
{ | ||
$this->guard()->logout(); | ||
|
||
$request->session()->invalidate(); | ||
|
||
return redirect('/'); | ||
} | ||
|
||
/** | ||
* Get the guard to be used during authentication. | ||
* | ||
* @return \Illuminate\Contracts\Auth\StatefulGuard | ||
*/ | ||
protected function guard() | ||
{ | ||
return Auth::guard(); | ||
} | ||
} |
Oops, something went wrong.