File size: 6,049 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
import config, { isCalypsoLive } from '@automattic/calypso-config';
import page from '@automattic/calypso-router';
import { includes, isEmpty } from 'lodash';
import PropTypes from 'prop-types';
import validUrl from 'valid-url';
import makeJsonSchemaParser from 'calypso/lib/make-json-schema-parser';
import { navigate } from 'calypso/lib/navigate';
import { addQueryArgs, untrailingslashit } from 'calypso/lib/route';
import { urlToSlug } from 'calypso/lib/url';
import {
JPC_PATH_PLANS,
JPC_PATH_REMOTE_INSTALL,
REMOTE_PATH_AUTH,
JPC_PATH_CHECKOUT,
} from './constants';
import { authorizeQueryDataSchema } from './schema';
export function authQueryTransformer( queryObject ) {
return {
// Required
clientId: parseInt( queryObject.client_id, 10 ),
closeWindowAfterLogin: '1' === queryObject.close_window_after_login,
closeWindowAfterAuthorize: '1' === queryObject.close_window_after_auth,
homeUrl: queryObject.home_url,
isPopup: '1' === queryObject.is_popup,
nonce: queryObject._wp_nonce,
redirectUri: queryObject.redirect_uri,
scope: queryObject.scope,
secret: queryObject.secret,
site: queryObject.site,
state: queryObject.state,
// Optional
// TODO: verify
authApproved: !! queryObject.auth_approved,
alreadyAuthorized: !! queryObject.already_authorized,
blogname: queryObject.blogname || null,
from: queryObject.from || '[unknown]',
jpVersion: queryObject.jp_version || null,
redirectAfterAuth: validUrl.isWebUri( queryObject.redirect_after_auth )
? queryObject.redirect_after_auth
: null,
siteIcon: queryObject.site_icon || null,
siteUrl: queryObject.site_url || null,
userEmail: queryObject.user_email || null,
woodna_service_name: queryObject.woodna_service_name || null,
woodna_help_url: queryObject.woodna_help_url || null,
allowSiteConnection: queryObject.skip_user || queryObject.allow_site_connection || null,
// Used by woo core profiler flow to determine if we need to show a success notice after installing extensions or not.
installedExtSuccess: queryObject.installed_ext_success || null,
// This is a temporary param used for Jetpack A.I and Jetpack Boost A/B testing in WooCommerce core
// Related WooCommerce PR: https://github.com/woocommerce/woocommerce/pull/39799
// this param will be removed after the expierment is over
plugin_name: queryObject.plugin_name || null,
};
}
export const authQueryPropTypes = PropTypes.shape( {
authApproved: PropTypes.bool,
alreadyAuthorized: PropTypes.bool,
blogname: PropTypes.string,
clientId: PropTypes.number.isRequired,
from: PropTypes.string.isRequired,
homeUrl: PropTypes.string.isRequired,
jpVersion: PropTypes.string,
nonce: PropTypes.string.isRequired,
redirectAfterAuth: PropTypes.string,
redirectUri: PropTypes.string.isRequired,
scope: PropTypes.string.isRequired,
secret: PropTypes.string.isRequired,
site: PropTypes.string.isRequired,
siteIcon: PropTypes.string,
siteUrl: PropTypes.string,
state: PropTypes.string.isRequired,
userEmail: PropTypes.string,
installedExtSuccess: PropTypes.string,
plugin_name: PropTypes.string,
} );
export function addCalypsoEnvQueryArg( url ) {
let calypsoEnv = config( 'env_id' );
if ( 'object' === typeof window && window.COMMIT_SHA && isCalypsoLive() ) {
calypsoEnv = `live-${ COMMIT_SHA }`;
}
return addQueryArgs( { calypso_env: calypsoEnv }, url );
}
/**
* Sanitize a user-supplied URL so we can use it for network requests.
* @param {string} inputUrl User-supplied URL
* @returns {string} Sanitized URL
*/
export function cleanUrl( inputUrl ) {
let url = inputUrl.trim().toLowerCase();
if ( url && url.substr( 0, 4 ) !== 'http' ) {
url = 'http://' + url;
}
url = url.replace( /wp-admin\/?$/, '' );
return untrailingslashit( url );
}
/**
* Convert an auth query scope to a role
*
* Auth queries include a scope like `role:hash`. This function will attempt to extract the role
* when provided with a scope.
* @param {string} scope From authorization query
* @returns {?string} Role parsed from scope if found
*/
export function getRoleFromScope( scope ) {
if ( ! includes( scope, ':' ) ) {
return null;
}
const role = scope.split( ':', 1 )[ 0 ];
if ( ! isEmpty( role ) ) {
return role;
}
return null;
}
/**
* Parse an authorization query
* @property {Function} parser Lazy-instatiated parser
* @param {Object} query Authorization query
* @returns {?Object} Query after transformation. Null if invalid or errored during transform.
*/
export function parseAuthorizationQuery( query ) {
if ( ! parseAuthorizationQuery.parser ) {
parseAuthorizationQuery.parser = makeJsonSchemaParser(
authorizeQueryDataSchema,
authQueryTransformer
);
}
try {
return parseAuthorizationQuery.parser( query );
} catch ( error ) {
// The parser is expected to throw SchemaError or TransformerError on bad input.
}
return null;
}
/**
* Manage Jetpack Connect redirect after various site states
* @param {string} type Redirect type
* @param {string} url Site url
* @param {?string} product Product slug
* @param {?Object} queryArgs Query parameters
* @returns {string} Redirect url
*/
export function redirect( type, url, product = null, queryArgs = {} ) {
let urlRedirect = '';
const instr = '/jetpack/connect/instructions';
if ( type === 'plans_selection' ) {
urlRedirect = JPC_PATH_PLANS + '/' + urlToSlug( url );
page.redirect( urlRedirect );
}
if ( type === 'remote_install' ) {
urlRedirect = JPC_PATH_REMOTE_INSTALL;
page.redirect( urlRedirect );
}
if ( type === 'remote_auth' ) {
urlRedirect = addQueryArgs( queryArgs, addCalypsoEnvQueryArg( url + REMOTE_PATH_AUTH ) );
navigate( urlRedirect );
}
if ( type === 'install_instructions' ) {
urlRedirect = addQueryArgs( { url }, instr );
page.redirect( urlRedirect );
}
if ( type === 'checkout' ) {
urlRedirect = `${ JPC_PATH_CHECKOUT }/${ urlToSlug( url ) }/${ product }`;
page.redirect( addQueryArgs( queryArgs, urlRedirect ) );
}
return urlRedirect;
}
|