Skip to content

Commit

Permalink
SSO: switch from module codebase to Connection package, part 3 (#37153)
Browse files Browse the repository at this point in the history
* SSO: remove references to Jetpack class

* Switch to modern display of notice and add missing i18n wrapping

* Load SSO feature from the Connection package

* Switch to Connection package for all SSO references

* Deprecate all module classes and methods

* Remove module test

* Remove test reference too

* Remove one more test directory (multisite)

* Update Phan config

* Move SSO callables to the Connection package

See https://github.com/Automattic/jetpack/pull/37153/files#r1586113523

* Add deprecated class back

Co-authored-by: sergeymitr <sergeymitr@gmail.com>

* Remove deprecated private methods

See https://github.com/Automattic/jetpack/pull/37153/files#r1586438953

Co-authored-by: sergeymitr <sergeymitr@gmail.com>

* Remove private properties and methods

See Automattic/jetpack#37153 (comment)

Co-authored-by: sergeymitr <sergeymitr@gmail.com>

* Move user generation to Utils class

See Automattic/jetpack#37153 (comment)

* Try fixing tests

* Revert "Try fixing tests"

This reverts commit 0ea30ae53034e025fa3e7938470991b65d254204.

* Bump versions

* Ensure the SSO module is used in tests

* Add new SSO XML-RPC method

* Jetpack Connection: Bump package version

---------

Co-authored-by: sergeymitr <sergeymitr@gmail.com>
Co-authored-by: Foteini Giannaropoulou <giannaropoulou.foteini@gmail.com>

Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/9080205827

Upstream-Ref: Automattic/jetpack@101ddb3
  • Loading branch information
jeherve authored and matticbot committed May 14, 2024
1 parent 5f50766 commit ffa1667
Show file tree
Hide file tree
Showing 17 changed files with 187 additions and 161 deletions.
66 changes: 33 additions & 33 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions vendor/automattic/jetpack-connection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.8.1-alpha] - unreleased

This is an alpha version! The changes listed here are not final.

### Changed
- SSO: do not rely on the Jetpack class anymore.

## [2.8.0] - 2024-05-13
### Added
- SSO: Ensuring tooltips are accessible [#37302]
Expand Down Expand Up @@ -1058,6 +1065,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Separate the connection library into its own package.

[2.8.1-alpha]: https://github.com/Automattic/jetpack-connection/compare/v2.8.0...v2.8.1-alpha
[2.8.0]: https://github.com/Automattic/jetpack-connection/compare/v2.7.7...v2.8.0
[2.7.7]: https://github.com/Automattic/jetpack-connection/compare/v2.7.6...v2.7.7
[2.7.6]: https://github.com/Automattic/jetpack-connection/compare/v2.7.5...v2.7.6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
class Package_Version {

const PACKAGE_VERSION = '2.8.0';
const PACKAGE_VERSION = '2.8.1-alpha';

const PACKAGE_SLUG = 'connection';

Expand Down
49 changes: 49 additions & 0 deletions vendor/automattic/jetpack-connection/src/class-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,53 @@ public static function filter_register_request_body( $properties ) {
)
);
}

/**
* Generate a new user from a SSO attempt.
*
* @param object $user_data WordPress.com user information.
*/
public static function generate_user( $user_data ) {
$username = $user_data->login;
/**
* Determines how many times the SSO module can attempt to randomly generate a user.
*
* @module sso
*
* @since jetpack-4.3.2
*
* @param int 5 By default, SSO will attempt to random generate a user up to 5 times.
*/
$num_tries = (int) apply_filters( 'jetpack_sso_allowed_username_generate_retries', 5 );

$exists = username_exists( $username );
$tries = 0;
while ( $exists && $tries++ < $num_tries ) {
$username = $user_data->login . '_' . $user_data->ID . '_' . wp_rand();
$exists = username_exists( $username );
}

if ( $exists ) {
return false;
}

$user = (object) array();
$user->user_pass = wp_generate_password( 20 );
$user->user_login = wp_slash( $username );
$user->user_email = wp_slash( $user_data->email );
$user->display_name = $user_data->display_name;
$user->first_name = $user_data->first_name;
$user->last_name = $user_data->last_name;
$user->url = $user_data->url;
$user->description = $user_data->description;

if ( isset( $user_data->role ) && $user_data->role ) {
$user->role = $user_data->role;
}

$created_user_id = wp_insert_user( $user );

update_user_meta( $created_user_id, 'wpcom_user_id', $user_data->ID );
return get_userdata( $created_user_id );
}
}
12 changes: 7 additions & 5 deletions vendor/automattic/jetpack-connection/src/sso/class-force-2fa.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ public function plugins_loaded() {
$this->role = apply_filters( 'jetpack_force_2fa_cap', 'manage_options' );

// Bail if Jetpack SSO is not active
if (
! class_exists( 'Jetpack' )
|| ! ( new Modules() )->is_active( 'sso' )
) {
if ( ! ( new Modules() )->is_active( 'sso' ) ) {
add_action( 'admin_notices', array( $this, 'admin_notice' ) );
return;
}
Expand All @@ -75,7 +72,12 @@ public function admin_notice() {
* @module SSO
*/
if ( apply_filters( 'jetpack_force_2fa_dependency_notice', true ) && current_user_can( $this->role ) ) {
printf( '<div class="%1$s"><p>%2$s</p></div>', 'notice notice-warning', 'Jetpack Force 2FA requires Jetpack and the Jetpack SSO module.' );
wp_admin_notice(
esc_html__( 'Jetpack Force 2FA requires Jetpack’s SSO feature.', 'jetpack-connection' ),
array(
'type' => 'warning',
)
);
}
}

Expand Down
Loading

0 comments on commit ffa1667

Please sign in to comment.