Skip to content

Commit cae735a

Browse files
[11.x] Support prompting re-consent when redirecting for authorization (#1567)
* Support prompting re-consent * Update AuthorizationController.php Co-authored-by: Taylor Otwell <[email protected]>
1 parent 8aeec71 commit cae735a

File tree

2 files changed

+71
-9
lines changed

2 files changed

+71
-9
lines changed

src/Http/Controllers/AuthorizationController.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,11 @@ public function authorize(ServerRequestInterface $psrRequest,
6363
});
6464

6565
$scopes = $this->parseScopes($authRequest);
66+
$user = $request->user();
67+
$client = $clients->find($authRequest->getClient()->getIdentifier());
6668

67-
$token = $tokens->findValidToken(
68-
$user = $request->user(),
69-
$client = $clients->find($authRequest->getClient()->getIdentifier())
70-
);
71-
72-
if (($token && $token->scopes === collect($scopes)->pluck('id')->all()) ||
73-
$client->skipsAuthorization()) {
69+
if ($request->get('prompt') !== 'consent' &&
70+
($client->skipsAuthorization() || $this->hasValidToken($tokens, $user, $client, $scopes))) {
7471
return $this->approveRequest($authRequest, $user);
7572
}
7673

@@ -101,6 +98,22 @@ protected function parseScopes($authRequest)
10198
);
10299
}
103100

101+
/**
102+
* Determine if a valid token exists for the given user, client, and scopes.
103+
*
104+
* @param \Laravel\Passport\TokenRepository $tokens
105+
* @param \Illuminate\Database\Eloquent\Model $user
106+
* @param \Laravel\Passport\Client $client
107+
* @param array $scopes
108+
* @return bool
109+
*/
110+
protected function hasValidToken($tokens, $user, $client, $scopes)
111+
{
112+
$token = $tokens->findValidToken($user, $client);
113+
114+
return $token && $token->scopes === collect($scopes)->pluck('id')->all();
115+
}
116+
104117
/**
105118
* Approve the authorization request.
106119
*

tests/Unit/AuthorizationControllerTest.php

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public function test_authorization_view_is_presented()
4646
$session->shouldReceive('put')->withSomeOfArgs('authToken');
4747
$session->shouldReceive('put')->with('authRequest', $authRequest);
4848
$request->shouldReceive('user')->andReturn($user = m::mock());
49+
$request->shouldReceive('get')->with('prompt')->andReturn(null);
4950

5051
$authRequest->shouldReceive('getClient->getIdentifier')->andReturn(1);
5152
$authRequest->shouldReceive('getScopes')->andReturn([new Scope('scope-1')]);
@@ -116,18 +117,21 @@ public function test_request_is_approved_if_valid_token_exists()
116117
$request->shouldReceive('user')->once()->andReturn($user = m::mock());
117118
$user->shouldReceive('getAuthIdentifier')->andReturn(1);
118119
$request->shouldNotReceive('session');
120+
$request->shouldReceive('get')->with('prompt')->andReturn(null);
119121

120122
$authRequest->shouldReceive('getClient->getIdentifier')->once()->andReturn(1);
121123
$authRequest->shouldReceive('getScopes')->once()->andReturn([new Scope('scope-1')]);
122124
$authRequest->shouldReceive('setUser')->once()->andReturnNull();
123125
$authRequest->shouldReceive('setAuthorizationApproved')->once()->with(true);
124126

125127
$clients = m::mock(ClientRepository::class);
126-
$clients->shouldReceive('find')->with(1)->andReturn('client');
128+
$clients->shouldReceive('find')->with(1)->andReturn($client = m::mock(Client::class));
129+
130+
$client->shouldReceive('skipsAuthorization')->andReturn(false);
127131

128132
$tokens = m::mock(TokenRepository::class);
129133
$tokens->shouldReceive('findValidToken')
130-
->with($user, 'client')
134+
->with($user, $client)
131135
->andReturn($token = m::mock(Token::class));
132136
$token->shouldReceive('getAttribute')->with('scopes')->andReturn(['scope-1']);
133137

@@ -158,6 +162,7 @@ public function test_request_is_approved_if_client_can_skip_authorization()
158162
$request->shouldReceive('user')->once()->andReturn($user = m::mock());
159163
$user->shouldReceive('getAuthIdentifier')->andReturn(1);
160164
$request->shouldNotReceive('session');
165+
$request->shouldReceive('get')->with('prompt')->andReturn(null);
161166

162167
$authRequest->shouldReceive('getClient->getIdentifier')->once()->andReturn(1);
163168
$authRequest->shouldReceive('getScopes')->once()->andReturn([new Scope('scope-1')]);
@@ -178,4 +183,48 @@ public function test_request_is_approved_if_client_can_skip_authorization()
178183
m::mock(ServerRequestInterface::class), $request, $clients, $tokens
179184
)->getContent());
180185
}
186+
187+
public function test_authorization_view_is_presented_if_request_has_prompt_equals_to_consent()
188+
{
189+
Passport::tokensCan([
190+
'scope-1' => 'description',
191+
]);
192+
193+
$server = m::mock(AuthorizationServer::class);
194+
$response = m::mock(ResponseFactory::class);
195+
196+
$controller = new AuthorizationController($server, $response);
197+
$server->shouldReceive('validateAuthorizationRequest')
198+
->andReturn($authRequest = m::mock(AuthorizationRequest::class));
199+
200+
$request = m::mock(Request::class);
201+
$request->shouldReceive('session')->andReturn($session = m::mock());
202+
$session->shouldReceive('put')->withSomeOfArgs('authToken');
203+
$session->shouldReceive('put')->with('authRequest', $authRequest);
204+
$request->shouldReceive('user')->andReturn($user = m::mock());
205+
$request->shouldReceive('get')->with('prompt')->andReturn('consent');
206+
207+
$authRequest->shouldReceive('getClient->getIdentifier')->once()->andReturn(1);
208+
$authRequest->shouldReceive('getScopes')->once()->andReturn([new Scope('scope-1')]);
209+
210+
$clients = m::mock(ClientRepository::class);
211+
$clients->shouldReceive('find')->with(1)->andReturn($client = m::mock(Client::class));
212+
$client->shouldReceive('skipsAuthorization')->andReturn(false);
213+
214+
$tokens = m::mock(TokenRepository::class);
215+
$tokens->shouldNotReceive('findValidToken');
216+
217+
$response->shouldReceive('view')->once()->andReturnUsing(function ($view, $data) use ($client, $user) {
218+
$this->assertSame('passport::authorize', $view);
219+
$this->assertEquals($client, $data['client']);
220+
$this->assertEquals($user, $data['user']);
221+
$this->assertSame('description', $data['scopes'][0]->description);
222+
223+
return 'view';
224+
});
225+
226+
$this->assertSame('view', $controller->authorize(
227+
m::mock(ServerRequestInterface::class), $request, $clients, $tokens
228+
));
229+
}
181230
}

0 commit comments

Comments
 (0)