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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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
import config from '@automattic/calypso-config';
import { Button, Card, Dialog, Gridicon } from '@automattic/components';
import debugModule from 'debug';
import { localize, fixMe } from 'i18n-calypso';
import { flowRight, get, map } from 'lodash';
import { Component } from 'react';
import { connect } from 'react-redux';
import Site from 'calypso/blocks/site';
import SitePlaceholder from 'calypso/blocks/site/placeholder';
import EmailVerificationGate from 'calypso/components/email-verification/email-verification-gate';
import EmptyContent from 'calypso/components/empty-content';
import FormattedHeader from 'calypso/components/formatted-header';
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 Main from 'calypso/components/main';
import Notice from 'calypso/components/notice';
import NoticeAction from 'calypso/components/notice/notice-action';
import { recordTracksEvent } from 'calypso/lib/analytics/tracks';
import { decodeEntities } from 'calypso/lib/formatting';
import { login } from 'calypso/lib/paths';
import { addQueryArgs } from 'calypso/lib/route';
import { getCurrentUser } from 'calypso/state/current-user/selectors';
import { validateSSONonce, authorizeSSO } from 'calypso/state/jetpack-connect/actions';
import { getSSO } from 'calypso/state/jetpack-connect/selectors';
import HelpButton from './help-button';
import MainWrapper from './main-wrapper';
import { persistSsoApproved } from './persistence-utils';

/*
 * Module variables
 */
const debug = debugModule( 'calypso:jetpack-connect:sso' );

class JetpackSsoForm extends Component {
	state = {
		showTermsDialog: false,
	};

	componentDidMount() {
		this.maybeValidateSSO();
	}

	componentDidUpdate( prevProps ) {
		this.maybeValidateSSO();

		if ( this.props.ssoUrl && ! prevProps.ssoUrl ) {
			// After receiving the SSO URL, which will log the user in on remote site,
			// we redirect user to remote site to be logged in.
			//
			// Note: We add `calypso_env` so that when we are redirected back to Calypso,
			// we land in the same development environment.
			const configEnv = config( 'env_id' ) || process.env.NODE_ENV;
			const redirect = addQueryArgs( { calypso_env: configEnv }, this.props.ssoUrl );
			debug( 'Redirecting to: ' + redirect );
			window.location.href = redirect;
		}
	}

	onApproveSSO = ( event ) => {
		event.preventDefault();
		recordTracksEvent( 'calypso_jetpack_sso_log_in_button_click' );

		const { siteId, ssoNonce } = this.props;
		const siteUrl = get( this.props, 'blogDetails.URL' );

		persistSsoApproved( siteId );

		debug( 'Approving sso' );
		this.props.authorizeSSO( siteId, ssoNonce, siteUrl );
	};

	onCancelClick = ( event ) => {
		debug( 'Clicked return to site link' );
		recordTracksEvent( 'calypso_jetpack_sso_return_to_site_link_click' );
		this.returnToSiteFallback( event );
	};

	onTryAgainClick = ( event ) => {
		debug( 'Clicked try again link' );
		recordTracksEvent( 'calypso_jetpack_sso_try_again_link_click' );
		this.returnToSiteFallback( event );
	};

	onClickSignInDifferentUser = () => {
		recordTracksEvent( 'calypso_jetpack_sso_sign_in_different_user_link_click' );
	};

	onClickSharedDetailsModal = ( event ) => {
		event.preventDefault();
		recordTracksEvent( 'calypso_jetpack_sso_shared_details_link_click' );
		this.setState( {
			showTermsDialog: true,
		} );
	};

	closeTermsDialog = () => {
		this.setState( {
			showTermsDialog: false,
		} );
	};

	returnToSiteFallback = ( event ) => {
		// If, for some reason, the API request failed and we do not have the admin URL,
		// then fallback to the user's last location.
		if ( ! get( this.props, 'blogDetails.admin_url' ) ) {
			recordTracksEvent( 'calypso_jetpack_sso_admin_url_fallback_redirect' );
			event.preventDefault();
			window.history.back();
		}
	};

	isButtonDisabled() {
		const { currentUser } = this.props;
		const { nonceValid, isAuthorizing, isValidating, ssoUrl, authorizationError } = this.props;
		return !! (
			! nonceValid ||
			isAuthorizing ||
			isValidating ||
			ssoUrl ||
			authorizationError ||
			! currentUser.email_verified
		);
	}

	getSignInLink() {
		return login( { redirectTo: window.location.href } );
	}

	maybeValidateSSO() {
		const { ssoNonce, siteId, nonceValid, isAuthorizing, isValidating } = this.props;

		if (
			ssoNonce &&
			siteId &&
			'undefined' === typeof nonceValid &&
			! isAuthorizing &&
			! isValidating
		) {
			this.props.validateSSONonce( siteId, ssoNonce );
		}
	}

	maybeRenderErrorNotice() {
		const { authorizationError, nonceValid, translate } = this.props;

		if ( ! authorizationError && false !== nonceValid ) {
			return null;
		}

		return (
			<Notice
				status="is-error"
				text={ translate( 'Oops, something went wrong.' ) }
				showDismiss={ false }
			>
				<NoticeAction
					href={ get( this.props, 'blogDetails.admin_url', '#' ) }
					onClick={ this.onTryAgainClick }
				>
					{ translate( 'Try again' ) }
				</NoticeAction>
			</Notice>
		);
	}

	renderSiteCard() {
		const { blogDetails } = this.props;
		let site = <SitePlaceholder />;

		if ( blogDetails ) {
			const siteObject = {
				ID: null,
				url: get( this.props, 'blogDetails.URL', '' ),
				admin_url: get( this.props, 'blogDetails.admin_url', '' ),
				domain: get( this.props, 'blogDetails.domain', '' ),
				icon: get( this.props, 'blogDetails.icon', { img: '', ico: '' } ),
				is_vip: false,
				title: decodeEntities( get( this.props, 'blogDetails.title', '' ) ),
			};
			site = <Site site={ siteObject } />;
		}

		return <Card className="jetpack-connect__site">{ site }</Card>;
	}

	getSharedDetailLabel( key ) {
		const { translate } = this.props;
		switch ( key ) {
			case 'ID':
				key = translate( 'User ID', { context: 'User Field' } );
				break;
			case 'login':
				key = translate( 'Login', { context: 'User Field' } );
				break;
			case 'email':
				key = translate( 'Email', { context: 'User Field' } );
				break;
			case 'url':
				key = translate( 'URL', { context: 'User Field' } );
				break;
			case 'first_name':
				key = translate( 'First Name', { context: 'User Field' } );
				break;
			case 'last_name':
				key = translate( 'Last Name', { context: 'User Field' } );
				break;
			case 'display_name':
				key = translate( 'Display Name', { context: 'User Field' } );
				break;
			case 'description':
				key = translate( 'Description', { context: 'User Field' } );
				break;
			case 'two_step_enabled':
				key = translate( 'Two-Step Authentication', { context: 'User Field' } );
				break;
			case 'external_user_id':
				key = translate( 'External User ID', { context: 'User Field' } );
				break;
		}

		return key;
	}

	getSharedDetailValue( key, value ) {
		const { translate } = this.props;
		if ( 'two_step_enabled' === key && value !== '' ) {
			value = true === value ? translate( 'Enabled' ) : translate( 'Disabled' );
		}

		return decodeEntities( value );
	}

	getReturnToSiteText() {
		const { translate } = this.props;
		const text = (
			<span className="jetpack-connect__sso-return-to-site">
				<Gridicon icon="arrow-left" size={ 18 } />
				{ translate( 'Return to %(siteName)s', {
					args: {
						siteName: get( this.props, 'blogDetails.title' ),
					},
				} ) }
			</span>
		);

		return this.maybeWrapWithPlaceholder( text );
	}

	getTOSText() {
		const { translate } = this.props;
		// translators: "share details" is a link to a legal document.
		// "share details" implies that both WordPress.com and %(siteName) will have access to the user info
		// siteName is the partner's site name (eg. Google)
		const text = translate(
			'By logging in you agree to {{detailsLink}}share details{{/detailsLink}} between WordPress.com and %(siteName)s.',
			{
				components: {
					detailsLink: (
						// eslint-disable-next-line jsx-a11y/anchor-is-valid
						<a
							href="#"
							onClick={ this.onClickSharedDetailsModal }
							className="jetpack-connect__sso-actions-modal-link"
						/>
					),
				},
				args: {
					siteName: get( this.props, 'blogDetails.title' ),
				},
			}
		);

		return this.maybeWrapWithPlaceholder( text );
	}

	getSubHeaderText() {
		const { translate } = this.props;
		// translators: siteName is a partner site name. Eg "Google.com" or "Tumblr.com".
		const text = translate(
			'To use Single Sign-On, WordPress.com needs to be able to connect to your account on %(siteName)s.',
			{
				args: {
					siteName: get( this.props, 'blogDetails.title' ),
				},
			}
		);
		return this.maybeWrapWithPlaceholder( text );
	}

	maybeWrapWithPlaceholder( input ) {
		const title = get( this.props, 'blogDetails.title' );
		if ( title ) {
			return input;
		}

		return <span className="jetpack-connect__sso-placeholder">{ input }</span>;
	}

	renderSharedDetailsList() {
		const expectedSharedDetails = {
			ID: '',
			login: '',
			email: '',
			url: '',
			first_name: '',
			last_name: '',
			display_name: '',
			description: '',
			two_step_enabled: '',
			external_user_id: '',
		};
		const sharedDetails = this.props.sharedDetails || expectedSharedDetails;

		return (
			<table className="jetpack-connect__sso-shared-details-table">
				<tbody>
					{ map( sharedDetails, ( value, key ) => {
						return (
							<tr key={ key } className="jetpack-connect__sso-shared-detail-row">
								<td className="jetpack-connect__sso-shared-detail-label">
									{ this.getSharedDetailLabel( key ) }
								</td>
								<td className="jetpack-connect__sso-shared-detail-value">
									{ this.getSharedDetailValue( key, value ) }
								</td>
							</tr>
						);
					} ) }
				</tbody>
			</table>
		);
	}

	renderSharedDetailsDialog() {
		const { translate } = this.props;
		const buttons = [
			{
				action: 'close',
				label: translate( 'Got it', {
					context: 'Used in a button. Similar phrase would be, "I understand".',
				} ),
			},
		];

		return (
			<Dialog
				buttons={ buttons }
				onClose={ this.closeTermsDialog }
				isVisible={ this.state.showTermsDialog }
				className="jetpack-connect__sso-terms-dialog"
			>
				<div className="jetpack-connect__sso-terms-dialog-content">
					<p className="jetpack-connect__sso-shared-details-intro">
						{ translate(
							'When you approve logging in with WordPress.com, we will send the following details to your site.'
						) }
					</p>

					{ this.renderSharedDetailsList() }
				</div>
			</Dialog>
		);
	}

	renderBadPathArgsError() {
		const { translate } = this.props;
		return (
			<Main>
				<EmptyContent
					title={ fixMe( {
						text: 'This URL should not be accessed directly',
						newCopy: translate( 'This URL should not be accessed directly' ),
						oldCopy: translate( 'Oops, this URL should not be accessed directly' ),
					} ) }
					line={ translate(
						'Please click the {{em}}Log in with WordPress.com button{{/em}} on your Jetpack site.',
						{
							components: {
								em: <em />,
							},
						}
					) }
					action={ translate( 'Read Single Sign-On Documentation' ) }
					actionURL="https://jetpack.com/support/sso/"
				/>
			</Main>
		);
	}

	render() {
		const { currentUser } = this.props;
		const { ssoNonce, siteId, validationError, translate } = this.props;

		if ( ! ssoNonce || ! siteId || validationError ) {
			return this.renderBadPathArgsError();
		}

		return (
			<MainWrapper>
				<div className="jetpack-connect__sso">
					<FormattedHeader
						headerText={ translate( 'Connect with WordPress.com' ) }
						subHeaderText={ this.getSubHeaderText() }
					/>

					{ this.renderSiteCard() }

					<EmailVerificationGate
						noticeText={ translate( 'You must verify your email to sign in with WordPress.com.' ) }
						noticeStatus="is-info"
					>
						<Card className="jetpack-connect__logged-in-card">
							{ currentUser.email_verified && this.maybeRenderErrorNotice() }
							<div className="jetpack-connect__sso-user-profile">
								<Gravatar user={ currentUser } size={ 120 } imgSize={ 400 } />
								<h3 className="jetpack-connect__sso-log-in-as">
									{
										// translators: %s is the user's display name. Eg: Login in as "John Doe"
										translate( 'Log in as {{strong}}%s{{/strong}}', {
											args: currentUser.display_name,
											components: {
												strong: <strong className="jetpack-connect__sso-display-name" />,
											},
										} )
									}
								</h3>
								<div className="jetpack-connect__sso-user-email">{ currentUser.email }</div>
							</div>

							<LoggedOutFormFooter className="jetpack-connect__sso-actions">
								<p className="jetpack-connect__tos-link">{ this.getTOSText() }</p>

								<Button primary onClick={ this.onApproveSSO } disabled={ this.isButtonDisabled() }>
									{ translate( 'Log in' ) }
								</Button>
							</LoggedOutFormFooter>
						</Card>
					</EmailVerificationGate>

					<LoggedOutFormLinks>
						<LoggedOutFormLinkItem
							href={ this.getSignInLink() }
							onClick={ this.onClickSignInDifferentUser }
						>
							{ translate( 'Sign in as a different user' ) }
						</LoggedOutFormLinkItem>
						<LoggedOutFormLinkItem
							rel="external"
							href={ get( this.props, 'blogDetails.admin_url', '#' ) }
							onClick={ this.onCancelClick }
						>
							{ this.getReturnToSiteText() }
						</LoggedOutFormLinkItem>
						<HelpButton />
					</LoggedOutFormLinks>
				</div>

				{ this.renderSharedDetailsDialog() }
			</MainWrapper>
		);
	}
}

const connectComponent = connect(
	( state ) => {
		const jetpackSSO = getSSO( state );
		return {
			ssoUrl: get( jetpackSSO, 'ssoUrl' ),
			isAuthorizing: get( jetpackSSO, 'isAuthorizing' ),
			isValidating: get( jetpackSSO, 'isValidating' ),
			nonceValid: get( jetpackSSO, 'nonceValid' ),
			authorizationError: get( jetpackSSO, 'authorizationError' ),
			validationError: get( jetpackSSO, 'validationError' ),
			blogDetails: get( jetpackSSO, 'blogDetails' ),
			sharedDetails: get( jetpackSSO, 'sharedDetails' ),
			currentUser: getCurrentUser( state ),
		};
	},
	{
		authorizeSSO,
		validateSSONonce,
	}
);

export default flowRight( connectComponent, localize )( JetpackSsoForm );