Version 5.0.0 beta 1

This commit is contained in:
Neo committed 2025-12-19 16:27:35 -08:00
1 parent 25ddeb65d6
commit 15c7beabc5
6736 files changed
+627902 -497943

No files matched your search

+88 -69
View File
@@ -11,28 +11,47 @@
namespace IPS\Login\Handler\OAuth2;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use DomainException;
use IPS\File;
use IPS\Helpers\Form\Color;
use IPS\Helpers\Form\Translatable;
use IPS\Helpers\Form\Upload;
use IPS\Http\Url;
use IPS\Lang;
use IPS\Login;
use IPS\Login\Exception;
use IPS\Login\Handler\OAuth2;
use IPS\Member;
use RuntimeException;
use function count;
use function defined;
use function is_array;
use function is_string;
use const IPS\OAUTH_REQUIRES_HTTPS;
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* Wordpress oAuth Login Handler
*/
class _Wordpress extends \IPS\Login\Handler\OAuth2
class Wordpress extends OAuth2
{
/**
* @brief Can we have multiple instances of this handler?
*/
public static $allowMultiple = TRUE;
public static bool $allowMultiple = TRUE;
/**
* Get title
*
* @return string
*/
public static function getTitle()
public static function getTitle(): string
{
return 'login_handler_wordpress';
}
@@ -45,7 +64,7 @@ class _Wordpress extends \IPS\Login\Handler\OAuth2
return array( 'savekey' => new \IPS\Helpers\Form\[Type]( ... ), ... );
* @endcode
*/
public function acpForm()
public function acpForm(): array
{
$return = array();
$return[] = array( 'login_handler_wordpress_settings', 'login_handler_wordpress_oauth_info' );
@@ -58,7 +77,7 @@ class _Wordpress extends \IPS\Login\Handler\OAuth2
{
$active = 'accountManagementSettings';
}
if ( !\is_string( $v ) and !\is_array( $v ) )
if ( !is_string( $v ) and !is_array( $v ) )
{
${$active}[ $k ] = $v;
}
@@ -66,24 +85,24 @@ class _Wordpress extends \IPS\Login\Handler\OAuth2
$endpointValidation = function( $val )
{
if ( \IPS\OAUTH_REQUIRES_HTTPS and $val and $val instanceof \IPS\Http\Url )
if ( OAUTH_REQUIRES_HTTPS and $val and $val instanceof Url )
{
if ( $val->data[ \IPS\Http\Url::COMPONENT_SCHEME ] !== 'https' )
if ( $val->data[ Url::COMPONENT_SCHEME ] !== 'https' )
{
throw new \DomainException('authorization_endpoint_https');
throw new DomainException('authorization_endpoint_https');
}
if ( $val->data[ \IPS\Http\Url::COMPONENT_FRAGMENT ] )
if ( $val->data[ Url::COMPONENT_FRAGMENT ] )
{
throw new \DomainException('authorization_endpoint_fragment');
throw new DomainException('authorization_endpoint_fragment');
}
}
};
$return['wordpress_url'] = new \IPS\Helpers\Form\Url( 'wordpress_url', isset( $this->settings['wordpress_url'] ) ? $this->settings['wordpress_url'] : NULL, NULL, array( 'placeholder' => 'https://example.com/' ), $endpointValidation, NULL, NULL, 'wordpress_url' );
$return['wordpress_url'] = new \IPS\Helpers\Form\Url( 'wordpress_url', $this->settings['wordpress_url'] ?? NULL, NULL, array( 'placeholder' => 'https://example.com/' ), $endpointValidation, NULL, NULL, 'wordpress_url' );
$return[] = 'login_handler_oauth_ui';
$return['button_color'] = new \IPS\Helpers\Form\Color( 'oauth_custom_button_color', isset( $this->settings['button_color'] ) ? $this->settings['button_color'] : '#23282d', NULL, array(), NULL, NULL, NULL, 'button_color' );
$return['button_text'] = new \IPS\Helpers\Form\Translatable( 'oauth_custom_button_text', NULL, NULL, array( 'placeholder' => \IPS\Member::loggedIn()->language()->addToStack('oauth_custom_button_text_custom_placeholder'), 'app' => 'core', 'key' => ( $this->id ? "core_custom_oauth_{$this->id}" : NULL ) ), NULL, NULL, NULL, 'button_text' );
$return['button_icon'] = new \IPS\Helpers\Form\Upload( 'oauth_custom_button_icon', ( isset( $this->settings['button_icon'] ) and $this->settings['button_icon'] ) ? \IPS\File::get( 'core_Login', $this->settings['button_icon'] ) : NULL, FALSE, array( 'storageExtension' => 'core_Login' ), NULL, NULL, NULL, 'button_icon' );
$return['button_color'] = new Color( 'oauth_custom_button_color', $this->settings['button_color'] ?? '#23282d', NULL, array(), NULL, NULL, NULL, 'button_color' );
$return['button_text'] = new Translatable( 'oauth_custom_button_text', NULL, NULL, array( 'placeholder' => Member::loggedIn()->language()->addToStack('oauth_custom_button_text_custom_placeholder'), 'app' => 'core', 'key' => ( $this->id ? "core_custom_oauth_{$this->id}" : NULL ) ), NULL, NULL, NULL, 'button_text' );
$return['button_icon'] = new Upload( 'oauth_custom_button_icon', ( isset( $this->settings['button_icon'] ) and $this->settings['button_icon'] ) ? File::get( 'core_Login', $this->settings['button_icon'] ) : NULL, FALSE, array( 'storageExtension' => 'core_Login' ), NULL, NULL, NULL, 'button_icon' );
$return[] = 'account_management_settings';
foreach ( $accountManagementSettings as $k => $v )
@@ -100,7 +119,7 @@ class _Wordpress extends \IPS\Login\Handler\OAuth2
* @param array $values Values from form
* @return array
*/
public function acpFormSave( &$values )
public function acpFormSave( array &$values ): array
{
$return = parent::acpFormSave( $values );
$return['button_icon'] = (string) $return['button_icon'];
@@ -114,7 +133,7 @@ class _Wordpress extends \IPS\Login\Handler\OAuth2
* @param array $values Values from the form
* @return array
*/
public function formatFormValues( $values )
public function formatFormValues( array $values ): array
{
$parent = parent::formatFormValues( $values );
@@ -124,7 +143,7 @@ class _Wordpress extends \IPS\Login\Handler\OAuth2
{
$this->save();
}
\IPS\Lang::saveCustom( 'core', "core_custom_oauth_{$this->id}", $values['oauth_custom_button_text'] );
Lang::saveCustom( 'core', "core_custom_oauth_{$this->id}", $values['oauth_custom_button_text'] );
unset( $values['button_text'] );
}
@@ -136,7 +155,7 @@ class _Wordpress extends \IPS\Login\Handler\OAuth2
*
* @return string
*/
public function buttonColor()
public function buttonColor(): string
{
return $this->settings['button_color'];
}
@@ -146,28 +165,28 @@ class _Wordpress extends \IPS\Login\Handler\OAuth2
*
* @return string
*/
public function buttonIcon()
public function buttonIcon(): string
{
return ( isset( $this->settings['button_icon'] ) and $this->settings['button_icon'] ) ? \IPS\File::get( 'core_Login', $this->settings['button_icon'] ) : 'wordpress';
return 'wordpress';
}
/**
* Get logo to display in information about logins with this method
* Returns NULL for methods where it is not necessary to indicate the method, e..g Standard
*
* @return \IPS\Http\Url
* @return Url|string|null
*/
public function logoForDeviceInformation()
public function logoForDeviceInformation(): Url|string|null
{
return ( isset( $this->settings['button_icon'] ) and $this->settings['button_icon'] ) ? \IPS\File::get( 'core_Login', $this->settings['button_icon'] )->url : NULL;
return ( isset( $this->settings['button_icon'] ) and $this->settings['button_icon'] ) ? File::get( 'core_Login', $this->settings['button_icon'] )->url : NULL;
}
/**
* Get logo to display in user cp sidebar
*
* @return \IPS\Http\Url
* @return Url|string|null
*/
public function logoForUcp()
public function logoForUcp(): Url|string|null
{
return $this->logoForDeviceInformation() ?: 'wordpress';
}
@@ -177,7 +196,7 @@ class _Wordpress extends \IPS\Login\Handler\OAuth2
*
* @return string
*/
public function buttonText()
public function buttonText(): string
{
return "core_custom_oauth_{$this->id}";
}
@@ -187,7 +206,7 @@ class _Wordpress extends \IPS\Login\Handler\OAuth2
*
* @return string
*/
protected function _authenticationType()
protected function _authenticationType(): string
{
return static::AUTHENTICATE_POST; // Just because it's possible their server isn't configured to accept HTTP Authorization whereas we know this will always work
}
@@ -197,7 +216,7 @@ class _Wordpress extends \IPS\Login\Handler\OAuth2
*
* @return string
*/
protected function grantType()
protected function grantType(): string
{
return 'authorization_code';
}
@@ -208,7 +227,7 @@ class _Wordpress extends \IPS\Login\Handler\OAuth2
* @param array|NULL $additional Any additional scopes to request
* @return array
*/
protected function scopesToRequest( $additional=NULL )
protected function scopesToRequest( array $additional=NULL ): array
{
return array( "profile" );
}
@@ -216,31 +235,31 @@ class _Wordpress extends \IPS\Login\Handler\OAuth2
/**
* Authorization Endpoint
*
* @param \IPS\Login $login The login object
* @return \IPS\Http\Url
* @param Login $login The login object
* @return Url
*/
protected function authorizationEndpoint( \IPS\Login $login )
protected function authorizationEndpoint( Login $login ): Url
{
return \IPS\Http\Url::external( rtrim( $this->settings['wordpress_url'], "/" ) . "/wp-json/moserver/authorize" );
return Url::external( rtrim( $this->settings['wordpress_url'], "/" ) . "/wp-json/moserver/authorize" );
}
/**
* Token Endpoint
*
* @return \IPS\Http\Url
* @return Url
*/
protected function tokenEndpoint()
protected function tokenEndpoint(): Url
{
return \IPS\Http\Url::external( rtrim( $this->settings['wordpress_url'], "/" ) . "/wp-json/moserver/token" );
return Url::external( rtrim( $this->settings['wordpress_url'], "/" ) . "/wp-json/moserver/token" );
}
/**
* Get authenticated user's identifier (may not be a number)
*
* @param string $accessToken Access Token
* @return string
* @param string $accessToken Access Token
* @return string|null
*/
protected function authenticatedUserId( $accessToken )
protected function authenticatedUserId( string $accessToken ): ?string
{
if ( isset( $this->_userData( $accessToken )[ 'username' ] ) )
{
@@ -256,7 +275,7 @@ class _Wordpress extends \IPS\Login\Handler\OAuth2
* @param string $accessToken Access Token
* @return string|NULL
*/
protected function authenticatedUserName( $accessToken )
protected function authenticatedUserName( string $accessToken ): ?string
{
if ( isset( $this->_userData( $accessToken )[ 'display_name' ] ) )
{
@@ -272,7 +291,7 @@ class _Wordpress extends \IPS\Login\Handler\OAuth2
* @param string $accessToken Access Token
* @return string|NULL
*/
protected function authenticatedEmail( $accessToken )
protected function authenticatedEmail( string $accessToken ): ?string
{
if ( isset( $this->_userData( $accessToken )[ 'email' ] ) )
{
@@ -285,17 +304,17 @@ class _Wordpress extends \IPS\Login\Handler\OAuth2
* Get user's profile photo
* May return NULL if server doesn't support this
*
* @param \IPS\Member $member Member
* @return \IPS\Http\Url|NULL
* @throws \IPS\Login\Exception The token is invalid and the user needs to reauthenticate
* @throws \DomainException General error where it is safe to show a message to the user
* @throws \RuntimeException Unexpected error from service
* @param Member $member Member
* @return Url|NULL
* @throws Exception The token is invalid and the user needs to reauthenticate
* @throws DomainException General error where it is safe to show a message to the user
* @throws RuntimeException Unexpected error from service
*/
public function userProfilePhoto( \IPS\Member $member )
public function userProfilePhoto( Member $member ): ?Url
{
if ( !( $link = $this->_link( $member ) ) or ( $link['token_expires'] and $link['token_expires'] < time() ) )
{
throw new \IPS\Login\Exception( NULL, \IPS\Login\Exception::INTERNAL_ERROR );
throw new Exception( "", Exception::INTERNAL_ERROR );
}
if ( isset( $this->_userData( $link['token_access_token'] )[ 'avatar' ] ) )
@@ -304,7 +323,7 @@ class _Wordpress extends \IPS\Login\Handler\OAuth2
try
{
return \IPS\Http\Url::external( $url );
return Url::external( $url );
}
catch ( \Exception $e ) { }
}
@@ -315,17 +334,17 @@ class _Wordpress extends \IPS\Login\Handler\OAuth2
* Get user's profile name
* May return NULL if server doesn't support this
*
* @param \IPS\Member $member Member
* @param Member $member Member
* @return string|NULL
* @throws \IPS\Login\Exception The token is invalid and the user needs to reauthenticate
* @throws \DomainException General error where it is safe to show a message to the user
* @throws \RuntimeException Unexpected error from service
* @throws Exception The token is invalid and the user needs to reauthenticate
* @throws DomainException General error where it is safe to show a message to the user
* @throws RuntimeException Unexpected error from service
*/
public function userProfileName( \IPS\Member $member )
public function userProfileName( Member $member ): ?string
{
if ( !( $link = $this->_link( $member ) ) or ( $link['token_expires'] and $link['token_expires'] < time() ) )
{
throw new \IPS\Login\Exception( NULL, \IPS\Login\Exception::INTERNAL_ERROR );
throw new Exception( "", Exception::INTERNAL_ERROR );
}
return $this->authenticatedUserName( $link['token_access_token'] );
@@ -334,11 +353,11 @@ class _Wordpress extends \IPS\Login\Handler\OAuth2
/**
* Syncing Options
*
* @param \IPS\Member $member The member we're asking for (can be used to not show certain options iof the user didn't grant those scopes)
* @param Member $member The member we're asking for (can be used to not show certain options iof the user didn't grant those scopes)
* @param bool $defaultOnly If TRUE, only returns which options should be enabled by default for a new account
* @return array
*/
public function syncOptions( \IPS\Member $member, $defaultOnly = FALSE )
public function syncOptions( Member $member, bool $defaultOnly=FALSE ): array
{
$return = array();
@@ -358,20 +377,20 @@ class _Wordpress extends \IPS\Login\Handler\OAuth2
/**
* @brief Cached user data
*/
protected $_cachedUserData = array();
protected array $_cachedUserData = array();
/**
* Get user data
*
* @param string $accessToken Access Token
* @throws \IPS\Login\Exception The token is invalid and the user needs to reauthenticate
* @throws \RuntimeException Unexpected error from service
* @throws Exception The token is invalid and the user needs to reauthenticate
* @throws RuntimeException Unexpected error from service
*/
protected function _userData( $accessToken )
protected function _userData( string $accessToken ): array
{
if ( !isset( $this->_cachedUserData[ $accessToken ] ) )
{
$data = \IPS\Http\Url::external( rtrim( $this->settings['wordpress_url'], "/" ) . "/wp-json/moserver/resource" )
$data = Url::external( rtrim( $this->settings['wordpress_url'], "/" ) . "/wp-json/moserver/resource" )
->request()
->setHeaders( array(
'Authorization' => "Bearer {$accessToken}"
@@ -380,9 +399,9 @@ class _Wordpress extends \IPS\Login\Handler\OAuth2
->decodeJson();
/* As with the Custom handler - if we did not get expected data, try using the query string method. */
if ( !\count( $data ) )
if ( !count( $data ) )
{
$data = \IPS\Http\Url::external( rtrim( $this->settings['wordpress_url'], "/" ) . "/wp-json/moserver/resource" )
$data = Url::external( rtrim( $this->settings['wordpress_url'], "/" ) . "/wp-json/moserver/resource" )
->setQueryString( 'access_token', $accessToken )
->request()
->get()
@@ -397,10 +416,10 @@ class _Wordpress extends \IPS\Login\Handler\OAuth2
/**
* Forgot Password URL
*
* @return \IPS\Http\Url|NULL
* @return Url|NULL
*/
public function forgotPasswordUrl()
public function forgotPasswordUrl(): ?Url
{
return \IPS\Http\Url::external( rtrim( $this->settings['wordpress_url'], "/" ) . "/wp-login.php?action=lostpassword" );
return Url::external( rtrim( $this->settings['wordpress_url'], "/" ) . "/wp-login.php?action=lostpassword" );
}
}