Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow login flows to return Continuation instances #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions src/main/php/web/auth/Continuation.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php namespace web\auth;

class Continuation {
public $uri, $value;

/**
* Creates a new continuation
*
* @param string $uri
* @param var $value
*/
public function __construct($uri, $value= null) {
$this->uri= $uri;
$this->value= $value;
}
}
20 changes: 13 additions & 7 deletions src/main/php/web/auth/SessionBased.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,19 @@ public function filter($req, $res, $invocation) {

if (null === $user) {

// Authentication may require redirection in order to fulfill its job.
// In this case, return early from this method w/o passing control on.
if (null === ($result= $this->flow->authenticate($req, $res, $session))) return;

// Otherwise, authorize and transmit session
$user= $this->authorize($session, $result);
$session->transmit($res);
// Authentication may require redirection (indicated by returning NULL)
// or showing a page (indicated by returing a Continuation instance) in
// order to fulfill its job.
$result= $this->flow->authenticate($req, $res, $session);
if (null === $result) {
return;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of redirection implemented in the Flow class, this could be also refactored to if ($result instanceof Redirect) { ... } and then handling that here. This would further simplify the implementations IMO.

} else if ($result instanceof Continuation) {
$req->rewrite($result->uri)->pass('auth', $result->value);
$session->transmit($res);
} else {
$user= $this->authorize($session, $result);
$session->transmit($res);
}
}

return $invocation->proceed($req->pass('user', $user)->pass('token', $token), $res);
Expand Down
13 changes: 12 additions & 1 deletion src/test/php/web/auth/unittest/SessionBasedTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use lang\IllegalStateException;
use unittest\Assert;
use web\auth\{SessionBased, Flow};
use web\auth\{Continuation, SessionBased, Flow};
use web\io\{TestInput, TestOutput};
use web\session\{ISession, ForTesting};
use web\{Request, Response};
Expand Down Expand Up @@ -155,4 +155,15 @@ public function session_is_attached_after_authentication() {
Assert::equals(1, $attached->value('times'));
Assert::equals($user, $attached->value('auth')[1]);
}

#[Test]
public function continuation_rewrites_uri_and_passes_value() {
$sessions= new ForTesting();
$auth= new SessionBased($this->authenticate(new Continuation('/login', 'test')), $sessions);
$this->handle([], $auth->required(function($req, $res) use(&$passed) {
$passed= [$req->uri()->path(), $req->value('auth')];
}));

Assert::equals(['/login', 'test'], $passed);
}
}