Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP #9683

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft

WIP #9683

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions includes/Modules/Sign_In_With_Google.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,6 @@ private function render_signin_button() {
$redirect_to = trim( $redirect_to );
}

$config = array(
'client_id' => $settings['clientID'],
'login_uri' => $login_uri,
'ux_mode' => 'redirect',
);

$btn_args = array(
'theme' => $settings['theme'],
'text' => $settings['text'],
Expand All @@ -257,10 +251,26 @@ private function render_signin_button() {
const parent = document.createElement( 'div' );
document.getElementById( 'login' ).insertBefore( parent, document.getElementById( 'loginform' ) );

google.accounts.id.initialize( <?php echo wp_json_encode( $config ); ?> );
async function handleCredentialResponse(response) {
const res = await fetch('<?php echo esc_js( $login_uri ) ?>', {
method: 'POST',
body: new URLSearchParams(response)
});
if (res.ok && res.redirected) {
location.assign(res.url);
}
}

google.accounts.id.initialize( {
client_id: '<?php echo esc_js( $settings['clientID'] ) ?>',
callback: handleCredentialResponse,
} );
google.accounts.id.renderButton( parent, <?php echo wp_json_encode( $btn_args ); ?> );
<?php if ( ! empty( $redirect_to ) ) : // phpcs:ignore Generic.WhiteSpace.ScopeIndent.Incorrect ?>
<?php if ( $settings['oneTapEnabled'] ) : ?>
google.accounts.id.prompt();
<?php endif ?>

<?php if ( ! empty( $redirect_to ) ) : // phpcs:ignore Generic.WhiteSpace.ScopeIndent.Incorrect ?>
const expires = new Date();
expires.setTime( expires.getTime() + 1000 * 60 * 5 );
document.cookie = "<?php echo esc_js( Authenticator::REDIRECT_COOKIE_NAME ); ?>=<?php echo esc_js( $redirect_to ); ?>;expires=" + expires.toUTCString() + ";path=<?php echo esc_js( Authenticator::get_cookie_path() ); ?>";
Expand Down
10 changes: 5 additions & 5 deletions includes/Modules/Sign_In_With_Google/Authenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ public function authenticate_user( Input $input ) {
$login_url = wp_login_url();

// Check if the CSRF token is valid, if not redirect to the login page with an error.
$csrf_cookie = $input->filter( INPUT_COOKIE, 'g_csrf_token' );
$csrf_post = $input->filter( INPUT_POST, 'g_csrf_token' );
if ( ! $csrf_cookie || $csrf_cookie !== $csrf_post ) {
return add_query_arg( 'error', self::ERROR_INVALID_CSRF_TOKEN, $login_url );
}
// $csrf_cookie = $input->filter( INPUT_COOKIE, 'g_csrf_token' );
// $csrf_post = $input->filter( INPUT_POST, 'g_csrf_token' );
// if ( ! $csrf_cookie || $csrf_cookie !== $csrf_post ) {
// return add_query_arg( 'error', self::ERROR_INVALID_CSRF_TOKEN, $login_url );
// }

$credential = $input->filter( INPUT_POST, 'credential' );

Expand Down