1. Go to this page and download the library: Download bertugfahriozer/ci4oauth2 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/ */
bertugfahriozer / ci4oauth2 example snippets
namespace Config;
use CodeIgniter\Config\BaseConfig;
class Oauth2Conf extends BaseConfig
{
public array $tables = ['client_table' => 'oauth_clients',
'access_token_table' => 'oauth_access_tokens',
'refresh_token_table' => 'oauth_refresh_tokens',
'code_table' => 'oauth_authorization_codes',
'user_table' => 'oauth_users',
'jwt_table' => 'oauth_jwt',
'jti_table' => 'oauth_jti',
'scope_table' => 'oauth_scopes',
'public_key_table' => 'oauth_public_keys'];
public array $jwtConf=[
'aud'=> 'https://oauth' // you must edit here
];
public int $oauthFilterCap = 60;
public int $rateLimitCap = 2;
/* --------------------------------------------------------------------
* Encryption Algorithm to use
* --------------------------------------------------------------------
* Valid values are
* - PASSWORD_DEFAULT (default)
* - PASSWORD_BCRYPT
* - PASSWORD_ARGON2I - As of PHP 7.2 only if compiled with support for it
* - PASSWORD_ARGON2ID - As of PHP 7.3 only if compiled with support for it
*
* If you choose to use any ARGON algorithm, then you might want to
* uncomment the "ARGON2i/D Algorithm" options to suit your needs
*/
public $hashAlgorithm = PASSWORD_DEFAULT;
public array $phpHashConfig = [
'hashMemoryCost' => 2048,
'hashTimeCost' => 4,
'hashThreads' => 4,
'hashCost' => 10
];
}
namespace App\Controllers;
use CodeIgniter\Controller;
use ci4oauth2\Libraries\Oauth;
class AuthController extends Controller {
private $oauth;
private $respond;
public function __construct() {
$config = config('Oauth2Conf');
$oauth = new Oauth($this->request->getPost('grant_type'), $config);
$this->respond = $oauth->server->handleTokenRequest($req);
}
public function authorize() {
return $this->respond(json_decode($this->respond->getResponseBody()), $this->respond->getStatusCode());
}
}
/**
* Generate a JWT
*
* @param $privateKey The private key to use to sign the token
* @param $iss The issuer, usually the client_id
* @param $sub The subject, usually a user_id
* @param $aud The audience, usually the URI for the oauth server
* @param $exp The expiration date. If the current time is greater than the exp, the JWT is invalid
* @param $nbf The "not before" time. If the current time is less than the nbf, the JWT is invalid
* @param $jti The "jwt token identifier", or nonce for this JWT
*
* @return string
*/
function generateJWT($privateKey, $iss, $sub, $aud, $exp = null, $nbf = null, $jti = null) {
if (!$exp) {
$exp = time() + 1000;
}
$params = array(
'iss' => $iss,
'sub' => $sub,
'aud' => $aud,
'exp' => $exp,
'iat' => time(),
);
if ($nbf) {
$params['nbf'] = $nbf;
}
if ($jti) {
$params['jti'] = $jti;
}
$jwtUtil = new OAuth2\Encryption\Jwt();
return $jwtUtil->encode($params, $privateKey, 'RS256');
}
composer
php spark make:config
php spark migrate -all
namespace Config;
use CodeIgniter\Config\BaseConfig;
class Oauth2Conf extends BaseConfig
{
public array $tables = ['client_table' => 'oauth_clients',
'access_token_table' => 'oauth_access_tokens',
'refresh_token_table' => 'oauth_refresh_tokens',
'code_table' => 'oauth_authorization_codes',
'user_table' => 'oauth_users',
'jwt_table' => 'oauth_jwt',
'jti_table' => 'oauth_jti',
'scope_table' => 'oauth_scopes',
'public_key_table' => 'oauth_public_keys'];
public array $jwtConf=[
'aud'=> 'https://oauth' // you must edit here
];
public int $oauthFilterCap = 60;
public int $rateLimitCap = 2;
/* --------------------------------------------------------------------
* Encryption Algorithm to use
* --------------------------------------------------------------------
* Valid values are
* - PASSWORD_DEFAULT (default)
* - PASSWORD_BCRYPT
* - PASSWORD_ARGON2I - As of PHP 7.2 only if compiled with support for it
* - PASSWORD_ARGON2ID - As of PHP 7.3 only if compiled with support for it
*
* If you choose to use any ARGON algorithm, then you might want to
* uncomment the "ARGON2i/D Algorithm" options to suit your needs
*/
public $hashAlgorithm = PASSWORD_DEFAULT;
public array $phpHashConfig = [
'hashMemoryCost' => 2048,
'hashTimeCost' => 4,
'hashThreads' => 4,
'hashCost' => 10
];
}
namespace App\Controllers;
use CodeIgniter\Controller;
use ci4oauth2\Libraries\Oauth;
class AuthController extends Controller
{
private $oauth;
private $respond;
public function __construct()
{
$config = config('Oauth2Conf');
$oauth = new Oauth($this->request->getPost('grant_type'),$config);
$this->respond = $oauth->server->handleTokenRequest($req);
}
public function authorize()
{
return $this->respond(json_decode($this->respond->getResponseBody()), $this->respond->getStatusCode());
}
}
/**
* Generate a JWT
*
* @param $privateKey The private key to use to sign the token
* @param $iss The issuer, usually the client_id
* @param $sub The subject, usually a user_id
* @param $aud The audience, usually the URI for the oauth server
* @param $exp The expiration date. If the current time is greater than the exp, the JWT is invalid
* @param $nbf The "not before" time. If the current time is less than the nbf, the JWT is invalid
* @param $jti The "jwt token identifier", or nonce for this JWT
*
* @return string
*/
function generateJWT($privateKey, $iss, $sub, $aud, $exp = null, $nbf = null, $jti = null)
{
if (!$exp) {
$exp = time() + 1000;
}
$params = array(
'iss' => $iss,
'sub' => $sub,
'aud' => $aud,
'exp' => $exp,
'iat' => time(),
);
if ($nbf) {
$params['nbf'] = $nbf;
}
if ($jti) {
$params['jti'] = $jti;
}
$jwtUtil = new OAuth2\Encryption\Jwt();
return $jwtUtil->encode($params, $privateKey, 'RS256');
}