PHP code example of jorgemudry / laravel-remote-token-auth
1. Go to this page and download the library: Download jorgemudry/laravel-remote-token-auth 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/ */
jorgemudry / laravel-remote-token-auth example snippets
Route::get('/users', function (Request $request) {
return $request->user();
})->middleware('auth:rta');
return [
// Name of the guard registered by the package.
'guard' => 'rta',
// Endpoint of the external service that validates the token.
'endpoint' => env('REMOTE_TOKEN_AUTH_ENDPOINT', ''),
// Timeouts (in seconds, fractions allowed) for the validation request,
// and the statuses that mean "token rejected" rather than "service down".
'http' => [
'timeout' => env('REMOTE_TOKEN_AUTH_TIMEOUT', 5),
'connect_timeout' => env('REMOTE_TOKEN_AUTH_CONNECT_TIMEOUT', 2),
'rejection_statuses' => [400, 401, 403],
],
'response' => [
// Dot-notation path to the user attributes inside the response.
'user_path' => '',
// Class used to represent the authenticated user.
'user_class' => AuthenticatedUser::class,
],
// Optional cache for successful validations (see below).
'cache' => [
'enabled' => env('REMOTE_TOKEN_AUTH_CACHE', false),
'store' => null,
'ttl' => 60,
'prefix' => 'remote-token-auth',
],
// The four steps of the authentication pipeline (see "Advanced Usage").
'actions' => [
'token-resolver' => GetTokenFromRequestAction::class,
'token-validator' => MakeValidationRequestAction::class,
'attributes-resolver' => GetAttributesFromResponseAction::class,
'user-maker' => CreateUserFromAttributesAction::class,
],
];
namespace App\Auth;
use Illuminate\Support\Facades\Http;
use JorgeMudry\LaravelRemoteTokenAuth\Contracts\AccessTokenInterface;
use JorgeMudry\LaravelRemoteTokenAuth\Contracts\ValidatesToken;
class PostValidationAction implements ValidatesToken
{
public function execute(AccessTokenInterface $token): array
{
return Http::acceptJson()
->timeout(5)
->asForm()
->post(config('services.auth.introspection_url'), ['token' => $token->token()])
->throw()
->json();
}
}
namespace App\Auth;
use JorgeMudry\LaravelRemoteTokenAuth\ValueObjects\AuthenticatedUser;
class ApiUser extends AuthenticatedUser
{
public function isAdmin(): bool
{
return $this->getAttribute('role') === 'admin';
}
}
namespace App\Providers;
use App\Adapters\MyAdapter;
use Illuminate\Support\ServiceProvider;
use JorgeMudry\LaravelRemoteTokenAuth\Contracts\AdapterInterface;
class AuthServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->app->bind(AdapterInterface::class, MyAdapter::class);
}
}