private String getUserName(HttpServletRequest request) { List<NameValuePair> list = URLEncodedUtils.parse(request.getQueryString(), UTF8_CHARSET); if (list != null) { for (NameValuePair nv : list) { if (PseudoAuthenticator.USER_NAME.equals(nv.getName())) { return nv.getValue(); } } } return null; }
private void _testUserName(boolean anonymous) throws Exception { PseudoAuthenticationHandler handler = new PseudoAuthenticationHandler(); try { Properties props = new Properties(); props.setProperty(PseudoAuthenticationHandler.ANONYMOUS_ALLOWED, Boolean.toString(anonymous)); handler.init(props); HttpServletRequest request = Mockito.mock(HttpServletRequest.class); HttpServletResponse response = Mockito.mock(HttpServletResponse.class); Mockito.when(request.getQueryString()).thenReturn(PseudoAuthenticator.USER_NAME + "=" + "user"); AuthenticationToken token = handler.authenticate(request, response); Assert.assertNotNull(token); Assert.assertEquals("user", token.getUserName()); Assert.assertEquals("user", token.getName()); Assert.assertEquals(PseudoAuthenticationHandler.TYPE, token.getType()); } finally { handler.destroy(); } }
private void _testUserName(boolean anonymous) throws Exception { PseudoAuthenticationHandler handler = new PseudoAuthenticationHandler(); try { Properties props = new Properties(); props.setProperty(PseudoAuthenticationHandler.ANONYMOUS_ALLOWED, Boolean.toString(anonymous)); handler.init(props); HttpServletRequest request = Mockito.mock(HttpServletRequest.class); HttpServletResponse response = Mockito.mock(HttpServletResponse.class); Mockito.when(request.getParameter(PseudoAuthenticator.USER_NAME)).thenReturn("user"); AuthenticationToken token = handler.authenticate(request, response); assertNotNull(token); assertEquals("user", token.getUserName()); assertEquals("user", token.getName()); assertEquals(PseudoAuthenticationHandler.TYPE, token.getType()); } finally { handler.destroy(); } }
public PseudoDelegationTokenAuthenticator() { super(new PseudoAuthenticator() { @Override protected String getUserName() { try { return UserGroupInformation.getCurrentUser().getShortUserName(); } catch (IOException ex) { throw new RuntimeException(ex); } } }); }
@Override protected Authenticator getFallBackAuthenticator() { return new PseudoAuthenticator() { @Override protected String getUserName() { try { return UserGroupInformation.getLoginUser().getUserName(); } catch (IOException e) { throw new SecurityException("Failed to obtain current username", e); } } }; }
private String getUserName(HttpServletRequest request) { String queryString = request.getQueryString(); if(queryString == null || queryString.length() == 0) { return null; } List<NameValuePair> list = URLEncodedUtils.parse(queryString, UTF8_CHARSET); if (list != null) { for (NameValuePair nv : list) { if (PseudoAuthenticator.USER_NAME.equals(nv.getName())) { return nv.getValue(); } } } return null; }
/** * Get username specified by custom username HTTP header. * * @return Name of user sending the request */ public String getUserName() { if (AuthenticationManager.getAuthenticationHandler().isSecurityEnabled()) { return HttpUserGroupInformation.get().getShortUserName(); } else { return request.getParameter(PseudoAuthenticator.USER_NAME); } }
private String addUsername(String strUrl) { String paramSeparator = (strUrl.contains("?")) ? "&" : "?"; strUrl += paramSeparator + PseudoAuthenticator.USER_NAME + "=" + System.getProperty("user.name"); return strUrl; }
/** * Authenticates an HTTP client request. * <p/> * It extracts the {@link PseudoAuthenticator#USER_NAME} parameter from the query string and creates * an {@link AuthenticationToken} with it. * <p/> * If the HTTP client request does not contain the {@link PseudoAuthenticator#USER_NAME} parameter and * the handler is configured to allow anonymous users it returns the {@link AuthenticationToken#ANONYMOUS} * token. * <p/> * If the HTTP client request does not contain the {@link PseudoAuthenticator#USER_NAME} parameter and * the handler is configured to disallow anonymous users it throws an {@link AuthenticationException}. * * @param request the HTTP client request. * @param response the HTTP client response. * * @return an authentication token if the HTTP client request is accepted and credentials are valid. * * @throws IOException thrown if an IO error occurred. * @throws AuthenticationException thrown if HTTP client request was not accepted as an authentication request. */ @Override public AuthenticationToken authenticate(HttpServletRequest request, HttpServletResponse response) throws IOException, AuthenticationException { AuthenticationToken token; String userName = request.getParameter(PseudoAuthenticator.USER_NAME); if (userName == null) { if (getAcceptAnonymous()) { token = AuthenticationToken.ANONYMOUS; } else { throw new AuthenticationException("Anonymous requests are disallowed"); } } else { token = new AuthenticationToken(userName, userName, getType()); } return token; }