Skip to content

Commit

Permalink
reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredchu committed Mar 30, 2017
1 parent 8d0fcff commit 4fce890
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 205 deletions.
106 changes: 55 additions & 51 deletions src/OAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,64 +11,68 @@
use Firebase\JWT\JWT;
use Requests;

class OAuth {
/*
* token life time in second
* */
public $tokenLifeTime;
public $key;
public $iss;
class OAuth
{
/*
* token life time in second
* */
public $tokenLifeTime;
public $key;
public $iss;

protected $expireTimestamp;
protected $accessToken;
protected $expireTimestamp;
protected $accessToken;

/**
* OAuth constructor.
*
* @param $key
* @param $iss
*/
public function __construct( $key, $iss, $tokenLifeTime = 3600 ) {
$this->key = $key;
$this->iss = $iss;
$this->tokenLifeTime = $tokenLifeTime;
}
/**
* OAuth constructor.
*
* @param $key
* @param $iss
*/
public function __construct($key, $iss, $tokenLifeTime = 3600)
{
$this->key = $key;
$this->iss = $iss;
$this->tokenLifeTime = $tokenLifeTime;
}

protected function requestAccessToken() {
$currentTimestamp = time();
$this->expireTimestamp = $currentTimestamp + $this->tokenLifeTime;
$jsonToken = array(
"iss" => $this->iss,
"scope" => "https://www.googleapis.com/auth/firebase.database https://www.googleapis.com/auth/userinfo.email",
"aud" => "https://www.googleapis.com/oauth2/v4/token",
"exp" => $this->expireTimestamp,
"iat" => $currentTimestamp
);
$jwt = JWT::encode( $jsonToken, $this->key, 'RS256' );
protected function requestAccessToken()
{
$currentTimestamp = time();
$this->expireTimestamp = $currentTimestamp + $this->tokenLifeTime;
$jsonToken = array(
"iss" => $this->iss,
"scope" => "https://www.googleapis.com/auth/firebase.database https://www.googleapis.com/auth/userinfo.email",
"aud" => "https://www.googleapis.com/oauth2/v4/token",
"exp" => $this->expireTimestamp,
"iat" => $currentTimestamp
);
$jwt = JWT::encode($jsonToken, $this->key, 'RS256');

$OAuthResponse = Requests::post( 'https://www.googleapis.com/oauth2/v4/token', array(), array(
'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
'assertion' => $jwt
) );
$OAuthResponse = Requests::post('https://www.googleapis.com/oauth2/v4/token', array(), array(
'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
'assertion' => $jwt
));

if ( $OAuthResponse->status_code == 200 ) {
$this->accessToken = json_decode( $OAuthResponse->body )->access_token;
if ($OAuthResponse->status_code == 200) {
$this->accessToken = json_decode($OAuthResponse->body)->access_token;

return true;
}
return true;
}

return false;
}
return false;
}

public function getAccessToken() {
$currentTime = time();
if ( $this->expireTimestamp < $currentTime ) {
$startTime = time();
$this->requestAccessToken();
$endTime = time();
$this->expireTimestamp -= ( $endTime - $startTime );
}
public function getAccessToken()
{
$currentTime = time();
if ($this->expireTimestamp < $currentTime) {
$startTime = time();
$this->requestAccessToken();
$endTime = time();
$this->expireTimestamp -= ($endTime - $startTime);
}

return $this->accessToken;
}
return $this->accessToken;
}
}
62 changes: 32 additions & 30 deletions src/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,36 @@
namespace JCFirebase;


class Option {
const __default = null;

const OPTION_SHALLOW = 'shallow';
const SHALLOW_TRUE = 'true';
const SHALLOW_FALSE = 'false';

const OPTION_PRINT = 'print';
const PRINT_PRETTY = 'pretty';
const PRINT_SILENT = 'silent';

const REQ_TYPE_GET = 0;
const REQ_TYPE_PUT = 1;
const REQ_TYPE_POST = 2;
const REQ_TYPE_PATCH = 3;
const REQ_TYPE_DELETE = 4;

public static function isAllowPrint( $reqType = self::REQ_TYPE_GET, $pType = self::PRINT_PRETTY ) {
if ( $pType == self::PRINT_PRETTY ) {
return true;
}

if ( $pType == self::PRINT_SILENT ) {
if ( $reqType == self::REQ_TYPE_DELETE ) {
return false;
}
}

return true;
}
class Option
{
const __default = null;

const OPTION_SHALLOW = 'shallow';
const SHALLOW_TRUE = 'true';
const SHALLOW_FALSE = 'false';

const OPTION_PRINT = 'print';
const PRINT_PRETTY = 'pretty';
const PRINT_SILENT = 'silent';

const REQ_TYPE_GET = 0;
const REQ_TYPE_PUT = 1;
const REQ_TYPE_POST = 2;
const REQ_TYPE_PATCH = 3;
const REQ_TYPE_DELETE = 4;

public static function isAllowPrint($reqType = self::REQ_TYPE_GET, $pType = self::PRINT_PRETTY)
{
if ($pType == self::PRINT_PRETTY) {
return true;
}

if ($pType == self::PRINT_SILENT) {
if ($reqType == self::REQ_TYPE_DELETE) {
return false;
}
}

return true;
}
}
Loading

0 comments on commit 4fce890

Please sign in to comment.