Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
83.33% covered (success)
83.33%
5 / 6
CRAP
93.48% covered (success)
93.48%
43 / 46
Authenticator
0.00% covered (danger)
0.00%
0 / 1
83.33% covered (success)
83.33%
5 / 6
15
97.73% covered (success)
97.73%
43 / 44
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 authenticate
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
6 / 6
 register
100.00% covered (success)
100.00%
1 / 1
6
100.00% covered (success)
100.00%
15 / 15
 set_auth_cookies
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
5 / 5
 maybe_create_username
0.00% covered (danger)
0.00%
0 / 1
3.02
87.50% covered (success)
87.50%
7 / 8
 can_register_with_email
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
7 / 7
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                    ]
99                );
100
101                /**
102                 * Fires once the user has been registered successfully.
103                 */
104                do_action( 'rtcamp.google_user_created', $uid, $user );
105
106                return get_user_by( 'id', $uid );
107            }
108
109            /* translators: %s is replaced with email ID of user trying to register */
110            throw new Exception( sprintf( __( 'Cannot register with this email: %s', 'login-with-google' ), $user->email ) );
111
112        } catch ( Throwable $e ) {
113
114            throw $e;
115        }
116
117    }
118
119    /**
120     * Set auth cookies for WordPress login.
121     *
122     * @param WP_User $user WP User object.
123     *
124     * @return void
125     */
126    public function set_auth_cookies( WP_User $user ) {
127        wp_clear_auth_cookie();
128        wp_set_current_user( $user->ID, $user->user_login );
129        wp_set_auth_cookie( $user->ID );
130    }
131
132    /**
133     * Assign the `login` property to user object
134     * if it doesn't exists.
135     *
136     * @param stdClass $user User object.
137     *
138     * @return stdClass
139     */
140    private function maybe_create_username( stdClass $user ): stdClass {
141        if ( property_exists( $user, 'login' ) || ! property_exists( $user, 'email' ) ) {
142            return $user;
143        }
144
145        $email       = $user->email;
146        $user_login  = sanitize_user( current( explode( '@', $email ) ), true );
147        $user_login  = Helper::unique_username( $user_login );
148        $user->login = $user_login;
149
150        return $user;
151    }
152
153    /**
154     * Check if given email can be used for registration.
155     *
156     * @param string $email Email ID.
157     *
158     * @return bool
159     */
160    private function can_register_with_email( string $email ): bool {
161        $whitelisted_domains = explode( ',', $this->settings->whitelisted_domains );
162        $whitelisted_domains = array_map( 'strtolower', $whitelisted_domains );
163        $whitelisted_domains = array_map( 'trim', $whitelisted_domains );
164        $email_parts         = explode( '@', $email );
165        $email_parts         = array_map( 'strtolower', $email_parts );
166
167        return in_array( $email_parts[1], $whitelisted_domains, true );
168    }
169}