forked from WebGoat/WebGoat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue WebGoat#160: Provide Async Error Handling
Added Toast notification for unexpected errors On 401 and 403 Errors, user is redirected to login
- Loading branch information
Showing
8 changed files
with
160 additions
and
4 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
webgoat-container/src/main/java/org/owasp/webgoat/AjaxAuthenticationEntryPoint.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* ************************************************************************************************* | ||
* <p> | ||
* <p> | ||
* This file is part of WebGoat, an Open Web Application Security Project | ||
* utility. For details, please see http://www.owasp.org/ | ||
* <p> | ||
* Copyright (c) 2002 - 20014 Bruce Mayhew | ||
* <p> | ||
* This program is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License as published by the Free Software | ||
* Foundation; either version 2 of the License, or (at your option) any later | ||
* version. | ||
* <p> | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
* details. | ||
* <p> | ||
* You should have received a copy of the GNU General Public License along with | ||
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple | ||
* Place - Suite 330, Boston, MA 02111-1307, USA. | ||
* <p> | ||
* Getting Source ============== | ||
* <p> | ||
* Source for this application is maintained at https://github.com/WebGoat/WebGoat, a repository | ||
* for free software projects. | ||
*/ | ||
|
||
package org.owasp.webgoat; | ||
|
||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
|
||
/** | ||
* <p>LabelService class.</p> | ||
* | ||
* @author zupzup | ||
*/ | ||
|
||
public class AjaxAuthenticationEntryPoint extends LoginUrlAuthenticationEntryPoint { | ||
public AjaxAuthenticationEntryPoint(String loginFormUrl) { | ||
super(loginFormUrl); | ||
} | ||
|
||
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { | ||
if(request.getHeader("x-requested-with") != null) { | ||
response.sendError(401, authException.getMessage()); | ||
} else { | ||
super.commence(request, response, authException); | ||
} | ||
} | ||
} |
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
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
5 changes: 3 additions & 2 deletions
5
webgoat-container/src/main/resources/static/js/goatApp/goatApp.js
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
34 changes: 34 additions & 0 deletions
34
webgoat-container/src/main/resources/static/js/goatApp/support/goatAsyncErrorHandler.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
define( | ||
['backbone', 'underscore'], | ||
function(Backbone, _) { | ||
return { | ||
init: function() { | ||
var backboneSync = Backbone.sync; | ||
|
||
var asyncErrorHandler = function(error) { | ||
return function(jqXHR) { | ||
var statusCode = jqXHR.status; | ||
var errorCodes = { | ||
404: true, | ||
500: true, | ||
503: true, | ||
504: true | ||
}; | ||
|
||
if (statusCode === 401 || statusCode === 403) { | ||
window.top.location.href = "login"; | ||
} else if(errorCodes[statusCode]) { | ||
Backbone.trigger("error:unhandled"); | ||
} | ||
}; | ||
}; | ||
|
||
Backbone.sync = function(method, model, options) { | ||
// override error handler | ||
options.error = asyncErrorHandler(options.error); | ||
return backboneSync(method, model, options); | ||
} | ||
} | ||
}; | ||
} | ||
); |
35 changes: 35 additions & 0 deletions
35
webgoat-container/src/main/resources/static/js/goatApp/view/ErrorNotificationView.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
define(['jquery', | ||
'underscore', | ||
'backbone'], | ||
function($, | ||
_, | ||
Backbone) { | ||
return Backbone.View.extend({ | ||
el:'#error-notification-container', | ||
initialize: function() { | ||
Backbone.on("error:unhandled", this.showNotification.bind(this)); | ||
this.hideNotification(); | ||
this.currentTimeout = null; | ||
}, | ||
|
||
showNotification: function() { | ||
var self = this; | ||
if (!this.$el.is(':visible')) { | ||
this.$el.show(350); | ||
} | ||
if (this.currentTimeout != null) { | ||
window.clearTimeout(this.currentTimeout); | ||
} | ||
this.currentTimeout = window.setTimeout(function() { | ||
self.hideNotification(); | ||
self.currentTimeout = null; | ||
}, 3000); | ||
}, | ||
|
||
hideNotification: function() { | ||
if (this.$el.is(':visible')) { | ||
this.$el.hide(350); | ||
} | ||
} | ||
}); | ||
}); |
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
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