Guest User

Untitled

a guest
May 5th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. package mori.web.controller;
  2.  
  3. import javax.servlet.http.*;
  4. import java.util.*;
  5. import java.sql.*;
  6. import java.io.*;
  7. import java.net.*;
  8. import java.util.*;
  9. import javax.servlet.*;
  10. import javax.servlet.http.*;
  11. import sun.misc.*; // See warning below
  12.  
  13. import org.apache.struts.Globals;
  14. import org.apache.struts.action.*;
  15. import org.apache.commons.lang.StringUtils;
  16. import org.apache.commons.logging.*;
  17.  
  18. import mori.web.model.registry.*;
  19.  
  20.  
  21. /*
  22. import net.vitarara.web.struts.controller.BaseAction;
  23. import net.vitarara.ejb.*;
  24. import net.vitarara.utils.*;
  25.  
  26. import mori.ejb.data.registry.SiteUserPermsValue;
  27. import mori.ejb.model.registry.GLMemberManagerBean;
  28.  
  29. import mori.service.*;
  30. import mori.service.fundraising.*;
  31. import mori.service.registry.*;
  32. import mori.web.model.registry.*;
  33. import mori.web.view.*;
  34. import mori.web.*;
  35. */
  36.  
  37. public class MORIHttpBasicAuthBaseAction extends MORIBaseAction {
  38.  
  39.  
  40. public ActionForward execute ( ActionMapping mapping, ActionForm form,
  41. HttpServletRequest request, HttpServletResponse response )
  42. throws Exception {
  43.  
  44. if (doBasicAuth (request, response) ) {
  45. // Attempt to service the request checking role based permissions.
  46.  
  47. String url = request.getRequestURI (); // Need to know the requested URI to test for role based permissions.
  48.  
  49. // Get the user's object.
  50. SiteUserBean user = getSiteUser ( request );
  51.  
  52. // Check that the user has the proper role
  53. // for the service.
  54. if (getPermissionsManager().userHasRole ( user, url ) ) {
  55. return _execute(mapping, form, request, response);
  56. } else {
  57. // user not permitted; ELSE: user is permitted
  58. // Send a 500 error code.
  59. response.sendError (500);
  60. return null;
  61. }
  62. } else {
  63. // login or password can not be blank.
  64. response.sendError(500);
  65. return null;
  66. }
  67. }
  68.  
  69. private boolean doBasicAuth (HttpServletRequest request, HttpServletResponse response) {
  70.  
  71. String userID = null;
  72. String password = null;
  73.  
  74. // Get the Authorization header, if one was supplied
  75. String authHeader = request.getHeader("Authorization");
  76.  
  77. return authHeaderWellFormed (authHeader) && doLogin (extractLogin (authHeader), extractPassword (authHeader), request);
  78. }
  79.  
  80. private boolean authHeaderWellFormed (String authHeader) {
  81. boolean result = false;
  82.  
  83. if (!StringUtils.isBlank (authHeader) ) {
  84. StringTokenizer st = new StringTokenizer (authHeader);
  85. if (st.countTokens == 2) {
  86. String authType = st.nextToken ();
  87. String loginPassword = st.nextToken ();
  88.  
  89. // We only support HTTP Basic Authentication.
  90. // The loginPassword must contain a ":".
  91.  
  92. if (authType.equalsIgnoreCase ("Basic") && loginPassword.indexOf (":") != -1) {
  93. result = true;
  94. }
  95. }
  96. }
  97.  
  98. return result;
  99. }
  100.  
  101. /**
  102. * Extract the login from the Authorization header String. This method requires
  103. * a properly formatted Basic auth header be passed to him.
  104. */
  105. private String extractLogin (String authHeader) {
  106.  
  107. // Get the loginPassword off of the authHeader.
  108. StringTokenizer st = new StringTokenizer(authHeader);
  109. st.nextToken();
  110. String loginPassword = st.nextToken();
  111.  
  112. // The loginPassword is a delimited String of login:password.
  113. return loginPassword.substring (0, loginPassword.indexOf (":") );
  114. }
  115.  
  116. /**
  117. * Extract the password from the Authorization header String. This method requires
  118. * a properly formatted Basic auth header be passed to him.
  119. */
  120. private String extractPassword (String authHeader) {
  121.  
  122. // Get the loginPassword off of the authHeader.
  123. StringTokenizer st = new StringTokenizer(authHeader);
  124. st.nextToken();
  125. String loginPassword = st.nextToken();
  126.  
  127. return loginPassword.substring (loginPassword.indexOf(":") + 1);
  128. }
  129. }
Add Comment
Please, Sign In to add comment