Skip to content

Commit

Permalink
Updating PassportServiceProvider to use a Key object for faster encry…
Browse files Browse the repository at this point in the history
…ption/decryption, when combined with oauth2-server package patch from thephpleague/oauth2-server#814
  • Loading branch information
EricTendian committed Sep 11, 2018
1 parent 9b6e761 commit f1aa3e7
Showing 1 changed file with 24 additions and 1 deletion.
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 @@ -201,7 +203,7 @@ public function makeAuthorizationServer()
$this->app->make(Bridge\AccessTokenRepository::class),
$this->app->make(Bridge\ScopeRepository::class),
$this->makeCryptKey('oauth-private.key'),
app('encrypter')->getKey()
$this->makeEncryptionKey(app('encrypter')->getKey())
);
}

Expand Down Expand Up @@ -235,6 +237,27 @@ protected function makeCryptKey($key)
);
}

/**
* 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

0 comments on commit f1aa3e7

Please sign in to comment.