PHP code example of jeremy379 / laravel-openid-connect

1. Go to this page and download the library: Download jeremy379/laravel-openid-connect 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/ */

    

jeremy379 / laravel-openid-connect example snippets


public function boot(): void
    {
        Passport::tokensCan(config('openid.passport.tokens_can'));
    }

$scopes = array_merge($yourScopes, config('openid.passport.tokens_can'));
Passport::tokensCan($scopes);

public function boot(): void
    {
        Passport::authorizationView('auth.oauth.authorize');
    }

public function register(): void
    {
        //...
        $this->app->bind(AuthorizationViewResponse::class, fn() => new SimpleViewResponse('auth.approve'));

    }

public function register(): void
    {
        //...
        $this->app->register(\OpenIDConnect\Laravel\PassportServiceProvider::class);
    }


# app/Entities/IdentityEntity.php
namespace App\Entities;

use League\OAuth2\Server\Entities\Traits\EntityTrait;
use OpenIDConnect\Claims\Traits\WithClaims;
use OpenIDConnect\Entities\Traits\WithCustomPermittedFor;
use OpenIDConnect\Interfaces\IdentityEntityInterface;

class IdentityEntity implements IdentityEntityInterface
{
    use EntityTrait;
    use WithClaims;
    use WithCustomPermittedFor;

    /**
     * The user to collect the additional information for
     */
    protected User $user;

    /**
     * Custom audiences
     * explanation: https://openid.net/specs/openid-connect-core-1_0.html#IDToken
     * When building id token, client id is merged with getPermittedFor()
     */
    public function __construct()
    {
        $this->setPermittedFor([
            'https://api.example.com/v1/resource',
            'custom aud 2'
        ]);
    }

    /**
     * The identity repository creates this entity and provides the user id
     * @param mixed $identifier
     */
    public function setIdentifier($identifier): void
    {
        $this->identifier = $identifier;
        $this->user = User::findOrFail($identifier);
    }

    /**
     * When building the id_token, this entity's claims are collected
     */
    public function getClaims(): array
    {
        return [
            'email' => $this->user->email,
        ];
    }
}

$config = Configuration::forSymmetricSigner(
    new \Lcobucci\JWT\Signer\Rsa\Sha256(),
    InMemory::file(base_path('oauth-public.key')) //This is the public key generate by passport. You need to share it.
  );
  
  //Parse the token
  
  $token = $config->parser()->parse($idtoken);
  
  $signatureValid = $config->validator()->validate($token, new \Lcobucci\JWT\Validation\Constraint\SignedWith($config->signer(), $config->signingKey()));

'token_headers' => ['kid' => base64_encode('public-key-added-2023-01-01')]

'routes' => [
    // ...
    'userinfo' => true,
],
sh
php artisan vendor:publish --tag=openid

<form method="post" action="{{ route('passport.authorizations.approve').'?nonce='.$request->nonce }}">