Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a Key object when instantiating AuthorizationServer for 200x-300x faster encryption of refresh token #820

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/PassportServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Laravel\Passport;

use DateInterval;
use Defuse\Crypto\Key as EncryptionKey;
use Defuse\Crypto\Encoding as EncryptionEncoding;
use Illuminate\Auth\RequestGuard;
use Illuminate\Auth\Events\Logout;
use Illuminate\Support\Facades\Auth;
Expand Down Expand Up @@ -204,7 +206,7 @@ public function makeAuthorizationServer()
$this->app->make(Bridge\AccessTokenRepository::class),
$this->app->make(Bridge\ScopeRepository::class),
$this->makeCryptKey('private'),
app('encrypter')->getKey()
$this->makeEncryptionKey(app('encrypter')->getKey())
);
}

Expand Down Expand Up @@ -240,6 +242,27 @@ protected function makeCryptKey($type)
return new CryptKey($key, null, false);
}

/**
* Create a Key instance for encrypting the refresh token
*
* @param string $keyBytes
* @return \Defuse\Crypto\Key
*/
protected function makeEncryptionKey($keyBytes)
{
// First, we will encode Laravel's encryption key into a format that the Defuse\Crypto\Key class can use,
// so we can instantiate a new Key object. We need to do this as the Key class has a private constructor method
// which means we cannot directly instantiate the class based on our Laravel encryption key.
$encryptionKeyAscii = EncryptionEncoding::saveBytesToChecksummedAsciiSafeString(
EncryptionKey::KEY_CURRENT_VERSION,
$keyBytes
);

// Instantiate a Key object so we can take advantage of significantly faster encryption/decryption
// from https://github.com/thephpleague/oauth2-server/pull/814. The improvement is 200x-300x faster.
return EncryptionKey::loadFromAsciiSafeString($encryptionKeyAscii);
}

/**
* Register the token guard.
*
Expand Down