Skip to content

Commit

Permalink
feat: use WPCOM as a proxy for Google OAuth2 flow (#962)
Browse files Browse the repository at this point in the history
Behind a feature flag (env variable) for now.
  • Loading branch information
adekbadek authored Jun 1, 2021
1 parent d7aebe2 commit b95fcc0
Show file tree
Hide file tree
Showing 10 changed files with 496 additions and 296 deletions.
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

0 comments on commit b95fcc0

Please sign in to comment.