/** * Verifies user credentials. * * @param oldPassword the password to verify. * @param participantId the participantId of the user. * @throws LoginException if the user provided incorrect password. */ private void verifyCredentials(String password, ParticipantId participantId) throws LoginException { MultiMap<String> parameters = new MultiMap<String>(); parameters.putAllValues(ImmutableMap.of("password", password, "address", participantId.getAddress())); CallbackHandler callbackHandler = new HttpRequestBasedCallbackHandler(parameters); LoginContext context = new LoginContext("Wave", new Subject(), callbackHandler, configuration); // If authentication fails, login() will throw a LoginException. context.login(); }
@SuppressWarnings("unchecked") private LoginContext login(BufferedReader body) throws IOException, LoginException { try { Subject subject = new Subject(); String parametersLine = body.readLine(); // Throws UnsupportedEncodingException. byte[] utf8Bytes = parametersLine.getBytes("UTF-8"); CharsetDecoder utf8Decoder = Charset.forName("UTF-8").newDecoder(); utf8Decoder.onMalformedInput(CodingErrorAction.IGNORE); utf8Decoder.onUnmappableCharacter(CodingErrorAction.IGNORE); // Throws CharacterCodingException. CharBuffer parsed = utf8Decoder.decode(ByteBuffer.wrap(utf8Bytes)); parametersLine = parsed.toString(); MultiMap<String> parameters = new UrlEncoded(parametersLine); CallbackHandler callbackHandler = new HttpRequestBasedCallbackHandler(parameters); LoginContext context = new LoginContext("Wave", subject, callbackHandler, configuration); // If authentication fails, login() will throw a LoginException. context.login(); return context; } catch (CharacterCodingException cce) { throw new LoginException("Character coding exception (not utf-8): " + cce.getLocalizedMessage()); } catch (UnsupportedEncodingException uee) { throw new LoginException("ad character encoding specification: " + uee.getLocalizedMessage()); } }
public void testBindsUsernameAndPassword() throws IOException, UnsupportedCallbackException { MultiMap<String> args = new MultiMap<String>(); args.add("address", "seph@example.com"); args.add("password", "internet"); CallbackHandler handler = new HttpRequestBasedCallbackHandler(args); Callback[] callbacks = new Callback[] {new NameCallback("ignored"), new PasswordCallback("ignored", false),}; handler.handle(callbacks); assertEquals("seph@example.com", ((NameCallback) callbacks[0]).getName()); assertEquals("internet", new String(((PasswordCallback) callbacks[1]).getPassword())); }
public void testBindsUsernameAndPassword() throws IOException, UnsupportedCallbackException { MultiMap<String> args = new MultiMap<String>(); args.add("address", "seph@example.com"); args.add("password", "internet"); CallbackHandler handler = new HttpRequestBasedCallbackHandler(args); Callback[] callbacks = new Callback[] {new NameCallback("ignored"), new PasswordCallback("ignored", false),}; handler.handle(callbacks); assertEquals("seph@example.com", ((NameCallback) callbacks[0]).getName()); assertEquals("internet", new String(((PasswordCallback) callbacks[1]).getPassword())); }
public void testCallbackThrowsHandlingUnsupportedCallback() throws IOException { CallbackHandler handler = new HttpRequestBasedCallbackHandler(new MultiMap<String>()); try { handler.handle(new Callback[] {new Callback() {}}); fail("Should have thrown due to unsupported callback"); } catch (UnsupportedCallbackException e) { // Pass. } } }
public void testCallbackThrowsHandlingUnsupportedCallback() throws IOException { CallbackHandler handler = new HttpRequestBasedCallbackHandler(new MultiMap<String>()); try { handler.handle(new Callback[] {new Callback() {}}); fail("Should have thrown due to unsupported callback"); } catch (UnsupportedCallbackException e) { // Pass. } } }