Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 35 additions & 12 deletions plugins/jsconnect/class.jsconnect.plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@ public function __construct(\Garden\Web\Cookie $cookie, UserModel $userModel) {
$this->userModel = $userModel;
}

/**
* Create a URL that directs the browser to the V3 redirect to create the JWT.
*
* @param array $provider JSConnect settings.
* @return string URL with the target.
*/
private static function entryRedirectURL(array $provider): string {
$target = Gdn::request()->get('Target', Gdn::request()->get('Target'));
if (!$target) {
$target = '/' . ltrim(Gdn::request()->path());
}
if (stringBeginsWith($target, '/entry/signin')) {
$target = '/';
}

$baseURL = url('/entry/jsconnect-redirect');
return $baseURL . '?' . http_build_query([
'client_id' => $provider[self::FIELD_PROVIDER_CLIENT_ID],
'target' => $target
]);
}

/**
* Get the AuthenticationSchemeAlias value.
*
Expand Down Expand Up @@ -173,18 +195,7 @@ public static function connectButton($provider, $options = []) {
* @return string
*/
private static function connectButtonV3(array $provider): string {
$target = Gdn::request()->get('target', Gdn::request()->get('target'));
if (!$target) {
$target = '/'.ltrim(Gdn::request()->path());
}
if (stringBeginsWith($target, '/entry/signin')) {
$target = '/';
}

$url = url('/entry/jsconnect-redirect').'?'.http_build_query([
'client_id' => $provider[self::FIELD_PROVIDER_CLIENT_ID],
'target' => $target
]);
$url = self::entryRedirectURL($provider);

$result = '<div class="JsConnect-Guest">'.
anchor(
Expand Down Expand Up @@ -421,6 +432,18 @@ public function authenticationProviderModel_calculateJsConnect_handler($sender,
$provider['RegisterUrlFinal'] = static::getRegisterUrl($provider, $target);
}

/**
* If this is the default provider and V3, make sure it goes through the redirect URL.
*
* @param EntryController $sender
* @param array $args
*/
public function entryController_overrideSignIn_handler($sender, $args) {
$protocol = $args['DefaultProvider']['Protocol'] ?? null;
if ($protocol === self::PROTOCOL_V3) {
$args['DefaultProvider']['SignInUrl'] = static::entryRedirectURL($args['DefaultProvider']);
}
}
/**
* Add jsConnect buttons to the page.
*
Expand Down