Skip to content

Commit 890787e

Browse files
authored
Fix WordPress Frames OAuth connection and person grants (#3)
* Fix WordPress Frames OAuth callback encoding and person grants * Offer reconnection for legacy app credentials
1 parent 4e0ada1 commit 890787e

2 files changed

Lines changed: 80 additions & 12 deletions

File tree

‎packages/wordpress-plugin/includes/class-spacefast-frames-api.php‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static function resource(): string
3232
public static function connected(): bool
3333
{
3434
$connection = self::connection();
35-
return !empty($connection['client_id']) && !empty($connection['access_token']);
35+
return empty($connection['client_secret']) && !empty($connection['client_id']) && !empty($connection['access_token']);
3636
}
3737

3838
public static function save_api_base(string $value): void
@@ -64,7 +64,7 @@ public static function start_oauth(): string
6464
'client_name' => sprintf(__('Spacefast for %s', 'spacefast-frames'), wp_parse_url(home_url('/'), PHP_URL_HOST)),
6565
'client_uri' => 'https://github.com/spacefast/frames',
6666
'redirect_uris' => array($redirect),
67-
'token_endpoint_auth_method' => 'client_secret_post',
67+
'token_endpoint_auth_method' => 'none',
6868
'grant_types' => array('authorization_code', 'refresh_token'),
6969
'response_types' => array('code'),
7070
'type' => 'web',
@@ -73,21 +73,19 @@ public static function start_oauth(): string
7373
)
7474
);
7575
$client_id = self::required_string($registration, 'client_id');
76-
$client_secret = self::required_string($registration, 'client_secret');
7776
$state = self::random_token(32);
7877
$verifier = self::random_token(48);
7978
$challenge = self::base64url(hash('sha256', $verifier, true));
8079
set_transient(
8180
'spacefast_frames_oauth_' . hash('sha256', $state),
8281
array(
8382
'client_id' => $client_id,
84-
'client_secret' => self::seal($client_secret),
8583
'verifier' => self::seal($verifier),
8684
'api_base' => self::api_base(),
8785
),
8886
10 * MINUTE_IN_SECONDS
8987
);
90-
return add_query_arg(
88+
return self::api_base() . '/v1/auth/oauth2/authorize?' . http_build_query(
9189
array(
9290
'client_id' => $client_id,
9391
'redirect_uri' => $redirect,
@@ -98,7 +96,9 @@ public static function start_oauth(): string
9896
'code_challenge' => $challenge,
9997
'code_challenge_method' => 'S256',
10098
),
101-
self::api_base() . '/v1/auth/oauth2/authorize'
99+
'',
100+
'&',
101+
PHP_QUERY_RFC3986
102102
);
103103
}
104104

@@ -112,7 +112,6 @@ public static function finish_oauth(string $code, string $state): void
112112
}
113113
$api_base = untrailingslashit((string) ($pending['api_base'] ?? ''));
114114
$client_id = (string) ($pending['client_id'] ?? '');
115-
$client_secret = self::unseal((string) ($pending['client_secret'] ?? ''));
116115
$verifier = self::unseal((string) ($pending['verifier'] ?? ''));
117116
$tokens = self::token_request(
118117
$api_base,
@@ -121,7 +120,6 @@ public static function finish_oauth(string $code, string $state): void
121120
'code' => $code,
122121
'redirect_uri' => self::redirect_uri(),
123122
'client_id' => $client_id,
124-
'client_secret' => $client_secret,
125123
'code_verifier' => $verifier,
126124
'resource' => $api_base . '/v1',
127125
)
@@ -130,7 +128,6 @@ public static function finish_oauth(string $code, string $state): void
130128
array(
131129
'api_base' => $api_base,
132130
'client_id' => $client_id,
133-
'client_secret' => self::seal($client_secret),
134131
),
135132
$tokens
136133
);
@@ -187,6 +184,9 @@ public static function request(string $method, string $path, ?array $body = null
187184
private static function fresh_connection(): array
188185
{
189186
$connection = self::connection();
187+
if (!empty($connection['client_secret'])) {
188+
throw new RuntimeException(__('Reconnect Spacefast to continue.', 'spacefast-frames'));
189+
}
190190
$expires = (int) ($connection['expires_at'] ?? 0);
191191
if ($expires > 0 && $expires <= time() + 60 && !empty($connection['refresh_token'])) {
192192
return self::refresh(false);
@@ -201,8 +201,7 @@ private static function refresh(bool $force): array
201201
return $connection;
202202
}
203203
$refresh = self::unseal((string) ($connection['refresh_token'] ?? ''));
204-
$secret = self::unseal((string) ($connection['client_secret'] ?? ''));
205-
if ($refresh === '' || $secret === '') {
204+
if ($refresh === '' || !empty($connection['client_secret'])) {
206205
throw new RuntimeException(__('Reconnect Spacefast to continue.', 'spacefast-frames'));
207206
}
208207
$tokens = self::token_request(
@@ -211,7 +210,6 @@ private static function refresh(bool $force): array
211210
'grant_type' => 'refresh_token',
212211
'refresh_token' => $refresh,
213212
'client_id' => (string) ($connection['client_id'] ?? ''),
214-
'client_secret' => $secret,
215213
'resource' => self::resource(),
216214
)
217215
);

‎packages/wordpress-plugin/tests/unit.php‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,25 @@
22
declare(strict_types=1);
33

44
define('ABSPATH', __DIR__);
5+
define('MINUTE_IN_SECONDS', 60);
6+
define('SPACEFAST_FRAMES_VERSION', 'test');
7+
$options = array();
8+
$transients = array();
9+
function get_option($name, $default = false) { return $GLOBALS['options'][$name] ?? $default; }
10+
function update_option($name, $value, $autoload = null) { $GLOBALS['options'][$name] = $value; }
11+
function delete_option($name) { unset($GLOBALS['options'][$name]); }
12+
function set_transient($name, $value, $ttl) { $GLOBALS['transients'][$name] = $value; }
13+
function get_transient($name) { return $GLOBALS['transients'][$name] ?? false; }
14+
function delete_transient($name) { unset($GLOBALS['transients'][$name]); }
15+
function untrailingslashit($value) { return rtrim($value, '/'); }
16+
function esc_url_raw($value) { return $value; }
17+
function admin_url($path) { return 'https://wordpress.example/wp-admin/' . $path; }
18+
function is_wp_error($value) { return false; }
19+
function wp_remote_retrieve_response_code($response) { return $response['response']['code']; }
20+
function wp_remote_retrieve_body($response) { return $response['body']; }
21+
function wp_remote_request($url, $args) { return ($GLOBALS['transport'])($url, $args); }
22+
function wp_remote_post($url, $args) { return wp_remote_request($url, $args); }
23+
524
function __($message, $domain = null) { return $message; }
625
function home_url($path = '') { return 'https://wordpress.example' . $path; }
726
function sanitize_text_field($value) { return trim((string) $value); }
@@ -10,6 +29,7 @@ function wp_parse_url($url, $component = -1) { return parse_url($url, $component
1029
function wp_salt($scheme = 'auth') { return 'spacefast-wordpress-unit-test-' . $scheme; }
1130

1231
require_once dirname(__DIR__) . '/includes/class-spacefast-frames-plugin.php';
32+
require_once dirname(__DIR__) . '/includes/class-spacefast-frames-api.php';
1333

1434
function call_private(string $method, mixed ...$arguments): mixed
1535
{
@@ -54,3 +74,53 @@ function same(mixed $expected, mixed $actual, string $message): void
5474
$refused = true;
5575
}
5676
same(true, $refused, 'A modified block grant is refused.');
77+
78+
79+
$challenge = '';
80+
$GLOBALS['transport'] = function ($url, $args) use (&$challenge) {
81+
if (str_ends_with($url, '/register')) {
82+
$registration = json_decode($args['body'], true);
83+
same('none', $registration['token_endpoint_auth_method'], 'Frames registers a public client for person authority.');
84+
same(array(admin_url('options-general.php?page=spacefast-frames&spacefast_oauth=callback')), $registration['redirect_uris'], 'The complete callback query is registered.');
85+
$body = array('client_id' => 'frames-test-client');
86+
} elseif (str_ends_with($url, '/token')) {
87+
$fields = $args['body'];
88+
same(false, array_key_exists('client_secret', $fields), 'Person grants do not send a client secret.');
89+
same('frames-test-client', $fields['client_id'], 'The registered client owns the token request.');
90+
if ($fields['grant_type'] === 'authorization_code') {
91+
same($challenge, rtrim(strtr(base64_encode(hash('sha256', $fields['code_verifier'], true)), '+/', '-_'), '='), 'The exchange proves the authorize request PKCE challenge.');
92+
same(admin_url('options-general.php?page=spacefast-frames&spacefast_oauth=callback'), $fields['redirect_uri'], 'The exchange preserves the registered callback.');
93+
$body = array('access_token' => 'initial-test-token', 'refresh_token' => 'test-refresh', 'expires_in' => 900);
94+
} else {
95+
same('test-refresh', $fields['refresh_token'], 'Refresh uses the stored token.');
96+
$body = array('access_token' => 'refreshed-test-token', 'expires_in' => 900);
97+
}
98+
} else {
99+
same('Bearer refreshed-test-token', $args['headers']['Authorization'], 'API calls use the refreshed person credential.');
100+
$body = array('data' => array('id' => 'frame-test-session'));
101+
}
102+
return array('response' => array('code' => 200), 'body' => json_encode($body));
103+
};
104+
105+
$authorize = Spacefast_Frames_API::start_oauth();
106+
parse_str(parse_url($authorize, PHP_URL_QUERY), $parameters);
107+
same(admin_url('options-general.php?page=spacefast-frames&spacefast_oauth=callback'), $parameters['redirect_uri'], 'Authorize keeps the callback query inside redirect_uri.');
108+
same(false, array_key_exists('spacefast_oauth', $parameters), 'Callback parameters cannot escape into authorize parameters.');
109+
$challenge = $parameters['code_challenge'];
110+
Spacefast_Frames_API::finish_oauth('test-code', $parameters['state']);
111+
same(false, array_key_exists('client_secret', Spacefast_Frames_API::connection()), 'The connection stores no confidential client authority.');
112+
same(true, Spacefast_Frames_API::connected(), 'A person credential shows as connected.');
113+
$options['spacefast_frames_connection']['expires_at'] = time() - 1;
114+
same(array('data' => array('id' => 'frame-test-session')), Spacefast_Frames_API::request('POST', '/v1/spaces/spc_test/frame-session', array('path' => '/')), 'An expired person credential refreshes before launching a Frame.');
115+
116+
$options['spacefast_frames_connection']['client_secret'] = 'old-confidential-connection';
117+
same(false, Spacefast_Frames_API::connected(), 'Legacy app credentials offer the Connect action again.');
118+
$refused = false;
119+
try {
120+
Spacefast_Frames_API::request('GET', '/v1/spaces');
121+
} catch (RuntimeException $error) {
122+
same('Reconnect Spacefast to continue.', $error->getMessage(), 'An old app credential asks the administrator to reconnect.');
123+
$refused = true;
124+
}
125+
same(true, $refused, 'Existing confidential connections cannot launch Frames with app authority.');
126+
echo "WordPress unit tests passed.\n";

0 commit comments

Comments
 (0)