且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Laravel 5.6 Passport OAuth Max登录尝试

更新时间:2023-12-03 22:53:52

我已经设法完成了我想做的事情,如果有人遇到这个问题,这就是我所做的...

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

创建了一个自定义AuthController和登录方法来替换Laravel Passport的默认oauth/token:

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...";
}

最后,由于Passport(OAuth2)使用PSR-7请求(服务器请求接口),因此我们需要将标准Laravel请求转换为PSR-7才能发出访问令牌:

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);

这是完整的登录方法:

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...";
    }
}