Created
November 14, 2019 09:27
-
-
Save mrron313/521752911fdf7ef89affa7524744d22d to your computer and use it in GitHub Desktop.
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
<?php | |
namespace App\Auth\Grants; | |
use RuntimeException; | |
use Illuminate\Http\Request; | |
use App\Exceptions\OtpException; | |
use Laravel\Passport\Bridge\User; | |
use League\OAuth2\Server\RequestEvent; | |
use App\Auth\Grants\OtpVerifierFactory; | |
use Psr\Http\Message\ServerRequestInterface; | |
use League\OAuth2\Server\Grant\AbstractGrant; | |
use League\OAuth2\Server\Entities\UserEntityInterface; | |
use League\OAuth2\Server\Entities\ClientEntityInterface; | |
use League\OAuth2\Server\Exception\OAuthServerException; | |
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface; | |
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface; | |
class OtpGrant extends AbstractGrant | |
{ | |
/** | |
* @param RefreshTokenRepositoryInterface $refreshTokenRepository | |
*/ | |
public function __construct( | |
RefreshTokenRepositoryInterface $refreshTokenRepository | |
) { | |
$this->setRefreshTokenRepository($refreshTokenRepository); | |
$this->refreshTokenTTL = new \DateInterval('P1M'); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function respondToAccessTokenRequest( | |
ServerRequestInterface $request, | |
ResponseTypeInterface $responseType, | |
\DateInterval $accessTokenTTL | |
) { | |
// Validate request | |
$client = $this->validateClient($request); | |
$scopes = $this->validateScopes($this->getRequestParameter('scope', $request)); | |
$user = $this->validateUser($request, $client); | |
// Finalize the requested scopes | |
$scopes = $this->scopeRepository->finalizeScopes($scopes, $this->getIdentifier(), $client, $user->getIdentifier()); | |
// Issue and persist new tokens | |
$accessToken = $this->issueAccessToken($accessTokenTTL, $client, $user->getIdentifier(), $scopes); | |
$refreshToken = $this->issueRefreshToken($accessToken); | |
// Inject tokens into response | |
$responseType->setAccessToken($accessToken); | |
$responseType->setRefreshToken($refreshToken); | |
return $responseType; | |
} | |
protected function validateUser(ServerRequestInterface $request, ClientEntityInterface $client) | |
{ | |
$otp = $this->getRequestParameter('otp', $request); | |
if (is_null($otp)) { | |
throw OAuthServerException::invalidRequest('otp'); | |
} | |
$otpVerifierParam = $this->getRequestParameter('otp_verifier', $request); | |
$otpVerifier = OtpVerifierFactory::getOtpVerifier( | |
$this->getRequestParameter('otp_verifier', $request, 'BL_INTERNAL') | |
); | |
if ( is_null($otpVerifier) ) { | |
throw OtpException::invalidOtpVerifier(); | |
} | |
$isValidOtp = $otpVerifier->verify($otp); | |
if (!$isValidOtp){ | |
throw OtpException::invalidOtp(); | |
} | |
$username = $this->getRequestParameter('username', $request); | |
if (is_null($username)) { | |
throw OAuthServerException::invalidRequest('username'); | |
} | |
$user = $this->getUserEntityByUserOtp( | |
$username, | |
$this->getIdentifier(), | |
$client | |
); | |
if ($user instanceof UserEntityInterface === false) { | |
$this->getEmitter()->emit(new RequestEvent(RequestEvent::USER_AUTHENTICATION_FAILED, $request)); | |
throw OAuthServerException::invalidCredentials(); | |
} | |
return $user; | |
} | |
private function getUserEntityByUserOtp($username, $grantType, ClientEntityInterface $clientEntity) | |
{ | |
$provider = config('auth.guards.api.provider'); | |
if (is_null($model = config('auth.providers.'.$provider.'.model'))) { | |
throw new RuntimeException('Unable to determine authentication model from configuration.'); | |
} | |
$user = (new $model)->where('username', $username)->first(); | |
if (is_null($user)) { | |
return; | |
} | |
return new User($user->getAuthIdentifier()); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getIdentifier() | |
{ | |
return 'otp_grant'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i am getting
Error: Class 'App\Auth\Grants\OtpVerifierFactory' not found in file C:\inetpub\wwwroot\sports\app\Auth\Grants\OTPGrant.php on line 68