Skip to content

Commit 7fe3413

Browse files
authored
Merge pull request #84 from stellarwp/feature/provide-license-in-auth-url
Add optional license key to provide in auth url callback
2 parents 704c755 + 0aeb036 commit 7fe3413

File tree

4 files changed

+95
-7
lines changed

4 files changed

+95
-7
lines changed

src/Uplink/Auth/Auth_Url_Builder.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ final class Auth_Url_Builder {
1616
*/
1717
private $auth_url_manager;
1818

19+
/**
20+
* @var string
21+
*/
22+
private $license_key;
23+
1924
/**
2025
* @param Nonce $nonce The Nonce creator.
2126
* @param Auth_Url $auth_url_manager The auth URL manager.
@@ -57,6 +62,11 @@ public function build( string $slug, string $domain = '' ): string {
5762
'uplink_slug' => $slug,
5863
];
5964

65+
// Optionally include a license key if set.
66+
if ( ! empty( $this->license_key ) ) {
67+
$args['uplink_license'] = $this->license_key;
68+
}
69+
6070
$url = add_query_arg(
6171
array_filter( array_merge( $_GET, $args ) ),
6272
admin_url( $pagenow )
@@ -70,4 +80,16 @@ public function build( string $slug, string $domain = '' ): string {
7080
);
7181
}
7282

83+
/**
84+
* Optionally set a license key to provide in uplink_callback query arg.
85+
*
86+
* @param string $key The license key to pass in the auth url.
87+
*
88+
* @return self
89+
*/
90+
public function set_license( string $key ): self {
91+
$this->license_key = $key;
92+
93+
return $this;
94+
}
7395
}

src/Uplink/Components/Admin/Authorize_Button_Controller.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function __construct(
7070
/**
7171
* Renders the authorize-button view.
7272
*
73-
* @param array{slug?: string, domain?: string} $args The Product slug and license domain.
73+
* @param array{slug?: string, domain?: string, license?: string} $args The Product slug and license domain.
7474
*
7575
* @see src/views/admin/authorize-button.php
7676
*
@@ -79,14 +79,15 @@ public function __construct(
7979
public function render( array $args = [] ): void {
8080
global $pagenow;
8181

82-
$slug = $args['slug'] ?? '';
83-
$domain = $args['domain'] ?? '';
82+
$slug = $args['slug'] ?? '';
83+
$domain = $args['domain'] ?? '';
84+
$license = $args['license'] ?? '';
8485

8586
if ( empty ( $slug ) ) {
8687
throw new InvalidArgumentException( __( 'The Product slug cannot be empty', '%TEXTDOMAIN%' ) );
8788
}
8889

89-
$url = $this->url_builder->build( $slug, $domain );
90+
$url = $this->url_builder->set_license( $license )->build( $slug, $domain );
9091

9192
if ( ! $url ) {
9293
return;

src/Uplink/functions.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,15 @@ function get_container(): ContainerInterface {
3737
*
3838
* @param string $slug The Product slug to render the button for.
3939
* @param string $domain An optional domain associated with a license key to pass along.
40+
* @param string $license The license that should be authenticated before token generation.
4041
*/
41-
function render_authorize_button( string $slug, string $domain = '' ): void {
42+
function render_authorize_button( string $slug, string $domain = '', string $license = '' ): void {
4243
try {
4344
get_container()->get( Authorize_Button_Controller::class )
4445
->render( [
45-
'slug' => $slug,
46-
'domain' => $domain,
46+
'slug' => $slug,
47+
'domain' => $domain,
48+
'license' => $license,
4749
] );
4850
} catch ( Throwable $e ) {
4951
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php declare( strict_types=1 );
2+
3+
namespace StellarWP\Uplink\Tests\Auth;
4+
5+
use StellarWP\Uplink\API\V3\Auth\Auth_Url;
6+
use StellarWP\Uplink\Auth\Auth_Url_Builder;
7+
use StellarWP\Uplink\Auth\Nonce;
8+
use StellarWP\Uplink\Config;
9+
use StellarWP\Uplink\Tests\Traits\With_Uopz;
10+
use StellarWP\Uplink\Tests\UplinkTestCase;
11+
use StellarWP\Uplink\Uplink;
12+
13+
final class AuthUrlBuilderTest extends UplinkTestCase {
14+
15+
use With_Uopz;
16+
17+
/**
18+
* @var Auth_Url_Builder
19+
*/
20+
private $auth_url_builder;
21+
22+
protected function setUp(): void {
23+
parent::setUp();
24+
25+
Config::set_token_auth_prefix( 'kadence_' );
26+
27+
// Run init again to reload all providers.
28+
Uplink::init();
29+
30+
$this->set_class_fn_return( Auth_Url::class, 'get', 'https://theeventscalendar.com/account-auth' );
31+
$this->set_class_fn_return( Nonce::class, 'create', 'abcd1234' );
32+
33+
$this->auth_url_builder = $this->container->get( Auth_Url_Builder::class );
34+
}
35+
36+
public function test_it_builds_url(): void {
37+
$url = $this->auth_url_builder->build( 'the-events-calendar', 'theeventscalendar.com' );
38+
39+
$callback = base64_encode( 'http://wordpress.test/wp-admin/index.php?uplink_domain=theeventscalendar.com&uplink_slug=the-events-calendar&_uplink_nonce=abcd1234' );
40+
$expected = 'https://theeventscalendar.com/account-auth?uplink_callback=' . rawurlencode( $callback );
41+
42+
$this->assertSame( $expected, $url );
43+
}
44+
45+
public function test_it_builds_url_with_license(): void {
46+
$url = $this->auth_url_builder->set_license('some-license-key')->build( 'the-events-calendar', 'theeventscalendar.com' );
47+
48+
$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' );
49+
$expected = 'https://theeventscalendar.com/account-auth?uplink_callback=' . rawurlencode( $callback );
50+
51+
$this->assertSame( $expected, $url );
52+
}
53+
54+
public function test_it_builds_url_with_empty_license(): void {
55+
$url = $this->auth_url_builder->set_license('')->build( 'the-events-calendar', 'theeventscalendar.com' );
56+
57+
$callback = base64_encode( 'http://wordpress.test/wp-admin/index.php?uplink_domain=theeventscalendar.com&uplink_slug=the-events-calendar&_uplink_nonce=abcd1234' );
58+
$expected = 'https://theeventscalendar.com/account-auth?uplink_callback=' . rawurlencode( $callback );
59+
60+
$this->assertSame( $expected, $url );
61+
}
62+
63+
}

0 commit comments

Comments
 (0)