1. Go to this page and download the library: Download alegz/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/ */
namespace app\storage;
/**
*
* @author Stefano Mtangoo <mwinjilisti at gmail dot com>
*/
class JwtAccessToken extends \OAuth2\Storage\JwtAccessToken
{
public function setAccessToken($oauth_token, $client_id, $user_id, $expires, $scope = null)
{
}
public function unsetAccessToken($access_token)
{
}
}
namespace app\storage;
class PublicKeyStorage implements \OAuth2\Storage\PublicKeyInterface{
private $pbk = null;
private $pvk = null;
public function __construct()
{
//files should be in same directory as this file
//keys can be generated using OpenSSL tool with command:
/*
private key:
openssl genrsa -out privkey.pem 2048
public key:
openssl rsa -in privkey.pem -pubout -out pubkey.pem
*/
$this->pbk = file_get_contents('privkey.pem', true);
$this->pvk = file_get_contents('pubkey.pem', true);
}
public function getPublicKey($client_id = null){
return $this->pbk;
}
public function getPrivateKey($client_id = null){
return $this->pvk;
}
public function getEncryptionAlgorithm($client_id = null){
return 'HS256';
}
}
namespace app\storage;
/**
*
* @author Stefano Mtangoo <mwinjilisti at gmail dot com>
*/
class JwtAccessToken extends \OAuth2\Storage\JwtAccessToken
{
public function setAccessToken($oauth_token, $client_id, $user_id, $expires, $scope = null)
{
}
public function unsetAccessToken($access_token)
{
}
}
namespace app\storage;
class PublicKeyStorage implements \OAuth2\Storage\PublicKeyInterface{
private $pbk = null;
private $pvk = null;
public function __construct()
{
//files should be in same directory as this file
//keys can be generated using OpenSSL tool with command:
/*
private key:
openssl genrsa -out privkey.pem 2048
public key:
openssl rsa -in privkey.pem -pubout -out pubkey.pem
*/
$this->pbk = file_get_contents('privkey.pem', true);
$this->pvk = file_get_contents('pubkey.pem', true);
}
public function getPublicKey($client_id = null){
return $this->pbk;
}
public function getPrivateKey($client_id = null){
return $this->pvk;
}
public function getEncryptionAlgorithm($client_id = null){
return 'HS256';
}
}
php composer.phar
use Yii;
class User extends common\models\User implements \OAuth2\Storage\UserCredentialsInterface
{
/**
* Implemented for Oauth2 Interface
*/
public static function findIdentityByAccessToken($token, $type = null)
{
/** @var \filsh\yii2\oauth2server\Module $module */
$module = Yii::$app->getModule('oauth2');
$token = $module->getServer()->getResourceController()->getToken();
return !empty($token['user_id'])
? static::findIdentity($token['user_id'])
: null;
}
/**
* Implemented for Oauth2 Interface
*/
public function checkUserCredentials($username, $password)
{
$user = static::findByUsername($username);
if (empty($user)) {
return false;
}
return $user->validatePassword($password);
}
/**
* Implemented for Oauth2 Interface
*/
public function getUserDetails($username)
{
$user = static::findByUsername($username);
return ['user_id' => $user->getId()];
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.