Skip to content

Commit cc8dbfd

Browse files
authored
Merge pull request #72 from ConvertKit/v4-api-encoded-state
v4 API: Encode `state` parameter
2 parents 44ddc7e + 5d1485f commit cc8dbfd

File tree

2 files changed

+45
-15
lines changed

2 files changed

+45
-15
lines changed

src/class-convertkit-api-v4.php

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,7 @@ private function generate_and_store_code_verifier() {
248248
$code_verifier = random_bytes( 64 );
249249

250250
// Encode to Base64 string.
251-
$code_verifier = base64_encode( $code_verifier ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions
252-
253-
// Convert Base64 to Base64URL by replacing “+” with “-” and “/” with “_”.
254-
$code_verifier = strtr( $code_verifier, '+/', '-_' );
255-
256-
// Remove padding character from the end of line.
257-
$code_verifier = rtrim( $code_verifier, '=' );
251+
$code_verifier = $this->base64_urlencode( $code_verifier );
258252

259253
// Store in database for later use.
260254
update_option( 'ck_code_verifier', $code_verifier );
@@ -317,15 +311,38 @@ private function delete_code_verifier() {
317311

318312
}
319313

314+
/**
315+
* Base64URL encode the given string.
316+
*
317+
* @since 2.0.0
318+
*
319+
* @param string $str String to encode.
320+
* @return string Encoded string.
321+
*/
322+
public function base64_urlencode( $str ) {
323+
324+
// Encode to Base64 string.
325+
$str = base64_encode( $str ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions
326+
327+
// Convert Base64 to Base64URL by replacing “+” with “-” and “/” with “_”.
328+
$str = strtr( $str, '+/', '-_' );
329+
330+
// Remove padding character from the end of line.
331+
$str = rtrim( $str, '=' );
332+
333+
return $str;
334+
335+
}
336+
320337
/**
321338
* Returns the URL used to begin the OAuth process
322339
*
323340
* @since 2.0.0
324341
*
325-
* @param bool|string $state Optional state parameter to include in OAuth request.
326-
* @return string OAuth URL
342+
* @param bool|string $return_url Return URL.
343+
* @return string OAuth URL
327344
*/
328-
public function get_oauth_url( $state = false ) {
345+
public function get_oauth_url( $return_url = false ) {
329346

330347
// Generate and store code verifier and challenge.
331348
$code_verifier = $this->generate_and_store_code_verifier();
@@ -340,9 +357,15 @@ public function get_oauth_url( $state = false ) {
340357
'code_challenge_method' => 'S256',
341358
);
342359

343-
// If a state parameter needs to be included, add it now.
344-
if ( $state ) {
345-
$args['state'] = rawurlencode( $state );
360+
if ( $return_url ) {
361+
$args['state'] = $this->base64_urlencode(
362+
wp_json_encode(
363+
array(
364+
'return_to' => $return_url,
365+
'client_id' => $this->client_id,
366+
)
367+
)
368+
);
346369
}
347370

348371
// Return OAuth URL.

tests/wpunit/APITest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,15 +351,22 @@ public function testGetOAuthURLWithState()
351351
{
352352
// Confirm the OAuth URL returned is correct.
353353
$this->assertEquals(
354-
$this->api->get_oauth_url( 'an-example-state' ),
354+
$this->api->get_oauth_url( 'https://example.com' ),
355355
'https://app.convertkit.com/oauth/authorize?' . http_build_query(
356356
[
357357
'client_id' => $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'],
358358
'response_type' => 'code',
359359
'redirect_uri' => $_ENV['CONVERTKIT_OAUTH_REDIRECT_URI'],
360360
'code_challenge' => $this->api->generate_code_challenge( $this->api->get_code_verifier() ),
361361
'code_challenge_method' => 'S256',
362-
'state' => 'an-example-state',
362+
'state' => $this->api->base64_urlencode(
363+
wp_json_encode(
364+
array(
365+
'return_to' => 'https://example.com',
366+
'client_id' => $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'],
367+
)
368+
)
369+
),
363370
]
364371
)
365372
);

0 commit comments

Comments
 (0)