public void handle (HttpExchange t) throws IOException { InputStream is = t.getRequestBody(); while (is.read () != -1) ; is.close(); t.sendResponseHeaders(200, -1); HttpPrincipal p = t.getPrincipal(); if (!p.getUsername().equals(USERNAME)) { error = true; } if (!p.getRealm().equals(REALM)) { error = true; } t.close(); }
public static String userName(HttpExchange t) { HttpPrincipal p = t.getPrincipal(); if (p == null) { return ""; } return p.getUsername(); }
@Test public void testGetPrincipal() { // setup HttpPrincipal expectedResult = mock(HttpPrincipal.class); when(this.httpExchange.getPrincipal()).thenReturn(expectedResult); // act HttpPrincipal result = this.httpExchangeImpl.getPrincipal(); // assert assertEquals(expectedResult, result); }
@Override public HttpPrincipal getPrincipal() { return _principal; }
public void setPrincipal(HttpPrincipal principal) { this._principal = principal; }
@Override public HttpPrincipal getPrincipal() { return null; }
@Override protected void before(HttpExchange exchange) { ((MockHttpExchange) exchange) .setPrincipal(new HttpPrincipal(PRINCIPAL_NAME, "my realm")); }
@Override public HttpPrincipal getPrincipal() { throw new RuntimeException("Glowroot should not call this directly as it may fail"); }
@Override public HttpPrincipal getPrincipal() { return principal; }
void setPrincipal(HttpPrincipal principal) { this.principal = principal; }
@Override public HttpPrincipal getPrincipal() { return this.httpExchange.getPrincipal(); }
@Override public HttpPrincipal getPrincipal() { // TODO Auto-generated method stub return null; }
private HttpPrincipal validateUser(HttpExchange httpExchange, Map<String, String> challengeParameters) { String realm = challengeParameters.get("realm"); String username = challengeParameters.get("username"); if (realm == null || realm.length() == 0 || username == null || username.length() == 0) { return null; } String password = passwords.getProperty(username); if (password == null) { return null; } try { MessageDigest md5 = MessageDigest.getInstance("MD5"); md5.update(username.getBytes()); md5.update(COL); md5.update(realm.getBytes()); md5.update(COL); md5.update(password.getBytes()); byte[] ha1 = Utils.toHexBytes(md5.digest()); md5.update(httpExchange.getRequestMethod().getBytes()); md5.update(COL); md5.update(challengeParameters.get("uri").getBytes()); byte[] ha2 = Utils.toHexBytes(md5.digest()); md5.update(ha1); md5.update(COL); md5.update(challengeParameters.get("nonce").getBytes()); md5.update(COL); md5.update(ha2); byte[] expectedResponse = Utils.toHexBytes(md5.digest()); byte[] actualResponse = challengeParameters.get("response").getBytes(); if (MessageDigest.isEqual(expectedResponse, actualResponse)) { return new HttpPrincipal(username, realm); } } catch (NoSuchAlgorithmException e) { throw new IllegalStateException("No MD5? Should not be possible", e); } return null; }