Skip to content

Commit

Permalink
Merge pull request #6 from hans-thomas/adding-styleci-badge
Browse files Browse the repository at this point in the history
- Adding styleci badge
  • Loading branch information
hans-thomas committed Jul 31, 2023
2 parents 33bd8e7 + 5d41783 commit 870841c
Show file tree
Hide file tree
Showing 42 changed files with 3,638 additions and 3,636 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/hans-thomas/sphinx/php.yml)
![GitHub top language](https://img.shields.io/github/languages/top/hans-thomas/sphinx)
![GitHub release (latest by date)](https://img.shields.io/github/v/release/hans-thomas/sphinx)
![StyleCi](https://github.styleci.io/repos/464496173/shield?style=plastic)

Sphinx is a feature reach Jwt-based authentication system that make zero queries to database during authorization.

Expand Down
1 change: 1 addition & 0 deletions docs/content/docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ toc = false
![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/hans-thomas/sphinx/php.yml)
![GitHub top language](https://img.shields.io/github/languages/top/hans-thomas/sphinx)
![GitHub release (latest by date)](https://img.shields.io/github/v/release/hans-thomas/sphinx)
![StyleCi](https://github.styleci.io/repos/464496173/shield?style=plastic)

Install the package via composer

Expand Down
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 870841c

Please sign in to comment.