Skip to content

Commit

Permalink
Allow UserIdProviderInterface to be injected into Authorize Route
Browse files Browse the repository at this point in the history
  • Loading branch information
chadicus committed Dec 1, 2016
1 parent 2cf60bd commit 144bd72
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions src/Authorize.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,42 @@ final class Authorize implements RouteCallbackInterface
*/
private $template;

/**
* Extracts user_id from the incoming request.
*
* @var UserIdProviderInterface
*/
private $userIdProvider;

/**
* Construct a new instance of Authorize.
*
* @param OAuth2\Server $server The oauth2 server imstance.
* @param object $view The slim framework view helper.
* @param string $template The template for /authorize.
* @param OAuth2\Server $server The oauth2 server imstance.
* @param object $view The slim framework view helper.
* @param string $template The template for /authorize.
* @param UserIdProviderInterface $userIdProvider Object to extract a user_id based on the incoming request.
*
* @throws \InvalidArgumentException Thrown if $view is not an object implementing a render method.
*/
public function __construct(OAuth2\Server $server, $view, $template = '/authorize.phtml')
{
public function __construct(
OAuth2\Server $server,
$view,
$template = '/authorize.phtml',
UserIdProviderInterface $userIdProvider = null
) {
if (!is_object($view) || !method_exists($view, 'render')) {
throw new \InvalidArgumentException('$view must implement a render() method');
}

$this->server = $server;
$this->view = $view;
$this->template = $template;

if ($userIdProvider == null) {
$userIdProvider = new UserIdProvider();
}

$this->userIdProvider = $userIdProvider;
}

/**
Expand All @@ -79,7 +97,12 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
return $response->withHeader('Content-Type', 'text/html');
}

$this->server->handleAuthorizeRequest($oauth2Request, $oauth2Response, $authorized === 'yes');
$this->server->handleAuthorizeRequest(
$oauth2Request,
$oauth2Response,
$authorized === 'yes',
$this->userIdProvider->getUserId($request, $arguments)
);

return Http\ResponseBridge::fromOAuth2($oauth2Response);
}
Expand Down

0 comments on commit 144bd72

Please sign in to comment.