Skip to content

Instantly share code, notes, and snippets.

@AdSegura
Created August 3, 2019 13:53
Show Gist options
  • Save AdSegura/0d1bd695ece0a5e0b3ad6582c51c1a5f to your computer and use it in GitHub Desktop.
Save AdSegura/0d1bd695ece0a5e0b3ad6582c51c1a5f to your computer and use it in GitHub Desktop.
Get AccessToken Bearer JWT Laravel Passport
// Code from https://github.com/laravel/passport/issues/779#issuecomment-429536388
<?php
namespace App;
use Laravel\Passport\Passport;
use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Signer\Key;
use Lcobucci\JWT\Signer\Rsa\Sha256;
use League\OAuth2\Server\CryptKey;
class AccessToken extends \Laravel\Passport\Token
{
/**
* Generate a JWT from the access token
*
* @return string
*/
public function convertToJWT()
{
$privateKey = new CryptKey(
'file://' . Passport::keyPath('oauth-private.key'),
null,
false
);
return (string) (new Builder())
->permittedFor($this->client_id)
->identifiedBy($this->id, true)
->issuedAt(time())
->canOnlyBeUsedAfter(time())
->expiresAt($this->expires_at->getTimestamp())
->relatedTo($this->user->id)
->withClaim('scopes', [])
->getToken(new Sha256(), new Key($privateKey->getKeyPath(), $privateKey->getPassPhrase()));
}
}
<?php
namespace App;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\Storage;
use Laravel\Passport\HasApiTokens;
use Lcobucci\JWT\Parser as JwtParser;
class User extends Authenticatable
{
use HasApiTokens, Notifiable;
/**
* @var
* personal Access Token
*/
protected $personalAccessToken;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'avatar',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
/**
* Get avatar URL as avatar
*
* @param $avatar
* @return string avatar URL
*/
public function getAvatarAttribute($avatar)
{
return env('APP_URL') . Storage::url($avatar);
}
/**
* find Actual Active Access Token for the request
*/
protected function findAccessToken()
{
$jwt = new JwtParser();
$this->personalAccessToken = $this->tokens->find(
$jwt
->parse(request()->bearerToken())
->getClaim('jti')
);
}
/**
* revoke Personal Access Token
* @return mixed
*/
public function revokePersonalAccessToken()
{
if($this->findAccessToken())
return $this->personalAccessToken->revoke();
}
/**
* get Bearer
* will recreate a valid bearer token from Passport AccessToken
*
* @return mixed
*/
public function getBearer()
{
$token = AccessToken::where('user_id', $this->id)
->where('expires_at', '>', Carbon::now())
->orderBy('created_at', 'desc')
->first();
return $token->convertToJWT();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment