PHP code example of keukenmagazijn / passport-authenticator

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

    

keukenmagazijn / passport-authenticator example snippets


return [
    'passport' => [
        'endpoints' => [
            'base_uri' => env('APP_BASE_URI', null), // https://external.app
            'authorize' => '/oauth/authorize', // Authorize our client and user route to retrieve a code.
            'token' => '/oauth/token' // Route we give our code to and get our tokens from.
        ],
        'user' => [
            'email' => env('APP_USER_EMAIL', null), // username of external app.
            'password' => env('APP_USER_PASSWORD', null), // password of external app.
        ],
        'secret' => env('APP_SECRET', null), // API client secret used to connect.
        'client_id' => env('APP_CLIENT_ID', null), // API client id used to connect .
        'redirect_uri' => env('APP_REDIRECT_URI', null), // API redirect uri.
    ]
];

 namespace App\Components\PassportAuthenticators\Instances;

use App\Components\PassportAuthenticators\Factories\ExampleFactory;
use Keukenmagazijn\PassportAuthenticator\Abstracts\ConcretePassportAuthenticator;
use Keukenmagazijn\PassportAuthenticator\Instances\Authenticator;

class ExampleAuthenticator extends ConcretePassportAuthenticator
{
    /**
     * We refer to the our factory here.
     * @return Authenticator
     */
    protected function getAuthenticatorInstance(): Authenticator
    {
        /** @var ExampleFactory $_factory */
        $_factory = \App::make(ExampleFactory::class);
        return $_factory->build();
    }

    /**
     * @return string
     */
    protected function getBaseUri(): string
    {
        return config('external_application.passport.endpoints.base_uri');
    }
}

public function buildAuthenticator(
    string $appName,
    string $clientId,
    string $clientSecret,
    string $redirectUri,
    string $userEmail,
    string $userPassword,
    string $authorizeUri,
    string $tokenUri
): Authenticator;

 namespace App\Components\PassportAuthenticators\Factories;

use Keukenmagazijn\PassportAuthenticator\Factories\ConcretePassportAuthenticatorFactory;
use Keukenmagazijn\PassportAuthenticator\Instances\Authenticator;

class ExampleFactory extends ConcretePassportAuthenticatorFactory
{
    /**
     * @return Authenticator
     */
    public function build(): Authenticator {
        return $this->buildAuthenticator(
            'external_example_app',
            config('external_application.passport.client_id'),
            config('external_application.passport.secret'),
            config('external_application.passport.redirect_uri'),
            config('external_application.passport.user.email'),
            config('external_application.passport.user.password'),
            sprintf("%s%s", config('external_application.passport.endpoints.base_uri'), config('external_application.passport.endpoints.authorize')),
            sprintf("%s%s", config('external_application.passport.endpoints.base_uri'), config('external_application.passport.endpoints.token'))
        );
    }
}

/** @var \App\Components\PassportAuthenticators\Instances\ExampleAuthenticator $_authenticator */
$_authenticator = \App::make(\App\Components\PassportAuthenticators\Instances\ExampleAuthenticator::class);
return $_authenticator->get("/api/v2/products"); // returns the body.