- md5
- 8cc0ce73e129bf111f8eee7b4deac6ba
- sha1
- a4d39fb63a07df143e3bea51c28184c27fd0aa25
- sha256
- 1e2fa8741ffa254634266f9f347d6206564c656a47333a44a5e1dfbecdea9298
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
<?php
/**
* @package Joomla.Plugin
* @subpackage System.Webauthn
*
* @copyright Copyright (C) 2005 - 2020 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Plugin\System\Webauthn\PluginTraits;
// Protect from unauthorized access
defined('_JEXEC') or die();
use Exception;
use Joomla\CMS\Factory;
use Joomla\CMS\Helper\AuthenticationHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\User\UserHelper;
use Joomla\Plugin\System\Webauthn\Helper\Joomla;
/**
* Inserts Webauthn buttons into login modules
*
* @since 4.0.0
*/
trait AdditionalLoginButtons
{
/**
* Do I need to I inject buttons? Automatically detected (i.e. disabled if I'm already logged in).
*
* @var boolean|null
* @since 4.0.0
*/
protected $allowButtonDisplay = null;
/**
* Have I already injected CSS and JavaScript? Prevents double inclusion of the same files.
*
* @var boolean
* @since 4.0.0
*/
private $injectedCSSandJS = false;
/**
* Should I allow this plugin to add a WebAuthn login button?
*
* @return boolean
*
* @since 4.0.0
*/
private function mustDisplayButton(): bool
{
if (is_null($this->allowButtonDisplay))
{
$this->allowButtonDisplay = false;
/**
* Do not add a WebAuthn login button if we are already logged in
*/
try
{
if (!Factory::getApplication()->getIdentity()->guest)
{
return false;
}
}
catch (Exception $e)
{
return false;
}
/**
* Don't try to show a button if we can't figure out if this is a front- or backend page (it's probably a
* CLI or custom application).
*/
try
{
Joomla::isAdminPage();
}
catch (Exception $e)
{
return false;
}
/**
* Only display a button on HTML output
*/
if (Joomla::getDocumentType() != 'html')
{
return false;
}
/**
* WebAuthn only works on HTTPS. This is a security-related limitation of the W3C Web Authentication
* specification, not an issue with this plugin :)
*/
if (!Uri::getInstance()->isSsl())
{
return false;
}
// All checks passed; we should allow displaying a WebAuthn login button
$this->allowButtonDisplay = true;
}
return $this->allowButtonDisplay;
}
/**
* Creates additional login buttons
*
* @param string $form The HTML ID of the form we are enclosed in
*
* @return array
*
* @throws Exception
*
* @see AuthenticationHelper::getLoginButtons()
*
* @since 4.0.0
*/
public function onUserLoginButtons(string $form): array
{
// If we determined we should not inject a button return early
if (!$this->mustDisplayButton())
{
return [];
}
// Load the language files
$this->loadLanguage();
// Load necessary CSS and Javascript files
$this->addLoginCSSAndJavascript();
// Return URL
$uri = new Uri(Uri::base() . 'index.php');
$uri->setVar(Joomla::getToken(), '1');
// Unique ID for this button (allows display of multiple modules on the page)
$randomId = 'plg_system_webauthn-' . UserHelper::genRandomPassword(12) . '-' . UserHelper::genRandomPassword(8);
// Set up the JavaScript callback
$url = $uri->toString();
return [
[
'label' => 'PLG_SYSTEM_WEBAUTHN_LOGIN_LABEL',
'tooltip' => 'PLG_SYSTEM_WEBAUTHN_LOGIN_DESC',
'id' => $randomId,
'data-webauthn-form' => $form,
'data-webauthn-url' => $url,
'image' => 'plg_system_webauthn/webauthn-black.png',
'class' => 'plg_system_webauthn_login_button',
],
];
}
/**
* Injects the WebAuthn CSS and Javascript for frontend logins, but only once per page load.
*
* @return void
*
* @since 4.0.0
*/
private function addLoginCSSAndJavascript(): void
{
if ($this->injectedCSSandJS)
{
return;
}
// Set the "don't load again" flag
$this->injectedCSSandJS = true;
/** @var \Joomla\CMS\WebAsset\WebAssetManager $wa */
$wa = Factory::getApplication()->getDocument()->getWebAssetManager();
if (!$wa->assetExists('style', 'plg_system_webauthn.button'))
{
$wa->registerStyle('plg_system_webauthn.button', 'plg_system_webauthn/button.css');
}
if (!$wa->assetExists('script', 'plg_system_webauthn.login'))
{
$wa->registerScript('plg_system_webauthn.login', 'plg_system_webauthn/login.js', [], ['defer' => true], ['core']);
}
$wa->useStyle('plg_system_webauthn.button')
->useScript('plg_system_webauthn.login');
// Load language strings client-side
Text::script('PLG_SYSTEM_WEBAUTHN_ERR_CANNOT_FIND_USERNAME');
Text::script('PLG_SYSTEM_WEBAUTHN_ERR_EMPTY_USERNAME');
Text::script('PLG_SYSTEM_WEBAUTHN_ERR_INVALID_USERNAME');
// Store the current URL as the default return URL after login (or failure)
Joomla::setSessionVar('returnUrl', Uri::current(), 'plg_system_webauthn');
}
}
The same content is in 3 releases, Joomla 4.0.0-beta2 to 4.0.0-beta4. See them all, or every version of this file.