Skip to content

Commit 7c0ddc4

Browse files
authored
Merge pull request #118 from lart2150/feature/support-no-consent-clients
feat: add a configuration option to support clients that don't require consent
2 parents a65653b + 8fac67b commit 7c0ddc4

File tree

4 files changed

+50
-30
lines changed

4 files changed

+50
-30
lines changed

src/Http/Handlers/AuthenticateHandler.php

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
use OAuth2\Response;
77
use OpenIDConnectServer\Http\RequestHandler;
88
use OpenIDConnectServer\Http\Router;
9+
use OpenIDConnectServer\Storage\ClientCredentialsStorage;
910
use OpenIDConnectServer\Storage\ConsentStorage;
1011

1112
class AuthenticateHandler extends RequestHandler {
1213
private ConsentStorage $consent_storage;
13-
private array $clients;
14+
private ClientCredentialsStorage $clients;
1415

15-
public function __construct( ConsentStorage $consent_storage, array $clients ) {
16+
public function __construct( ConsentStorage $consent_storage, ClientCredentialsStorage $clients ) {
1617
$this->consent_storage = $consent_storage;
1718
$this->clients = $clients;
1819
}
@@ -22,15 +23,19 @@ public function handle( Request $request, Response $response ): Response {
2223
auth_redirect();
2324
}
2425

25-
$client_name = $this->get_client_name( $request );
26+
$client_id = $request->query( 'client_id' );
27+
28+
$client_name = $this->clients->getClientName( $client_id );
2629
if ( empty( $client_name ) ) {
2730
$response->setStatusCode( 404 );
2831

2932
return $response;
3033
}
3134

32-
$client_id = $request->query( 'client_id' );
33-
if ( ! $this->consent_storage->needs_consent( get_current_user_id(), $client_id ) ) {
35+
if (
36+
! $this->clients->clientRequiresConsent( $client_id )
37+
|| ! $this->consent_storage->needs_consent( get_current_user_id(), $client_id )
38+
) {
3439
$this->redirect( $request );
3540
// TODO: return response instead of exiting.
3641
exit;
@@ -155,25 +160,6 @@ private function redirect( Request $request ) {
155160
);
156161
}
157162

158-
/**
159-
* TODO: Remove this function in favour of ClientCredentialsStorage?
160-
*/
161-
private function get_client_name( Request $request ): string {
162-
$client_id = $request->query( 'client_id' );
163-
164-
if ( ! isset( $this->clients[ $client_id ] ) ) {
165-
return '';
166-
}
167-
168-
$client = $this->clients[ $client_id ];
169-
170-
if ( empty( $client['name'] ) ) {
171-
return '';
172-
}
173-
174-
return $client['name'];
175-
}
176-
177163
private function get_cancel_url( Request $request ) {
178164
return add_query_arg(
179165
array(

src/Http/Handlers/AuthorizeHandler.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@
88
use OAuth2\Response;
99
use OAuth2\Server as OAuth2Server;
1010
use OpenIDConnectServer\Http\RequestHandler;
11+
use OpenIDConnectServer\Storage\ClientCredentialsStorage;
1112
use OpenIDConnectServer\Storage\ConsentStorage;
1213

1314
const OIDC_DEFAULT_MINIMAL_CAPABILITY = 'edit_posts';
1415

1516
class AuthorizeHandler extends RequestHandler {
1617
private OAuth2Server $server;
1718
private ConsentStorage $consent_storage;
19+
private ClientCredentialsStorage $clients;
1820

19-
public function __construct( OAuth2Server $server, ConsentStorage $consent_storage ) {
21+
public function __construct( OAuth2Server $server, ConsentStorage $consent_storage, ClientCredentialsStorage $clients ) {
2022
$this->server = $server;
2123
$this->consent_storage = $consent_storage;
24+
$this->clients = $clients;
2225
}
2326

2427
public function handle( Request $request, Response $response ): Response {
@@ -44,7 +47,10 @@ public function handle( Request $request, Response $response ): Response {
4447
$user = wp_get_current_user();
4548

4649
$client_id = $request->query( 'client_id', $request->request( 'client_id' ) );
47-
if ( $this->consent_storage->needs_consent( $user->ID, $client_id ) ) {
50+
if (
51+
$this->clients->clientRequiresConsent( $client_id )
52+
&& $this->consent_storage->needs_consent( $user->ID, $client_id )
53+
) {
4854
if ( ! isset( $_POST['authorize'] ) || __( 'Authorize', 'openid-connect-server' ) !== $_POST['authorize'] ) {
4955
$response->setError( 403, 'user_authorization_required', 'This application requires your consent.' );
5056
return $response;

src/OpenIDConnectServer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020

2121
class OpenIDConnectServer {
2222
private string $public_key;
23-
private array $clients;
23+
private ClientCredentialsStorage $clients;
2424
private Router $router;
2525
private ConsentStorage $consent_storage;
2626

2727
public function __construct( string $public_key, string $private_key, array $clients ) {
2828
$this->public_key = $public_key;
29-
$this->clients = $clients;
29+
$this->clients = new ClientCredentialsStorage( $clients );
3030
$this->router = new Router();
3131
$this->consent_storage = new ConsentStorage();
3232

@@ -38,7 +38,7 @@ public function __construct( string $public_key, string $private_key, array $cli
3838

3939
$server = new Server( new AuthorizationCodeStorage(), $config );
4040
$server->addStorage( new PublicKeyStorage( $public_key, $private_key ), 'public_key' );
41-
$server->addStorage( new ClientCredentialsStorage( $clients ), 'client_credentials' );
41+
$server->addStorage( $this->clients, 'client_credentials' );
4242
$server->addStorage( new UserClaimsStorage(), 'user_claims' );
4343

4444
// Declare rest routes.
@@ -50,7 +50,7 @@ public function __construct( string $public_key, string $private_key, array $cli
5050
);
5151
$this->router->add_rest_route(
5252
'authorize',
53-
new AuthorizeHandler( $server, $this->consent_storage ),
53+
new AuthorizeHandler( $server, $this->consent_storage, $this->clients ),
5454
array( 'GET', 'POST' ),
5555
$this->expected_arguments_specification( 'authorize' ),
5656
);

src/Storage/ClientCredentialsStorage.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,34 @@ public function getClientDetails( $client_id ) {
2525
);
2626
}
2727

28+
public function getClientName( $client_id ) {
29+
if ( ! $this->has( $client_id ) ) {
30+
return '';
31+
}
32+
33+
$client = $this->get( $client_id );
34+
35+
if ( empty( $client['name'] ) ) {
36+
return '';
37+
}
38+
39+
return $client['name'];
40+
}
41+
42+
public function clientRequiresConsent( $client_id ): bool {
43+
if ( ! $this->has( $client_id ) ) {
44+
return true;
45+
}
46+
47+
$client = $this->get( $client_id );
48+
49+
if ( ! array_key_exists( 'requires_consent', $client ) ) {
50+
return true;
51+
}
52+
53+
return false !== $client['requires_consent'];
54+
}
55+
2856
public function getClientScope( $client_id ) {
2957
if ( ! $this->has( $client_id ) ) {
3058
return '';

0 commit comments

Comments
 (0)