Skip to content

Commit

Permalink
Merge pull request #5 from hans-thomas/analysis-EPd2Jv
Browse files Browse the repository at this point in the history
Apply fixes from StyleCI
  • Loading branch information
hans-thomas committed Jul 31, 2023
2 parents 70e61da + 8b7b6a6 commit 5d41783
Show file tree
Hide file tree
Showing 40 changed files with 3,636 additions and 3,636 deletions.
44 changes: 22 additions & 22 deletions src/Drivers/Constraints/ExpirationValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@

namespace Hans\Sphinx\Drivers\Constraints;

use DateTimeImmutable;
use Hans\Sphinx\Exceptions\SphinxErrorCode;
use Hans\Sphinx\Exceptions\SphinxException;
use Lcobucci\JWT\Token;
use Lcobucci\JWT\Validation\Constraint;
use Symfony\Component\HttpFoundation\Response as ResponseAlias;
use DateTimeImmutable;
use Hans\Sphinx\Exceptions\SphinxErrorCode;
use Hans\Sphinx\Exceptions\SphinxException;
use Lcobucci\JWT\Token;
use Lcobucci\JWT\Validation\Constraint;
use Symfony\Component\HttpFoundation\Response as ResponseAlias;

final class ExpirationValidator implements Constraint
final class ExpirationValidator implements Constraint
{
/**
* @param Token $token
*
* @throws SphinxException
*/
public function assert(Token $token): void
{
/**
* @param Token $token
*
* @throws SphinxException
*/
public function assert(Token $token): void
{
$diff = ( new DateTimeImmutable('UTC') )->diff($token->claims()->get('exp'));
if ('-' == $diff->format('%R')) {
throw new SphinxException(
'Token expired!',
SphinxErrorCode::TOKEN_EXPIRED,
ResponseAlias::HTTP_FORBIDDEN
);
}
$diff = ( new DateTimeImmutable('UTC') )->diff($token->claims()->get('exp'));
if ('-' == $diff->format('%R')) {
throw new SphinxException(
'Token expired!',
SphinxErrorCode::TOKEN_EXPIRED,
ResponseAlias::HTTP_FORBIDDEN
);
}
}
}
74 changes: 37 additions & 37 deletions src/Drivers/Constraints/RoleIdValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,47 @@

namespace Hans\Sphinx\Drivers\Constraints;

use Hans\Sphinx\Exceptions\SphinxErrorCode;
use Hans\Sphinx\Exceptions\SphinxException;
use Lcobucci\JWT\Token;
use Lcobucci\JWT\Validation\Constraint;
use Symfony\Component\HttpFoundation\Response as ResponseAlias;
use Hans\Sphinx\Exceptions\SphinxErrorCode;
use Hans\Sphinx\Exceptions\SphinxException;
use Lcobucci\JWT\Token;
use Lcobucci\JWT\Validation\Constraint;
use Symfony\Component\HttpFoundation\Response as ResponseAlias;

final class RoleIdValidator implements Constraint
final class RoleIdValidator implements Constraint
{
/**
* @param Token $token
*
* @throws SphinxException
*/
public function assert(Token $token): void
{
/**
* @param Token $token
*
* @throws SphinxException
*/
public function assert(Token $token): void
{
$role_id = $token->headers()->get('role_id', false);
$role_version = $token->headers()->get('role_version', false);
$role_id = $token->headers()->get('role_id', false);
$role_version = $token->headers()->get('role_version', false);

if (!$role_id) {
throw new SphinxException(
'Role id not found in header!',
SphinxErrorCode::ROLE_NOT_FOUND,
ResponseAlias::HTTP_FORBIDDEN
);
}
if (!$role_version) {
throw new SphinxException(
'Role\'s version not found in header!',
SphinxErrorCode::ROLE_VERSION_NOT_FOUND,
ResponseAlias::HTTP_FORBIDDEN
);
}
if (!$role_id) {
throw new SphinxException(
'Role id not found in header!',
SphinxErrorCode::ROLE_NOT_FOUND,
ResponseAlias::HTTP_FORBIDDEN
);
}
if (!$role_version) {
throw new SphinxException(
'Role\'s version not found in header!',
SphinxErrorCode::ROLE_VERSION_NOT_FOUND,
ResponseAlias::HTTP_FORBIDDEN
);
}

$role = app(sphinx_config('role_model'))->findAndCache($role_id);
$role = app(sphinx_config('role_model'))->findAndCache($role_id);

if ($role->getVersion() != $role_version) {
throw new SphinxException(
'User\'s token is out-of-date!',
SphinxErrorCode::TOKEN_IS_OUT_OF_DATE,
ResponseAlias::HTTP_FORBIDDEN
);
}
if ($role->getVersion() != $role_version) {
throw new SphinxException(
'User\'s token is out-of-date!',
SphinxErrorCode::TOKEN_IS_OUT_OF_DATE,
ResponseAlias::HTTP_FORBIDDEN
);
}
}
}
74 changes: 37 additions & 37 deletions src/Drivers/Constraints/SecretVerificationValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,47 @@

namespace Hans\Sphinx\Drivers\Constraints;

use Hans\Sphinx\Exceptions\SphinxErrorCode;
use Hans\Sphinx\Exceptions\SphinxException;
use Lcobucci\JWT\Signer;
use Lcobucci\JWT\Token;
use Lcobucci\JWT\Validation\Constraint;
use Symfony\Component\HttpFoundation\Response;
use Hans\Sphinx\Exceptions\SphinxErrorCode;
use Hans\Sphinx\Exceptions\SphinxException;
use Lcobucci\JWT\Signer;
use Lcobucci\JWT\Token;
use Lcobucci\JWT\Validation\Constraint;
use Symfony\Component\HttpFoundation\Response;

final class SecretVerificationValidator implements Constraint
final class SecretVerificationValidator implements Constraint
{
private Signer $signer;
private Signer\Key $key;

public function __construct(Signer $signer, Signer\Key $key)
{
private Signer $signer;
private Signer\Key $key;
$this->signer = $signer;
$this->key = $key;
}

public function __construct(Signer $signer, Signer\Key $key)
{
$this->signer = $signer;
$this->key = $key;
/**
* @param Token $token
*
* @throws SphinxException
*
* @return void
*/
public function assert(Token $token): void
{
if ($token->headers()->get('alg') !== $this->signer->algorithmId()) {
throw new SphinxException(
'Token signer mismatch!',
SphinxErrorCode::TOKEN_MISMATCH,
Response::HTTP_FORBIDDEN
);
}

/**
* @param Token $token
*
* @throws SphinxException
*
* @return void
*/
public function assert(Token $token): void
{
if ($token->headers()->get('alg') !== $this->signer->algorithmId()) {
throw new SphinxException(
'Token signer mismatch!',
SphinxErrorCode::TOKEN_MISMATCH,
Response::HTTP_FORBIDDEN
);
}

if (!$this->signer->verify($token->signature()->hash(), $token->payload(), $this->key)) {
throw new SphinxException(
'Token signature mismatch!',
SphinxErrorCode::TOKEN_MISMATCH,
Response::HTTP_FORBIDDEN
);
}
if (!$this->signer->verify($token->signature()->hash(), $token->payload(), $this->key)) {
throw new SphinxException(
'Token signature mismatch!',
SphinxErrorCode::TOKEN_MISMATCH,
Response::HTTP_FORBIDDEN
);
}
}
}
76 changes: 38 additions & 38 deletions src/Drivers/Constraints/SessionIdValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,48 @@

namespace Hans\Sphinx\Drivers\Constraints;

use Hans\Sphinx\Exceptions\SphinxErrorCode;
use Hans\Sphinx\Exceptions\SphinxException;
use Hans\Sphinx\Models\Session;
use Lcobucci\JWT\Token;
use Lcobucci\JWT\Validation\Constraint;
use Symfony\Component\HttpFoundation\Response as ResponseAlias;
use Hans\Sphinx\Exceptions\SphinxErrorCode;
use Hans\Sphinx\Exceptions\SphinxException;
use Hans\Sphinx\Models\Session;
use Lcobucci\JWT\Token;
use Lcobucci\JWT\Validation\Constraint;
use Symfony\Component\HttpFoundation\Response as ResponseAlias;

final class SessionIdValidator implements Constraint
final class SessionIdValidator implements Constraint
{
/**
* @param Token $token
*
* @throws SphinxException
*/
public function assert(Token $token): void
{
/**
* @param Token $token
*
* @throws SphinxException
*/
public function assert(Token $token): void
{
$session_id = $token->headers()->get('session_id', false);
$sessionable_version = $token->headers()->get('sessionable_version', false);
$session_id = $token->headers()->get('session_id', false);
$sessionable_version = $token->headers()->get('sessionable_version', false);

if (!$session_id) {
throw new SphinxException(
'Session id not found in header!',
SphinxErrorCode::SESSION_NOT_FOUND,
ResponseAlias::HTTP_FORBIDDEN
);
}
if (!$sessionable_version) {
throw new SphinxException(
"User's version not found in header!",
SphinxErrorCode::USERS_VERSION_NOT_FOUND,
ResponseAlias::HTTP_FORBIDDEN
);
}
if (!$session_id) {
throw new SphinxException(
'Session id not found in header!',
SphinxErrorCode::SESSION_NOT_FOUND,
ResponseAlias::HTTP_FORBIDDEN
);
}
if (!$sessionable_version) {
throw new SphinxException(
"User's version not found in header!",
SphinxErrorCode::USERS_VERSION_NOT_FOUND,
ResponseAlias::HTTP_FORBIDDEN
);
}

$session = Session::findAndCache($session_id);
$session = Session::findAndCache($session_id);

if ($session->sessionable_version != $sessionable_version) {
throw new SphinxException(
'Token is out-of-date!',
SphinxErrorCode::TOKEN_IS_OUT_OF_DATE,
ResponseAlias::HTTP_FORBIDDEN
);
}
if ($session->sessionable_version != $sessionable_version) {
throw new SphinxException(
'Token is out-of-date!',
SphinxErrorCode::TOKEN_IS_OUT_OF_DATE,
ResponseAlias::HTTP_FORBIDDEN
);
}
}
}
Loading

0 comments on commit 5d41783

Please sign in to comment.