Skip to content

Commit

Permalink
Refactor auth middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
jenky committed Jun 21, 2023
1 parent 5a0a542 commit 864b439
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 32 deletions.
21 changes: 21 additions & 0 deletions src/Middleware/Auth/AuthenticationMiddlewareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Jenky\Atlas\Middleware\Auth;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

trait AuthenticationMiddlewareTrait
{
/**
* @var string|\Stringable
*/
private $token;

public function __invoke(RequestInterface $request, callable $next): ResponseInterface
{
return $next($request->withHeader('Authorization', (string) $this->token));
}
}
23 changes: 3 additions & 20 deletions src/Middleware/Auth/BasicAuthentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,14 @@

namespace Jenky\Atlas\Middleware\Auth;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

final class BasicAuthentication
{
/**
* @var string
*/
private $username;

/**
* @var string
*/
private $password;
use AuthenticationMiddlewareTrait;

public function __construct(string $username, string $password)
{
$this->username = $username;
$this->password = $password;
}

public function __invoke(RequestInterface $request, callable $next): ResponseInterface
{
$credential = $this->username.':'.$this->password;
$credential = $username.':'.$password;

return $next($request->withHeader('Authorization', 'Basic '.base64_encode($credential)));
$this->token = 'Basic '.base64_encode($credential);
}
}
16 changes: 5 additions & 11 deletions src/Middleware/Auth/TokenAuthentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,17 @@

namespace Jenky\Atlas\Middleware\Auth;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

final class TokenAuthentication
{
/**
* @var string
*/
private $token;
use AuthenticationMiddlewareTrait;

public function __construct(string $token, string $tokenPrefix = 'Bearer')
public function __construct(string $token)
{
$this->token = $tokenPrefix.' '.trim($token);
$this->token = $token;
}

public function __invoke(RequestInterface $request, callable $next): ResponseInterface
public static function from(string $token, string $tokenPrefix = 'Bearer'): self
{
return $next($request->withHeader('Authorization', $this->token));
return new self($tokenPrefix.' '.trim($token));
}
}
2 changes: 1 addition & 1 deletion tests/AuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function test_token_auth(): void

$this->assertTrue($response->unauthorized());

$connector->middleware()->before('fake_auth', new TokenAuthentication('#zKh#4KNu$Bq4^b97KJ6'));
$connector->middleware()->before('fake_auth', TokenAuthentication::from('#zKh#4KNu$Bq4^b97KJ6'));

$response = $connector->send(new DummyRequest('http://localhost'));

Expand Down

0 comments on commit 864b439

Please sign in to comment.