Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -16,7 +16,7 @@
}
],
"require": {
"cube-php/core": "^0.1.17",
"cube-php/core": "^0.1.23",
"firebase/php-jwt": "^6.10"
},
"extra": {
Expand Down
55 changes: 48 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions src/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*
Expand Down
9 changes: 9 additions & 0 deletions src/AuthTokenType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Cube\Packages\Auth;

enum AuthTokenType: string
{
case BEARER = 'Bearer';
case BASIC = 'Basic';
}