Skip to content

Instantly share code, notes, and snippets.

@onamfc
Last active June 1, 2023 20:18
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save onamfc/0422da15743918e653888441ba6226ca to your computer and use it in GitHub Desktop.
Save onamfc/0422da15743918e653888441ba6226ca to your computer and use it in GitHub Desktop.
Add Custom Claims to Passport 8 / Laravel 6
<?php
namespace App\Passport;
use App\User;
use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Signer\Key;
use League\OAuth2\Server\CryptKey;
use Lcobucci\JWT\Signer\Rsa\Sha256;
use Laravel\Passport\Bridge\AccessToken as BaseToken;
class AccessToken extends BaseToken {
private $privateKey;
/**
* Generate a string representation from the access token
*/
public function __toString() {
return (string) $this->convertToJWT( $this->privateKey );
}
/**
* Set the private key used to encrypt this access token.
*/
public function setPrivateKey( CryptKey $privateKey ) {
$this->privateKey = $privateKey;
}
public function convertToJWT( CryptKey $privateKey ) {
$builder = new Builder();
$builder->permittedFor( $this->getClient()->getIdentifier() )
->identifiedBy( $this->getIdentifier(), true )
->issuedAt( time() )
->canOnlyBeUsedAfter( time() )
->expiresAt( $this->getExpiryDateTime()->getTimestamp() )
->relatedTo( $this->getUserIdentifier() )
->withClaim( 'iss', 'http://localhost:8080/' )
->withClaim( 'scopes', $this->getScopes() );
if ( $user = User::find( $this->getUserIdentifier() ) ) {
$builder
->withClaim( 'uid', $user->id );
// Include additional user claims for user here
}
return $builder
->getToken( new Sha256(), new Key( $privateKey->getKeyPath(), $privateKey->getPassPhrase() ) );
}
}
<?php
namespace App\Repositories;
use App\Passport\AccessToken;
use Laravel\Passport\Bridge\AccessTokenRepository as BaseRepository;
use League\OAuth2\Server\Entities\ClientEntityInterface;
class AccessTokenRepository extends BaseRepository {
public function getNewToken( ClientEntityInterface $clientEntity, array $scopes, $userIdentifier = null ) {
return new AccessToken( $userIdentifier, $scopes, $clientEntity );
}
}
// located in config/app.php
'providers' => [
...
/*
* Application Service Providers...
*/
App\Providers\PassportServiceProvider::class,
],
<?php
namespace App\Providers;
use App\Repositories\AccessTokenRepository;
use Laravel\Passport\Bridge\ClientRepository;
use Laravel\Passport\Bridge\ScopeRepository;
use League\OAuth2\Server\AuthorizationServer;
class PassportServiceProvider extends \Laravel\Passport\PassportServiceProvider {
public function makeAuthorizationServer() {
return new AuthorizationServer(
$this->app->make( ClientRepository::class ),
$this->app->make( AccessTokenRepository::class ),
$this->app->make( ScopeRepository::class ),
$this->makeCryptKey( 'private' ),
app( 'encrypter' )->getKey()
);
}
}
@h-rafiee
Copy link

now how can i get specific item of token in my code !?

@amthekkel
Copy link

now how can i get specific item of token in my code !?

Any help with this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment