PHP code example of longthanhtran / yii2-oauth2-server
1. Go to this page and download the library: Download longthanhtran/yii2-oauth2-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/ */
longthanhtran / yii2-oauth2-server example snippets
use longthanhtran\oauth2\Module;
use League\OAuth2\Server\Grant\AuthCodeGrant;
use League\OAuth2\Server\Grant\ClientCredentialsGrant;
use League\OAuth2\Server\Grant\PasswordGrant;
use League\OAuth2\Server\Grant\RefreshTokenGrant;
return [
'oauth2' => [
'class' => 'longthanhtran\oauth2\Module',
'privateKey' => __DIR__ . '/../keys/private.key',
'publicKey' => __DIR__ . '/../keys/public.key',
'encryptionKey' => "you-need-to-prepare-this-encryption-key",
'enableGrantTypes' => function(Module $module) {
$server = $module->authorizationServer;
// Client Credentials Grant
$server->enableGrantType(
new ClientCredentialsGrant(),
new DateInterval('PT1H') // expires after 1 hour
);
// Authorization Code Grant
$authCodeGrant = new AuthCodeGrant(
$module->authCodeRepository,
$module->refreshTokenRepository,
new DateInterval('PT10M') // expires after 10 minutes
);
$authCodeGrant->setRefreshTokenTTL(
new DateInterval('P1M') // expires after 1 month
);
$server->enableGrantType(
$authCodeGrant,
new DateInterval('PT1H') // expires after 1 hour
);
// Refresh Token Grant
$refreshTokenGrant = new RefreshTokenGrant(
$module->refreshTokenRepository
);
$refreshTokenGrant->setRefreshTokenTTL(
new DateInterval('P1M') // expires after 1 month
);
$server->enableGrantType(
$refreshTokenGrant,
new DateInterval('PT1H') // expires after 1 hour
);
// Password Grant - legacy grant
$passwordGrant = new PasswordGrant(
$module->userRepository,
$module->refreshTokenRepository
);
$passwordGrant->setRefreshTokenTTL(new DateInterval('P1M'));
$server->enableGrantType(
$passwordGrant,
new DateInterval('PT1H') // expires after 1 hour
);
}
]
];
namespace app\models;
use League\OAuth2\Server\Entities\ClientEntityInterface;
trait UserQueryTrait {
public function getUserEntityByUserCredentials($username,
$password,
$grantType,
ClientEntityInterface $clientEntity)
{
$user = User::findOne(['username' => $username]);
if ($user && $user->validatePassword($password)) {
return $user;
}
return null;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.