From 92939a028583c7dc75d253065abe6f0ab64f6bf1 Mon Sep 17 00:00:00 2001 From: Tanner Record Date: Thu, 22 Aug 2024 12:27:50 -0400 Subject: [PATCH 1/7] Add optional license param to provide in auth url callback --- src/Uplink/Auth/Auth_Url_Builder.php | 9 ++-- tests/wpunit/Auth/AuthUrlBuilderTest.php | 63 ++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 tests/wpunit/Auth/AuthUrlBuilderTest.php diff --git a/src/Uplink/Auth/Auth_Url_Builder.php b/src/Uplink/Auth/Auth_Url_Builder.php index d13f9076..27bd8c4e 100644 --- a/src/Uplink/Auth/Auth_Url_Builder.php +++ b/src/Uplink/Auth/Auth_Url_Builder.php @@ -35,10 +35,11 @@ public function __construct( * * @param string $slug The product/service slug. * @param string $domain An optional domain associated with a license key to pass along. + * @param string $license An optional license key to pass along. * * @return string */ - public function build( string $slug, string $domain = '' ): string { + public function build( string $slug, string $domain = '', string $license = '' ): string { global $pagenow; if ( empty( $pagenow ) ) { @@ -53,8 +54,9 @@ public function build( string $slug, string $domain = '' ): string { // Query arguments to combine with $_GET and add to the authorization URL. $args = [ - 'uplink_domain' => $domain, - 'uplink_slug' => $slug, + 'uplink_domain' => $domain, + 'uplink_slug' => $slug, + 'uplink_license' => $license, ]; $url = add_query_arg( @@ -69,5 +71,4 @@ public function build( string $slug, string $domain = '' ): string { ] ) ); } - } diff --git a/tests/wpunit/Auth/AuthUrlBuilderTest.php b/tests/wpunit/Auth/AuthUrlBuilderTest.php new file mode 100644 index 00000000..f03bd8f1 --- /dev/null +++ b/tests/wpunit/Auth/AuthUrlBuilderTest.php @@ -0,0 +1,63 @@ +set_class_fn_return( Auth_Url::class, 'get', 'https://theeventscalendar.com/account-auth' ); + $this->set_class_fn_return( Nonce::class, 'create', 'abcd1234' ); + + $this->auth_url_builder = $this->container->get( Auth_Url_Builder::class ); + } + + public function test_it_builds_url(): void { + $url = $this->auth_url_builder->build( 'the-events-calendar', 'theeventscalendar.com' ); + + $callback = base64_encode( 'http://wordpress.test/wp-admin/index.php?uplink_domain=theeventscalendar.com&uplink_slug=the-events-calendar&_uplink_nonce=abcd1234' ); + $expected = 'https://theeventscalendar.com/account-auth?uplink_callback=' . rawurlencode( $callback ); + + $this->assertSame( $expected, $url ); + } + + public function test_it_builds_url_with_license(): void { + $url = $this->auth_url_builder->build( 'the-events-calendar', 'theeventscalendar.com', 'some-license-key' ); + + $callback = base64_encode( 'http://wordpress.test/wp-admin/index.php?uplink_domain=theeventscalendar.com&uplink_slug=the-events-calendar&uplink_license=some-license-key&_uplink_nonce=abcd1234' ); + $expected = 'https://theeventscalendar.com/account-auth?uplink_callback=' . rawurlencode( $callback ); + + $this->assertSame( $expected, $url ); + } + + public function test_it_builds_url_with_empty_license(): void { + $url = $this->auth_url_builder->build( 'the-events-calendar', 'theeventscalendar.com', '' ); + + $callback = base64_encode( 'http://wordpress.test/wp-admin/index.php?uplink_domain=theeventscalendar.com&uplink_slug=the-events-calendar&_uplink_nonce=abcd1234' ); + $expected = 'https://theeventscalendar.com/account-auth?uplink_callback=' . rawurlencode( $callback ); + + $this->assertSame( $expected, $url ); + } + +} From d306ae110497d6f8ea674040ffa2506bc5eed081 Mon Sep 17 00:00:00 2001 From: Tanner Record Date: Thu, 22 Aug 2024 15:34:51 -0400 Subject: [PATCH 2/7] Convert to attach license key in a fluent manner --- src/Uplink/Auth/Auth_Url_Builder.php | 27 +++++++++++++++++++++--- tests/wpunit/Auth/AuthUrlBuilderTest.php | 4 ++-- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/Uplink/Auth/Auth_Url_Builder.php b/src/Uplink/Auth/Auth_Url_Builder.php index 27bd8c4e..cf26a6f5 100644 --- a/src/Uplink/Auth/Auth_Url_Builder.php +++ b/src/Uplink/Auth/Auth_Url_Builder.php @@ -16,6 +16,11 @@ final class Auth_Url_Builder { */ private $auth_url_manager; + /** + * @var string + */ + private $license_key; + /** * @param Nonce $nonce The Nonce creator. * @param Auth_Url $auth_url_manager The auth URL manager. @@ -35,11 +40,10 @@ public function __construct( * * @param string $slug The product/service slug. * @param string $domain An optional domain associated with a license key to pass along. - * @param string $license An optional license key to pass along. * * @return string */ - public function build( string $slug, string $domain = '', string $license = '' ): string { + public function build( string $slug, string $domain = '' ): string { global $pagenow; if ( empty( $pagenow ) ) { @@ -56,9 +60,13 @@ public function build( string $slug, string $domain = '', string $license = '' ) $args = [ 'uplink_domain' => $domain, 'uplink_slug' => $slug, - 'uplink_license' => $license, ]; + // Optionally include a license key if set. + if ( ! empty( $this->license_key ) ) { + $args['uplink_license'] = $this->license_key; + } + $url = add_query_arg( array_filter( array_merge( $_GET, $args ) ), admin_url( $pagenow ) @@ -71,4 +79,17 @@ public function build( string $slug, string $domain = '', string $license = '' ) ] ) ); } + + /** + * Optionally set a license key to provide in uplink_callback query arg. + * + * @param string $key The license key to pass in the auth url. + * + * @return self + */ + public function set_license( string $key ): self { + $this->license_key = $key; + + return $this; + } } diff --git a/tests/wpunit/Auth/AuthUrlBuilderTest.php b/tests/wpunit/Auth/AuthUrlBuilderTest.php index f03bd8f1..099dfbf8 100644 --- a/tests/wpunit/Auth/AuthUrlBuilderTest.php +++ b/tests/wpunit/Auth/AuthUrlBuilderTest.php @@ -43,7 +43,7 @@ public function test_it_builds_url(): void { } public function test_it_builds_url_with_license(): void { - $url = $this->auth_url_builder->build( 'the-events-calendar', 'theeventscalendar.com', 'some-license-key' ); + $url = $this->auth_url_builder->set_license('some-license-key')->build( 'the-events-calendar', 'theeventscalendar.com' ); $callback = base64_encode( 'http://wordpress.test/wp-admin/index.php?uplink_domain=theeventscalendar.com&uplink_slug=the-events-calendar&uplink_license=some-license-key&_uplink_nonce=abcd1234' ); $expected = 'https://theeventscalendar.com/account-auth?uplink_callback=' . rawurlencode( $callback ); @@ -52,7 +52,7 @@ public function test_it_builds_url_with_license(): void { } public function test_it_builds_url_with_empty_license(): void { - $url = $this->auth_url_builder->build( 'the-events-calendar', 'theeventscalendar.com', '' ); + $url = $this->auth_url_builder->set_license('')->build( 'the-events-calendar', 'theeventscalendar.com' ); $callback = base64_encode( 'http://wordpress.test/wp-admin/index.php?uplink_domain=theeventscalendar.com&uplink_slug=the-events-calendar&_uplink_nonce=abcd1234' ); $expected = 'https://theeventscalendar.com/account-auth?uplink_callback=' . rawurlencode( $callback ); From a6143067cf7de0a32c15faf15d7aeb8eafa9343c Mon Sep 17 00:00:00 2001 From: Tanner Record Date: Mon, 26 Aug 2024 14:53:25 -0400 Subject: [PATCH 3/7] Fix minor alignment issue --- src/Uplink/Auth/Auth_Url_Builder.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Uplink/Auth/Auth_Url_Builder.php b/src/Uplink/Auth/Auth_Url_Builder.php index cf26a6f5..7e48ed37 100644 --- a/src/Uplink/Auth/Auth_Url_Builder.php +++ b/src/Uplink/Auth/Auth_Url_Builder.php @@ -58,8 +58,8 @@ public function build( string $slug, string $domain = '' ): string { // Query arguments to combine with $_GET and add to the authorization URL. $args = [ - 'uplink_domain' => $domain, - 'uplink_slug' => $slug, + 'uplink_domain' => $domain, + 'uplink_slug' => $slug, ]; // Optionally include a license key if set. From a3a85787c37b35213b600236756dee8459ef5607 Mon Sep 17 00:00:00 2001 From: Tanner Record Date: Mon, 26 Aug 2024 16:00:13 -0400 Subject: [PATCH 4/7] Optionally add license to auth button callback url --- .../Components/Admin/Authorize_Button_Controller.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Uplink/Components/Admin/Authorize_Button_Controller.php b/src/Uplink/Components/Admin/Authorize_Button_Controller.php index bf415c3d..b4fa7519 100644 --- a/src/Uplink/Components/Admin/Authorize_Button_Controller.php +++ b/src/Uplink/Components/Admin/Authorize_Button_Controller.php @@ -79,14 +79,15 @@ public function __construct( public function render( array $args = [] ): void { global $pagenow; - $slug = $args['slug'] ?? ''; - $domain = $args['domain'] ?? ''; + $slug = $args['slug'] ?? ''; + $domain = $args['domain'] ?? ''; + $license = $args['license'] ?? ''; if ( empty ( $slug ) ) { throw new InvalidArgumentException( __( 'The Product slug cannot be empty', '%TEXTDOMAIN%' ) ); } - $url = $this->url_builder->build( $slug, $domain ); + $url = $this->url_builder->set_license( $license )->build( $slug, $domain ); if ( ! $url ) { return; From fbfa55a6e296f1ed5f1dc6aed7e6c305b50d2e17 Mon Sep 17 00:00:00 2001 From: Tanner Record Date: Mon, 26 Aug 2024 16:08:42 -0400 Subject: [PATCH 5/7] Pass optional license to authorize button --- src/Uplink/functions.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Uplink/functions.php b/src/Uplink/functions.php index 51096627..e9f06cb3 100644 --- a/src/Uplink/functions.php +++ b/src/Uplink/functions.php @@ -38,12 +38,13 @@ function get_container(): ContainerInterface { * @param string $slug The Product slug to render the button for. * @param string $domain An optional domain associated with a license key to pass along. */ -function render_authorize_button( string $slug, string $domain = '' ): void { +function render_authorize_button( string $slug, string $domain = '', string $license = '' ): void { try { get_container()->get( Authorize_Button_Controller::class ) ->render( [ - 'slug' => $slug, - 'domain' => $domain, + 'slug' => $slug, + 'domain' => $domain, + 'license' => $license, ] ); } catch ( Throwable $e ) { if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { From d23cfdd95b03145b289d07a77903637d1444279f Mon Sep 17 00:00:00 2001 From: Tanner Record Date: Mon, 26 Aug 2024 16:40:23 -0400 Subject: [PATCH 6/7] Fix static analysis issue --- src/Uplink/Components/Admin/Authorize_Button_Controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Uplink/Components/Admin/Authorize_Button_Controller.php b/src/Uplink/Components/Admin/Authorize_Button_Controller.php index b4fa7519..4751ef79 100644 --- a/src/Uplink/Components/Admin/Authorize_Button_Controller.php +++ b/src/Uplink/Components/Admin/Authorize_Button_Controller.php @@ -70,7 +70,7 @@ public function __construct( /** * Renders the authorize-button view. * - * @param array{slug?: string, domain?: string} $args The Product slug and license domain. + * @param array{slug?: string, domain?: string, license?: string} $args The Product slug and license domain. * * @see src/views/admin/authorize-button.php * From 0aeb036696cc642a7964ae5df91f379d970d62cc Mon Sep 17 00:00:00 2001 From: Tanner Record Date: Mon, 26 Aug 2024 16:40:45 -0400 Subject: [PATCH 7/7] Add missing parameter comment --- src/Uplink/functions.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Uplink/functions.php b/src/Uplink/functions.php index e9f06cb3..37f2e56c 100644 --- a/src/Uplink/functions.php +++ b/src/Uplink/functions.php @@ -37,6 +37,7 @@ function get_container(): ContainerInterface { * * @param string $slug The Product slug to render the button for. * @param string $domain An optional domain associated with a license key to pass along. + * @param string $license The license that should be authenticated before token generation. */ function render_authorize_button( string $slug, string $domain = '', string $license = '' ): void { try {