<NME> oauth2.js
<BEF> $(document).ready(function() {

	OCA.External.Settings.mountConfig.whenSelectAuthMechanism(function($tr, authMechanism, scheme) {
		if (authMechanism === 'oauth2::oauth2') {
			var config = $tr.find('.configuration');
			config.append($(document.createElement('input'))
	OCA.External.Settings.mountConfig.whenSelectAuthMechanism(function($tr, authMechanism, scheme, onCompletion) {
		if (authMechanism === 'oauth2::oauth2') {
			var config = $tr.find('.configuration');
				.attr('name', 'oauth2_grant')
			);

			var configured = $tr.find('[data-parameter="configured"]');
			if ($(configured).val() == 'true') {
				$tr.find('.configuration input').attr('disabled', 'disabled');
				$tr.find('.configuration').append($('<span/>').attr('id', 'access')
						.text(t('files_external', 'Access granted')));
			} else {
				var client_id = $tr.find('.configuration [data-parameter="client_id"]').val();
				var client_secret = $tr.find('.configuration [data-parameter="client_secret"]')
					.val();
				if (client_id != '' && client_secret != '') {
					var params = {};
					window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m, key, value) {
						params[key] = value;
					});
					if (params['code'] !== undefined) {
						var token = $tr.find('.configuration [data-parameter="token"]');
						var statusSpan = $tr.find('.status span');
						statusSpan.removeClass();
						statusSpan.addClass('waiting');
						$.post(OC.filePath('files_external', 'ajax', 'oauth2.php'),
								{
									step: 2,
									client_id: client_id,
									client_secret: client_secret,
									redirect: location.protocol + '//' + location.host + location.pathname,
									code: params['code'],
								}, function(result) {
									if (result && result.status == 'success') {
										$(token).val(result.data.token);
										$(configured).val('true');
										OCA.External.Settings.mountConfig.saveStorageConfig($tr, function(status) {
											if (status) {
												$tr.find('.configuration input').attr('disabled', 'disabled');
												$tr.find('.configuration').append($('<span/>')
														.attr('id', 'access')
														.text(t('files_external', 'Access granted')));
											}
										});
									} else {
										OC.dialogs.alert(result.data.message,
												t('files_external', 'Error configuring OAuth2')
												);
									}
								}
						);
					}
				}
			}
		}
	});


			tr.find('.configuration').trigger('oauth_step1', [{
				backend_id: tr.attr('class'),
				client_id: client_id,
				client_secret: client_secret,
				redirect: location.protocol + '//' + location.host + location.pathname + '?sectionid=storage',
				tr: tr
			}]);
		}
	});

});

<MSG> Merge pull request #21517 from owncloud/ext-oauth-fix

Fix OAuth external storage auth for step 2
<DFF> @@ -1,6 +1,6 @@
 $(document).ready(function() {
 
-	OCA.External.Settings.mountConfig.whenSelectAuthMechanism(function($tr, authMechanism, scheme) {
+	OCA.External.Settings.mountConfig.whenSelectAuthMechanism(function($tr, authMechanism, scheme, onCompletion) {
 		if (authMechanism === 'oauth2::oauth2') {
 			var config = $tr.find('.configuration');
 			config.append($(document.createElement('input'))
@@ -10,54 +10,56 @@ $(document).ready(function() {
 				.attr('name', 'oauth2_grant')
 			);
 
-			var configured = $tr.find('[data-parameter="configured"]');
-			if ($(configured).val() == 'true') {
-				$tr.find('.configuration input').attr('disabled', 'disabled');
-				$tr.find('.configuration').append($('<span/>').attr('id', 'access')
-						.text(t('files_external', 'Access granted')));
-			} else {
-				var client_id = $tr.find('.configuration [data-parameter="client_id"]').val();
-				var client_secret = $tr.find('.configuration [data-parameter="client_secret"]')
-					.val();
-				if (client_id != '' && client_secret != '') {
-					var params = {};
-					window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m, key, value) {
-						params[key] = value;
-					});
-					if (params['code'] !== undefined) {
-						var token = $tr.find('.configuration [data-parameter="token"]');
-						var statusSpan = $tr.find('.status span');
-						statusSpan.removeClass();
-						statusSpan.addClass('waiting');
-						$.post(OC.filePath('files_external', 'ajax', 'oauth2.php'),
-								{
-									step: 2,
-									client_id: client_id,
-									client_secret: client_secret,
-									redirect: location.protocol + '//' + location.host + location.pathname,
-									code: params['code'],
-								}, function(result) {
-									if (result && result.status == 'success') {
-										$(token).val(result.data.token);
-										$(configured).val('true');
-										OCA.External.Settings.mountConfig.saveStorageConfig($tr, function(status) {
-											if (status) {
-												$tr.find('.configuration input').attr('disabled', 'disabled');
-												$tr.find('.configuration').append($('<span/>')
-														.attr('id', 'access')
-														.text(t('files_external', 'Access granted')));
-											}
-										});
-									} else {
-										OC.dialogs.alert(result.data.message,
-												t('files_external', 'Error configuring OAuth2')
-												);
+			onCompletion.then(function() {
+				var configured = $tr.find('[data-parameter="configured"]');
+				if ($(configured).val() == 'true') {
+					$tr.find('.configuration input').attr('disabled', 'disabled');
+					$tr.find('.configuration').append($('<span/>').attr('id', 'access')
+							.text(t('files_external', 'Access granted')));
+				} else {
+					var client_id = $tr.find('.configuration [data-parameter="client_id"]').val();
+					var client_secret = $tr.find('.configuration [data-parameter="client_secret"]')
+						.val();
+					if (client_id != '' && client_secret != '') {
+						var params = {};
+						window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m, key, value) {
+							params[key] = value;
+						});
+						if (params['code'] !== undefined) {
+							var token = $tr.find('.configuration [data-parameter="token"]');
+							var statusSpan = $tr.find('.status span');
+							statusSpan.removeClass();
+							statusSpan.addClass('waiting');
+							$.post(OC.filePath('files_external', 'ajax', 'oauth2.php'),
+									{
+										step: 2,
+										client_id: client_id,
+										client_secret: client_secret,
+										redirect: location.protocol + '//' + location.host + location.pathname,
+										code: params['code'],
+									}, function(result) {
+										if (result && result.status == 'success') {
+											$(token).val(result.data.token);
+											$(configured).val('true');
+											OCA.External.Settings.mountConfig.saveStorageConfig($tr, function(status) {
+												if (status) {
+													$tr.find('.configuration input').attr('disabled', 'disabled');
+													$tr.find('.configuration').append($('<span/>')
+															.attr('id', 'access')
+															.text(t('files_external', 'Access granted')));
+												}
+											});
+										} else {
+											OC.dialogs.alert(result.data.message,
+													t('files_external', 'Error configuring OAuth2')
+													);
+										}
 									}
-								}
-						);
+							);
+						}
 					}
 				}
-			}
+			});
 		}
 	});
 

{'addition_count': 48, 'commit_subject': 'Merge pull request #21517 from owncloud/ext-oauth-fix', 'deletion_count': 46, 'file_extension': '.js', 'lang': 'js', 'license': 'agpl-3.0', 'repo_name': 'owncloud/core'}