PHP code example of charithar / oauth2-openid-connect-server

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

    

charithar / oauth2-openid-connect-server example snippets


use Charithar\OpenIDConnectServer\ClaimExtractor;
use Charithar\OpenIDConnectServer\ClaimSets\StandardClaimSets;
use Charithar\OpenIDConnectServer\Context\OidcRequestContext;
use Charithar\OpenIDConnectServer\Grants\OidcAuthCodeGrant;
use Charithar\OpenIDConnectServer\ResponseTypes\IdTokenResponse;
use League\OAuth2\Server\AuthorizationServer;
use League\OAuth2\Server\Grant\RefreshTokenGrant;

$context = new OidcRequestContext();

$claimExtractor = new ClaimExtractor(StandardClaimSets::all());

$idTokenResponse = new IdTokenResponse(
    $userRepository,        // implements Charithar\OpenIDConnectServer\Repositories\UserRepositoryInterface
    $claimExtractor,
    $signingKeyRepository,  // implements Charithar\OpenIDConnectServer\Keys\SigningKeyRepositoryInterface
    $context,
    'https://issuer.example.com'
);

$authServer = new AuthorizationServer(
    $clientRepository,
    $accessTokenRepository,
    $scopeRepository,
    $privateKey,
    $encryptionKey,
    $idTokenResponse
);

$authCodeGrant = new OidcAuthCodeGrant(
    $context,
    $authCodeRepository,
    $refreshTokenRepository,
    new DateInterval('PT10M')
);

$authServer->enableGrantType($authCodeGrant, new DateInterval('PT1H'));
$authServer->enableGrantType(new RefreshTokenGrant($refreshTokenRepository), new DateInterval('PT1H'));

use Charithar\OpenIDConnectServer\Discovery\DiscoveryDocument;
use Charithar\OpenIDConnectServer\Discovery\DiscoveryRequestHandler;
use Charithar\OpenIDConnectServer\Jwks\JwksFactory;
use Charithar\OpenIDConnectServer\Jwks\JwksRequestHandler;
use Charithar\OpenIDConnectServer\UserInfo\UserInfoRequestHandler;
use Charithar\OpenIDConnectServer\Logout\LogoutRequestHandler;
use Charithar\OpenIDConnectServer\Revocation\TokenRevocationRequestHandler;
use Charithar\OpenIDConnectServer\Introspection\TokenIntrospectionRequestHandler;
use League\OAuth2\Server\ResourceServer;

// GET /.well-known/openid-configuration
$discoveryHandler = new DiscoveryRequestHandler($discoveryDocument, $responseFactory);

// GET /.well-known/jwks.json
$jwksHandler = new JwksRequestHandler(new JwksFactory($signingKeyRepository), $responseFactory);

// UserInfoRequestHandler validates the Bearer token itself, via league's own
// ResourceServer - construct one with the same access token repository and
// public key your resource server / access-token validation already uses.
$resourceServer = new ResourceServer($accessTokenRepository, $publicKey);

// GET/POST /oauth2/userinfo (Bearer token)
$userInfoHandler = new UserInfoRequestHandler($resourceServer, $userRepository, $claimExtractor, $responseFactory);

// GET /logout (RP-Initiated Logout)
$logoutHandler = new LogoutRequestHandler($clientRepository, $signingKeyRepository, 'https://issuer.example.com', $responseFactory, $sessionHandler);

// POST /oauth2/revoke - mount behind ClientCredentialsMiddleware
$revocationHandler = new TokenRevocationRequestHandler($clientRepository, $accessTokenRepository, $refreshTokenRepository, $encryptionKey, $responseFactory);

// POST /oauth2/introspect - mount behind ClientCredentialsMiddleware
$introspectionHandler = new TokenIntrospectionRequestHandler($clientRepository, $accessTokenRepository, $refreshTokenRepository, $encryptionKey, $responseFactory);