@Override public Optional<Subject> map(final Optional<Object> wrapper) { try { LOG.info("logging in user as {}", username); LoginContext lc = new LoginContext("PropertyFile", callbacks -> { LOG.info("Callback Handler invoked "); Stream.of(callbacks).forEach(cb -> { if (cb instanceof NameCallback) { ((NameCallback) cb).setName(username); } else if (cb instanceof PasswordCallback) { ((PasswordCallback) cb).setPassword(password.toCharArray()); } }); }); lc.login(); return Optional.of(lc.getSubject()); } catch (LoginException e) { LOG.error("Authentication failed", e); return Optional.empty(); } }
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException { for (Callback c: callbacks) { if (c instanceof NameCallback) { ((NameCallback) c).setName(username); } else if (c instanceof PasswordCallback) { ((PasswordCallback) c).setPassword(password.toCharArray()); } else if (c instanceof KeyChoiceCallback) { KeyChoiceCallback kcc = (KeyChoiceCallback)c; try { int idx = kcc.lookupKey(key); kcc.setSelectedIndex(idx); } catch (IllegalArgumentException e) { throw new MissingCredsException(kcc); } } else { throw new UnsupportedCallbackException(c); } } }
@Override public boolean login() throws LoginException { final Callback[] callbacks = new Callback[] {new NameCallback("f"), new PasswordCallback("f", false)}; try { this.callbackHandler.handle(callbacks); } catch (final Exception e) { throw new LoginException(); } final String userName = ((NameCallback) callbacks[0]).getName(); final String password = new String(((PasswordCallback) callbacks[1]).getPassword()); if ("test".equals(userName) && "test".equals(password)) { return true; } throw new LoginException(); }
public boolean login() throws LoginException { final Callback[] callbacks = new Callback[] {new NameCallback("f"), new PasswordCallback("f", false)}; try { this.callbackHandler.handle(callbacks); } catch (final Exception e) { throw new LoginException(); } final String userName = ((NameCallback) callbacks[0]).getName(); final String password = new String(((PasswordCallback) callbacks[1]).getPassword()); if ("test".equals(userName) && "test".equals(password)) { return true; } throw new LoginException(); }
public void handle(Callback[] callbacks) throws UnsupportedCallbackException { boolean didSetName = false; boolean didSetPassword = false; for (Callback cb : callbacks) if (cb instanceof NameCallback) { ((NameCallback) cb).setName(u); didSetName = true; } else if (cb instanceof PasswordCallback) { ((PasswordCallback) cb).setPassword(p); didSetPassword = true; } else { throw new UnsupportedCallbackException(cb, "Unsupported Callback type: " + cb.getClass().getName()); } if (!didSetName) throw new IllegalStateException( "Supplied Callbacks does not include a NameCallback"); if (!didSetPassword) throw new IllegalStateException("Supplied Callbacks " + "does not include a PasswordCallback"); }
@Override public void handle(final Callback[] callbacks) throws UnsupportedCallbackException { Arrays.stream(callbacks).filter(callback -> { if (callback.getClass().equals(NameCallback.class)) { ((NameCallback) callback).setName(this.userName); return false; } if (callback.getClass().equals(PasswordCallback.class)) { ((PasswordCallback) callback).setPassword(this.password.toCharArray()); return false; } return true; }).findFirst().ifPresent(callback -> { throw new RuntimeException(new UnsupportedCallbackException(callback, "Unrecognized Callback")); }); }
public boolean login() throws LoginException { final Callback[] callbacks = new Callback[] {new NameCallback("f"), new PasswordCallback("f", false)}; try { this.callbackHandler.handle(callbacks); } catch (final Exception e) { throw new LoginException(); } final String userName = ((NameCallback) callbacks[0]).getName(); final String password = new String(((PasswordCallback) callbacks[1]).getPassword()); if (userName.equals("test") && password.equals("test")) { return true; } throw new LoginException(); }
@Override public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException { if (callbacks != null) { for (Callback callback : callbacks) { if (callback instanceof NameCallback) { ((NameCallback) callback).setName(username); } else if (callback instanceof PasswordCallback) { ((PasswordCallback) callback).setPassword(password); } else { throw new UnsupportedCallbackException(callback); } } clearCredentials(); } }
/** * Retrieve or display the information requested in the provided Callbacks. * * @param callbacks * an array of <code>Callback</code> objects provided by an * underlying security service which contains the information * requested to be retrieved or displayed. * * @throws IOException * if an input or output error occurs. * * @throws UnsupportedCallbackException * if the implementation of this method does not support one or * more of the Callbacks specified in the <code>callbacks</code> * parameter. */ @Override public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException { for (Callback callback : callbacks) { if (callback instanceof NameCallback) { ((NameCallback) callback).setName(wsProxyInfo .getUserCredentials().getUser()); } else if (callback instanceof PasswordCallback) { ((PasswordCallback) callback).setPassword(wsProxyInfo .getUserCredentials().getPassword().toCharArray()); } else { throw new UnsupportedCallbackException(callback, "Unrecognized Callback"); } } }
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException { System.out.println("Callback Handler - handle called"); for (int i = 0; i < callbacks.length; i++) { if (callbacks[i] instanceof NameCallback) { NameCallback nameCallback = (NameCallback) callbacks[i]; nameCallback.setName(name); } else if (callbacks[i] instanceof PasswordCallback) { PasswordCallback passwordCallback = (PasswordCallback) callbacks[i]; passwordCallback.setPassword(password.toCharArray()); } else { throw new UnsupportedCallbackException(callbacks[i], "The submitted Callback is unsupported"); } } }
static SaslClient createSaslClient(final String user, final String password) throws SaslException { return Sasl.createSaslClient(new String[]{"PLAIN"}, user, null, null, null, new CallbackHandler() { @Override public void handle(final Callback[] callbacks) throws IOException, UnsupportedCallbackException { for (final Callback callback : callbacks) { if (callback instanceof PasswordCallback) { ((PasswordCallback) callback).setPassword(password.toCharArray()); } else if (callback instanceof NameCallback) { ((NameCallback) callback).setName(user); } } } }); }
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException { for (Callback callback : callbacks) { if (callback instanceof NameCallback) { NameCallback ncb = (NameCallback) callback; ncb.setName(username); } else if (callback instanceof PasswordCallback) { PasswordCallback pwcb = (PasswordCallback) callback; pwcb.setPassword(password.toCharArray()); } else { throw new UnsupportedCallbackException( callback, "We got a " + callback.getClass().getCanonicalName() + ", but only NameCallback and PasswordCallback is supported"); } } }
/** {@inheritDoc} */ @Override public KeyStore.Entry engineGetEntry(final String alias, final ProtectionParameter protParam) { if (protParam instanceof KeyStore.PasswordProtection) { final PasswordCallback pwc = new CachePasswordCallback(((KeyStore.PasswordProtection)protParam).getPassword()); this.cryptoCard.setPasswordCallback(pwc); } if (!engineContainsAlias(alias)) { return null; } final PrivateKey key = (PrivateKey) engineGetKey( alias, null // Le pasamos null porque ya hemos establecido el PasswordCallback o el CallbackHander antes ); return new PrivateKeyEntry(key, engineGetCertificateChain(alias)); }
/** * Retrieve the information requested in the provided Callbacks. This * implementation only recognizes <code>NameCallback</code> and * <code>PasswordCallback</code> instances. * * @param callbacks The set of callbacks to be processed * * @exception IOException if an input/output error occurs * @exception UnsupportedCallbackException if the login method requests * an unsupported callback type */ public void handle(Callback callbacks[]) throws IOException, UnsupportedCallbackException { for (int i = 0; i < callbacks.length; i++) { if (callbacks[i] instanceof NameCallback) { if (realm.getDebug() >= 3) realm.log("Returning username " + username); ((NameCallback) callbacks[i]).setName(username); } else if (callbacks[i] instanceof PasswordCallback) { if (realm.getDebug() >= 3) realm.log("Returning password " + password); ((PasswordCallback) callbacks[i]).setPassword (password.toCharArray()); } else { throw new UnsupportedCallbackException(callbacks[i]); } } }
@Override public ExchangeSession openSession(ExchangeLogin login) { ApiKeyCallback apiKey = new ApiKeyCallback(); ApiSecretCallback apiSecret = new ApiSecretCallback(); PasswordCallback password = new PasswordCallback("GDAX passphrase", false); Callback[] callbacks = new Callback[]{ apiKey, apiSecret, password }; CallbackHandler callbackHandler = login.getCallbackHandler(); try { callbackHandler.handle(callbacks); } catch (IOException | UnsupportedCallbackException e) { throw new RuntimeException(e); } String apiKeyTxt = apiKey.getApiKey(); char[] passphrase = password.getPassword(); SecretKeySpec keySpec = new SecretKeySpec(apiSecret.getSecretData(), "HmacSHA256"); return new GdaxExchangeSession(this, apiKeyTxt, keySpec, passphrase); }
@Override public CallbackHandler getCallbackHandler() { return callbacks -> { for (Callback cb : callbacks) { if (cb instanceof ApiKeyCallback) { ((ApiKeyCallback) cb).setApiKey(apiKey); } else if (cb instanceof ApiSecretCallback) { ((ApiSecretCallback) cb).setSecretData(apiSecret); } else if (cb instanceof PasswordCallback) { ((PasswordCallback) cb).setPassword(passphrase); } else { throw new UnsupportedCallbackException(cb); } } }; }
/** * Handles the callbacks; only supports {@link NameCallback} and {@link PasswordCallback}. * @throws UnsupportedCallbackException if an unsupported callback is encountered */ @Override public void handle(final Callback[] callbacks) throws UnsupportedCallbackException { for (final Callback callback : callbacks) { if (callback instanceof NameCallback) { ((NameCallback) callback).setName(username); } else if (callback instanceof PasswordCallback) { ((PasswordCallback) callback) .setPassword(password.toCharArray()); } else { throw new UnsupportedCallbackException (callback, "Unrecognized Callback"); } } }
@Override public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException { for (Callback callback : callbacks) { if (callback instanceof NameCallback) { NameCallback nameCallback = (NameCallback) callback; nameCallback.setName(nameCallback.getDefaultName()); } else if (callback instanceof PasswordCallback) { PasswordCallback passwordCallback = (PasswordCallback) callback; passwordCallback.setPassword(TestJaasConfig.PASSWORD.toCharArray()); } else if (callback instanceof RealmCallback) { RealmCallback realmCallback = (RealmCallback) callback; realmCallback.setText(realmCallback.getDefaultText()); } else if (callback instanceof AuthorizeCallback) { AuthorizeCallback authCallback = (AuthorizeCallback) callback; if (TestJaasConfig.USERNAME.equals(authCallback.getAuthenticationID())) { authCallback.setAuthorized(true); authCallback.setAuthorizedID(authCallback.getAuthenticationID()); } } } }
@Test public void testSaslClientCallbackHandler() throws UnsupportedCallbackException { final Token<? extends TokenIdentifier> token = createTokenMock(); when(token.getIdentifier()).thenReturn(DEFAULT_USER_NAME.getBytes()); when(token.getPassword()).thenReturn(DEFAULT_USER_PASSWORD.getBytes()); final NameCallback nameCallback = mock(NameCallback.class); final PasswordCallback passwordCallback = mock(PasswordCallback.class); final RealmCallback realmCallback = mock(RealmCallback.class); final RealmChoiceCallback realmChoiceCallback = mock(RealmChoiceCallback.class); Callback[] callbackArray = {nameCallback, passwordCallback, realmCallback, realmChoiceCallback}; final SaslClientCallbackHandler saslClCallbackHandler = new SaslClientCallbackHandler(token); saslClCallbackHandler.handle(callbackArray); verify(nameCallback).setName(anyString()); verify(realmCallback).setText(anyString()); verify(passwordCallback).setPassword(any(char[].class)); }
/** Construye una APDU ISO 7816-4 de verificación de PIN (CHV, <i>Card Holder Verification</i>). * @param cla Clase (CLA) de la APDU. * @param pinPc Pin de la tarjeta inteligente. */ public VerifyApduCommand(final byte cla, final PasswordCallback pinPc) { super( cla, // CLA VerifyApduCommand.INS_VERIFY, // INS (byte)0x00, // P1 (byte)0x81, // P2 new byte[] { // Data (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, // Siempre de 0x10h (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, // de tamano (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 }, null // Le ); if (pinPc == null) { throw new IllegalArgumentException( "No se puede verificar el titular con un PasswordCallback nulo" //$NON-NLS-1$ ); } this.pwc = pinPc; }
@Override public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException { for (Callback callback : callbacks) { if (callback instanceof NameCallback) { NameCallback nc = (NameCallback) callback; nc.setName(userName); } else if (callback instanceof PasswordCallback) { PasswordCallback pc = (PasswordCallback) callback; pc.setPassword(password); } else { throw new UnsupportedCallbackException(callback, "Unrecognized Callback"); } } }
private static byte[] charArrayToByteArray(final PasswordCallback pinPc) { if (pinPc == null) { throw new IllegalArgumentException( "El PasswordCallback del PIN no puede ser nulo" //$NON-NLS-1$ ); } final char[] in = pinPc.getPassword(); if (in == null) { return new byte[0]; } final byte[] ret = new byte[in.length]; for (int i=0; i<in.length; i++) { ret[i] = (byte) in[i]; } return ret; }
public void handle(Callback[] callbacks) throws UnsupportedCallbackException, IOException { for (int i=0; i<callbacks.length; i++) { Callback callBack = callbacks[i]; if (callBack instanceof NameCallback) { getAnswer(); ((NameCallback)callBack).setName(username); } else if (callBack instanceof PasswordCallback) { getAnswer(); ((PasswordCallback)callBack).setPassword(password); if (password != null) Arrays.fill(password, ' '); } else { throw new UnsupportedCallbackException(callBack, "Call back not supported"); } } }
@Override public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException { System.out.println("TestCallbackHandler will get resolved through" + " auth.login.defaultCallbackHandler property."); for (Callback callback : callbacks) { if (callback instanceof NameCallback) { ((NameCallback) callback).setName(USER_NAME); } else if (callback instanceof PasswordCallback) { ((PasswordCallback) callback).setPassword( PASSWORD.toCharArray()); } else { throw new UnsupportedCallbackException(callback); } } }
@Override public void verifyPin(final PasswordCallback pinPc) throws ApduConnectionException, PinException { if (pinPc == null) { throw new BadPinException("No se ha establecido un PasswordCallback"); //$NON-NLS-1$ } final CommandApdu chv = new VerifyApduCommand(CLA, pinPc); final ResponseApdu verifyResponse = sendArbitraryApdu(chv); if (!verifyResponse.isOk()) { if (verifyResponse.getStatusWord().getMsb() == ERROR_PIN_SW1) { throw new BadPinException(verifyResponse.getStatusWord().getLsb() - (byte) 0xC0); } throw new ApduConnectionException( "Error en el envio de la verificacion de PIN con respuesta: " + verifyResponse.getStatusWord() //$NON-NLS-1$ ); } }
@Override public boolean login() throws LoginException { if (debug) { logger.debug(this.getClass().getName() + " login called."); } if (Constants.FEDORA_HOME == null || "".equals(Constants.FEDORA_HOME.trim())) { logger.error("FEDORA_HOME constant is not set"); return false; } final NameCallback nc = new NameCallback("username"); final PasswordCallback pc = new PasswordCallback("password", false); final Callback[] callbacks = new Callback[] { nc, pc }; try { handler.handle(callbacks); } catch (IOException ioe) { ioe.printStackTrace(); throw new LoginException("IOException occured: " + ioe.getMessage()); } catch (UnsupportedCallbackException ucbe) { ucbe.printStackTrace(); throw new LoginException("UnsupportedCallbackException encountered: " + ucbe.getMessage()); } // Grab the username and password from the callbacks. final String username = nc.getName(); final String password = new String(pc.getPassword()); successLogin = authenticate(username, password); return successLogin; }
@Override public void handle(final Callback[] callbacks) throws UnsupportedCallbackException { for (final Callback callback : callbacks) { if (callback.getClass().equals(NameCallback.class)) { ((NameCallback) callback).setName(this.userName); } else if (callback.getClass().equals(PasswordCallback.class)) { ((PasswordCallback) callback).setPassword(this.password .toCharArray()); } else { throw new UnsupportedCallbackException(callback, "Unrecognized Callback"); } } }
public void handle(final Callback[] callbacks) throws UnsupportedCallbackException { for (final Callback callback : callbacks) { if (callback.getClass().equals(NameCallback.class)) { ((NameCallback) callback).setName(this.userName); } else if (callback.getClass().equals(PasswordCallback.class)) { ((PasswordCallback) callback).setPassword(this.password .toCharArray()); } else { throw new UnsupportedCallbackException(callback, "Unrecognized Callback"); } } }
@Override public boolean login() throws LoginException { NameCallback userNameCallback = new NameCallback("userName"); PasswordCallback passwordCallback = new PasswordCallback("password", false); Callback[] callbacks = { userNameCallback, passwordCallback }; try { callbackHandler.handle(callbacks); } catch (IOException | UnsupportedCallbackException e) { throw new BrokerAuthenticationException("Error while handling callback ", e); } String userName = userNameCallback.getName(); char[] password = passwordCallback.getPassword(); return userName.equals("user") && Arrays.equals(password, new char[] { 'p', 'a', 's', 's' }); }
public void handle(Callback[] callbacks) throws UnsupportedCallbackException { for (Callback callback : callbacks) { if (callback instanceof NameCallback) { handleNameCallback((NameCallback) callback); } else if (callback instanceof PasswordCallback) { handlePasswordCallback((PasswordCallback) callback); } else if (callback instanceof RealmCallback) { handleRealmCallback((RealmCallback) callback); } else if (callback instanceof AuthorizeCallback) { handleAuthorizeCallback((AuthorizeCallback) callback); } } }
private void handlePasswordCallback(PasswordCallback pc) { if ("super".equals(this.userName) && System.getProperty(SYSPROP_SUPER_PASSWORD) != null) { // superuser: use Java system property for password, if available. pc.setPassword(System.getProperty(SYSPROP_SUPER_PASSWORD).toCharArray()); } else if (credentials.containsKey(userName) ) { pc.setPassword(credentials.get(userName).toCharArray()); } else { LOG.warn("No password found for user: " + userName); } }
public void handle( Callback[] callbacks ) throws IOException, UnsupportedCallbackException { for (int i = 0; i < callbacks.length; i++) { if (callbacks[i] instanceof NameCallback) { NameCallback nc = (NameCallback) callbacks[i]; nc.setName(username); } else if (callbacks[i] instanceof PasswordCallback) { PasswordCallback pc = (PasswordCallback) callbacks[i]; pc.setPassword(password); } } }
public void initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState, Map<String, ?> options) { try { NameCallback nameCallback = new NameCallback("prompt"); PasswordCallback passwordCallback = new PasswordCallback("prompt", false); callbackHandler.handle(new Callback[] { nameCallback, passwordCallback }); } catch (Exception e) { throw new RuntimeException(e); } }
private static CallbackHandler getUsernamePasswordHandler(final String username, final String password) { return callbacks -> { for (Callback callback : callbacks) { if (callback instanceof NameCallback) { final NameCallback nameCallback = (NameCallback) callback; nameCallback.setName(username); } else if (callback instanceof PasswordCallback) { final PasswordCallback passCallback = (PasswordCallback) callback; passCallback.setPassword(password.toCharArray()); } else { System.err.println("Unsupported Callback: " + callback.getClass().getName()); } } }; }
/** Construye una APDU ISO 7816-4 de verificación de PIN (CHV, <i>Card Holder Verification</i>). * @param cla Clase (CLA) de la APDU. * @param pinPc Pin de la tarjeta inteligente. */ public CeresVerifyApduCommand(final byte cla, final PasswordCallback pinPc) { super( cla, // CLA INS_VERIFY, // INS (byte)0x00, // P1 (byte)0x00, // P2 charArrayToByteArray(pinPc), // Data null // Le ); }
/** * Called by login() to acquire the username and password strings for authentication. This method does no validation * of either. * * @return String[], [0] = username, [1] = password */ private String[] getUsernameAndPassword() throws LoginException { if (callbackHandler == null) { throw new LoginException("No CallbackHandler available to collect authentication information"); } NameCallback nc = new NameCallback("User name: ", "guest"); PasswordCallback pc = new PasswordCallback("Password: ", false); Callback[] callbacks = { nc, pc }; String username = null; String password = null; try { callbackHandler.handle(callbacks); username = nc.getName(); char[] tmpPassword = pc.getPassword(); if (tmpPassword != null) { credential = new char[tmpPassword.length]; System.arraycopy(tmpPassword, 0, credential, 0, tmpPassword.length); pc.clearPassword(); password = new String(credential); } } catch (IOException ioe) { throw new LoginException(ioe.toString()); } catch (UnsupportedCallbackException uce) { throw new LoginException("CallbackHandler does not support: " + uce.getCallback()); } return new String[] { username, password }; }