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: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
- All authenticators now return an `Innmind\Immutable\Attempt`
- All authenticators can return any type inside the `Attempt`
- Requires `innmind/foundation:~1.9`
- `Innmind\HttpAuthentication\ViaAuthorization` constructor is private, use `::of()` instead
- `Innmind\HttpAuthentication\ViaBasicAuthorization` constructor is private, use `::of()` instead
- `Innmind\HttpAuthentication\ViaForm` constructor is private, use `::of()` instead
- `Innmind\HttpAuthentication\ViaUrlAuthority` constructor is private, use `::of()` instead

### Removed

Expand Down
20 changes: 14 additions & 6 deletions src/ViaAuthorization.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,11 @@
*/
final class ViaAuthorization
{
/** @var callable(Authorization): Attempt<T> */
private $resolve;

/**
* @param callable(Authorization): Attempt<T> $resolve
* @param \Closure(Authorization): Attempt<T> $resolve
*/
public function __construct(callable $resolve)
private function __construct(private \Closure $resolve)
{
$this->resolve = $resolve;
}

/**
Expand All @@ -36,4 +32,16 @@ public function __invoke(ServerRequest $request): Attempt
->attempt(static fn() => new \RuntimeException('Failed to resolve identity'))
->flatMap(fn($value) => ($this->resolve)($value));
}

/**
* @template A
*
* @param callable(Authorization): Attempt<A> $resolve
*
* @return self<A>
*/
public static function of(callable $resolve): self
{
return new self(\Closure::fromCallable($resolve));
}
}
20 changes: 14 additions & 6 deletions src/ViaBasicAuthorization.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,11 @@
*/
final class ViaBasicAuthorization
{
/** @var callable(string, string): Attempt<T> */
private $resolve;

/**
* @param callable(string, string): Attempt<T> $resolve
* @param \Closure(string, string): Attempt<T> $resolve
*/
public function __construct(callable $resolve)
private function __construct(private \Closure $resolve)
{
$this->resolve = $resolve;
}

/**
Expand All @@ -47,4 +43,16 @@ public function __invoke(ServerRequest $request): Attempt
return ($this->resolve)($user, $password);
});
}

/**
* @template A
*
* @param callable(string, string): Attempt<A> $resolve
*
* @return self<A>
*/
public static function of(callable $resolve): self
{
return new self(\Closure::fromCallable($resolve));
}
}
20 changes: 14 additions & 6 deletions src/ViaForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,11 @@
*/
final class ViaForm
{
/** @var callable(Form): Attempt<T> */
private $resolve;

/**
* @param callable(Form): Attempt<T> $resolve
* @param \Closure(Form): Attempt<T> $resolve
*/
public function __construct(callable $resolve)
private function __construct(private \Closure $resolve)
{
$this->resolve = $resolve;
}

/**
Expand All @@ -39,4 +35,16 @@ public function __invoke(ServerRequest $request): Attempt
->attempt(static fn() => new \RuntimeException('Failed to resolve identity'))
->flatMap(fn($request) => ($this->resolve)($request->form()));
}

/**
* @template A
*
* @param callable(Form): Attempt<A> $resolve
*
* @return self<A>
*/
public static function of(callable $resolve): self
{
return new self(\Closure::fromCallable($resolve));
}
}
20 changes: 14 additions & 6 deletions src/ViaUrlAuthority.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@
*/
final class ViaUrlAuthority
{
/** @var callable(User, Password): Attempt<T> */
private $resolve;

/**
* @param callable(User, Password): Attempt<T> $resolve
* @param \Closure(User, Password): Attempt<T> $resolve
*/
public function __construct(callable $resolve)
private function __construct(private \Closure $resolve)
{
$this->resolve = $resolve;
}

/**
Expand All @@ -41,4 +37,16 @@ public function __invoke(ServerRequest $request): Attempt

return ($this->resolve)($user, $password);
}

/**
* @template A
*
* @param callable(User, Password): Attempt<A> $resolve
*
* @return self<A>
*/
public static function of(callable $resolve): self
{
return new self(\Closure::fromCallable($resolve));
}
}
6 changes: 3 additions & 3 deletions tests/ViaAuthorizationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ViaAuthorizationTest extends TestCase
{
public function testReturnNothingWhenNoAuthorizationHeader()
{
$authenticate = new ViaAuthorization(
$authenticate = ViaAuthorization::of(
static fn() => throw new \Exception,
);
$request = ServerRequest::of(
Expand All @@ -38,7 +38,7 @@ public function testReturnNothingWhenNoAuthorizationHeader()

public function testReturnNothingWhenAuthorizationHeaderNotParsedCorrectly()
{
$authenticate = new ViaAuthorization(
$authenticate = ViaAuthorization::of(
static fn() => throw new \Exception,
);
$request = ServerRequest::of(
Expand All @@ -58,7 +58,7 @@ public function testReturnNothingWhenAuthorizationHeaderNotParsedCorrectly()

public function testInvokation()
{
$authenticate = new ViaAuthorization(
$authenticate = ViaAuthorization::of(
static fn($value) => Attempt::result($value),
);
$expected = Authorization::of('Bearer', 'foo');
Expand Down
8 changes: 4 additions & 4 deletions tests/ViaBasicAuthorizationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ViaBasicAuthorizationTest extends TestCase
{
public function testReturnNothingWhenNoAuthorizationHeader()
{
$authenticate = new ViaBasicAuthorization(
$authenticate = ViaBasicAuthorization::of(
static fn() => throw new \Exception,
);
$request = ServerRequest::of(
Expand All @@ -38,7 +38,7 @@ public function testReturnNothingWhenNoAuthorizationHeader()

public function testReturnNothingWhenAuthorizationHeaderNotParsedCorrectly()
{
$authenticate = new ViaBasicAuthorization(
$authenticate = ViaBasicAuthorization::of(
static fn() => throw new \Exception,
);
$request = ServerRequest::of(
Expand All @@ -58,7 +58,7 @@ public function testReturnNothingWhenAuthorizationHeaderNotParsedCorrectly()

public function testReturnNothingWhenNotBasicAuthorization()
{
$authenticate = new ViaBasicAuthorization(
$authenticate = ViaBasicAuthorization::of(
static fn() => throw new \Exception,
);
$request = ServerRequest::of(
Expand All @@ -78,7 +78,7 @@ public function testReturnNothingWhenNotBasicAuthorization()

public function testInvokation()
{
$authenticate = new ViaBasicAuthorization(
$authenticate = ViaBasicAuthorization::of(
static fn($user, $password) => Attempt::result([$user, $password]),
);
$request = ServerRequest::of(
Expand Down
4 changes: 2 additions & 2 deletions tests/ViaFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ViaFormTest extends TestCase
{
public function testReturnNothingWhenNotPostRequest()
{
$authenticate = new ViaForm(
$authenticate = ViaForm::of(
static fn() => throw new \Exception,
);
$request = ServerRequest::of(
Expand All @@ -34,7 +34,7 @@ public function testReturnNothingWhenNotPostRequest()

public function testInvokation()
{
$authenticate = new ViaForm(
$authenticate = ViaForm::of(
static fn($value) => Attempt::result($value),
);
$request = ServerRequest::of(
Expand Down
4 changes: 2 additions & 2 deletions tests/ViaUrlAuthorityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ViaUrlAuthorityTest extends TestCase
{
public function testReturnNothingWhenNoUserProvidedInTheUrl()
{
$authenticate = new ViaUrlAuthority(
$authenticate = ViaUrlAuthority::of(
static fn() => throw new \Exception,
);
$url = Url::of('https://localhost/');
Expand All @@ -35,7 +35,7 @@ public function testReturnNothingWhenNoUserProvidedInTheUrl()

public function testInvokation()
{
$authenticate = new ViaUrlAuthority(
$authenticate = ViaUrlAuthority::of(
static fn($user, $password) => Attempt::result([$user, $password]),
);
$url = Url::of('https://user:password@localhost/');
Expand Down
Loading