Skip to content

Commit 8606aaf

Browse files
authored
Merge pull request #84 from ConvertKit/oauth-support-tenant-name
OAuth: Support `tenant_name` Parameter
2 parents e1833d0 + 064a035 commit 8606aaf

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

src/class-convertkit-api-v4.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -339,10 +339,11 @@ public function base64_urlencode( $str ) {
339339
*
340340
* @since 2.0.0
341341
*
342-
* @param bool|string $return_url Return URL.
343-
* @return string OAuth URL
342+
* @param bool|string $return_url Return URL.
343+
* @param bool|string $tenant_name Tenant Name (if specified, issues tokens specific to that name. Useful for using the same account on multiple sites).
344+
* @return string OAuth URL
344345
*/
345-
public function get_oauth_url( $return_url = false ) {
346+
public function get_oauth_url( $return_url = false, $tenant_name = false ) {
346347

347348
// Generate and store code verifier and challenge.
348349
$code_verifier = $this->generate_and_store_code_verifier();
@@ -368,6 +369,10 @@ public function get_oauth_url( $return_url = false ) {
368369
);
369370
}
370371

372+
if ( $tenant_name ) {
373+
$args['tenant_name'] = rawurlencode( $tenant_name );
374+
}
375+
371376
// Return OAuth URL.
372377
return add_query_arg(
373378
$args,

tests/wpunit/APITest.php

+26
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,32 @@ public function testGetOAuthURLWithState()
365365
);
366366
}
367367

368+
/**
369+
* Test that get_oauth_url() returns the correct URL to begin the OAuth process
370+
* when a tenant_name parameter is supplied.
371+
*
372+
* @since 2.0.5
373+
*
374+
* @return void
375+
*/
376+
public function testGetOAuthURLWithTenantName()
377+
{
378+
// Confirm the OAuth URL returned is correct.
379+
$this->assertEquals(
380+
$this->api->get_oauth_url( false, 'https://example.com' ),
381+
'https://app.kit.com/oauth/authorize?' . http_build_query(
382+
[
383+
'client_id' => $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'],
384+
'response_type' => 'code',
385+
'redirect_uri' => $_ENV['CONVERTKIT_OAUTH_REDIRECT_URI'],
386+
'code_challenge' => $this->api->generate_code_challenge( $this->api->get_code_verifier() ),
387+
'code_challenge_method' => 'S256',
388+
'tenant_name' => 'https://example.com',
389+
]
390+
)
391+
);
392+
}
393+
368394
/**
369395
* Test that get_access_token() returns the expected data.
370396
*

0 commit comments

Comments
 (0)