Skip to content

Commit f1b5935

Browse files
committed
dispense Authentication service from HttpContext
1 parent 72bff50 commit f1b5935

File tree

10 files changed

+15
-28
lines changed

10 files changed

+15
-28
lines changed

lib/Extensions/ServiceCollectionExtensions.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ public static function addAntiForgery(IServiceCollection $services, Closure $con
5757

5858
public static function addAuthentication(IServiceCollection $services, Closure $configure)
5959
{
60-
$services->addSingleton(IAuthentication::class, function ($provider) use ($configure): Authentication {
61-
$builder = new AuthenticationBuilder($provider->getService(HttpContext::class));
60+
$services->addSingleton(IAuthentication::class, function () use ($configure): Authentication {
61+
$builder = new AuthenticationBuilder();
6262
$configure($builder);
6363
return $builder->build();
6464
});

lib/Security/Authentication/AuthenticationBuilder.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
namespace DevNet\Web\Security\Authentication;
1010

11-
use DevNet\System\Runtime\LauncherProperties;
12-
use DevNet\Web\Http\Message\HttpContext;
1311
use DevNet\Web\Security\Authentication\JwtBearer\JwtBearerHandler;
1412
use DevNet\Web\Security\Authentication\JwtBearer\JwtBearerOptions;
1513
use DevNet\Web\Security\Authentication\Cookies\AuthenticationCookieHandler;
@@ -19,12 +17,6 @@
1917
class AuthenticationBuilder
2018
{
2119
private array $authentications;
22-
private HttpContext $httpContext;
23-
24-
public function __construct(HttpContext $httpContext)
25-
{
26-
$this->httpContext = $httpContext;
27-
}
2820

2921
public function addCookie(string $authenticationScheme = AuthenticationScheme::CookieSession, Closure $configuration = null): void
3022
{
@@ -43,7 +35,7 @@ public function addJwtBearer(string $authenticationScheme = AuthenticationScheme
4335
$configuration($options);
4436
}
4537

46-
$this->authentications[$authenticationScheme] = new JwtBearerHandler($this->httpContext, $options);
38+
$this->authentications[$authenticationScheme] = new JwtBearerHandler($options);
4739
}
4840

4941
public function build(): Authentication

lib/Security/Authentication/Cookies/AuthenticationCookieHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class AuthenticationCookieHandler implements IAuthenticationHandler, IAuthentica
2727
public function __construct(AuthenticationCookieOptions $options)
2828
{
2929
$this->options = $options;
30-
$this->session = new Session($options->CookieName);
30+
$this->session = new Session($options->CookieName, $options->CookiePath);
3131

3232
if (!$this->options->ExpireTime) {
3333
$this->options->ExpireTime = new TimeSpan();

lib/Security/Authentication/Cookies/AuthenticationCookieOptions.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
namespace DevNet\Web\Security\Authentication\Cookies;
1010

11-
use DevNet\System\Runtime\LauncherProperties;
1211
use DevNet\System\TimeSpan;
1312

1413
class AuthenticationCookieOptions
@@ -22,9 +21,7 @@ public function __construct(string $cookieName = 'Identity', string $cookiePath
2221
$this->CookieName = $cookieName;
2322
$this->CookiePath = $cookiePath;
2423

25-
if ($this->CookieName == 'Identity') {
26-
$this->CookieName .= "-" . md5(LauncherProperties::getRootDirectory());
27-
}
24+
$this->CookieName = $this->CookieName . "-" . md5($this->CookieName . $_SERVER['DOCUMENT_ROOT']);
2825

2926
if (!$expireTime) {
3027
$this->ExpireTime = TimeSpan::fromDays(7);

lib/Security/Authentication/JwtBearer/JwtBearerHandler.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
namespace DevNet\Web\Security\Authentication\JwtBearer;
1010

1111
use DevNet\System\PropertyTrait;
12-
use DevNet\Web\Http\Message\HttpContext;
1312
use DevNet\Web\Security\Authentication\AuthenticationResult;
1413
use DevNet\Web\Security\Authentication\IAuthenticationHandler;
1514
use DevNet\Web\Security\Tokens\Jwt\JwtSecurityTokenHandler;
@@ -19,13 +18,11 @@ class JwtBearerHandler implements IAuthenticationHandler
1918
{
2019
use PropertyTrait;
2120

22-
private HttpContext $HttpContext;
2321
private JwtBearerOptions $options;
2422
private JwtSecurityTokenHandler $handler;
2523

26-
public function __construct(HttpContext $httpContext, JwtBearerOptions $options)
24+
public function __construct(JwtBearerOptions $options)
2725
{
28-
$this->httpContext = $httpContext;
2926
$this->options = $options;
3027
$this->handler = new JwtSecurityTokenHandler();
3128
}
@@ -37,12 +34,13 @@ public function get_Options(): JwtBearerOptions
3734

3835
public function readToken(): string
3936
{
40-
$headers = $this->httpContext->Request->Headers->getValues('Authorization');
41-
if (!$headers) {
37+
$headers = getallheaders();;
38+
$bearerToken = $headers['Authorization'];
39+
if (!$bearerToken) {
4240
throw new Exception("The request is missing the authorization header!");
4341
}
4442

45-
if (!preg_match("/^Bearer\s+(.*)$/", $headers[0], $matches)) {
43+
if (!preg_match("/^Bearer\s+(.*)$/", $bearerToken[0], $matches)) {
4644
throw new Exception("Incorrect authentication header scheme!");
4745
}
4846

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @link https://github.com/DevNet-Framework
77
*/
88

9-
namespace DevNet\Web\Security\Tokens;
9+
namespace DevNet\Web\Security;
1010

1111
class Base64UrlEncoder
1212
{

lib/Security/Tokens/Csrf/AntiforgeryOptions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ class AntiForgeryOptions
1717

1818
public function __construct()
1919
{
20-
$this->CookieName = $this->CookieName . "-" . md5($this->CookieName . $_SERVER['DOCUMENT_ROOT']);;
20+
$this->CookieName = $this->CookieName . "-" . md5($this->CookieName . $_SERVER['DOCUMENT_ROOT']);
2121
}
2222
}

lib/Security/Tokens/Jwt/JwtHeader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace DevNet\Web\Security\Tokens\Jwt;
1010

11-
use DevNet\Web\Security\Tokens\Base64UrlEncoder;
11+
use DevNet\Web\Security\Base64UrlEncoder;
1212

1313
class JwtHeader
1414
{

lib/Security/Tokens/Jwt/JwtPayload.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace DevNet\Web\Security\Tokens\Jwt;
1010

1111
use DevNet\Web\Security\Claims\ClaimsIdentity;
12-
use DevNet\Web\Security\Tokens\Base64UrlEncoder;
12+
use DevNet\Web\Security\Base64UrlEncoder;
1313

1414
class JwtPayload
1515
{

lib/Security/Tokens/Jwt/JwtSecurityTokenHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use DevNet\Web\Security\Claims\Claim;
1212
use DevNet\Web\Security\Claims\ClaimsIdentity;
13-
use DevNet\Web\Security\Tokens\Base64UrlEncoder;
13+
use DevNet\Web\Security\Base64UrlEncoder;
1414
use DevNet\Web\Security\Tokens\Jwt\JwtSecurityToken;
1515
use DateTime;
1616

0 commit comments

Comments
 (0)