1 2 24 25 26 27 28 package com.lutris.http; 29 30 import javax.servlet.http.HttpServletRequest ; 31 32 import com.lutris.appserver.server.httpPresentation.HttpPresentationException; 33 import com.lutris.appserver.server.httpPresentation.HttpPresentationRequest; 34 import com.lutris.util.Convert; 35 36 64 public class BasicAuth { 65 66 private BasicAuth() {} 68 69 79 public static BasicAuthResult getAuthentication( 80 HttpPresentationRequest req) { 81 if (req == null) 82 return null; 83 String authHeader = null; 84 try { 85 authHeader = req.getHeader("Authorization"); 86 } catch (HttpPresentationException hpe) { 87 } 88 return getAuth(authHeader); 89 } 90 91 92 102 114 115 125 public static BasicAuthResult getAuthentication(HttpServletRequest req) { 126 if (req == null) 127 return null; 128 return getAuth(req.getHeader("Authorization")); 129 } 130 131 132 private static BasicAuthResult getAuth(String authHeader) { 133 if (authHeader == null) 134 return null; 136 139 if (!authHeader.startsWith("Basic ")) 140 return null; 142 authHeader = authHeader.substring(6); 143 byte[] bytes = Convert.fromBase64String(authHeader); 144 authHeader = new String (bytes); 145 int colon = authHeader.indexOf(":"); 146 if (colon < 0) 147 return null; 149 String un = authHeader.substring(0, colon); 150 String pw = authHeader.substring(colon + 1); 151 return new BasicAuthResult(un, pw); 152 } 153 154 } 155 156 | Popular Tags |