Skip to content

Commit

Permalink
guard api with authentication required
Browse files Browse the repository at this point in the history
loginRequired was causing 302 redirect, which could not be handled at
jQuery level. authentication required response with 401, which can be
easily intercpted.
  • Loading branch information
cdaniel committed Aug 18, 2015
1 parent 5fe28db commit 9c56d83
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 14 deletions.
4 changes: 4 additions & 0 deletions client/mapeditor.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
padding-right: 1%;
}

.errormessage {
display : none;
}

.item {
border: 1px solid black;
background-color: silver;
Expand Down
12 changes: 9 additions & 3 deletions client/mapeditor.jade
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ html
head
meta(charset='utf-8')
title Wardley Maps Tool - #{map.history[0].name}
script(src='//cdnjs.cloudflare.com/ajax/libs/jquery/1.11.2/jquery.min.js')
script(src='//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js')
script(src='//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js')
script(src='//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js')
script(src='/3rd/jquery.form.js')
script(src='//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js')
script(src='//cdnjs.cloudflare.com/ajax/libs/x-editable/1.5.0/bootstrap3-editable/js/bootstrap-editable.min.js')
Expand All @@ -27,7 +27,7 @@ html
script(src='/3rd/raphael.min.js')
script(src='/3rd/wheelnav.js')
script(src='https://cdnjs.cloudflare.com/ajax/libs/jqueryui-touch-punch/0.2.3/jquery.ui.touch-punch.min.js')
link(rel='stylesheet', href='//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.2/jquery-ui.theme.min.css')
link(rel='stylesheet', href='//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.theme.min.css')
link(rel='stylesheet', href='//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css')
link(rel='stylesheet', href='//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap-theme.min.css')
link(rel='stylesheet', href='//cdnjs.cloudflare.com/ajax/libs/x-editable/1.5.0/bootstrap3-editable/css/bootstrap-editable.css')
Expand Down Expand Up @@ -83,6 +83,12 @@ html
span.glyphicon.glyphicon-log-out Logout
p Description:
a#description(href="#" data-type="text" data-title="Description" value=map.history[0].description data-pk="description" data-url='/api/map/partial/#{map._id}')=map.history[0].description
#lostsession.errormessage.alert.alert-warning
strong Session lost! 
| The server requires authorization. You will be redirected in 5 seconds.
#servernotresponding.errormessage.alert.alert-warning
strong Server is not responding correctly! 
| You will be redirected in 5 seconds.
//- canvas
#map-container.col-lg-10
.axis-support#support-1
Expand Down
38 changes: 28 additions & 10 deletions client/mapeditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,19 +203,37 @@ function saveMap() {
async : 'true',
contentType: 'application/json',
data : JSON.stringify(map),
success : function(result) {
if (result.status) {
console.log('something went wrong');
} else {
console.log('do something with this one');
}
saving = false;
lastSavedIndex = dirtyIndexCopy;
},
dataType : 'json',
error : function(request, error) {
console.log('An error while getting map list!', error);
if(request.status == 401){
// served by status code
return;
}
console.log('An error while saving a map!', error);
console.log('error ' + dirtyIndexCopy);
saving = false;
$("#servernotresponding").show();
setTimeout(function() {
window.location.href = "/";
}, 5000);
},
statusCode : {
200 : function(){
saving = false;
lastSavedIndex = dirtyIndexCopy;
},
302 : function() {
$("#lostsession").show();
setTimeout(function() {
window.location.href = "/";
}, 5000);
},
401 : function() {
$("#lostsession").show();
setTimeout(function() {
window.location.href = "/";
}, 5000);
}
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ var WardleyMapsApp = function(configOptions) {

app.use('/share', share.router);
app.use('/profile', userProvider.loginRequired, require('./router/profilerouter.js')().router);
app.use('/api', userProvider.loginRequired, require('./router/apirouter.js')(self.maps, self.exportmap).router);
app.use('/api', userProvider.authenticationRequired, require('./router/apirouter.js')(self.maps, self.exportmap).router);
app.use('/', userProvider.loginRequired, require('./router/mainrouter.js')(self.maps).router);


Expand Down
3 changes: 3 additions & 0 deletions server/user-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ module.exports = function(app) {
osUserMiddleware.loginRequired = function(req, res, next) {
next();
};
osUserMiddleware.authenticationRequired = function(req, res, next) {
next();
};
app.use(osUserMiddleware);
return osUserMiddleware;
}
Expand Down

0 comments on commit 9c56d83

Please sign in to comment.