Created
November 5, 2015 19:35
-
-
Save fastslack/706a9b2c19322a7c429e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(function($) { | |
// Extend jQuery to add random string generator | |
$.extend({ | |
randomKey: function (length, special) { | |
var iteration = 0; | |
var randomKey = ""; | |
var randomNumber; | |
if(special == undefined){ | |
var special = false; | |
} | |
while(iteration < length){ | |
randomNumber = (Math.floor((Math.random() * 100)) % 94) + 33; | |
if(!special){ | |
if ((randomNumber >=33) && (randomNumber <=47)) { continue; } | |
if ((randomNumber >=58) && (randomNumber <=64)) { continue; } | |
if ((randomNumber >=91) && (randomNumber <=96)) { continue; } | |
if ((randomNumber >=123) && (randomNumber <=126)) { continue; } | |
} | |
iteration++; | |
randomKey += String.fromCharCode(randomNumber); | |
} | |
return randomKey; | |
} | |
}); | |
// Extend jQuery to add JSON HTTP request | |
$.extend({ | |
fetchPostData: function (usuario, password) { | |
// Get the values from html input's | |
var username = usuario; | |
var password = password; | |
var random_key = $.randomKey(36); | |
// Encode the data to send | |
var user_encode = $.base64.encode(username +':'+ random_key); | |
var pass_encode = $.base64.encode($.randomKey(36)) +':'+ $.base64.encode(password+':'+random_key); | |
// Prepare the return object | |
$.postData = {}; | |
$.postData.oauth_client_id = user_encode; | |
$.postData.oauth_client_secret = $.base64.encode(pass_encode); | |
$.postData.oauth_response_type = 'temporary'; | |
$.postData.oauth_signature_method = 'PLAINTEXT'; | |
return $.postData; | |
} | |
}); | |
// Declare Ajax Object | |
$.extend({ | |
doLogin: function (usuario, password, returnData) { | |
console.log("DOLOGIN START"); | |
// Fetch the post data | |
var postData = $.fetchPostData(usuario, password); | |
// Define the beforeSend function | |
var beforeSendData = function(xhr) { | |
var base64 = $.base64.encode(usuario); | |
xhr.setRequestHeader('Authorization', 'Bearer ' + base64); | |
}; | |
// | |
// Request the temporary token | |
// | |
$.ajax({ url: urlWellMets + "/api/joomla:articles", | |
data: postData, | |
dataType: 'jsonp', | |
timeout : 10000, | |
crossDomain: true, | |
beforeSend : beforeSendData, | |
error: function (xhr, textStatus, errorThrown) { | |
console.log("DOLOGIN ERROR: " + errorThrown + "(" + textStatus + ")"); | |
returnData(null); | |
}, | |
success:function(data, textStatus, xhqr){ | |
console.log("DOLOGIN SUCCESS"); | |
// Decode the JSON response | |
postData.oauth_code = data.oauth_code; | |
postData.oauth_response_type = 'authorise'; | |
}, | |
complete: function(response, textStatus) { | |
console.log("DOLOGIN COMPLETE (" + textStatus + ")"); | |
if (textStatus == "success") | |
{ | |
// | |
// Request the authorization token | |
// | |
$.ajax({ url: urlWellMets + "/api/joomla:articles", | |
data: postData, | |
dataType: 'jsonp', | |
timeout : 10000, | |
crossDomain: true, | |
beforeSend : beforeSendData, | |
error: function (xhr, textStatus, errorThrown) { | |
console.log("DOLOGIN 2 ERROR: " + errorThrown + "(" + textStatus + ")"); | |
returnData(null); | |
}, | |
success:function(data, textStatus, xhqr){ | |
console.log("DOLOGIN 2 SUCCESS"); | |
// Decode the JSON response | |
// Add new values to new ajax request | |
postData.oauth_code = data.oauth_code; | |
postData.oauth_response_type = 'token'; | |
}, | |
complete: function(response, textStatus) { | |
console.log("DOLOGIN 2 COMPLETE (" + textStatus + ")"); | |
if (textStatus == "success") | |
{ | |
// | |
// Request the access_token | |
// | |
$.ajax({ url: urlWellMets + "/api/joomla:articles", | |
data: postData, | |
dataType: 'jsonp', | |
timeout : 10000, | |
crossDomain: true, | |
beforeSend : beforeSendData, | |
error: function (xhr, textStatus, errorThrown) { | |
console.log("DOLOGIN 3 ERROR: " + errorThrown + "(" + textStatus + ")"); | |
returnData(null); | |
}, | |
success:function(data, textStatus, xhqr){ | |
console.log("DOLOGIN 3 SUCCESS"); | |
// Decode the JSON response | |
console.log("DOLOGIN 3 RESULT" + data); | |
returnData(data); | |
}, | |
complete: function(response, textStatus) { | |
console.log("DOLOGIN 3 COMPLETE (" + textStatus + ")"); | |
} | |
}); // end ajax | |
} | |
} | |
}); // end ajax | |
} | |
} | |
}); // end ajax | |
} | |
}); | |
}(jQuery)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment