diff --git a/composer.json b/composer.json index c0c8b87..8bd68b4 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "cube-php/auth", "description": "PHP Cube Authentication Package", "type": "package", - "version": "0.0.4", + "version": "0.0.5", "license": "MIT", "autoload": { "psr-4": { @@ -16,7 +16,7 @@ } ], "require": { - "cube-php/core": "^0.1.17", + "cube-php/core": "^0.1.23", "firebase/php-jwt": "^6.10" }, "extra": { diff --git a/composer.lock b/composer.lock index a28e983..896bff8 100644 --- a/composer.lock +++ b/composer.lock @@ -4,23 +4,24 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "a8ea73fd282943a1bbbb31c0f19ff0e9", + "content-hash": "dbea4369fc1bf6ee0a72fb4bc444b62b", "packages": [ { "name": "cube-php/core", - "version": "0.1.17", + "version": "0.1.23", "source": { "type": "git", "url": "https://github.com/cube-php/core.git", - "reference": "7e943783f17724555d03c9622d03fad0d221da81" + "reference": "38b12a94909d93d03aeca1fe741603a4f88f3c73" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/cube-php/core/zipball/7e943783f17724555d03c9622d03fad0d221da81", - "reference": "7e943783f17724555d03c9622d03fad0d221da81", + "url": "https://api.github.com/repos/cube-php/core/zipball/38b12a94909d93d03aeca1fe741603a4f88f3c73", + "reference": "38b12a94909d93d03aeca1fe741603a4f88f3c73", "shasum": "" }, "require": { + "dammynex/pagenator": "^0.1.4", "symfony/cache": "^5.2", "symfony/console": "^5.2", "symfony/process": "^5.2", @@ -48,9 +49,49 @@ "description": "PHP Cube core utilities", "support": { "issues": "https://github.com/cube-php/core/issues", - "source": "https://github.com/cube-php/core/tree/v0.1.17" + "source": "https://github.com/cube-php/core/tree/v0.1.23" }, - "time": "2024-04-30T16:42:59+00:00" + "time": "2024-08-15T16:47:14+00:00" + }, + { + "name": "dammynex/pagenator", + "version": "0.1.4", + "source": { + "type": "git", + "url": "https://github.com/dammynex/pagenator.git", + "reference": "0757d1c59acb2fc6e476878cd09a3468ab22c573" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/dammynex/pagenator/zipball/0757d1c59acb2fc6e476878cd09a3468ab22c573", + "reference": "0757d1c59acb2fc6e476878cd09a3468ab22c573", + "shasum": "" + }, + "require-dev": { + "pestphp/pest": "^0.3.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Dammynex\\Pagenator\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Salawu Oluwadamilola Ezekiel", + "email": "damilolaoffical@gmail.com" + } + ], + "description": "Simple PHP Pager library", + "support": { + "issues": "https://github.com/dammynex/pagenator/issues", + "source": "https://github.com/dammynex/pagenator/tree/v0.1.4" + }, + "time": "2021-09-12T20:45:48+00:00" }, { "name": "firebase/php-jwt", diff --git a/src/Auth.php b/src/Auth.php index ba770b7..b5e5c1d 100644 --- a/src/Auth.php +++ b/src/Auth.php @@ -4,6 +4,7 @@ use Cube\App\App; use Cube\Http\Cookie; +use Cube\Http\Request; use Cube\Http\Session; use Cube\Interfaces\ModelInterface; use Cube\Misc\EventManager; @@ -174,6 +175,39 @@ public static function authByKey($value, bool $remember = false): bool return true; } + /** + * Retrieve token from request header + * + * @param Request $request + * @param AuthTokenType $type + * @return string + */ + public static function getTokenFromRequest(Request $request, AuthTokenType $type): string + { + $header = $request->getServer()->get('http_authorization'); + + if (!$header) { + throw new AuthException('Authorization header not found'); + } + + $specified_type = strtolower($type->value); + $sent_type = substr($header, 0, strlen($specified_type)); + + if (!$sent_type) { + throw new AuthException('No authorization'); + } + + if ($specified_type !== strtolower($sent_type)) { + throw new AuthException('Invalid authorization type'); + } + + $token = trim( + substr($header, strlen($specified_type)) + ); + + return $token; + } + /** * User * diff --git a/src/AuthTokenType.php b/src/AuthTokenType.php new file mode 100644 index 0000000..67d1d35 --- /dev/null +++ b/src/AuthTokenType.php @@ -0,0 +1,9 @@ +