Skip to content

Commit

Permalink
Fix ISession::transmit() not being called after authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Dec 23, 2020
1 parent 54ce9c0 commit 07ffb1a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
5 changes: 5 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ Web Authentication change log

## ?.?.? / ????-??-??

## 2.0.1 / 2020-12-23

* Fixed `ISession::transmit()` not being called after authentication
(@thekid)

## 2.0.0 / 2020-10-18

* Added support for redirecting to URLs with fragments (`/#/users/123`)
Expand Down
6 changes: 3 additions & 3 deletions src/main/php/web/auth/SessionBased.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ public function filter($req, $res, $invocation) {
$user= $result;
}

// Register in session
// Register in session, then continue with invocation
$session->register('user', $user);
$req->pass('user', $user);
return $invocation->proceed($req, $res);
$session->transmit($res);
return $invocation->proceed($req->pass('user', $user), $res);
}
}
}
47 changes: 33 additions & 14 deletions src/test/php/web/auth/unittest/SessionBasedTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
use unittest\Assert;
use web\auth\{SessionBased, Flow};
use web\io\{TestInput, TestOutput};
use web\session\ForTesting;
use web\session\{ForTesting, Transport};
use web\{Request, Response};

class SessionBasedTest {
private $sessions, $flow;
private $sessions;

/**
* Invokes handle() function
Expand All @@ -22,27 +22,30 @@ private function handle($headers, $handler) {
$handler->handle($req, $res);
}

private function authenticate($result) {
return newinstance(Flow::class, [], [
'authenticate' => function($req, $res, $invocation) use($result) {
return $result;
}
]);
}

#[Before]
public function setUp() {
public function sessions() {
$this->sessions= new ForTesting();
$this->flow= new class() extends Flow {
public function authenticate($req, $res, $invocation) {
// ...
}
};
}

#[Test]
public function can_create() {
new SessionBased($this->flow, $this->sessions);
new SessionBased($this->authenticate(null), $this->sessions);
}

#[Test]
public function required() {
$session= $this->sessions->create();
$session->register('user', ['username' => 'test']);

$auth= new SessionBased($this->flow, $this->sessions);
$auth= new SessionBased($this->authenticate(null), $this->sessions);
$this->handle(['Cookie' => 'session='.$session->id()], $auth->required(function($req, $res) use(&$user) {
$user= $req->value('user')['username'];
}));
Expand All @@ -52,15 +55,15 @@ public function required() {

#[Test]
public function handler_not_invoked_if_required_auth_missing() {
$auth= new SessionBased($this->flow, $this->sessions);
$auth= new SessionBased($this->authenticate(null), $this->sessions);
$this->handle([], $auth->required(function($req, $res) {
throw new IllegalStateException('Should not be reached');
}));
}

#[Test]
public function optional_without_session() {
$auth= new SessionBased($this->flow, $this->sessions);
$auth= new SessionBased($this->authenticate(null), $this->sessions);
$this->handle([], $auth->optional(function($req, $res) use(&$user) {
$user= $req->value('user')['username'] ?? 'guest';
}));
Expand All @@ -73,7 +76,7 @@ public function optional_with_session() {
$session= $this->sessions->create();
$session->register('user', ['username' => 'test']);

$auth= new SessionBased($this->flow, $this->sessions);
$auth= new SessionBased($this->authenticate(null), $this->sessions);
$this->handle(['Cookie' => 'session='.$session->id()], $auth->optional(function($req, $res) use(&$user) {
$user= $req->value('user')['username'] ?? 'guest';
}));
Expand All @@ -85,11 +88,27 @@ public function optional_with_session() {
public function optional_with_session_without_user() {
$session= $this->sessions->create();

$auth= new SessionBased($this->flow, $this->sessions);
$auth= new SessionBased($this->authenticate(null), $this->sessions);
$this->handle(['Cookie' => 'session='.$session->id()], $auth->optional(function($req, $res) use(&$user) {
$user= $req->value('user')['username'] ?? 'guest';
}));

Assert::equals('guest', $user);
}

#[Test]
public function session_is_attached_after_authentication() {
$user= ['username' => 'test'];
$attached= null;

$auth= new SessionBased($this->authenticate($user), $this->sessions->via(newinstance(Transport::class, [], [
'locate' => function($sessions, $request) { return null; },
'attach' => function($sessions, $response, $session) use(&$attached) { $attached= $session; },
'detach' => function($sessions, $response, $session) { }
])));
$this->handle([], $auth->required(function($req, $res) { }));

Assert::notEquals(null, $attached);
Assert::equals($user, $attached->value('user'));
}
}

0 comments on commit 07ffb1a

Please sign in to comment.