1. Go to this page and download the library: Download ridibooks/oauth2 library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
$client_info = new ClientInfo('client_id', 'client_secret', ['scope'], 'redirect_uri');
$auth_server_info = new AuthorizationServerInfo('authorization_url', 'token_url');
$granter = new Granter($client_info, $auth_server_info);
$authorization_url = $granter->authorize();
// Redirect to `$authorization_url`
use Ridibooks\OAuth2\Silex\Constant\OAuth2ProviderKeyConstant as KeyConstant;
use Ridibooks\OAuth2\Silex\Handler\LoginRequiredExceptionHandler;
use Ridibooks\OAuth2\Silex\Provider\OAuth2ServiceProvider;
use Ridibooks\OAuth2\Authorization\Validator\JwtTokenValidator;
use Example\UserProvder;
// `OAuth2ServiceProvider` 등록
$app->register(new OAuth2ServiceProvider(), [
KeyConstant::CLIENT_ID => 'example-client-id',
KeyConstant::CLIENT_SECRET => 'example-client-secret',
KeyConstant::JWT_VALIDATOR => new JwtTokenValidator($jwk_url)
]);
// 미들웨어 등록
$app->get('/auth-
use Ridibooks\OAuth2\Authorization\Authorizer;
use Ridibooks\OAuth2\Authorization\Exception\AuthorizationException;
use Ridibooks\OAuth2\Silex\Constant\OAuth2ProviderKeyConstant;
use Ridibooks\OAuth2\Silex\Provider\OAuth2ServiceProvider;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Ridibooks\OAuth2\Authorization\Validator\JwtTokenValidator;
...
// `OAuth2ServiceProvider` 등록
$app->register(new OAuth2ServiceProvider(), [
KeyConstant::CLIENT_ID => 'example-client-id',
KeyConstant::CLIENT_SECRET => 'example-client-secret',
KeyConstant::JWT_VALIDATOR => new JwtTokenValidator($jwk_url)
]);
...
$app->get('/', function (Application $app, Request $request) {
/** @var Authorizer $authorizer */
$authorizer = $app[OAuth2ProviderKeyConstant::AUTHORIZER];
try {
$token = $authorizer->authorize($request);
return $token->getSubject();
} catch (AuthorizationException $e) {
// handle authorization error ...
}
});
namespace Ridibooks\OAuth2\Example;
use Ridibooks\OAuth2\Symfony\Annotation\OAuth2;
use Ridibooks\OAuth2\Symfony\Provider\OAuth2ServiceProvider;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class ExampleController extends Controller
{
/** @var OAuth2ServiceProvider */
private $oauth2_service_provider;
/**
* @param OAuth2ServiceProvider $oauth2_service_provider
*/
public function __construct(OAuth2ServiceProvider $oauth2_service_provider)
{
$this->oauth2_service_provider = $oauth2_service_provider;
}
/**
* @Route("/oauth2", methods={"GET"})
* @OAuth2()
*
* @param Request $request
* @return Response
*/
public function normal(Request $request): Response
{
$user = $this->oauth2_service_provider->getMiddleware()->getUser();
return new JsonResponse([
'u_idx' => $user->getUidx(),
'u_id' => $user->getUid()
]);
}
}
namespace Ridibooks\OAuth2\Example;
use Ridibooks\OAuth2\Authorization\Token\JwtToken;
use Ridibooks\OAuth2\Symfony\Provider\OAuth2ServiceProvider;
use Ridibooks\OAuth2\Symfony\Provider\UserProviderInterface;
use Symfony\Component\HttpFoundation\Request;
class CustomUserProvider implement UserProviderInterface
{
/**
* @param JwtToken $token
* @param Request $request
* @param OAuth2ServiceProvider $oauth2_service_provider
* @return User
*/
public function getUser(JwtToken $token, Request $request, OAuth2ServiceProvider $oauth2_service_provider): User
{
...
}
}
namespace Ridibooks\OAuth2\Example;
use Ridibooks\OAuth2\Symfony\Annotation\OAuth2;
use Ridibooks\OAuth2\Symfony\Provider\OAuth2ServiceProvider;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class ExampleController extends Controller
{
/**
* @Route("/oauth2", methods={"GET"})
* @OAuth2(user_provider="Ridibooks\OAuth2\Example\CustomUserProvider")
*
* @param Request $request
* @return Response
*/
public function normal(Request $request): Response
{
...
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.