Initial commit

This commit is contained in:
Mark Sauer
2018-12-15 20:56:39 +08:00
commit 00d59edb08
6 changed files with 363 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
<?php
/*
Plugin Name: GoToMyAccounts Single Sign On
Description: Provides single sign on (SSO) capabilites in a WordPress site for logged in a user to seamlessly access pages in a GoToMyAccounts customer portal.
Version: 1.0.0
Author: GoToMyAccounts
Author URI: https://www.gotomyaccounts.com
License: GPL 3.0
*/
if (!defined('ABSPATH'))
{
exit;
}
if (!defined('GTMA_SSO_URL'))
{
define('GTMA_SSO_URL', plugin_dir_url(__FILE__));
}
if (!defined('GTMA_SSO_PATH'))
{
define('GTMA_SSO_PATH', plugin_dir_path(__FILE__));
}
if (!defined('GTMA_SSO_INC'))
{
define('GTMA_SSO_INC', GTMA_SSO_PATH . "includes/");
}
function gtma_sso()
{
if (!class_exists('GTMA_SSO'))
{
include(GTMA_SSO_INC . "gtma-init.php");
new GTMA_SSO();
}
}
add_action('plugins_loaded', 'gtma_sso');

View File

@@ -0,0 +1,110 @@
<?php
if (!defined('ABSPATH'))
{
exit;
}
class GTMA_SSO
{
protected $token = '';
protected $portalURL = '';
function __construct()
{
add_action('admin_menu', array($this, 'gtma_menu'));
if (is_user_logged_in())
{
add_action('template_redirect', array($this, 'gtma_redirect'));
}
}
function gtma_menu()
{
add_options_page(__('GoToMyAccount SSO'), __('GoToMyAccounts SSO', 'gtma_sso'), 'manage_options', "gtma_sso", array($this, "render_settings"), "dashicons-palmtree", 40);
}
function render_settings()
{
$page = (!empty($_GET['page'])) ? esc_attr($_GET['page']) : '';
$action = (!empty($_GET['action'])) ? esc_attr($_GET['action']) : '';
$id = (!empty($_GET['id'])) ? esc_attr($_GET['id']) : '';
if ($page == 'gtma_sso' && $action == 'delete' && $id != '')
{
$gtma_redirects = get_option('gtma_sso_redirects', array());
unset($gtma_redirects[$id]);
update_option('gtma_sso_redirects', $gtma_redirects);
}
if (isset($_POST['gmta_sso_submit']) && $page == 'gtma_sso')
{
update_option('gtma_portal_url', sanitize_text_field($_POST['gtma_portal_url']));
update_option('gtma_sso_password', sanitize_text_field($_POST['gtma_sso_password']));
$redirect = $_POST['redirect'];
$gtma_redirects = get_option('gtma_sso_redirects', array());
$gtma_redirects[$redirect['page']] = $redirect['address'];
update_option('gtma_sso_redirects', $gtma_redirects);
}
include(GTMA_SSO_INC . "gtma-settings.php");
}
function get_token()
{
$token = '';
$portalURL = get_option('gtma_portal_url', '');
$sso_password = get_option('gtma_sso_password', '');
$user = wp_get_current_user();
$email = $user->user_email;
if ($portalURL != '' && $sso_password != '')
{
try
{
$url = "https://appapi.gotomyaccounts.com/v1/SSO/?email=" . urlencode($email) . "&portal_hostname=" . urlencode($portalURL);
$args = array(
'method' => 'GET',
'timeout' => 60,
'sslverify' => false,
'headers' => array( "Authorization" => $sso_password ),
'body' => array(),
'cookies' => false
);
$response = wp_safe_remote_get($url, $args);
if (!is_wp_error($response))
{
$result = $response['response'];
if ($result['code'] == 200 && $result['message'] == 'OK')
{
$token = $response['body'];
} else
{
$token = '';
}
} else
{
$token = '';
}
} catch (Exception $exc)
{
$token = '';
}
}
return $token;
}
function gtma_redirect()
{
$gtma_redirects = get_option('gtma_sso_redirects', array());
$ids = array_keys($gtma_redirects);
$token = $this->get_token();
$portalURL = get_option('gtma_portal_url', '');
$id = get_the_ID();
if ((in_array($id, $ids)) && !empty($token) && !empty($portalURL))
{
$link = "https://" . $portalURL . "/sso?token=" . urlencode($token) . "&page=" . $gtma_redirects[$id];
wp_redirect($link);
exit;
}
}
}

View File

@@ -0,0 +1,210 @@
<?php
if (!defined('ABSPATH'))
{
exit;
}
$portalURL = get_option('gtma_portal_url', '');
$sso_password = get_option('gtma_sso_password', '');
$gtma_redirects = get_option('gtma_sso_redirects', array());
$avail_pages = get_pages();
$pages[""] = 'Select Page';
foreach ($avail_pages as $page)
{
$pages[$page->ID] = $page->post_title;
}
?>
<style>
p.description
{
font-style: normal;
}
table.gtma_redirects td.sort::before{
content: '\f333';
font-family: Dashicons;
text-align: center;
line-height: 1;
color: #999;
display: block;
width: 17px;
float: left;
height: 100%;
}
table.gtma_redirects th {
padding: 9px 7px!important;
vertical-align: middle;
}
table.gtma_redirects td.sort{
padding: 0 7px;
cursor: move;
font-size: 15px;
text-align: center;
vertical-align: middle;
}
table.gtma_redirects td.name{
font-weight: 700;
}
table.gtma_redirects tr:nth-child(odd) td{
background: #f9f9f9;
}
.gtma-sso-settings-header-h1 {
vertical-align:top;
}
.gtma-sso-header h1 {
height:90px;
}
.gtma-sso-header img {
float: left;
width: 100px;
height: 100px;
background: transparent;
}
.gtma-sso-header h1 {
position: relative;
top: 18px;
left: 10px;
}
#gtma_portal_url {
width:400px;
}
#gtma_sso_password {
width:500px;
}
</style>
<div class="gtma-sso-header">
<img src="https://assets.gotomyaccounts.com/images-public/logos/gtma_logo_100.png" alt="logo" />
<h1>GoToMyAccounts Single Sign On Settings</h1>
</div>
<hr>
<div>
<h2>
Quick-Start Instructions
</h2>
<strong>Note: In order for SSO to work, your logged in WordPress user must have a corresponding web portal account with the exact same email address.</strong>
<ol>
<li>Set your web portal address and SSO token</li>
<li>
Create empty pages for the portal redirects you desire. (Ex: Billing, Make a payment, Manage Users, etc)<br>
(These represent a single "deep link" into your web portal)
</li>
<li>Add your pages (links) to one or menus.</li>
<li>Click a menu item to test.</li>
</ol>
<h3>
<a target="_blank" href="https://help.gotomyaccounts.com/en-us/article/how-to-setup-wordpress-single-sign-on-sso-1g3udeu/">Full Documentation</a>
</h3>
</div>
<hr>
<div class="wrap" style="padding: 0px 10px 0px 10px;">
<form method="POST" action="<?php echo admin_url("options-general.php?page=gtma_sso"); ?>">
<table class="form-table">
<tr>
<th scope="row">
<label for="gtma_portal_url">
GoToMyAccounts Portal URL
</label>
</th>
<td>
<input name="gtma_portal_url" type="text" id="gtma_portal_url" placeholder="Portal URl" value="<?php echo $portalURL; ?>">
<p class="description">
Set your GoToMyAccounts portal URL (no http/https or trailing "/").<br>
<strong>Example</strong>: myportal.gotomyaccounts.com
</p>
</td>
</tr>
<tr>
<th scope="row">
<label for="gtma_sso_password">
SSO Token
</label>
</th>
<td>
<input name="gtma_sso_password" type="password" id="gtma_sso_password" placeholder="SSO Password" value="<?php echo $sso_password; ?>">
<p class="description">
<strong>Where do I find this?</strong> You can manage your SSO tokens from within your portal (when logged in with admin permissions). On the left
menu, click Integrations, then click "SSO Tokens". Copy a token from that page and paste it into this field.
</p>
</td>
</tr>
<tr valign="top">
<th scope="row">
Add Redirect
</th>
<td colspan="2">
<select name="redirect[page]" style="width: 35%;">
<?php
foreach ($pages as $id => $title)
{
echo '<option value="' . $id . '">' . $title . '</option>';
}
?>
</select>
<input type="text" name="redirect[address]" size="43" placeholder="Add Web Portal Redirect Page">
<br />
Actual Wordpress pages should not contain any content. The plugin will cause these pages to redirect instead to your Web Portal Redirect Page.<br>
If the redirect fails, the page will instead display an error message to the user.<br>
Example Web Portal Redirect Pages: "customer_dashboard.html", "payment.html", "customer_manage_users.html"
</td>
</tr>
</table>
<input type="submit" class="button button-primary" name="gmta_sso_submit" id="gmta_sso_submit" value="Save changes">
<hr style="margin-top: 1.5em;" >
<table class="form-table">
<tbody>
<tr valign="top">
<th scope="row" class="titledesc">Redirected Page</th>
<td class="forminp">
<table class="gtma_redirects widefat" cellspacing="0">
<thead>
<tr>
<th class="page">WordPress Page (should be empty)</th>
<th class="redirect">Web Portal Redirect Page</th>
<th class="actions">Actions</th>
</tr>
</thead>
<tbody class="ui-sortable">
<?php
if (!empty($gtma_redirects))
{
foreach ($gtma_redirects as $id => $redirect)
{
if ($id)
{
?>
<tr>
<td class="page">
<?php echo $pages[$id]; ?>
</td>
<td class="redirect"><?php echo $redirect ?></td>
<td class="actions"><a href="<?php echo admin_url("options-general.php?page=gtma_sso&action=delete&id=" . $id); ?>"><span class="dashicons dashicons-trash"></span></a></td>
</tr>
<?php
}
}
} else
{
?>
<tr>
<td colspan="4">No Redirects Configured</td>
</tr>
<?php
}
?>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</form>
</div>

View File

@@ -0,0 +1,2 @@
<?php
// Silence is golden.

View File

@@ -0,0 +1,2 @@
<?php
// Silence is golden.