Completed
Push — add/password-strength-checker ( 6d3894 )
by
unknown
07:14
created

Jetpack_Sync_Module_Auth   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 66
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
A init_listeners() 0 6 1
A check_password() 0 27 3
1
<?php
2
/**
3
 * Jetpack Auth Sync module.
4
 *
5
 * @package wpcomsh
6
 */
7
8
/**
9
 * The Sync module that implements listeners to authentication events.
10
 */
11
class Jetpack_Sync_Module_Auth extends Jetpack_Sync_Module {
12
13
	/**
14
	 * The event handler to be used for events.
15
	 *
16
	 * @var Callable
17
	 */
18
	private $handler;
19
20
	/**
21
	 * Returns the Sync module name.
22
	 *
23
	 * @return String $name
24
	 */
25
	public function name() {
26
		return 'auth';
27
	}
28
29
	/**
30
	 * Setting up a listener that would report unsafe password usage.
31
	 *
32
	 * @param Callable $callable action handler.
33
	 */
34
	public function init_listeners( $callable ) {
35
		$this->handler = $callable;
36
37
		// User authentication.
38
		add_filter( 'authenticate', array( $this, 'check_password' ), 1000, 3 );
39
	}
40
41
	/**
42
	 * A hook for the authenticate event that checks the password strength.
43
	 *
44
	 * @param WP_Error|WP_User $user     the user object, or an error.
45
	 * @param String           $username the username.
46
	 * @param Sting            $password the password used to authenticate.
47
	 * @return WP_Error|WP_User the same object that was passed into the function.
48
	 */
49
	public function check_password( $user, $username, $password ) {
50
		jetpack_require_lib( 'class.jetpack-password-checker' );
51
52
		// We are only interested in successful authentication events.
53
		if ( is_wp_error( $user ) ) {
54
			return $user;
55
		}
56
57
		$password_checker = new Jetpack_Password_Checker( $user->ID );
58
59
		$test_results = $password_checker->test( $password, true );
60
61
		// If the password passes tests, we don't do anything.
62
		if ( empty( $test_results['test_results']['failed'] ) ) {
63
			return $user;
64
		}
65
66
		call_user_func(
67
			$this->handler,
68
			array(
69
				'warning'          => 'The password failed at least one strength test.',
70
				'external_user_id' => $user->ID,
71
				'failures'         => $test_results['test_results']['failed'],
72
			)
73
		);
74
		return $user;
75
	}
76
}
77