Commit 3e8436aa authored by Thomas Lechauve's avatar Thomas Lechauve

Authentication process implemented (with OAuth2)

The fake server has been commented so instance information page won't
appear anymore.
parent ea194c88
This diff is collapsed.
......@@ -68,17 +68,18 @@ var inst1 =
tap_interface: "tap2"}
};
var fakeserver = sinon.fakeServer.create();
/*var fakeserver = sinon.fakeServer.create();
// Get instance
fakeserver.respondWith("GET", "/instance/200",[200, {"Content-Type":"application/json; charset=utf-8"}, JSON.stringify(inst0)]);
fakeserver.respondWith("GET", "/instance/201",[200, {"Content-Type":"application/json; charset=utf-8"}, JSON.stringify(inst1)]);
// Get instance FAIL
fakeserver.respondWith("GET", "/instance/408",[408, {"Content-Type":"application/json; charset=utf-8"}, "NOT FOUND"]);
fakeserver.respondWith("GET", "/instance/401",[401, {"Content-Type":"application/json; charset=utf-8"}, "NEED AUTH"]);
var tmp = $.ajax;
$.ajax = function(url, options){
/*$.ajax = function(url, options){
var result = tmp(url, options);
fakeserver.respond();
return result;
}
\ No newline at end of file
}*/
......@@ -4,51 +4,96 @@
* Date: 4/17/12
*/
;(function($) {
var methods = {
init: function() {
// Initialize slapos in this context
$(this).slapos({host: ''});
// Bind to urlChange event
return this.each(function(){
var self = $(this);
$.subscribe("urlChange", function(e, d){
if(d.route == "form") { self.form('displayForm'); }
else if(d.route == "service" && d.id) { self.form('displayData', d.id); }
});
});
},
displayData: function(id){
$(this).html("<p>Ajax loading...</p>")
.slapos('getInstance', id, function(data){
$(this).form('render', 'service', data);
}, {408: function(){ $(this).form('render', 'service-error', {id:id})}});
},
displayForm: function() {
$(this).form('render', 'simple-form');
$(this).find("form").submit(function(){
$.redirect({route:'disp', id:$(this).find("input:text").val()});
return false;
});
},
render: function(template, data){
$(this).html(ich[template](data, true));
}
};
$.fn.form = function(method){
if ( methods[method] ) {
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.form' );
}
};
var methods = {
init: function() {
// Initialize slapos in this context
$(this).slapos({host: '10.0.1.139:12002/erp5/portal_vifib_rest_api_v1'});
// Bind to urlChange event
return this.each(function(){
var self = $(this);
$.subscribe("urlChange", function(e, d){
if(d.route == "service" && d.id) { self.form('displayData', d.id); }
else if(d.route == "service") { self.form('displayForm'); }
});
$.subscribe("auth", function(e, d){
$(this).form("authenticate", d);
});
});
},
authenticate: function(data){
for(var d in data){
$(this).slapos('store', d, data[d]);
}
},
displayData: function(id){
$(this).html("<p>Ajax loading...</p>")
.slapos('getInstance', id, function(data){
$(this).form('render', 'service', data);
}, {401: function(){
$(this).form('render', 'auth', {
'host':'t139:12002/erp5/web_site_module/hosting/request-access-token',
'client_id': 'client',
'redirect':escape(window.location.href)
})
}});
},
displayForm: function() {
$(this).form('render', 'form.new.instance');
var data = {};
$(this).find("form").submit(function(){
$(this).find('input').serializeArray().map(function(elem){
data[elem["name"]] = elem["value"];
});
$(this).form('displayAsking', data);
return false;
});
},
displayAsking: function(data){
var request = {
software_type: "type_provided_by_the_software",
slave: false,
status: "started",
parameter: {
Custom1: "one string",
Custom2: "one float",
Custom3: ["abc", "def"],
},
sla: {
computer_id: "COMP-0",
}
};
$.extend(request, data);
$(this).html("<p>Requesting a new instance "+request["title"]+" ...</p>")
.slapos('newInstance', request, function(data){
$(this).html(data);}, {401: function(){
$(this).form('render', 'auth', {
'host':'t139:12002/erp5/web_site_module/hosting/request-access-token',
'client_id': 'client',
'redirect':escape(window.location.href)
})
}
});
},
render: function(template, data){
$(this).html(ich[template](data, true));
}
};
$.fn.form = function(method){
if ( methods[method] ) {
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.form' );
}
};
})(jQuery);
$("#main").form();
......@@ -7,6 +7,11 @@
;
// Hash parser utility
$.parseHash = function(hashTag){
var tokenized = $.extractAuth(hashTag);
if( tokenized ){
console.log(tokenized);
$.publish('auth', tokenized);
}
var splitted = hashTag.substr(1).split('/');
return {
route : splitted[0],
......@@ -15,6 +20,21 @@ $.parseHash = function(hashTag){
}
};
$.extractAuth = function(hashTag){
var del = hashTag.indexOf('&');
if( del != -1){
var splitted = hashTag.substring(del+1).split('&');
var result = {};
for( p in splitted ){
var s = splitted[p].split('=');
result[s[0]] = s[1];
}
return result;
}
return false;
};
$.genHash = function(url){
if('id' in url){ url['id'] = '/' + url['id']; }
if('method' in url){ url['method'] = '/' + url['method']; }
......@@ -45,4 +65,4 @@ $.redirect = function(url){ $.publish('redirect', url); };
$.subscribe('redirect', $.redirectHandler)
$(window).bind('hashchange', $.hashHandler);
$(window).bind('load', $.hashHandler);
\ No newline at end of file
$(window).bind('load', $.hashHandler);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment