PHP code example of alex-kalanis / oauth2

1. Go to this page and download the library: Download alex-kalanis/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/ */

    

alex-kalanis / oauth2 example snippets


kalanis\OAuth2\DI\Extension::install($configurator);


namespace MyApp\OAuth;

use kalanis\OAuth2\Grant\IGrant;
use kalanis\OAuth2\Application;
use kalanis\OAuth2\Exceptions\OAuthException;

class AuthorizationPresenter extends Application\OAuthPresenter
{

    /**
     * Authorization
     * @param string $response_type
     * @param string $redirect_uri
     * @param string|null $scope
     */
    public function actionAuthorize(string $response_type, string $redirect_uri, ?string $scope = NULL): void
    {
        if (!$this->user->isLoggedIn()) {
            $this->redirect('AnyUser:login', array('backlink' => $this->storeRequest()));
        }

        if ($response_type == 'code') {
            $this->issueAuthorizationCode($response_type, $redirect_uri, $scope);
        } else if ($response_type == 'token') {
            $this->issueAccessToken(IGrant::IMPLICIT, $redirect_uri);
        }
    }

    /**
     * Access token provider
     */
    public function actionToken(): void
    {
        try {
            $this->issueAccessToken();
        } catch (OAuthException $e) {
            $this->oauthError($e);
        }
    }

}

GET //oauth.presenter.url/authorize?response_type=code&client_id=CLIENT_ID&redirect_uri=REDIRECT_URI&scope=email

//redirect_uri/?code=AnlSCIWYbchsCc5sdc5ac4caca8a2

//redirect_uri/?error=unauthorized_client&error_description=Client+is+not+found

POST //oauth.presenter.url/token
	grant_type=authorization_code
	&code=AUTHORIZATION_CODE
	&client_id=CLIENT_ID
	&client_secret=CLIENT_SECRET

GET //oauth.presenter.url/authorization?response_type=token&client_id=CLIENT_ID&redirect_uri=REDIRECT_URI&scope=email

//redirect_uri/#error=unauthorized_client&error_description=Client+is+not+found

POST //oauth.presenter.url/token
	grant_type=password
	&username=USERNAME
	&password=PASSWORD
	&client_id=CLIENT_ID