Skip to content

New authorization view response for custom views #1607

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

Closed
wants to merge 6 commits into from
Closed
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
10 changes: 10 additions & 0 deletions src/Contracts/AuthorizationViewResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Laravel\Passport\Contracts;

use Illuminate\Contracts\Support\Responsable;

interface AuthorizationViewResponse extends Responsable
{
//
}
15 changes: 8 additions & 7 deletions src/Http/Controllers/AuthorizationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Support\Str;
use Laravel\Passport\Bridge\User;
use Laravel\Passport\ClientRepository;
use Laravel\Passport\Contracts\AuthorizationViewResponse;
use Laravel\Passport\Passport;
use Laravel\Passport\TokenRepository;
use League\OAuth2\Server\AuthorizationServer;
Expand Down Expand Up @@ -65,7 +66,7 @@ public function __construct(AuthorizationServer $server,
* @param \Illuminate\Http\Request $request
* @param \Laravel\Passport\ClientRepository $clients
* @param \Laravel\Passport\TokenRepository $tokens
* @return \Illuminate\Http\Response
* @return \Laravel\Passport\Contracts\AuthorizationViewResponse|mixed|void
*/
public function authorize(ServerRequestInterface $psrRequest,
Request $request,
Expand Down Expand Up @@ -109,12 +110,12 @@ public function authorize(ServerRequestInterface $psrRequest,
$request->session()->put('authToken', $authToken = Str::random());
$request->session()->put('authRequest', $authRequest);

return $this->response->view('passport::authorize', [
'client' => $client,
'user' => $user,
'scopes' => $scopes,
'request' => $request,
'authToken' => $authToken,
return app(AuthorizationViewResponse::class, [
'client' => $client,
'user' => $user,
'scopes' => $scopes,
'request' => $request,
'authToken' => $authToken,
]);
}

Expand Down
55 changes: 55 additions & 0 deletions src/Http/Responses/AuthorizationViewResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Laravel\Passport\Http\Responses;

use Laravel\Passport\Contracts\AuthorizationViewResponse as AuthorizationViewResponseContract;

class AuthorizationViewResponse implements AuthorizationViewResponseContract
{
/**
* The name of the view or the callable used to generate the view.
*
* @var string
*/
protected $view;

/**
* An array of arguments that may be passed to the view response and used in the view.
*
* @var string
*/
protected $parameters;

/**
* Create a new response instance.
*
* @param callable|string $view
* @return void
*/
public function __construct($view, $parameters = array())
{
$this->view = $view;
$this->parameters = $parameters;
}

/**
* Create an HTTP response that represents the object.
*
* @param \Illuminate\Http\Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function toResponse($request)
{
if (! is_callable($this->view) || is_string($this->view)) {
return view($this->view, $this->parameters);
}

$response = call_user_func($this->view, $this->parameters);

if ($response instanceof Responsable) {
return $response->toResponse($request);
}

return $response;
}
}
15 changes: 15 additions & 0 deletions src/Passport.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use DateInterval;
use DateTimeInterface;
use Illuminate\Contracts\Encryption\Encrypter;
use Laravel\Passport\Contracts\AuthorizationViewResponse as AuthorizationViewResponseContract;
use Laravel\Passport\Http\Responses\AuthorizationViewResponse;
use League\OAuth2\Server\ResourceServer;
use Mockery;
use Psr\Http\Message\ServerRequestInterface;
Expand Down Expand Up @@ -684,4 +686,17 @@ public static function withoutCookieSerialization()

return new static;
}

/**
* Specify which view should be used as the authorization view.
*
* @param callable|string $view
* @return void
*/
public static function authorizationView($view)
{
app()->singleton(AuthorizationViewResponseContract::class, function ($app, $parameters) use ($view) {
return new AuthorizationViewResponse($view, $parameters);
});
}
}
4 changes: 3 additions & 1 deletion src/PassportServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,12 @@ public function register()
$this->registerJWTParser();
$this->registerResourceServer();
$this->registerGuard();

Passport::authorizationView('passport::authorize');
}

/**
* Register the authorization server.
* Register the authorization server.`
*
* @return void
*/
Expand Down