import { PRODUCT_JETPACK_BACKUP_T1_YEARLY, WPCOM_FEATURES_BACKUPS, getJetpackProductOrPlanDisplayName, } from '@automattic/calypso-products'; import { getUrlParts } from '@automattic/calypso-url'; import { Button, Card, FormLabel, Gridicon } from '@automattic/components'; import { Spinner as WPSpinner, Modal } from '@wordpress/components'; import { Icon, chartBar, next, share } from '@wordpress/icons'; import clsx from 'clsx'; import debugModule from 'debug'; import { localize } from 'i18n-calypso'; import { flowRight, get, includes, startsWith } from 'lodash'; import PropTypes from 'prop-types'; import { Component, Fragment } from 'react'; import { connect } from 'react-redux'; import { formatSlugToURL } from 'calypso/blocks/importer/util'; import QuerySiteFeatures from 'calypso/components/data/query-site-features'; import QuerySitePurchases from 'calypso/components/data/query-site-purchases'; import QueryUserConnection from 'calypso/components/data/query-user-connection'; import FormSettingExplanation from 'calypso/components/forms/form-setting-explanation'; import Gravatar from 'calypso/components/gravatar'; import LoggedOutFormFooter from 'calypso/components/logged-out-form/footer'; import LoggedOutFormLinkItem from 'calypso/components/logged-out-form/link-item'; import LoggedOutFormLinks from 'calypso/components/logged-out-form/links'; import { decodeEntities } from 'calypso/lib/formatting'; import { navigate } from 'calypso/lib/navigate'; import { login } from 'calypso/lib/paths'; import { addQueryArgs } from 'calypso/lib/route'; import { urlToSlug } from 'calypso/lib/url'; import { clearStore, disablePersistence } from 'calypso/lib/user/store'; import { recordTracksEvent as recordTracksEventAction } from 'calypso/state/analytics/actions'; import { redirectToLogout } from 'calypso/state/current-user/actions'; import { getCurrentUser } from 'calypso/state/current-user/selectors'; import { authorize as authorizeAction, retryAuth as retryAuthAction, } from 'calypso/state/jetpack-connect/actions'; import { getAuthAttempts, getAuthorizationData, getUserAlreadyConnected, hasExpiredSecretError as hasExpiredSecretErrorSelector, hasXmlrpcError as hasXmlrpcErrorSelector, isRemoteSiteOnSitesList, isSiteBlockedError as isSiteBlockedSelector, } from 'calypso/state/jetpack-connect/selectors'; import { logoutUser } from 'calypso/state/logout/actions'; import { isFetchingSitePurchases, siteHasJetpackProductPurchase, } from 'calypso/state/purchases/selectors'; import { canCurrentUser } from 'calypso/state/selectors/can-current-user'; import getPartnerIdFromQuery from 'calypso/state/selectors/get-partner-id-from-query'; import getPartnerSlugFromQuery from 'calypso/state/selectors/get-partner-slug-from-query'; import isVipSite from 'calypso/state/selectors/is-vip-site'; import isWooJPCFlow from 'calypso/state/selectors/is-woo-jpc-flow'; import siteHasFeature from 'calypso/state/selectors/site-has-feature'; import { getSite, isRequestingSite, isRequestingSites } from 'calypso/state/sites/selectors'; import AuthFormHeader from './auth-form-header'; import { ALREADY_CONNECTED, ALREADY_CONNECTED_BY_OTHER_USER, DEFAULT_AUTHORIZE_ERROR, RETRY_AUTH, RETRYING_AUTH, SECRET_EXPIRED, SITE_BLOCKED, USER_IS_ALREADY_CONNECTED_TO_SITE, XMLRPC_ERROR, } from './connection-notice-types'; import { JPC_A4A_PATH, JPC_JETPACK_MANAGE_PATH, JPC_PATH_PLANS, REMOTE_PATH_AUTH, } from './constants'; import Disclaimer from './disclaimer'; import { OFFER_RESET_FLOW_TYPES } from './flow-types'; import HelpButton from './help-button'; import JetpackConnectNotices from './jetpack-connect-notices'; import MainWrapper from './main-wrapper'; import { clearPlan, isCalypsoStartedConnection, isSsoApproved, retrieveMobileRedirect, retrievePlan, retrieveSource, clearSource, } from './persistence-utils'; import AuthorizationScreenReaderIndicator from './screen-reader-indicator'; import { authQueryPropTypes, getRoleFromScope } from './utils'; import wooDnaConfig from './woo-dna-config'; import WooInstallExtSuccessNotice from './woo-install-ext-success-notice'; import { WooLoader } from './woo-loader'; import { ConnectingYourAccountStage, PlaceholderStage } from './woo-loader-stages'; /** * Constants */ const debug = debugModule( 'calypso:jetpack-connect:authorize-form' ); const MAX_AUTH_ATTEMPTS = 3; export class JetpackAuthorize extends Component { static propTypes = { authQuery: authQueryPropTypes.isRequired, // Connected props authAttempts: PropTypes.number.isRequired, authorizationData: PropTypes.shape( { authorizationCode: PropTypes.string, authorizeError: PropTypes.oneOfType( [ PropTypes.object, PropTypes.bool ] ), authorizeSuccess: PropTypes.bool, siteReceived: PropTypes.bool, } ).isRequired, authorize: PropTypes.func.isRequired, calypsoStartedConnection: PropTypes.bool, hasExpiredSecretError: PropTypes.bool, hasXmlrpcError: PropTypes.bool, isAlreadyOnSitesList: PropTypes.bool, isFetchingAuthorizationSite: PropTypes.bool, isFetchingSites: PropTypes.bool, isSiteBlocked: PropTypes.bool, isRequestingSitePurchases: PropTypes.bool, isWooJPC: PropTypes.bool, recordTracksEvent: PropTypes.func.isRequired, siteHasJetpackPaidProduct: PropTypes.bool, retryAuth: PropTypes.func.isRequired, translate: PropTypes.func.isRequired, user: PropTypes.object.isRequired, userAlreadyConnected: PropTypes.bool.isRequired, }; redirecting = false; retryingAuth = false; state = { isRedirecting: false, }; // @TODO: Please update https://github.com/Automattic/wp-calypso/issues/58453 if you are refactoring away from UNSAFE_* lifecycle methods! UNSAFE_componentWillMount() { const { recordTracksEvent, isMobileAppFlow } = this.props; const { from, clientId, closeWindowAfterLogin } = this.props.authQuery; const tracksProperties = { from, is_mobile_app_flow: isMobileAppFlow, site: clientId, }; if ( closeWindowAfterLogin && typeof window !== 'undefined' ) { // Certain connection flows may complete the login step within a popup window. // In these cases, we'll want to automatically close the window when the login // step is complete, and continue authorization in the parent window. debug( 'Closing window after login' ); window.close(); } recordTracksEvent( 'calypso_jpc_authorize_form_view', tracksProperties ); recordTracksEvent( 'calypso_jpc_auth_view', tracksProperties ); if ( this.shouldAutoAuthorize() ) { debug( 'Authorizing automatically on component mount' ); return this.authorize(); } } // @TODO: Please update https://github.com/Automattic/wp-calypso/issues/58453 if you are refactoring away from UNSAFE_* lifecycle methods! UNSAFE_componentWillReceiveProps( nextProps ) { const { retryAuth } = nextProps; const { authorizeError, authorizeSuccess, siteReceived } = nextProps.authorizationData; const { alreadyAuthorized, redirectAfterAuth, site } = nextProps.authQuery; if ( this.isJetpackPartnerCoupon( nextProps ) && ( siteReceived || authorizeSuccess ) ) { // The current implementation of the partner coupon URL is supposed to // just take over the entire flow and send directly to checkout. // This will happen by the partnerCouponRedirects controller logic if we // just redirect the customer to the plans page. // The reason we have to do this is because e.g. "shouldRedirectJetpackStart" has // logic that will always go straight to the redirect URI after authorization which // means we never hit the "plans" page where our partner coupon logic takes over. return this.redirect(); } else if ( this.isSso( nextProps ) || this.isWooRedirect( nextProps ) || this.isFromJpo( nextProps ) || this.isFromJetpackBoost( nextProps ) || this.shouldRedirectJetpackStart( nextProps ) || this.isFromBlazeAdsPlugin( nextProps ) || this.props.isVip ) { if ( authorizeSuccess ) { return this.externalRedirectOnce( redirectAfterAuth ); } } else if ( siteReceived ) { return this.redirect(); } else if ( nextProps.isAlreadyOnSitesList && alreadyAuthorized ) { return this.redirect(); } if ( authorizeError && nextProps.authAttempts < MAX_AUTH_ATTEMPTS && ! this.retryingAuth && ! nextProps.hasXmlrpcError && ! nextProps.hasExpiredSecretError && ! nextProps.isSiteBlocked && site ) { // Expired secret errors, and XMLRPC errors will be resolved in `handleResolve`. // Any other type of error, we will immediately and automatically retry the request as many times // as controlled by MAX_AUTH_ATTEMPTS. const attempts = this.props.authAttempts || 0; this.retryingAuth = true; return retryAuth( site, attempts + 1, nextProps.authQuery.from, redirectAfterAuth ); } } authorize() { this.props.authorize( { _wp_nonce: this.props.authQuery.nonce, client_id: this.props.authQuery.clientId, from: this.props.authQuery.from, jp_version: this.props.authQuery.jpVersion, redirect_uri: this.props.authQuery.redirectUri, scope: this.props.authQuery.scope, secret: this.props.authQuery.secret, state: this.props.authQuery.state, } ); } externalRedirectOnce( url ) { if ( ! this.redirecting ) { this.redirecting = true; debug( `Redirecting to ${ url }` ); navigate( url ); } } redirect() { const { isMobileAppFlow, mobileAppRedirect, siteHasBackups, fromSource } = this.props; const { from, homeUrl, redirectAfterAuth, scope, closeWindowAfterAuthorize } = this.props.authQuery; const { isRedirecting } = this.state; if ( isRedirecting ) { return; } if ( isMobileAppFlow ) { debug( `Redirecting to mobile app ${ mobileAppRedirect }` ); window.location.replace( mobileAppRedirect ); return; } if ( closeWindowAfterAuthorize && typeof window !== 'undefined' ) { // Certain connection flows may complete the authorization step within a popup window. // In these cases, we'll want to automatically close the window when the authorization // step is complete, and have the window opener detect this and re-check user authorization status. debug( 'Closing window after authorize' ); window.close(); } if ( fromSource === 'import' ) { clearSource(); debug( 'Closing window after authorize - from migration flow' ); window.close(); } const urlParams = new URLSearchParams( window.location.search ); const source = urlParams.get( 'source' ); if ( source === 'jetpack-manage' ) { const urlRedirect = addQueryArgs( { site_connected: urlToSlug( homeUrl ) }, JPC_JETPACK_MANAGE_PATH ); navigate( urlRedirect ); return; } else if ( source === 'a8c-for-agencies' ) { const urlRedirect = addQueryArgs( { site_connected: urlToSlug( homeUrl ) }, JPC_A4A_PATH ); navigate( urlRedirect ); return; } if ( this.isJetpackPartnerCoupon() ) { // The current implementation of the partner coupon URL is supposed to // just take over the entire flow and send directly to checkout. // This will happen by the partnerCouponRedirects controller logic if we // just redirect the customer to the plans page. // The reason we have to do this is because e.g. "shouldRedirectJetpackStart" has // logic that will always go straight to the redirect URI after authorization which // means we never hit the "plans" page where our partner coupon logic takes over. const redirectionTarget = addQueryArgs( { redirect: redirectAfterAuth }, `${ JPC_PATH_PLANS }/${ urlToSlug( homeUrl ) }` ); debug( `Jetpack Partner Coupon Redirecting to: ${ redirectionTarget }` ); navigate( redirectionTarget ); } else if ( this.isSso() || this.isWooRedirect() || this.isFromJpo() || this.isFromJetpackBoost() || this.isFromBlockEditor() || this.shouldRedirectJetpackStart() || getRoleFromScope( scope ) === 'subscriber' || this.isJetpackUpgradeFlow() || this.isFromJetpackConnectionManager() || this.isFromJetpackSocialPlugin() || this.isFromJetpackSearchPlugin() || this.isFromJetpackVideoPressPlugin() || ( this.isFromJetpackBackupPlugin() && siteHasBackups ) || this.isFromAutomatticForAgenciesPlugin() || this.isFromBlazeAdsPlugin() ) { debug( 'Going back to WP Admin.', 'Connection initiated via: ', from, 'SSO found:', this.isSso() ); this.externalRedirectOnce( redirectAfterAuth ); } else if ( this.isFromJetpackOnboarding() ) { debug( `Redirecting to My Jetpack page with 'from' url query arg: ${ redirectAfterAuth }` ); // Adding the `&from=jetpack-onboarding` query arg here to the url so Jetpack can know the user is coming directly from the Jetpack onboarding flow. this.externalRedirectOnce( addQueryArgs( { from }, redirectAfterAuth ) ); } else if ( this.isFromMyJetpackConnectAfterCheckout() ) { debug( `Redirecting to Calypso product license activation page: ${ redirectAfterAuth }` ); navigate( // The /jetpack/connect/authorize controller requires `redirectAfterAuth` to be a // valid well-formed uri (via validUrl.isWebUri()), so here we are removing the url // host so that it becomes a relative url. redirectAfterAuth.replace( /^(https?:\/\/wordpress\.com|http:\/\/calypso\.localhost:3000)/, '' ) ); } else if ( this.isFromJetpackBackupPlugin() && ! siteHasBackups ) { debug( `Redirecting directly to cart with ${ PRODUCT_JETPACK_BACKUP_T1_YEARLY } in cart.` ); navigate( `/checkout/${ urlToSlug( homeUrl ) }/${ PRODUCT_JETPACK_BACKUP_T1_YEARLY }` ); } else if ( this.isFromMigrationPlugin() ) { navigate( `/setup/site-migration?ref=jetpack-connect&from=${ urlToSlug( homeUrl ) }` ); } else { const redirectionTarget = this.getRedirectionTarget(); debug( `Redirecting to: ${ redirectionTarget }` ); navigate( redirectionTarget ); } this.setState( { isRedirecting: true } ); } redirectToXmlRpcErrorFallbackUrl() { const { state, redirectUri } = this.props.authQuery; const code = this.props.authorizationData.authorizationCode; const url = addQueryArgs( { code, state }, redirectUri ); debug( 'xmlrpc fallback to', url ); this.externalRedirectOnce( url ); } shouldAutoAuthorize() { const { alreadyAuthorized, authApproved, from } = this.props.authQuery; return ( this.isSso() || [ 'woocommerce-services-auto-authorize', 'woocommerce-setup-wizard' ].includes( from ) || // Auto authorize the old WooCommerce setup wizard only. ( ! this.props.isAlreadyOnSitesList && ! alreadyAuthorized && ( this.props.calypsoStartedConnection || authApproved ) ) ); } isFromJpo( props = this.props ) { const { from } = props.authQuery; return startsWith( from, 'jpo' ); } isFromJetpackBoost( props = this.props ) { const { from } = props.authQuery; return startsWith( from, 'jetpack-boost' ); } isFromBlockEditor( props = this.props ) { const { from } = props.authQuery; return 'jetpack-block-editor' === from; } /** * Check whether this a valid authorized SSO request * @param {Object} props Props to test * @param {?string} props.authQuery.from Where is the request from * @param {?number} props.authQuery.clientId Remote site ID * @returns {boolean} True if it's a valid SSO request otherwise false */ isSso( props = this.props ) { const { from, clientId } = props.authQuery; return 'sso' === from && isSsoApproved( clientId ); } /** * Check if the user is coming from the Jetpack upgrade flow. * @param {Object} props Props to test * @param {?string} props.authQuery.redirectAfterAuth Where were we redirected after auth. * @returns {boolean} True if the user is coming from the Jetpack upgrade flow, false otherwise. */ isJetpackUpgradeFlow( props = this.props ) { const { redirectAfterAuth } = props.authQuery; return ( redirectAfterAuth && redirectAfterAuth.includes( 'page=jetpack&action=authorize_redirect' ) ); } isFromJetpackConnectionManager( props = this.props ) { const { from } = props.authQuery; return startsWith( from, 'connection-ui' ); } isFromJetpackBackupPlugin( props = this.props ) { const { from } = props.authQuery; return startsWith( from, 'jetpack-backup' ); } isFromJetpackSearchPlugin( props = this.props ) { const { from } = props.authQuery; return startsWith( from, 'jetpack-search' ); } isFromJetpackSocialPlugin( props = this.props ) { const { from } = props.authQuery; return startsWith( from, 'jetpack-social' ); } isFromJetpackVideoPressPlugin( props = this.props ) { const { from } = props.authQuery; return startsWith( from, 'jetpack-videopress' ); } isFromMyJetpack( props = this.props ) { const { from } = props.authQuery; return startsWith( from, 'my-jetpack' ); } isWooRedirect = ( props = this.props ) => { const { from } = props.authQuery; return ( [ 'woocommerce-services-auto-authorize', 'woocommerce-setup-wizard', // Legacy flow not in use anymore. Keeping around just to // support redirects correctly. 'woocommerce-onboarding', 'woocommerce-core-profiler', ].includes( from ) || this.getWooDnaConfig( props ).isWooDnaFlow() ); }; isWooJPC( props = this.props ) { const { from } = props.authQuery; return ( // TODO: the two extra `from` checks shouldn't be necessary, // as they are part of the isWooJPCFlow check. But the unit tests // don't use the connected component and would otherwise fail. 'woocommerce-core-profiler' === from || 'woocommerce-onboarding' === from || this.props.isWooJPC ); } getWooDnaConfig( props = this.props ) { return wooDnaConfig( props.authQuery ); } isJetpackPartnerCoupon( props = this.props ) { const { from } = props.authQuery; return startsWith( from, 'jetpack-partner-coupon' ); } isFromMigrationPlugin( props = this.props ) { const { from } = props.authQuery; return startsWith( from, 'wpcom-migration' ); } isFromAutomatticForAgenciesPlugin( props = this.props ) { const { from } = props.authQuery; return startsWith( from, 'automattic-for-agencies-client' ); } isFromBlazeAdsPlugin( props = this.props ) { const { from } = props.authQuery; return startsWith( from, 'blaze-ads' ); } shouldRedirectJetpackStart( props = this.props ) { const { partnerSlug, partnerID } = props; return partnerID && 'pressable' !== partnerSlug; } isFromMyJetpackConnectAfterCheckout( props = this.props ) { const { from } = props.authQuery; return startsWith( from, 'connect-after-checkout' ); } isFromJetpackOnboarding( props = this.props ) { const { from } = props.authQuery; return startsWith( from, 'jetpack-onboarding' ); } getCompanyName() { return this.isFromAutomatticForAgenciesPlugin() ? 'Automattic, Inc.' : 'WordPress.com'; } handleSignIn = async ( e, loginURL ) => { e.preventDefault(); const { recordTracksEvent } = this.props; switch ( true ) { case this.isWooJPC(): // Logout user before redirecting to login page. try { await this.props.logoutUser(); disablePersistence(); await clearStore(); } catch ( error ) { // The logout endpoint might fail if the nonce has expired. // Clear wordpress_logged_in cookie to force logout. document.cookie = 'wordpress_logged_in=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/'; } recordTracksEvent( 'calypso_jpc_wc_coreprofiler_different_user_click' ); window.location.href = e.target.href; break; default: try { const { redirect_to: redirectTo } = await this.props.logoutUser( loginURL ); disablePersistence(); await clearStore(); window.location.href = redirectTo || '/'; } catch ( error ) { // The logout endpoint might fail if the nonce has expired. // In this case, redirect to wp-login.php?action=logout to get a new nonce generated this.props.redirectToLogout( loginURL ); } } }; handleSignOut = () => { const { recordTracksEvent } = this.props; recordTracksEvent( 'calypso_jpc_signout_click' ); this.props.redirectToLogout( window.location.href ); }; handleResolve = () => { const { site, recordTracksEvent } = this.props; this.retryingAuth = false; if ( this.props.hasExpiredSecretError ) { // In this case, we need to re-issue the secret. // We do this by redirecting to Jetpack client, which will automatically redirect back here. recordTracksEvent( 'calypso_jpc_resolve_expired_secret_error_click' ); this.externalRedirectOnce( site + REMOTE_PATH_AUTH ); return; } // Otherwise, we assume the site is having trouble receive XMLRPC requests. // To resolve, we redirect to the Jetpack Client, and attempt to complete the connection with // legacy functions on the client. recordTracksEvent( 'calypso_jpc_resolve_xmlrpc_error_click' ); this.redirectToXmlRpcErrorFallbackUrl(); }; handleSubmit = () => { const { recordTracksEvent } = this.props; const { authorizeError, authorizeSuccess } = this.props.authorizationData; const { alreadyAuthorized, redirectAfterAuth, from } = this.props.authQuery; if ( ! this.props.isAlreadyOnSitesList && ! this.props.isFetchingSites && alreadyAuthorized ) { recordTracksEvent( 'calypso_jpc_back_wpadmin_click' ); return this.externalRedirectOnce( redirectAfterAuth ); } if ( this.props.isAlreadyOnSitesList && alreadyAuthorized ) { recordTracksEvent( 'calypso_jpc_already_authorized_click' ); return this.redirect(); } if ( authorizeSuccess && ! alreadyAuthorized ) { recordTracksEvent( 'calypso_jpc_activate_click' ); return this.redirect(); } if ( authorizeError ) { recordTracksEvent( 'calypso_jpc_try_again_click' ); return this.handleResolve(); } if ( this.props.isAlreadyOnSitesList ) { recordTracksEvent( 'calypso_jpc_return_site_click' ); return this.redirect(); } recordTracksEvent( 'calypso_jpc_approve_click' ); if ( 'woocommerce-core-profiler' === from ) { recordTracksEvent( 'calypso_jpc_wc_coreprofiler_connect', { use_account: true } ); } return this.authorize(); }; isAuthorizing() { const { isAuthorizing } = this.props.authorizationData; return ! this.props.isAlreadyOnSitesList && isAuthorizing; } renderErrorDetails() { const { authorizeError } = this.props.authorizationData; return (
{ translate( 'WordPress.com was unable to reach your site and approve the connection. ' + 'Try again by clicking the button above; ' + "if that doesn't work you may need to {{link}}contact support{{/link}}.", { components: { link: ( ), }, } ) }
); } renderNotices() { const { authorizeError, isAuthorizing, authorizeSuccess, userAlreadyConnected } = this.props.authorizationData; const { alreadyAuthorized, site } = this.props.authQuery; let redirectToMobileApp = null; if ( this.props.isMobileAppFlow ) { redirectToMobileApp = ( reason ) => { const url = addQueryArgs( { reason }, this.props.mobileAppRedirect ); this.externalRedirectOnce( url ); }; } if ( alreadyAuthorized && ! this.props.isFetchingSites && ! this.props.isAlreadyOnSitesList ) { // For users who start their journey at `wordpress.com/jetpack/connect` or similar flows, we will discourage // additional users from linking. Although it is possible to link multiple users with Jetpack, the `jetpack/connect` // flows will be reserved for brand new connections. return ({ this.getUserText() }
{ this.getUserText() }
{ this.isFromMyJetpackConnectAfterCheckout() && ({ this.getProductActivationText() }
) } { this.renderNotices() } { this.renderStateAction() }{ translate( 'Your site connects to Jetpack’s cloud to offload the heavy work, helping it run faster and deliver powerful features.' ) }