Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion apps/files_external/js/gdrive.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
$(document).ready(function() {
var backendId = 'googledrive';
var backendUrl = OC.generateUrl('apps/files_external/ajax/oauth2.php');

function generateUrl($tr) {
// no mapping between client ID and Google 'project', so we always load the same URL
return 'https://console.developers.google.com/';
}

OCA.External.Settings.mountConfig.whenSelectBackend(function($tr, backend, onCompletion) {
if (backend === 'googledrive') {
if (backend === backendId) {
var backendEl = $tr.find('.backend');
var el = $(document.createElement('a'))
.attr('href', generateUrl($tr))
Expand All @@ -23,4 +25,35 @@ $(document).ready(function() {
}
});

/**
* ------- OAUTH2 Events ----------
*
* The files_external_{backendId} app's CUSTOM JS should handle the OAuth2 events itself
* instead on relying on the core for the implemention. These two functions
* [1] OCA.External.Settings.OAuth2.getAuthUrl, [2] OCA.External.Settings.OAuth2.verifyCode
* abstract away the details of sending the request to backend for getting the AuthURL or
* verifying the code, mounting the storage config etc
*/
$('.configuration').on('oauth_step1', function (event, data) {
if (data['backend_id'] !== backendId) {
return false; // means the trigger is not for this storage adapter
}

// Redirects the User on success else displays an alert (with error message sent by backend)
OCA.External.Settings.OAuth2.getAuthUrl(backendUrl, data);
});

$('.configuration').on('oauth_step2', function (event, data) {
if (data['backend_id'] !== backendId || data['code'] === undefined) {
return false; // means the trigger is not for this OAuth2 grant
}

OCA.External.Settings.OAuth2.verifyCode(backendUrl, data)
.fail(function (message) {
OC.dialogs.alert(message,
t('files_external', 'Error verifying OAuth2 Code for ' + backendId)
);
})
});

});
82 changes: 24 additions & 58 deletions apps/files_external/js/oauth2.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,22 @@ $(document).ready(function() {
displayGranted($tr);
} 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 + '?sectionid=storage',
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) {
displayGranted($tr);
}
});
} else {
OC.dialogs.alert(result.data.message,
t('files_external', 'Error configuring OAuth2')
);
}
}
);
}
var client_secret = $tr.find('.configuration [data-parameter="client_secret"]').val();

var params = {};
window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m, key, value) {
params[key] = value;
});

if (params['code'] !== undefined) {
$('.configuration').trigger('oauth_step2', [{
backend_id: $tr.attr('class'),
client_id: client_id,
client_secret: client_secret,
redirect: location.protocol + '//' + location.host + location.pathname + '?sectionid=storage',
tr: $tr,
code: params['code'] || '',
}]);
}
}
});
Expand All @@ -65,31 +45,17 @@ $(document).ready(function() {
$('#externalStorage').on('click', '[name="oauth2_grant"]', function(event) {
event.preventDefault();
var tr = $(this).parent().parent();
var configured = $(this).parent().find('[data-parameter="configured"]');
var client_id = $(this).parent().find('[data-parameter="client_id"]').val();
var client_secret = $(this).parent().find('[data-parameter="client_secret"]').val();

if (client_id != '' && client_secret != '') {
var token = $(this).parent().find('[data-parameter="token"]');
$.post(OC.filePath('files_external', 'ajax', 'oauth2.php'),
{
step: 1,
client_id: client_id,
client_secret: client_secret,
redirect: location.protocol + '//' + location.host + location.pathname + '?sectionid=storage',
}, function(result) {
if (result && result.status == 'success') {
$(configured).val('false');
$(token).val('false');
OCA.External.Settings.mountConfig.saveStorageConfig(tr, function(status) {
window.location = result.data.url;
});
} else {
OC.dialogs.alert(result.data.message,
t('files_external', 'Error configuring OAuth2')
);
}
}
);
$('.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
}])
}
});

Expand Down
91 changes: 91 additions & 0 deletions apps/files_external/js/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -1403,4 +1403,95 @@ OCA.External.Settings.GlobalStorageConfig = GlobalStorageConfig;
OCA.External.Settings.UserStorageConfig = UserStorageConfig;
OCA.External.Settings.MountConfigListView = MountConfigListView;

/**
* @namespace OAuth2 namespace which is used to verify a storage adapter
* using AuthMechanism as oauth2::oauth2
*/
OCA.External.Settings.OAuth2 = OCA.External.Settings.OAuth2 || {};

/**
* This function sends a request to the given backendUrl and gets the OAuth2 URL
* for any given backend storage, executes the callback if any, set the data-* parameters
* of the storage and REDIRECTS the client to Authentication page
*
* @param {String} backendUrl The backend URL to which request will be sent
* @param {Object} data Keys -> (backend_id, client_id, client_secret, redirect, tr)
*/
OCA.External.Settings.OAuth2.getAuthUrl = function (backendUrl, data) {
var $tr = data['tr'];
var configured = $tr.find('[data-parameter="configured"]');
var token = $tr.find('.configuration [data-parameter="token"]');

$.post(backendUrl, {
step: 1,
client_id: data['client_id'],
client_secret: data['client_secret'],
redirect: data['redirect'],
}, function (result) {
if (result && result.status == 'success') {
$(configured).val('false');
$(token).val('false');

OCA.External.Settings.mountConfig.saveStorageConfig($tr, function(status) {
if (!result.data.url) {
OC.dialogs.alert('Auth URL not set',
t('files_external', 'No URL provided by backend ' + data['backend_id'])
);
} else {
window.location = result.data.url;
}
});
} else {
OC.dialogs.alert(result.data.message,
t('files_external', 'Error getting OAuth2 URL for ' + data['backend_id'])
);
}
}
);
}

/**
* This function verifies the OAuth2 code returned to the client after verification
* by sending request to the backend with the given CODE and if the code is verified
* it sets the data-* params to configured and disables the authorize buttons
*
* @param {String} backendUrl The backend URL to which request will be sent
* @param {Object} data Keys -> (backend_id, client_id, client_secret, redirect, tr, code)
* @return {Promise} jQuery Deferred Promise object
*/
OCA.External.Settings.OAuth2.verifyCode = function (backendUrl, data) {
var $tr = data['tr'];
var configured = $tr.find('[data-parameter="configured"]');
var token = $tr.find('.configuration [data-parameter="token"]');
var statusSpan = $tr.find('.status span');
statusSpan.removeClass().addClass('waiting');

var deferredObject = $.Deferred();
$.post(backendUrl, {
step: 2,
client_id: data['client_id'],
client_secret: data['client_secret'],
redirect: data['redirect'],
code: data['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.auth-param')
.attr('disabled', 'disabled')
.addClass('disabled-success')
}
deferredObject.resolve(status);
});
} else {
deferredObject.reject(result.data.message);
}
}
);
return deferredObject.promise();
}

})();