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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## [Unreleased]

### Changed

- `Innmind\HttpAuthentication\Authenticator::__invoke()` now returns an `Innmind\Immutable\Attempt`
- All resolvers now return an `Innmind\Immutable\Attempt`

## 4.0.0 - 2023-11-01

### Changed
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
},
"require": {
"php": "~8.2",
"innmind/immutable": "~5.18",
"innmind/http": "~7.0"
},
"autoload": {
Expand Down
10 changes: 5 additions & 5 deletions src/Any.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Innmind\Http\ServerRequest;
use Innmind\Immutable\{
Sequence,
Maybe,
Attempt,
};

final class Any implements Authenticator
Expand All @@ -22,12 +22,12 @@ public function __construct(Authenticator ...$authenticators)
$this->authenticators = Sequence::of(...$authenticators);
}

public function __invoke(ServerRequest $request): Maybe
public function __invoke(ServerRequest $request): Attempt
{
/** @var Maybe<Identity> */
/** @var Attempt<Identity> */
return $this->authenticators->reduce(
Maybe::nothing(),
static fn(Maybe $identity, $authenticate) => $identity->otherwise(
Attempt::error(new \LogicException('No authenticator defined')),
static fn(Attempt $identity, $authenticate) => $identity->recover(
static fn() => $authenticate($request),
),
);
Expand Down
6 changes: 3 additions & 3 deletions src/Authenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
namespace Innmind\HttpAuthentication;

use Innmind\Http\ServerRequest;
use Innmind\Immutable\Maybe;
use Innmind\Immutable\Attempt;

interface Authenticator
{
/**
* @return Maybe<Identity>
* @return Attempt<Identity>
*/
public function __invoke(ServerRequest $request): Maybe;
public function __invoke(ServerRequest $request): Attempt;
}
5 changes: 3 additions & 2 deletions src/ValidateAuthorizationHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
ServerRequest,
Header\Authorization,
};
use Innmind\Immutable\Maybe;
use Innmind\Immutable\Attempt;

final class ValidateAuthorizationHeader implements Authenticator
{
Expand All @@ -18,7 +18,7 @@ public function __construct(Authenticator $authenticate)
$this->authenticate = $authenticate;
}

public function __invoke(ServerRequest $request): Maybe
public function __invoke(ServerRequest $request): Attempt
{
if (!$request->headers()->contains('Authorization')) {
return ($this->authenticate)($request);
Expand All @@ -27,6 +27,7 @@ public function __invoke(ServerRequest $request): Maybe
return $request
->headers()
->find(Authorization::class)
->attempt(static fn() => new \RuntimeException('No Authorization header'))
->flatMap(fn() => ($this->authenticate)($request));
}
}
5 changes: 3 additions & 2 deletions src/ViaAuthorization.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Header\AuthorizationValue,
};
use Innmind\Immutable\{
Maybe,
Attempt,
Predicate\Instance,
};

Expand All @@ -23,13 +23,14 @@ public function __construct(Resolver $resolve)
$this->resolve = $resolve;
}

public function __invoke(ServerRequest $request): Maybe
public function __invoke(ServerRequest $request): Attempt
{
return $request
->headers()
->find(Authorization::class)
->flatMap(static fn($header) => $header->values()->find(static fn() => true))
->keep(Instance::of(AuthorizationValue::class))
->attempt(static fn() => new \RuntimeException('Failed to resolve identity'))
->flatMap(fn($value) => ($this->resolve)($value));
}
}
8 changes: 4 additions & 4 deletions src/ViaAuthorization/NullResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

use Innmind\HttpAuthentication\Identity;
use Innmind\Http\Header\AuthorizationValue;
use Innmind\Immutable\Maybe;
use Innmind\Immutable\Attempt;

final class NullResolver implements Resolver
{
public function __invoke(AuthorizationValue $value): Maybe
public function __invoke(AuthorizationValue $value): Attempt
{
/** @var Maybe<Identity> */
return Maybe::nothing();
/** @var Attempt<Identity> */
return Attempt::error(new \LogicException('Not implemented'));
}
}
6 changes: 3 additions & 3 deletions src/ViaAuthorization/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

use Innmind\HttpAuthentication\Identity;
use Innmind\Http\Header\AuthorizationValue;
use Innmind\Immutable\Maybe;
use Innmind\Immutable\Attempt;

interface Resolver
{
/**
* @return Maybe<Identity>
* @return Attempt<Identity>
*/
public function __invoke(AuthorizationValue $value): Maybe;
public function __invoke(AuthorizationValue $value): Attempt;
}
5 changes: 3 additions & 2 deletions src/ViaBasicAuthorization.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
ServerRequest,
Header\Authorization,
};
use Innmind\Immutable\Maybe;
use Innmind\Immutable\Attempt;

final class ViaBasicAuthorization implements Authenticator
{
Expand All @@ -19,12 +19,13 @@ public function __construct(Resolver $resolve)
$this->resolve = $resolve;
}

public function __invoke(ServerRequest $request): Maybe
public function __invoke(ServerRequest $request): Attempt
{
return $request
->headers()
->find(Authorization::class)
->filter(static fn($header) => $header->scheme() === 'Basic')
->attempt(static fn() => new \RuntimeException('Failed to resolve identity'))
->flatMap(function($header) {
[$user, $password] = \explode(':', \base64_decode($header->parameter(), true));

Expand Down
8 changes: 4 additions & 4 deletions src/ViaBasicAuthorization/NullResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
namespace Innmind\HttpAuthentication\ViaBasicAuthorization;

use Innmind\HttpAuthentication\Identity;
use Innmind\Immutable\Maybe;
use Innmind\Immutable\Attempt;

final class NullResolver implements Resolver
{
public function __invoke(string $user, string $password): Maybe
public function __invoke(string $user, string $password): Attempt
{
/** @var Maybe<Identity> */
return Maybe::nothing();
/** @var Attempt<Identity> */
return Attempt::error(new \LogicException('Not implemented'));
}
}
6 changes: 3 additions & 3 deletions src/ViaBasicAuthorization/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
namespace Innmind\HttpAuthentication\ViaBasicAuthorization;

use Innmind\HttpAuthentication\Identity;
use Innmind\Immutable\Maybe;
use Innmind\Immutable\Attempt;

interface Resolver
{
/**
* @return Maybe<Identity>
* @return Attempt<Identity>
*/
public function __invoke(string $user, string $password): Maybe;
public function __invoke(string $user, string $password): Attempt;
}
8 changes: 6 additions & 2 deletions src/ViaForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
ServerRequest,
Method,
};
use Innmind\Immutable\Maybe;
use Innmind\Immutable\{
Maybe,
Attempt,
};

final class ViaForm implements Authenticator
{
Expand All @@ -19,10 +22,11 @@ public function __construct(Resolver $resolve)
$this->resolve = $resolve;
}

public function __invoke(ServerRequest $request): Maybe
public function __invoke(ServerRequest $request): Attempt
{
return Maybe::just($request)
->filter(static fn($request) => $request->method() === Method::post)
->attempt(static fn() => new \RuntimeException('Failed to resolve identity'))
->flatMap(fn($request) => ($this->resolve)($request->form()));
}
}
8 changes: 4 additions & 4 deletions src/ViaForm/NullResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

use Innmind\HttpAuthentication\Identity;
use Innmind\Http\ServerRequest\Form;
use Innmind\Immutable\Maybe;
use Innmind\Immutable\Attempt;

final class NullResolver implements Resolver
{
public function __invoke(Form $form): Maybe
public function __invoke(Form $form): Attempt
{
/** @var Maybe<Identity> */
return Maybe::nothing();
/** @var Attempt<Identity> */
return Attempt::error(new \LogicException('Not implemented'));
}
}
6 changes: 3 additions & 3 deletions src/ViaForm/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

use Innmind\HttpAuthentication\Identity;
use Innmind\Http\ServerRequest\Form;
use Innmind\Immutable\Maybe;
use Innmind\Immutable\Attempt;

interface Resolver
{
/**
* @return Maybe<Identity>
* @return Attempt<Identity>
*/
public function __invoke(Form $form): Maybe;
public function __invoke(Form $form): Attempt;
}
7 changes: 4 additions & 3 deletions src/ViaStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use Innmind\HttpAuthentication\ViaStorage\Storage;
use Innmind\Http\ServerRequest;
use Innmind\Immutable\Maybe;
use Innmind\Immutable\Attempt;

final class ViaStorage implements Authenticator
{
Expand All @@ -18,12 +18,13 @@ public function __construct(Authenticator $authenticate, Storage $storage)
$this->storage = $storage;
}

public function __invoke(ServerRequest $request): Maybe
public function __invoke(ServerRequest $request): Attempt
{
return $this
->storage
->get($request)
->otherwise(
->attempt(static fn() => new \RuntimeException('Identity not in storage'))
->recover(
fn() => ($this->authenticate)($request)->map(
function($identity) use ($request) {
$this->storage->set($request, $identity);
Expand Down
8 changes: 4 additions & 4 deletions src/ViaUrlAuthority.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
User,
Password,
};
use Innmind\Immutable\Maybe;
use Innmind\Immutable\Attempt;

final class ViaUrlAuthority implements Authenticator
{
Expand All @@ -20,14 +20,14 @@ public function __construct(Resolver $resolve)
$this->resolve = $resolve;
}

public function __invoke(ServerRequest $request): Maybe
public function __invoke(ServerRequest $request): Attempt
{
$user = $request->url()->authority()->userInformation()->user();
$password = $request->url()->authority()->userInformation()->password();

if ($user->equals(User::none()) && $password->equals(Password::none())) {
/** @var Maybe<Identity> */
return Maybe::nothing();
/** @var Attempt<Identity> */
return Attempt::error(new \RuntimeException('No authentication provided'));
}

return ($this->resolve)($user, $password);
Expand Down
8 changes: 4 additions & 4 deletions src/ViaUrlAuthority/NullResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
User,
Password,
};
use Innmind\Immutable\Maybe;
use Innmind\Immutable\Attempt;

final class NullResolver implements Resolver
{
public function __invoke(User $user, Password $password): Maybe
public function __invoke(User $user, Password $password): Attempt
{
/** @var Maybe<Identity> */
return Maybe::nothing();
/** @var Attempt<Identity> */
return Attempt::error(new \LogicException('Not implemented'));
}
}
6 changes: 3 additions & 3 deletions src/ViaUrlAuthority/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
User,
Password,
};
use Innmind\Immutable\Maybe;
use Innmind\Immutable\Attempt;

interface Resolver
{
/**
* @return Maybe<Identity>
* @return Attempt<Identity>
*/
public function __invoke(User $user, Password $password): Maybe;
public function __invoke(User $user, Password $password): Attempt;
}
8 changes: 4 additions & 4 deletions tests/AnyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
ProtocolVersion,
};
use Innmind\Url\Url;
use Innmind\Immutable\Maybe;
use Innmind\Immutable\Attempt;
use PHPUnit\Framework\TestCase;

class AnyTest extends TestCase
Expand Down Expand Up @@ -55,17 +55,17 @@ public function testInvokation()
->expects($this->once())
->method('__invoke')
->with($request)
->willReturn(Maybe::nothing());
->willReturn(Attempt::error(new \Exception));
$notImplemented
->expects($this->once())
->method('__invoke')
->with($request)
->willReturn(Maybe::nothing());
->willReturn(Attempt::error(new \Exception));
$expected
->expects($this->once())
->method('__invoke')
->with($request)
->willReturn(Maybe::just($identity = $this->createMock(Identity::class)));
->willReturn(Attempt::result($identity = $this->createMock(Identity::class)));
$notCalled
->expects($this->never())
->method('__invoke');
Expand Down
Loading
Loading