(function () {
$(function () {
if (!$("#OAuthTokenDiv").length) {
var basicAuthUI =
'
';
$(basicAuthUI).insertBefore('#api_selector div.input:last-child');
$("#input_apiKey").hide();
$("#input_getToken").on("click", function () {
var crypt = new JSEncrypt();
crypt.setKey("-----BEGIN PUBLIC KEY-----" +
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAynqY4P5mfcYttT840IgP" +
"tQxQXZNbHnYROPl5oU8wSiC3PepF/cjYNReva+4/5RZYSOR0ZqZmTZiJbxhdF7ic" +
"mydIpi3IFFtfNZwXNyDT5176EOyuhtNjLehwqFupHJ+3VYTUzqZPEMXC1jVilfIG" +
"VKJEGLgz4d5UpYrqFiodyJ/JZ+Qj1hyJV0QHSm8V9fUz6RCXFiUMo4oDEakh78eQ" +
"XSM+lM1FVkfGut42C1prpyouhkIMfsYA2XOOkjiVxYOwy0TEb+/QTvJAzOzPNU1O" +
"R9s9e8Uvw8cD8O1+gc9SDNO1FfUMJFnLdG9nhTFTQusM/cGEV4TAW4op8O+LbyZo" +
"nQIDAQAB" +
"-----END PUBLIC KEY-----");
var enc = crypt.encrypt($('#input_myapikey').val());
var authParams = new Object();
authParams.grant_type = "password";
authParams.username = $('#input_username').val();
authParams.password = enc;
var remoteUrl = $('#input_baseUrl').val();
var parsedUrl = parseURL(remoteUrl);
var destUrl = parsedUrl.protocol + "//" + parsedUrl.host + "/Token";
var settings = {
"async": true,
"dataType": "json",
"crossDomain": true,
"url": destUrl,
"type": "POST",
"headers": {
"Accept": "application/json",
"content-type": "application/x-www-form-urlencoded",
"cache-control": "no-cache",
},
"data": authParams,
success: function (data, text) {
swaggerUi.api.clientAuthorizations.add("key", new SwaggerClient.ApiKeyAuthorization("Authorization", "Bearer " + data.access_token, "header"));
// TODO: better UI (insert "Connected" text somewhere)
alert("Succesfully retrieved Token");
},
error: function (request, status, error) {
alert(error);
}
}
$.ajax(settings);
});
}
});
function addAuthorization() {
var username = $('#input_username').val();
var password = $('#input_myapikey').val();
if (username && username.trim() != "" && password && password.trim() != "") {
swaggerUi.api.clientAuthorizations.add("key", new SwaggerClient.ApiKeyAuthorization("Authorization", "Bearer " + username, "header"));
//var basicAuth = new SwaggerClient.PasswordAuthorization('basic', username, password);
//window.swaggerUi.api.clientAuthorizations.add("basicAuth", basicAuth);
//console.log("authorization added: username = " + username + ", password = " + password);
}
}
function parseURL(url) {
var parser = document.createElement('a'),
searchObject = {},
queries, split, i;
// Let the browser do the work
parser.href = url;
// Convert query string to object
queries = parser.search.replace(/^\?/, '').split('&');
for (i = 0; i < queries.length; i++) {
split = queries[i].split('=');
searchObject[split[0]] = split[1];
}
return {
protocol: parser.protocol,
host: parser.host,
hostname: parser.hostname,
port: parser.port,
pathname: parser.pathname,
search: parser.search,
searchObject: searchObject,
hash: parser.hash
};
}
})();