PHP code example of adexos / m2-oauth2-league-bridge

1. Go to this page and download the library: Download adexos/m2-oauth2-league-bridge 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/ */

    

adexos / m2-oauth2-league-bridge example snippets




declare(strict_types=1);

namespace Adexos\MyIdentityOauthModule\Client;

use Adexos\Oauth2LeagueBridge\Client\OauthClientFactory;

class MyOauthIdentity extends OauthClientFactory
{

}



declare(strict_types=1);

namespace Adexos\MyIdentityOauthModule\Http\Token;

use Adexos\MyIdentityOauthModule\Client\MyOauthIdentity;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use Magento\Framework\Exception\LocalizedException;

class IdentityTokenStrategy
{
    private MyOauthIdentity $myOauthIdentity;

    public function __construct(MyOauthIdentity $myOauthIdentity)
    {
        $this->myOauthIdentity = $myOauthIdentity;
    }

    /**
     * @throws LocalizedException
     * @throws IdentityProviderException
     */
    public function find(): ?string
    {
        $accessToken = $this->myOauthIdentity->getAccessTokenWithPersistence(
              new Password(),
              'custom_identifier'
              ['username' => 'a', 'password' => 'b', 'scope' => 'custom_scopes']
        );

        if ($accessToken === null) {
            return null;
        }

        return $accessToken->getToken();
    }
}

$accessToken = $this->myOauthIdentity->getCurrentTokenWithRefresh('custom_identifier');

    'cache' => [
        'frontend' => [
            'adexos_management_oauth_data_cache' => [
                'backend' => \Magento\Framework\Cache\Backend\Database::class,
                'backend_options' => []
            ]
        ]
    ]

'cache' => [
        'frontend' => [
            'adexos_management_oauth_data_cache' => [
                'backend' => \Magento\Framework\Cache\Backend\Redis::class,
                'backend_options' => [
                    'server' => 'redis',
                    'database' => '3',
                    'port' => '6379',
                    'compress_data' => '1'
                ]
            ]
        ]
    ]

    'cache' => [
        'frontend' => [
            'adexos_management_oauth_data_cache' => [
                'backend' => 'database',
                'backend_options' => []
            ]
        ]
    ]

$jwtResponse = $this->client->getAccessTokenWithPersistence(
    new Password(),
    IdentityTokenStrategy::OAUTH_TOKEN_CACHE_IDENTIFIER,
    ['username' => $username, 'password' => $password, 'scope' => self::TOKEN_SCOPE]
);

$this->jwtDecoder->decode(
    $token->getValues()['id_token'],
    IdTokenDTO::class,
    $this->scopeConfig->getValue(Config::XML_PATH_PUBLIC_KEY_JWT_TOKEN)
);