Skip to content

Commit

Permalink
Merge branch 'paywall-checkout-autologin'
Browse files Browse the repository at this point in the history
  • Loading branch information
mangrose committed Mar 14, 2024
2 parents 0145e12 + f58fdcd commit 8f2d159
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 3 deletions.
43 changes: 43 additions & 0 deletions checkout_landing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

function write_log($log) {
if (true === WP_DEBUG) {
if (is_array($log) || is_object($log)) {
error_log(print_r($log, true));
} else {
error_log("[SSO2][Landing] ".$log);
}
}
}

$token = $_POST["jwtToken"];
if (!isset($token)) {
write_log("No token available!");
die("No token available!");
}

$baseurl = explode( "wp-content" , $_SERVER['SCRIPT_FILENAME'] );
$baseurl = $baseurl[0];
require_once( $baseurl . "wp-load.php" );

use \Firebase\JWT\JWT;


try
{
$client_secret = get_option('tulo_paywall_secret');
$session = new Tulo_Payway_Session();
$payload = $session->decode_jwt($token, $client_secret);
if (isset($payload)) {
$session->process_checkout_landing($payload);
}

} catch(Firebase\JWT\ExpiredException $e) {
// we land here if the JWT token can not be decoded properly, in this case some claims have expired.
write_log("Could not decode JWT from Payway! Message: ".$e->getMessage());
}

die();


?>
15 changes: 13 additions & 2 deletions includes/class-tulo-paywall.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,23 @@ public function get_return_url() {
} else {
$currentUrl .= "?tpw_session_refresh=1";
}
return $currentUrl;
return str_replace("http://", "https://", $currentUrl);
}

public function get_current_url() {
global $wp;
return add_query_arg( $wp->query_vars, home_url( $wp->request ) );
$currentUrl = home_url( $wp->request );
$permalinkStructure = get_option( 'permalink_structure' );
if ($permalinkStructure == "plain" || $permalinkStructure == "") {
$queryVars = $wp->query_vars;
unset($queryVars['tpw_session_refresh']);
$currentUrl = add_query_arg( $queryVars, home_url( $wp->request ) );
}
return $currentUrl;
}

public function get_ticket_login_url() {
return plugin_dir_url(__DIR__)."checkout_landing.php";
}

public function get_account_origin() {
Expand Down
69 changes: 69 additions & 0 deletions includes/class-tulo-payway-sso2-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,75 @@ public function __construct() {
$this->api = new Tulo_Payway_API();
}

protected function process_paywall_checkout_login($payload) {
$this->common->write_log("---> process paywall checkout login");
if (isset($payload)) {
$delegated_ticket = $payload->dtid;
$account_id = $payload->aid;
$this->common->write_log("Authentication ticket: ".$delegated_ticket);
$this->common->write_log("Account id: ".$account_id);

$url = $this->get_sso2_url("authenticatewithticket");
$client_id = get_option('tulo_server_client_id');
$client_secret = get_option('tulo_server_secret');

$token = $this->get_delegated_ticket_token($client_id, $client_secret, $delegated_ticket);
$payload = json_encode(array("t" => $token));

$this->common->write_log("posting payload to: ".$url);
$response = $this->common->post_json_jwt($url, $payload);
if ($response["status"] == 200) {
$data = json_decode($response["data"]);
$decoded = $this->decode_token($data->t, $client_secret);
if ($decoded == null) {
$this->common->write_log("[ERROR] error processing response from sso request, token could not be decoded");
} else {
$sts = $decoded->sts;
$err = $decoded->err;
$at = $decoded->at;
$this->common->write_log(" sts.......: ".$sts);
$this->common->write_log(" at........: ".$at);
if ($sts == "loggedin" && $at != "") {
$this->fetch_user_and_login($at);
$this->common->write_log("<--- process paywall completed successfully, user is logged in, ready for reload.");
} else {
$this->common->write_log(" !! Error fetching access token => ".$err);
}
}

} else {
$this->common->write_log("[ERROR] error posting authenticate with ticket request");
$this->common->write_log($response);
}


}
}

private function get_delegated_ticket_token($client_id, $client_secret, $ticket) {
$organisation_id = get_option('tulo_organisation_id');
$ip_address = $_SERVER ['REMOTE_ADDR'];
$user_agent = $_SERVER['HTTP_USER_AGENT'];

$time = time();
$payload = array(
"cid" => $client_id,
"iss" => $organisation_id,
"sid" => $this->sso_session_id(),
"ipa" => $ip_address,
"uas" => $user_agent,
"at" => $ticket,
"aud" => "pw-sso",
"nbf" => $time,
"exp" => $time + 10,
"iat" => $time
);
$this->common->write_log("ticket token payload:");
$this->common->write_log($payload);

$token = JWT::encode($payload, $client_secret, 'HS256');
return $token;
}
/**
* Called from the landing page, checks session status and sets user in session if logged in
*/
Expand Down
4 changes: 4 additions & 0 deletions includes/class-tulo-payway-sso2-session.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ public function __construct() {
parent::__construct();
}

public function process_checkout_landing($payload) {
$this->process_paywall_checkout_login($payload);
}

public function is_logged_in() {
return $this->is_session_logged_in();
}
Expand Down
3 changes: 2 additions & 1 deletion public/class-tulo-payway-public.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function check_session($wp)
$this->session->refresh();
$currentUrl = home_url( $wp->request );
$permalinkStructure = get_option( 'permalink_structure' );
if ($permalinkStructure == "plain") {
if ($permalinkStructure == "plain" || $permalinkStructure == "") {
$queryVars = $wp->query_vars;
unset($queryVars['tpw_session_refresh']);
$currentUrl = add_query_arg( $queryVars, home_url( $wp->request ) );
Expand Down Expand Up @@ -372,6 +372,7 @@ private function initialize_paywall($post_restrictions)
utmSource: "",
loginUrl: "'.$paywall->get_login_url().'",
shopUrl: "'.$paywall->get_shop_url().'",
ticketLoginUrl: "'.$paywall->get_ticket_login_url().'",
utmMedium: "",
utmCampaign: "",
utmContent: "",
Expand Down

0 comments on commit 8f2d159

Please sign in to comment.