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

Use WPCOM as a proxy for Google OAuth2 flow #962

Merged
merged 7 commits into from
Jun 1, 2021
Merged
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
22 changes: 12 additions & 10 deletions assets/wizards/dashboard/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,28 @@ import './style.scss';

const Dashboard = ( { items } ) => {
const params = qs.parse( window.location.search );
const authCode = params.code;
const accessTokenInURL = params.access_token;
const [ authState, setAuthState ] = useState( {} );

const userBasicInfo = authState.user_basic_info;
const canUseOauth = authState.can_google_auth;

const displayAuth = canUseOauth && ! authCode;
const displayAuth = canUseOauth && ! accessTokenInURL;

useEffect( () => {
apiFetch( { path: '/newspack/v1/oauth/google' } ).then( setAuthState );
}, [] );

useEffect( () => {
if ( canUseOauth && authCode ) {
if ( canUseOauth && accessTokenInURL ) {
apiFetch( {
path: '/newspack/v1/oauth/google',
path: '/newspack/v1/oauth/google/finish',
method: 'POST',
data: {
auth_code: authCode,
state: params.state,
access_token: accessTokenInURL,
refresh_token: params.refresh_token,
csrf_token: params.csrf_token,
expires_at: params.expires_at,
},
} )
.then( () => {
Expand All @@ -78,7 +80,7 @@ const Dashboard = ( { items } ) => {

const goToAuthPage = () => {
apiFetch( {
path: '/newspack/v1/oauth/google/get-url',
path: '/newspack/v1/oauth/google/start',
} ).then( url => ( window.location = url ) );
};

Expand All @@ -95,7 +97,7 @@ const Dashboard = ( { items } ) => {
{ items.map( card => (
<DashboardCard { ...card } key={ card.slug } />
) ) }
{ authCode && (
{ accessTokenInURL && (
<div className="flex justify-around items-center">
<Waiting />
</div>
Expand All @@ -106,7 +108,7 @@ const Dashboard = ( { items } ) => {
<Card className="newspack-dashboard-card">
<Icon icon={ plugins } height={ 48 } width={ 48 } />
<div>
<h2>{ __( 'Google OAuth2' ) }</h2>
<h2>{ __( 'Google Connection' ) }</h2>
<p>
{ __( 'Authorized Google as', 'newspack' ) }{' '}
<strong>{ userBasicInfo.email }</strong>
Expand All @@ -116,7 +118,7 @@ const Dashboard = ( { items } ) => {
) : (
<ButtonCard
onClick={ goToAuthPage }
title={ __( 'Google OAuth2', 'newspack' ) }
title={ __( 'Google Connection', 'newspack' ) }
desc={ __( 'Authorize Newspack with Google', 'newspack' ) }
icon={ plugins }
tabIndex="0"
Expand Down
2 changes: 1 addition & 1 deletion assets/wizards/support/components/withWPCOMAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const withWPCOMAuth = WrappedComponent => {
if ( WPCOM_ACCESS_TOKEN ) {
this.setState( { isInFlight: true } );
apiFetch( {
path: `/newspack/v1/wizard/newspack-support-wizard/validate-access-token`,
path: `/newspack/v1/oauth/wpcom/validate`,
} )
.then( () => {
this.setState( { isInFlight: false, shouldAuthenticate: false } );
Expand Down
2 changes: 1 addition & 1 deletion assets/wizards/support/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class SupportWizard extends Component {
if ( hashData ) {
this.props
.wizardApiFetch( {
path: '/newspack/v1/wizard/newspack-support-wizard/wpcom_access_token',
path: '/newspack/v1/oauth/wpcom/token',
method: 'POST',
data: hashData,
} )
Expand Down
9 changes: 5 additions & 4 deletions includes/class-newspack.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ private function includes() {
include_once NEWSPACK_ABSPATH . 'includes/class-api.php';
include_once NEWSPACK_ABSPATH . 'includes/class-profile.php';
include_once NEWSPACK_ABSPATH . 'includes/class-analytics.php';
include_once NEWSPACK_ABSPATH . 'includes/google/class-google-oauth.php';
include_once NEWSPACK_ABSPATH . 'includes/google/class-google-services-connection.php';
include_once NEWSPACK_ABSPATH . 'includes/oauth/class-wpcom-oauth.php';
include_once NEWSPACK_ABSPATH . 'includes/oauth/class-google-oauth.php';
include_once NEWSPACK_ABSPATH . 'includes/oauth/class-google-services-connection.php';

include_once NEWSPACK_ABSPATH . 'includes/wizards/class-setup-wizard.php';
include_once NEWSPACK_ABSPATH . 'includes/wizards/class-dashboard.php';
Expand Down Expand Up @@ -162,8 +163,8 @@ public function handle_resets() {
wp_safe_redirect( admin_url( 'admin.php?page=newspack-setup-wizard' ) );
exit;
}
if ( Support_Wizard::get_wpcom_access_token() && 'reset-wpcom' === $newspack_reset ) {
delete_user_meta( get_current_user_id(), Support_Wizard::NEWSPACK_WPCOM_ACCESS_TOKEN );
if ( WPCOM_OAuth::get_wpcom_access_token() && 'reset-wpcom' === $newspack_reset ) {
delete_user_meta( get_current_user_id(), WPCOM_OAuth::NEWSPACK_WPCOM_ACCESS_TOKEN );
$redirect_url = add_query_arg( 'newspack-notice', __( 'Removed WPCOM Access Token', 'newspack' ), $redirect_url );
}
}
Expand Down
Loading