Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.6 Passport OAuth Max Login Attempts

I've just created a simple OAuth system with Laravel Passport. This system will be responsible for an external app user registration and authentication. Everything is working as i expect, and now i would like to implement a mechanism to lock users after a predefined number of failed login attempts.

I'm new to Laravel and Passport, is there any built in package that can manage this for me? Or do I have to develop this feature on my own? If so, how can i accomplish such task?

I've been searching all around the interwebs but until now i couldn't find anything regarding Passport OAuth.

like image 678
Ricky Avatar asked Mar 06 '23 10:03

Ricky


1 Answers

I've managed to accomplish what i wanted to do, if anyone comes across this issue, here's what i did...

Created a custom AuthController and login method to replace Laravel Passport's default oauth/token:

use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory;
use Illuminate\Http\Response;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Response;
use \Laravel\Passport\Http\Controllers\AccessTokenController as AccessTokenController;

class AuthController extends AccessTokenController
{
    use AuthenticatesUsers;

    //custom login method
    public function login(Request $request)
    {
        //...
    }
}

Before any other login actions, check if a user has reached the max login attempts:

//custom login method
public function login(Request $request)
{
    //check if the max number of login attempts has been reached
    if ($this->hasTooManyLoginAttempts($request)) 
    {
        $this->fireLockoutEvent($request);

        return "To many attempts...";
    }

    //...
}

Verify user credentials by attempting a login. If a logins succeeds reset the the failed attempts count. If it fails, increment the count:

//check if user has reached the max number of login attempts

//verify user credentials
$credentials = $request->only('email', 'password');

if (Auth::attempt($credentials)) 
{       
    //reset failed login attemps
    $this->clearLoginAttempts($request);

    //...
}
else
{       
    //count user failed login attempts
    $this->incrementLoginAttempts($request);

    return "Login failed...";
}

And finally, since Passport (OAuth2) uses PSR-7 requests (Server Request Interface), we need to convert the standard Laravel request to PSR-7 in order to issue the access token:

//Authentication passed...

//convert Laravel Request (Symfony Request) to PSR-7
$psr7Factory = new DiactorosFactory();
$psrRequest = $psr7Factory->createRequest($request);

//generate access token
$tokenResponse = parent::issueToken($psrRequest);

//return issued token
return Response::json($tokenResponse);

Here's the complete login method:

public function login(Request $request)
{
    //check if user has reached the max number of login attempts
    if ($this->hasTooManyLoginAttempts($request)) 
    {
        $this->fireLockoutEvent($request);

        return "To many attempts...";
    }


    //verify user credentials
    $credentials = $request->only('email', 'password');

    if (Auth::attempt($credentials)) 
    {
        //Authentication passed...

        //reset failed login attemps
        $this->clearLoginAttempts($request);

        //convert Laravel Request (Symfony Request) to PSR-7
        $psr7Factory = new DiactorosFactory();
        $psrRequest = $psr7Factory->createRequest($request);

        //generate access token
        $tokenResponse = parent::issueToken($psrRequest);

        //return issued token
        return Response::json($tokenResponse);
    } 
    else 
    {
        //count user failed login attempts
        $this->incrementLoginAttempts($request);

        return "Login failed...";
    }
}
like image 73
Ricky Avatar answered Mar 16 '23 17:03

Ricky