Skip to content

Commit

Permalink
Merge pull request #7 from humhub/enh/php-cs-fixer
Browse files Browse the repository at this point in the history
Use PHP CS Fixer
  • Loading branch information
luke- authored Oct 3, 2024
2 parents 3c166ad + 6ddbea8 commit 7a9258b
Show file tree
Hide file tree
Showing 16 changed files with 78 additions and 69 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/php-cs-fixer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: PHP CS Fixer

on: push

jobs:
fixers:
uses: humhub/actions/.github/workflows/module-php-cs-fixer.yml@main
2 changes: 0 additions & 2 deletions Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@

namespace humhub\modules\sso\jwt;


use humhub\modules\sso\jwt\authclient\JWT;
use Yii;

class Events
{

}
7 changes: 3 additions & 4 deletions authclient/JWT.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/
class JWT extends BaseClient implements StandaloneAuthClient
{

/**
* @var string url of the JWT provider
*/
Expand Down Expand Up @@ -53,7 +52,7 @@ class JWT extends BaseClient implements StandaloneAuthClient
public $allowedIPs = [];

/**
* @var boolean enable automatic login of 'allowed ips'.
* @var bool enable automatic login of 'allowed ips'.
*/
public $autoLogin = false;

Expand Down Expand Up @@ -109,9 +108,9 @@ public function setUserAttributes($userAttributes)
if (!isset($userAttributes['id'])) {
if ($this->idAttribute == 'email' && isset($userAttributes['email'])) {
$userAttributes['id'] = $userAttributes['email'];
} else if ($this->idAttribute == 'guid' && isset($userAttributes['guid'])) {
} elseif ($this->idAttribute == 'guid' && isset($userAttributes['guid'])) {
$userAttributes['guid'] = $userAttributes['guid'];
} else if ($this->idAttribute == 'username' && isset($userAttributes['username'])) {
} elseif ($this->idAttribute == 'username' && isset($userAttributes['username'])) {
$userAttributes['username'] = $userAttributes['username'];
}
}
Expand Down
2 changes: 1 addition & 1 deletion authclient/JWTPrimary.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ public function getSyncAttributes()
{
return $this->syncAttributes;
}
}
}
3 changes: 1 addition & 2 deletions config.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@
'namespace' => 'humhub\modules\sso\jwt',
'events' => [
[AuthController::class, AuthController::EVENT_BEFORE_ACTION, ['humhub\modules\sso\jwt\Module', 'onAuthClientCollectionInit']],
]
],
];
?>
5 changes: 5 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

1.1.4 (Unreleased)
-------------------------

- Enh: Use PHP CS Fixer

1.1.3 (November 23, 2023)
-------------------------

Expand Down
5 changes: 2 additions & 3 deletions examples/php/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

// Build token including your user data
$now = time();
$token = array(
$token = [
'iss' => 'example',
'jti' => md5($now . rand()),
'iat' => $now,
Expand All @@ -21,7 +21,7 @@
'firstname' => 'John',
'lastname' => 'Doe',
'email' => '[email protected]',
);
];

// Create JWT token
$jwt = JWT::encode($token, $key);
Expand All @@ -35,4 +35,3 @@
$location .= "&jwt=" . $jwt;

header("Location: " . $location);
?>
2 changes: 1 addition & 1 deletion examples/php/src/BeforeValidException.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

namespace Firebase\JWT;

class BeforeValidException extends \UnexpectedValueException
{

}
2 changes: 1 addition & 1 deletion examples/php/src/ExpiredException.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

namespace Firebase\JWT;

class ExpiredException extends \UnexpectedValueException
{

}
51 changes: 26 additions & 25 deletions examples/php/src/JWT.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php

namespace Firebase\JWT;
use \DomainException;
use \InvalidArgumentException;
use \UnexpectedValueException;
use \DateTime;

use DomainException;
use InvalidArgumentException;
use UnexpectedValueException;
use DateTime;

/**
* JSON Web Token implementation, based on this spec:
Expand All @@ -21,7 +22,6 @@
*/
class JWT
{

/**
* When checking nbf, iat or expiration times,
* we want to provide some extra leeway time to
Expand All @@ -37,12 +37,12 @@ class JWT
*/
public static $timestamp = null;

public static $supported_algs = array(
'HS256' => array('hash_hmac', 'SHA256'),
'HS512' => array('hash_hmac', 'SHA512'),
'HS384' => array('hash_hmac', 'SHA384'),
'RS256' => array('openssl', 'SHA256'),
);
public static $supported_algs = [
'HS256' => ['hash_hmac', 'SHA256'],
'HS512' => ['hash_hmac', 'SHA512'],
'HS384' => ['hash_hmac', 'SHA384'],
'RS256' => ['openssl', 'SHA256'],
];

/**
* Decodes a JWT string into a PHP object.
Expand All @@ -64,7 +64,7 @@ class JWT
* @uses jsonDecode
* @uses urlsafeB64Decode
*/
public static function decode($jwt, $key, $allowed_algs = array())
public static function decode($jwt, $key, $allowed_algs = [])
{
$timestamp = is_null(static::$timestamp) ? time() : static::$timestamp;

Expand All @@ -86,7 +86,7 @@ public static function decode($jwt, $key, $allowed_algs = array())
throw new UnexpectedValueException('Invalid claims encoding');
}
$sig = static::urlsafeB64Decode($cryptob64);

if (empty($header->alg)) {
throw new UnexpectedValueException('Empty algorithm');
}
Expand All @@ -113,7 +113,7 @@ public static function decode($jwt, $key, $allowed_algs = array())
// token can actually be used. If it's not yet that time, abort.
if (isset($payload->nbf) && $payload->nbf > ($timestamp + static::$leeway)) {
throw new BeforeValidException(
'Cannot handle token prior to ' . date(DateTime::ISO8601, $payload->nbf)
'Cannot handle token prior to ' . date(DateTime::ISO8601, $payload->nbf),
);
}

Expand All @@ -122,7 +122,7 @@ public static function decode($jwt, $key, $allowed_algs = array())
// correctly used the nbf claim).
if (isset($payload->iat) && $payload->iat > ($timestamp + static::$leeway)) {
throw new BeforeValidException(
'Cannot handle token prior to ' . date(DateTime::ISO8601, $payload->iat)
'Cannot handle token prior to ' . date(DateTime::ISO8601, $payload->iat),
);
}

Expand Down Expand Up @@ -152,14 +152,14 @@ public static function decode($jwt, $key, $allowed_algs = array())
*/
public static function encode($payload, $key, $alg = 'HS256', $keyId = null, $head = null)
{
$header = array('typ' => 'JWT', 'alg' => $alg);
$header = ['typ' => 'JWT', 'alg' => $alg];
if ($keyId !== null) {
$header['kid'] = $keyId;
}
if ( isset($head) && is_array($head) ) {
if (isset($head) && is_array($head)) {
$header = array_merge($head, $header);
}
$segments = array();
$segments = [];
$segments[] = static::urlsafeB64Encode(static::jsonEncode($header));
$segments[] = static::urlsafeB64Encode(static::jsonEncode($payload));
$signing_input = implode('.', $segments);
Expand Down Expand Up @@ -188,7 +188,7 @@ public static function sign($msg, $key, $alg = 'HS256')
throw new DomainException('Algorithm not supported');
}
list($function, $algorithm) = static::$supported_algs[$alg];
switch($function) {
switch ($function) {
case 'hash_hmac':
return hash_hmac($algorithm, $msg, $key, true);
case 'openssl':
Expand Down Expand Up @@ -222,14 +222,15 @@ private static function verify($msg, $signature, $key, $alg)
}

list($function, $algorithm) = static::$supported_algs[$alg];
switch($function) {
switch ($function) {
case 'openssl':
$success = openssl_verify($msg, $signature, $key, $algorithm);
if (!$success) {
throw new DomainException("OpenSSL unable to verify data: " . openssl_error_string());
} else {
return $signature;
}
// no break
case 'hash_hmac':
default:
$hash = hash_hmac($algorithm, $msg, $key, true);
Expand Down Expand Up @@ -271,7 +272,7 @@ public static function jsonDecode($input)
*them to strings) before decoding, hence the preg_replace() call.
*/
$max_int_length = strlen((string) PHP_INT_MAX) - 1;
$json_without_bigints = preg_replace('/:\s*(-?\d{'.$max_int_length.',})/', ': "$1"', $input);
$json_without_bigints = preg_replace('/:\s*(-?\d{' . $max_int_length . ',})/', ': "$1"', $input);
$obj = json_decode($json_without_bigints);
}

Expand Down Expand Up @@ -341,15 +342,15 @@ public static function urlsafeB64Encode($input)
*/
private static function handleJsonError($errno)
{
$messages = array(
$messages = [
JSON_ERROR_DEPTH => 'Maximum stack depth exceeded',
JSON_ERROR_CTRL_CHAR => 'Unexpected control character found',
JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON'
);
JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON',
];
throw new DomainException(
isset($messages[$errno])
? $messages[$errno]
: 'Unknown JSON error: ' . $errno
: 'Unknown JSON error: ' . $errno,
);
}

Expand Down
2 changes: 1 addition & 1 deletion examples/php/src/SignatureInvalidException.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

namespace Firebase\JWT;

class SignatureInvalidException extends \UnexpectedValueException
{

}
2 changes: 1 addition & 1 deletion module.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"sso",
"login"
],
"version": "1.1.3",
"version": "1.1.4",
"humhub": {
"minVersion": "1.14"
},
Expand Down
2 changes: 1 addition & 1 deletion vendors/php-jwt/src/BeforeValidException.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

namespace Firebase\JWT;

class BeforeValidException extends \UnexpectedValueException
{

}
2 changes: 1 addition & 1 deletion vendors/php-jwt/src/ExpiredException.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

namespace Firebase\JWT;

class ExpiredException extends \UnexpectedValueException
{

}
Loading

0 comments on commit 7a9258b

Please sign in to comment.