MAW-880: Plugin Cleanup
This commit is contained in:
@@ -1 +0,0 @@
|
||||
env.php
|
||||
@@ -1,8 +0,0 @@
|
||||
Cleanup with pbcbf
|
||||
Change style to PSR2
|
||||
Automatically log users in if they have an Okta session already
|
||||
Use promises for sign-out
|
||||
Remove templating from sign-in page
|
||||
Log users out of Okta when they log out of Wordpress
|
||||
Remove all hardcoded values
|
||||
Validate OIDC tokens via /token endpoint
|
||||
@@ -1,41 +0,0 @@
|
||||
# WordPress Okta Sign-In Widget
|
||||
|
||||
This plugin replaces the WordPress login screen with the Okta sign-in widget.
|
||||
|
||||
🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
|
||||
|
||||
This plugin was created to demonstrate the capability of replacing the WordPress login screen with the Okta sign-in widget for [this 2018 blog post](https://developer.okta.com/blog/2018/10/30/wordpress-authentication-with-okta).
|
||||
|
||||
🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
|
||||
|
||||
This plugin is not supported by Okta, and not updated regularly.
|
||||
|
||||
🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
|
||||
|
||||
If you would like to use an officially supported Okta WordPress integration, please see [this guide](https://plugins.miniorange.com/okta-single-sign-on-wordpress-sso-oauth-openid-connect) to configuring the [miniOrange WordPress SSO Plugin](https://www.okta.com/integrations/wordpress-oauth-single-sign-on-sso-by-miniorange/) with Okta.
|
||||
|
||||
🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
|
||||
|
||||
|
||||
## Setup Instructions
|
||||
|
||||
After dropping this folder into the WordPress plugins folder and activating the plugin, you should see a new Settings menu where you can configure your Okta settings to enable the plugin.
|
||||
|
||||
Make sure your admin user in WordPress has an email address that matches an Okta user, or enable native WordPress logins, otherwise you'll be locked out of your WordPress after configuring the plugin.
|
||||
|
||||
TODO:
|
||||
|
||||
* Clean up the UX around installing the plugin, like making sure the admin user can still log in after the plugin is activated
|
||||
* Handle errors better (or at all really)
|
||||
|
||||
## Development Environment
|
||||
|
||||
### Manual
|
||||
|
||||
Install WordPress and move plugin to `wp-content/plugins` directory
|
||||
|
||||
### Docker
|
||||
|
||||
Install Docker and docker-compose and run `docker-compose up`
|
||||
|
||||
Navigate to http://localhost:8080
|
||||
@@ -1,18 +0,0 @@
|
||||
version: '3.1'
|
||||
|
||||
services:
|
||||
wordpress:
|
||||
image: wordpress
|
||||
depends_on:
|
||||
- mysql
|
||||
ports:
|
||||
- 8080:80
|
||||
environment:
|
||||
WORDPRESS_DB_PASSWORD: password
|
||||
volumes:
|
||||
- .:/var/www/html/wp-content/plugins/okta-wordpress-sign-in-widget
|
||||
|
||||
mysql:
|
||||
image: mysql:5.7
|
||||
environment:
|
||||
MYSQL_ROOT_PASSWORD: password
|
||||
@@ -1,9 +0,0 @@
|
||||
var oktaSignIn = new OktaSignIn({
|
||||
baseUrl: '<?php echo parse_url($issuer = get_option('okta-issuer-url'), PHP_URL_SCHEME).'://'.parse_url($issuer, PHP_URL_HOST) ?>',
|
||||
redirectUri: '<?php echo wp_login_url() ?>',
|
||||
clientId: '<?php echo get_option('okta-widget-client-id') ?>',
|
||||
scopes: '<?php echo apply_filters( 'okta_widget_token_scope', 'openid email') ?>'.split(' '),
|
||||
authParams: {
|
||||
issuer: '<?php echo get_option('okta-issuer-url') ?>'
|
||||
}
|
||||
});
|
||||
@@ -1,104 +0,0 @@
|
||||
<?php
|
||||
namespace Okta;
|
||||
|
||||
class OktaAdmin{
|
||||
|
||||
public function __construct(){
|
||||
// https://codex.wordpress.org/Creating_Options_Pages
|
||||
add_action('admin_init', array($this, 'registerSettingsAction'));
|
||||
|
||||
// https://codex.wordpress.org/Adding_Administration_Menus
|
||||
add_action('admin_menu', array($this, 'optionsMenuAction'));
|
||||
}
|
||||
|
||||
public function registerSettingsAction() {
|
||||
add_settings_section(
|
||||
'okta-sign-in-widget-options-section',
|
||||
'',
|
||||
null,
|
||||
'okta-sign-in-widget'
|
||||
);
|
||||
|
||||
register_setting('okta-sign-in-widget', 'okta-issuer-url', array(
|
||||
'type' => 'string',
|
||||
'show_in_rest' => false,
|
||||
));
|
||||
add_settings_field(
|
||||
'okta-issuer-url',
|
||||
'Okta Issuer URI',
|
||||
function() { $this->optionsPageTextInputAction('okta-issuer-url', 'text', 'e.g. https://youroktadomain.okta.com/oauth2/default', 'Find your Issuer URI in the Admin console under <b>Security -> API</b>, or in the Developer console under <b>API -> Authorization Servers</b>'); },
|
||||
'okta-sign-in-widget',
|
||||
'okta-sign-in-widget-options-section'
|
||||
);
|
||||
|
||||
register_setting('okta-sign-in-widget', 'okta-widget-client-id', array(
|
||||
'type' => 'string',
|
||||
'show_in_rest' => false,
|
||||
));
|
||||
add_settings_field(
|
||||
'okta-widget-client-id',
|
||||
'Sign-In Widget Client ID',
|
||||
function() { $this->optionsPageTextInputAction('okta-widget-client-id', 'text', null, 'Register a "SPA" app in Okta and provide its Client ID here. Set the Login redirect URI in Okta to <code>'.wp_login_url().'</code>, and set the Logout redirect URI to <code>'.home_url().'</code>'); },
|
||||
'okta-sign-in-widget',
|
||||
'okta-sign-in-widget-options-section'
|
||||
);
|
||||
|
||||
register_setting('okta-sign-in-widget', 'okta-allow-wordpress-login', array(
|
||||
'type' => 'boolean',
|
||||
'show_in_rest' => false,
|
||||
));
|
||||
add_settings_field(
|
||||
'okta-allow-wordpress-login',
|
||||
'Allow Native WordPress Login',
|
||||
function() { $this->optionsPageCheckboxInputAction('okta-allow-wordpress-login', 'checkbox', 'Check this to allow local WordPress users to log in with a password. When unchecked, Okta will be the only way users can log in. Make sure you have a WordPress admin user with an email address matching an Okta user already.'); },
|
||||
'okta-sign-in-widget',
|
||||
'okta-sign-in-widget-options-section'
|
||||
);
|
||||
}
|
||||
|
||||
public function optionsMenuAction() {
|
||||
add_options_page(
|
||||
'Okta Sign-In Widget Options',
|
||||
'Okta Sign-In Widget',
|
||||
'manage_options',
|
||||
'okta-sign-in-widget',
|
||||
array($this, 'optionsPageAction')
|
||||
);
|
||||
}
|
||||
|
||||
public function optionsPageAction() {
|
||||
if (current_user_can('manage_options')) {
|
||||
include(plugin_dir_path(__FILE__)."../templates/options-form.php");
|
||||
} else {
|
||||
wp_die( 'You do not have sufficient permissions to access this page.' );
|
||||
}
|
||||
}
|
||||
|
||||
public function optionsPageTextInputAction($option_name, $type, $placeholder=false, $description=false) {
|
||||
$option_value = get_option($option_name, '');
|
||||
printf(
|
||||
'<input type="%s" id="%s" name="%s" value="%s" style="width: 100%%" autocomplete="off" placeholder="%s" />',
|
||||
esc_attr($type),
|
||||
esc_attr($option_name),
|
||||
esc_attr($option_name),
|
||||
esc_attr($option_value),
|
||||
esc_attr($placeholder)
|
||||
);
|
||||
if($description)
|
||||
echo '<p class="description">'.$description.'</p>';
|
||||
}
|
||||
|
||||
public function optionsPageCheckboxInputAction($option_name, $type, $description=false) {
|
||||
$option_value = get_option($option_name, false);
|
||||
printf(
|
||||
'<input type="%s" id="%s" name="%s" value="1" %s>',
|
||||
esc_attr($type),
|
||||
esc_attr($option_name),
|
||||
esc_attr($option_name),
|
||||
$option_value ? 'checked="checked"' : ''
|
||||
);
|
||||
if($description)
|
||||
echo '<p class="description">'.$description.'</p>';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
<script src="https://global.oktacdn.com/okta-signin-widget/5.12.0/js/okta-sign-in.min.js" type="text/javascript"></script>
|
||||
<link href="https://global.oktacdn.com/okta-signin-widget/5.12.0/css/okta-sign-in.min.css" type="text/css" rel="stylesheet"/>
|
||||
@@ -1,226 +0,0 @@
|
||||
<?php
|
||||
namespace Okta;
|
||||
|
||||
/**
|
||||
* Plugin Name: Okta Sign-In Widget
|
||||
* Plugin URI: https://github.com/oktadeveloper/okta-wordpress-sign-in-widget
|
||||
* Description: Log in to your site using the Okta Sign-In Widget
|
||||
* Version: 0.3.0
|
||||
* Author: Aaron Parecki, Tom Smith, Nico Triballier, Joël Franusic
|
||||
* Author URI: https://developer.okta.com/
|
||||
* License: MIT
|
||||
* License URI: http://opensource.org/licenses/MIT
|
||||
* Text Domain: okta
|
||||
* Domain Path: /languages
|
||||
* Update URI: false
|
||||
*/
|
||||
|
||||
include plugin_dir_path(__FILE__).'/includes/okta-admin.php';
|
||||
|
||||
class OktaSignIn
|
||||
{
|
||||
private $OktaAdmin;
|
||||
private $base_url = false;
|
||||
private $introspection_endpoint = false;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->OktaAdmin = new OktaAdmin;
|
||||
|
||||
$this->setBaseUrl();
|
||||
|
||||
// https://developer.wordpress.org/reference/hooks/login_init/
|
||||
add_action('login_init', array($this, 'loginAction'));
|
||||
|
||||
// This runs on every pageload to insert content into the HTML <head> section
|
||||
// https://codex.wordpress.org/Plugin_API/Action_Reference/wp_head
|
||||
add_action('wp_head', array($this, 'addLogInExistingSessionAction'));
|
||||
|
||||
add_action('init', array($this, 'startSessionAction'));
|
||||
}
|
||||
|
||||
private function setBaseUrl()
|
||||
{
|
||||
if($issuer = get_option('okta-issuer-url')) {
|
||||
$this->base_url = parse_url($issuer, PHP_URL_SCHEME).'://'.parse_url($issuer, PHP_URL_HOST);
|
||||
}
|
||||
}
|
||||
|
||||
private function getIntrospectionEndpoint() {
|
||||
if($this->introspection_endpoint)
|
||||
return $this->introspection_endpoint;
|
||||
|
||||
if(!$this->base_url)
|
||||
return false;
|
||||
|
||||
$response = wp_remote_get(get_option('okta-issuer-url').'/.well-known/openid-configuration');
|
||||
if(!$response)
|
||||
return false;
|
||||
|
||||
$metadata = json_decode($response['body'], true);
|
||||
if(!$metadata)
|
||||
return false;
|
||||
|
||||
if(!isset($metadata['introspection_endpoint']))
|
||||
return false;
|
||||
|
||||
return $this->introspection_endpoint = $metadata['introspection_endpoint'];
|
||||
}
|
||||
|
||||
public function startSessionAction()
|
||||
{
|
||||
if (session_status() != PHP_SESSION_ACTIVE) {
|
||||
session_start();
|
||||
}
|
||||
}
|
||||
|
||||
public function addLogInExistingSessionAction()
|
||||
{
|
||||
if (!is_user_logged_in()) {
|
||||
$this->startSessionAction();
|
||||
$_SESSION['redirect_to'] = $_SERVER['REQUEST_URI'];
|
||||
include("templates/log-in-existing-session.php");
|
||||
}
|
||||
}
|
||||
|
||||
private function httpPost($url, $body)
|
||||
{
|
||||
$args = array(
|
||||
'headers' => array(
|
||||
'Accept' => 'application/json',
|
||||
'Content-Type' => 'application/x-www-form-urlencoded',
|
||||
),
|
||||
'body' => $body,
|
||||
);
|
||||
return wp_remote_post($url, $args);
|
||||
}
|
||||
|
||||
public function loginAction()
|
||||
{
|
||||
// Support redirecting back to the page the user was on before they clicked log in
|
||||
$redirect_to = false;
|
||||
if (isset($_GET['redirect_to'])) {
|
||||
$redirect_to = $_GET['redirect_to'];
|
||||
$_SESSION['redirect_to'] = $_GET['redirect_to'];
|
||||
}
|
||||
|
||||
// When signing out of WordPress, tell the Okta JS library to log out of Okta as well
|
||||
if (isset($_GET["action"]) && $_GET["action"] === "logout") {
|
||||
$this->logUserOutOfOkta();
|
||||
}
|
||||
|
||||
if (isset($_GET['log_in_from_id_token'])) {
|
||||
$this->logUserIntoWordPressWithIDToken($_GET['log_in_from_id_token'], $redirect_to);
|
||||
exit;
|
||||
}
|
||||
|
||||
if($this->useWordpressLogin()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// If there is no code in the query string, show the Okta sign-in widget
|
||||
$template = plugin_dir_path(__FILE__) . 'templates/sign-in-form.php';
|
||||
load_template($template);
|
||||
exit;
|
||||
}
|
||||
|
||||
private function useWordpressLogin()
|
||||
{
|
||||
// Always skip showing the Okta widget on POST requests
|
||||
if($_SERVER['REQUEST_METHOD'] === 'POST')
|
||||
return true;
|
||||
|
||||
// If the plugin isn't configured yet, don't show the Okta widget
|
||||
if(!$this->base_url)
|
||||
return true;
|
||||
|
||||
// null when plugin is not configured, "1"/"0" after
|
||||
if(get_option('okta-allow-wordpress-login') === null || get_option('okta-allow-wordpress-login') === "1")
|
||||
{
|
||||
if(isset($_GET['wordpress_login']) && $_GET['wordpress_login'] == 'true')
|
||||
return true;
|
||||
|
||||
if(isset($_GET['action']) && $_GET['action'] == 'lostpassword')
|
||||
return true;
|
||||
|
||||
if(isset($_GET['checkemail']))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function logUserOutOfOkta() {
|
||||
$user = wp_get_current_user();
|
||||
|
||||
wp_clear_auth_cookie();
|
||||
|
||||
$template = plugin_dir_path(__FILE__) . 'templates/sign-out.php';
|
||||
load_template($template);
|
||||
exit;
|
||||
}
|
||||
|
||||
private function logUserIntoWordPressWithIDToken($id_token, $redirect_to)
|
||||
{
|
||||
$introspection_endpoint = $this->getIntrospectionEndpoint();
|
||||
|
||||
if(!$this->introspection_endpoint)
|
||||
die("The plugin is not configured properly. Please double check the Issuer URI in the configuration.");
|
||||
|
||||
/********************************************/
|
||||
// [jpf] TODO: Implement client-side id_token validation to speed up the verification process
|
||||
// (~300ms for /introspect endpoint v. ~5ms for client-side validation)
|
||||
$payload = array(
|
||||
'client_id' => get_option('okta-widget-client-id'),
|
||||
'token' => $id_token,
|
||||
'token_type_hint' => 'id_token'
|
||||
);
|
||||
$response = $this->httpPost($this->introspection_endpoint, $payload);
|
||||
if ($response === false) {
|
||||
die("Invalid id_token received from Okta");
|
||||
}
|
||||
$claims = json_decode($response['body'], true);
|
||||
if (!$claims['active']) {
|
||||
die("Okta reports that id_token is not active or client authentication failed:" . $claims['error_description']);
|
||||
}
|
||||
/********************************************/
|
||||
|
||||
$this->logUserIntoWordPressFromEmail($claims, $redirect_to);
|
||||
}
|
||||
|
||||
private function logUserIntoWordPressFromEmail($claims, $redirect_to)
|
||||
{
|
||||
$email = $claims['email'];
|
||||
|
||||
// Find or create the WordPress user for this email address
|
||||
$user = get_user_by('email', $email);
|
||||
if (!$user) {
|
||||
$random_password = wp_generate_password($length = 64, $include_standard_special_chars = false);
|
||||
$user_id = wp_create_user($email, $random_password, $email);
|
||||
$user = get_user_by('id', $user_id);
|
||||
} else {
|
||||
$user_id = $user->ID;
|
||||
}
|
||||
|
||||
do_action('okta_widget_before_login', $claims, $user);
|
||||
|
||||
// Actually log the user in now
|
||||
wp_set_current_user($user_id);
|
||||
wp_set_auth_cookie($user_id);
|
||||
error_log("Logging in WordPress user with ID of: " . $user_id);
|
||||
|
||||
// See also: https://developer.wordpress.org/reference/functions/do_action/
|
||||
// Run the wp_login actions now that the user is logged in
|
||||
do_action('wp_login', $user->user_login, $user);
|
||||
|
||||
if (isset($_SESSION['redirect_to'])) {
|
||||
$redirect_uri = $_SESSION['redirect_to'];
|
||||
unset($_SESSION['redirect_to']);
|
||||
} else {
|
||||
$redirect_uri = home_url();
|
||||
}
|
||||
wp_redirect($redirect_uri);
|
||||
}
|
||||
}
|
||||
|
||||
$okta = new OktaSignIn();
|
||||
@@ -1,17 +0,0 @@
|
||||
<?php include plugin_dir_path(__FILE__).'/../includes/widget.php'; ?>
|
||||
|
||||
<script>
|
||||
<?php include plugin_dir_path(__FILE__).'/../includes/initialize-widget.js.php'; ?>
|
||||
|
||||
oktaSignIn.authClient.session.exists()
|
||||
.then(function(exists) {
|
||||
if(exists) {
|
||||
oktaSignIn.authClient.token.getWithoutPrompt({
|
||||
responseType: ['id_token'],
|
||||
})
|
||||
.then(function(tokens){
|
||||
window.location.href = '<?php echo wp_login_url() ?>' + '?log_in_from_id_token=' + tokens.tokens.idToken.value;
|
||||
});
|
||||
}
|
||||
});
|
||||
</script>
|
||||
@@ -1,9 +0,0 @@
|
||||
<div class="wrap">
|
||||
<h1>Okta Sign-In Widget</h1>
|
||||
|
||||
<form method="post" action="options.php">
|
||||
<?php settings_fields( 'okta-sign-in-widget' ); ?>
|
||||
<?php do_settings_sections( 'okta-sign-in-widget' ); ?>
|
||||
<?php submit_button(); ?>
|
||||
</form>
|
||||
</div>
|
||||
@@ -1,67 +0,0 @@
|
||||
<?php include plugin_dir_path(__FILE__).'/../includes/widget.php'; ?>
|
||||
|
||||
<style type="text/css">
|
||||
body {
|
||||
font-family: montserrat,Arial,Helvetica,sans-serif;
|
||||
}
|
||||
|
||||
#wordpress-login{
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
#wordpress-login a{
|
||||
font-size:10px;
|
||||
color: #999;
|
||||
text-decoration:none;
|
||||
}
|
||||
|
||||
#error {
|
||||
max-width: 500px;
|
||||
margin: 20px auto;
|
||||
padding: 20px;
|
||||
border: 1px #d93934 solid;
|
||||
border-radius: 6px;
|
||||
}
|
||||
#error h2 {
|
||||
color: #d93934;
|
||||
}
|
||||
</style>
|
||||
|
||||
<?php if(isset($_GET['error'])): ?>
|
||||
<div id="error">
|
||||
<h2>Error: <?php echo htmlspecialchars($_GET['error']) ?></h2>
|
||||
<p><?php echo htmlspecialchars($_GET['error_description']) ?></p>
|
||||
</div>
|
||||
<?php endif ?>
|
||||
|
||||
<div id="primary" class="content-area">
|
||||
<div id="okta-login-container"></div>
|
||||
<?php if(get_option('okta-allow-wordpress-login')): ?>
|
||||
<div id="wordpress-login"><a href="<?php echo wp_login_url(); ?>?wordpress_login=true">Login via Wordpress</a></div>
|
||||
<?php endif ?>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
<?php include plugin_dir_path(__FILE__).'/../includes/initialize-widget.js.php'; ?>
|
||||
|
||||
oktaSignIn.authClient.token.getUserInfo().then(function(user) {
|
||||
console.log("Already logged in");
|
||||
oktaSignIn.authClient.tokenManager.get('idToken').then(function(idToken){
|
||||
window.location = '<?php echo wp_login_url() ?>?log_in_from_id_token='+idToken.value;
|
||||
});
|
||||
}, function(error) {
|
||||
oktaSignIn.showSignInToGetTokens({
|
||||
el: '#okta-login-container'
|
||||
}).then(function(tokens) {
|
||||
oktaSignIn.authClient.tokenManager.setTokens(tokens);
|
||||
|
||||
oktaSignIn.remove(); // Remove the widget from the DOM
|
||||
|
||||
const idToken = tokens.idToken;
|
||||
window.location = '<?php echo wp_login_url() ?>?log_in_from_id_token='+idToken.value;
|
||||
|
||||
}).catch(function(err) {
|
||||
console.error(err);
|
||||
});
|
||||
});
|
||||
</script>
|
||||
@@ -1,16 +0,0 @@
|
||||
<?php include plugin_dir_path(__FILE__).'/../includes/widget.php'; ?>
|
||||
|
||||
<script>
|
||||
<?php include plugin_dir_path(__FILE__).'/../includes/initialize-widget.js.php'; ?>
|
||||
|
||||
oktaSignIn.authClient.tokenManager.get('idToken').then(function(idToken){
|
||||
if(idToken) {
|
||||
oktaSignIn.authClient.signOut({
|
||||
idToken: idToken,
|
||||
postLogoutRedirectUri: '<?php echo home_url() ?>'
|
||||
});
|
||||
} else {
|
||||
window.location = '<?php echo home_url() ?>';
|
||||
}
|
||||
});
|
||||
</script>
|
||||
Reference in New Issue
Block a user