Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.37% covered (success)
97.37%
37 / 38
83.33% covered (success)
83.33%
5 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Authenticator
97.37% covered (success)
97.37%
37 / 38
83.33% covered (success)
83.33%
5 / 6
15
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 authenticate
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 register
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
6
 set_auth_cookies
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 maybe_create_username
85.71% covered (success)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
3.03
 can_register_with_email
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Authenticator class.
4 *
5 * This will authenticate the user. Also responsible for registration
6 * in case it is enabled in the settings.
7 *
8 * @package RtCamp\GoogleLogin
9 * @since 1.1.1
10 */
11
12declare(strict_types=1);
13
14namespace RtCamp\GoogleLogin\Utils;
15
16use WP_User;
17use stdClass;
18use Exception;
19use Throwable;
20use InvalidArgumentException;
21use RtCamp\GoogleLogin\Modules\Settings;
22
23/**
24 * Class Authenticator
25 *
26 * @package RtCamp\GoogleLogin\Utils
27 */
28class Authenticator {
29    /**
30     * Settings instance.
31     *
32     * @var Settings
33     */
34    private $settings;
35
36    /**
37     * Authenticator constructor.
38     *
39     * @param Settings $settings Settings instance.
40     */
41    public function __construct( Settings $settings ) {
42        $this->settings = $settings;
43    }
44
45    /**
46     * Authenticate the user.
47     *
48     * If registration setting is on, user will be created if
49     * that user does not exist in the application.
50     *
51     * @param stdClass $user User data object returned by Google.
52     *
53     * @return WP_User
54     * @throws InvalidArgumentException For invalid registrations.
55     */
56    public function authenticate( stdClass $user ): WP_User {
57        if ( ! property_exists( $user, 'email' ) ) {
58            throw new InvalidArgumentException( __( 'Email needs to be present for the user.', 'login-with-google' ) );
59        }
60
61        if ( email_exists( $user->email ) ) {
62            return get_user_by( 'email', $user->email );
63        }
64
65        /**
66         * Check if we need to register the user.
67         *
68         * @param stdClass $user User object from google.
69         * @since 1.0.0
70         */
71        return apply_filters( 'rtcamp.google_register_user', $this->maybe_create_username( $user ) );
72    }
73
74    /**
75     * Register the new user if setting is on for registration.
76     *
77     * @param stdClass $user User object from google.
78     *
79     * @return WP_User|null
80     * @throws Throwable Invalid email registration.
81     * @throws Exception Registration is off.
82     */
83    public function register( stdClass $user ): ?WP_User {
84        $register = true === (bool) $this->settings->registration_enabled || (bool) get_option( 'users_can_register', false );
85
86        if ( ! $register ) {
87            throw new Exception( __( 'Registration is not allowed.', 'login-with-google' ) );
88        }
89
90        try {
91            $whitelisted_domains = $this->settings->whitelisted_domains;
92            if ( empty( $whitelisted_domains ) || $this->can_register_with_email( $user->email ) ) {
93                $uid = wp_insert_user(
94                    [
95                        'user_login' => Helper::unique_username( $user->login ),
96                        'user_pass'  => wp_generate_password( 18 ),
97                        'user_email' => $user->email,
98                        'first_name' => $user->given_name ?? '',
99                        'last_name'  => $user->family_name ?? '',
100                    ]
101                );
102
103                /**
104                 * Fires once the user has been registered successfully.
105                 */
106                do_action( 'rtcamp.google_user_created', $uid, $user );
107
108                return get_user_by( 'id', $uid );
109            }
110
111            /* translators: %s is replaced with email ID of user trying to register */
112            throw new Exception( sprintf( __( 'Cannot register with this email: %s', 'login-with-google' ), $user->email ) );
113
114        } catch ( Throwable $e ) {
115
116            throw $e;
117        }
118
119    }
120
121    /**
122     * Set auth cookies for WordPress login.
123     *
124     * @param WP_User $user WP User object.
125     *
126     * @return void
127     */
128    public function set_auth_cookies( WP_User $user ) {
129        wp_clear_auth_cookie();
130        wp_set_current_user( $user->ID, $user->user_login );
131        wp_set_auth_cookie( $user->ID );
132    }
133
134    /**
135     * Assign the `login` property to user object
136     * if it doesn't exists.
137     *
138     * @param stdClass $user User object.
139     *
140     * @return stdClass
141     */
142    private function maybe_create_username( stdClass $user ): stdClass {
143        if ( property_exists( $user, 'login' ) || ! property_exists( $user, 'email' ) ) {
144            return $user;
145        }
146
147        $email       = $user->email;
148        $user_login  = sanitize_user( current( explode( '@', $email ) ), true );
149        $user_login  = Helper::unique_username( $user_login );
150        $user->login = $user_login;
151
152        return $user;
153    }
154
155    /**
156     * Check if given email can be used for registration.
157     *
158     * @param string $email Email ID.
159     *
160     * @return bool
161     */
162    private function can_register_with_email( string $email ): bool {
163        $whitelisted_domains = explode( ',', $this->settings->whitelisted_domains );
164        $whitelisted_domains = array_map( 'strtolower', $whitelisted_domains );
165        $whitelisted_domains = array_map( 'trim', $whitelisted_domains );
166        $email_parts         = explode( '@', $email );
167        $email_parts         = array_map( 'strtolower', $email_parts );
168
169        return in_array( $email_parts[1], $whitelisted_domains, true );
170    }
171}