settings = $settings; $this->authenticator = new AhJwtAuthAuthenticator( $settings ); add_action( 'admin_notices', array( $this, 'ahjwtauth_admin_notice' ) ); add_action( 'init', array( $this, 'ahjwtauth_log_user_in' ) ); } /** * Logs user in based on JWT * * If a valid JWT is present in the HTTP request it is verified and parsed so * the user contained in the token can be signed in * * @return void */ public function ahjwtauth_log_user_in() { // if user is already logged in just return immediately. if ( is_user_logged_in() ) { return; } // Allow background tasks (WP-Cron, WP-CLI) to bypass JWT checks. if ( defined( 'DOING_CRON' ) && DOING_CRON ) { return; } if ( defined( 'WP_CLI' ) && WP_CLI ) { return; } $user = $this->authenticator->authenticate( getallheaders() ); if ( $user instanceof AhJwtAuthSignInFailure ) { $this->handle_failure( $user ); return; } wp_clear_auth_cookie(); wp_set_current_user( $user->ID ); wp_set_auth_cookie( $user->ID ); do_action( 'wp_login', $user->user_login, $user ); // redirect after login. $redirect_to = current_user_can( 'manage_options' ) ? admin_url() : home_url(); if ( isset( $_GET['redirect_to'], $_GET['redirect_to_nonce'] ) && wp_verify_nonce( sanitize_key( $_GET['redirect_to_nonce'] ), 'ahjwtauth_log_user_in' ) ) { $redirect_to = esc_url_raw( wp_unslash( $_GET['redirect_to'] ) ); } wp_safe_redirect( $redirect_to ); exit; } /** * Prints any admin notices * * If any warning or error values have been set, they are printed using this function * * @return void */ public function ahjwtauth_admin_notice() { // Only print admin notices to administrative users. if ( current_user_can( 'manage_options' ) ) { if ( isset( $this->error ) ) { $class = 'notice notice-error'; $message = $this->error; printf( '
%2$s
%2$s