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

Fix OAuth external storage auth for step 2
This commit is contained in:
Thomas Müller 2016-01-11 13:36:58 +01:00
commit c1d8a03662
2 changed files with 82 additions and 78 deletions

View File

@ -1,6 +1,6 @@
$(document).ready(function() { $(document).ready(function() {
OCA.External.Settings.mountConfig.whenSelectAuthMechanism(function($tr, authMechanism, scheme) { OCA.External.Settings.mountConfig.whenSelectAuthMechanism(function($tr, authMechanism, scheme, onCompletion) {
if (authMechanism === 'oauth1::oauth1') { if (authMechanism === 'oauth1::oauth1') {
var config = $tr.find('.configuration'); var config = $tr.find('.configuration');
config.append($(document.createElement('input')) config.append($(document.createElement('input'))
@ -10,39 +10,41 @@ $(document).ready(function() {
.attr('name', 'oauth1_grant') .attr('name', 'oauth1_grant')
); );
var configured = $tr.find('[data-parameter="configured"]'); onCompletion.then(function() {
if ($(configured).val() == 'true') { var configured = $tr.find('[data-parameter="configured"]');
$tr.find('.configuration input').attr('disabled', 'disabled'); if ($(configured).val() == 'true') {
$tr.find('.configuration').append('<span id="access" style="padding-left:0.5em;">'+t('files_external', 'Access granted')+'</span>'); $tr.find('.configuration input').attr('disabled', 'disabled');
} else { $tr.find('.configuration').append('<span id="access" style="padding-left:0.5em;">'+t('files_external', 'Access granted')+'</span>');
var app_key = $tr.find('.configuration [data-parameter="app_key"]').val(); } else {
var app_secret = $tr.find('.configuration [data-parameter="app_secret"]').val(); var app_key = $tr.find('.configuration [data-parameter="app_key"]').val();
if (app_key != '' && app_secret != '') { var app_secret = $tr.find('.configuration [data-parameter="app_secret"]').val();
var pos = window.location.search.indexOf('oauth_token') + 12; if (app_key != '' && app_secret != '') {
var token = $tr.find('.configuration [data-parameter="token"]'); var pos = window.location.search.indexOf('oauth_token') + 12;
if (pos != -1 && window.location.search.substr(pos, $(token).val().length) == $(token).val()) { var token = $tr.find('.configuration [data-parameter="token"]');
var token_secret = $tr.find('.configuration [data-parameter="token_secret"]'); if (pos != -1 && window.location.search.substr(pos, $(token).val().length) == $(token).val()) {
var statusSpan = $tr.find('.status span'); var token_secret = $tr.find('.configuration [data-parameter="token_secret"]');
statusSpan.removeClass(); var statusSpan = $tr.find('.status span');
statusSpan.addClass('waiting'); statusSpan.removeClass();
$.post(OC.filePath('files_external', 'ajax', 'oauth1.php'), { step: 2, app_key: app_key, app_secret: app_secret, request_token: $(token).val(), request_token_secret: $(token_secret).val() }, function(result) { statusSpan.addClass('waiting');
if (result && result.status == 'success') { $.post(OC.filePath('files_external', 'ajax', 'oauth1.php'), { step: 2, app_key: app_key, app_secret: app_secret, request_token: $(token).val(), request_token_secret: $(token_secret).val() }, function(result) {
$(token).val(result.access_token); if (result && result.status == 'success') {
$(token_secret).val(result.access_token_secret); $(token).val(result.access_token);
$(configured).val('true'); $(token_secret).val(result.access_token_secret);
OCA.External.Settings.mountConfig.saveStorageConfig($tr, function(status) { $(configured).val('true');
if (status) { OCA.External.Settings.mountConfig.saveStorageConfig($tr, function(status) {
$tr.find('.configuration input').attr('disabled', 'disabled'); if (status) {
$tr.find('.configuration').append('<span id="access" style="padding-left:0.5em;">'+t('files_external', 'Access granted')+'</span>'); $tr.find('.configuration input').attr('disabled', 'disabled');
} $tr.find('.configuration').append('<span id="access" style="padding-left:0.5em;">'+t('files_external', 'Access granted')+'</span>');
}); }
} else { });
OC.dialogs.alert(result.data.message, t('files_external', 'Error configuring OAuth1')); } else {
} OC.dialogs.alert(result.data.message, t('files_external', 'Error configuring OAuth1'));
}); }
});
}
} }
} }
} });
} }
}); });

View File

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