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 = ...@@ -68,17 +68,18 @@ var inst1 =
tap_interface: "tap2"} tap_interface: "tap2"}
}; };
var fakeserver = sinon.fakeServer.create(); /*var fakeserver = sinon.fakeServer.create();
// Get instance // Get instance
fakeserver.respondWith("GET", "/instance/200",[200, {"Content-Type":"application/json; charset=utf-8"}, JSON.stringify(inst0)]); 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)]); fakeserver.respondWith("GET", "/instance/201",[200, {"Content-Type":"application/json; charset=utf-8"}, JSON.stringify(inst1)]);
// Get instance FAIL // Get instance FAIL
fakeserver.respondWith("GET", "/instance/408",[408, {"Content-Type":"application/json; charset=utf-8"}, "NOT FOUND"]); 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; var tmp = $.ajax;
$.ajax = function(url, options){ /*$.ajax = function(url, options){
var result = tmp(url, options); var result = tmp(url, options);
fakeserver.respond(); fakeserver.respond();
return result; return result;
} }*/
\ No newline at end of file
...@@ -4,51 +4,96 @@ ...@@ -4,51 +4,96 @@
* Date: 4/17/12 * Date: 4/17/12
*/ */
;(function($) { ;(function($) {
var methods = { var methods = {
init: function() { init: function() {
// Initialize slapos in this context // Initialize slapos in this context
$(this).slapos({host: ''}); $(this).slapos({host: '10.0.1.139:12002/erp5/portal_vifib_rest_api_v1'});
// Bind to urlChange event // Bind to urlChange event
return this.each(function(){ return this.each(function(){
var self = $(this); var self = $(this);
$.subscribe("urlChange", function(e, d){ $.subscribe("urlChange", function(e, d){
if(d.route == "form") { self.form('displayForm'); } if(d.route == "service" && d.id) { self.form('displayData', d.id); }
else 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);
});
displayData: function(id){ });
$(this).html("<p>Ajax loading...</p>") },
.slapos('getInstance', id, function(data){
$(this).form('render', 'service', data); authenticate: function(data){
}, {408: function(){ $(this).form('render', 'service-error', {id:id})}}); for(var d in data){
}, $(this).slapos('store', d, data[d]);
}
displayForm: function() { },
$(this).form('render', 'simple-form');
displayData: function(id){
$(this).find("form").submit(function(){ $(this).html("<p>Ajax loading...</p>")
$.redirect({route:'disp', id:$(this).find("input:text").val()}); .slapos('getInstance', id, function(data){
return false; $(this).form('render', 'service', data);
}); }, {401: function(){
}, $(this).form('render', 'auth', {
'host':'t139:12002/erp5/web_site_module/hosting/request-access-token',
render: function(template, data){ 'client_id': 'client',
$(this).html(ich[template](data, true)); 'redirect':escape(window.location.href)
} })
}; }});
},
$.fn.form = function(method){
if ( methods[method] ) { displayForm: function() {
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 )); $(this).form('render', 'form.new.instance');
} else if ( typeof method === 'object' || ! method ) { var data = {};
return methods.init.apply( this, arguments ); $(this).find("form").submit(function(){
} else { $(this).find('input').serializeArray().map(function(elem){
$.error( 'Method ' + method + ' does not exist on jQuery.form' ); 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); })(jQuery);
$("#main").form(); $("#main").form();
...@@ -7,6 +7,11 @@ ...@@ -7,6 +7,11 @@
; ;
// Hash parser utility // Hash parser utility
$.parseHash = function(hashTag){ $.parseHash = function(hashTag){
var tokenized = $.extractAuth(hashTag);
if( tokenized ){
console.log(tokenized);
$.publish('auth', tokenized);
}
var splitted = hashTag.substr(1).split('/'); var splitted = hashTag.substr(1).split('/');
return { return {
route : splitted[0], route : splitted[0],
...@@ -15,6 +20,21 @@ $.parseHash = function(hashTag){ ...@@ -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){ $.genHash = function(url){
if('id' in url){ url['id'] = '/' + url['id']; } if('id' in url){ url['id'] = '/' + url['id']; }
if('method' in url){ url['method'] = '/' + url['method']; } if('method' in url){ url['method'] = '/' + url['method']; }
...@@ -45,4 +65,4 @@ $.redirect = function(url){ $.publish('redirect', url); }; ...@@ -45,4 +65,4 @@ $.redirect = function(url){ $.publish('redirect', url); };
$.subscribe('redirect', $.redirectHandler) $.subscribe('redirect', $.redirectHandler)
$(window).bind('hashchange', $.hashHandler); $(window).bind('hashchange', $.hashHandler);
$(window).bind('load', $.hashHandler); $(window).bind('load', $.hashHandler);
\ No newline at end of file
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