Java Code Examples for javax.security.sasl.RealmCallback

The following examples show how to use javax.security.sasl.RealmCallback. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: Bats   Source File: DefaultCallbackHandler.java    License: Apache License 2.0 6 votes vote down vote up
protected void processCallback(Callback callback) throws IOException, UnsupportedCallbackException
{
  if (callback instanceof NameCallback) {
    NameCallback namecb = (NameCallback)callback;
    namecb.setName(context.getValue(SecurityContext.USER_NAME));
  } else if (callback instanceof PasswordCallback) {
    PasswordCallback passcb = (PasswordCallback)callback;
    passcb.setPassword(context.getValue(SecurityContext.PASSWORD));
  } else if (callback instanceof RealmCallback) {
    RealmCallback realmcb = (RealmCallback)callback;
    realmcb.setText(context.getValue(SecurityContext.REALM));
  } else if (callback instanceof TextOutputCallback) {
    TextOutputCallback textcb = (TextOutputCallback)callback;
    if (textcb.getMessageType() == TextOutputCallback.INFORMATION) {
      logger.info(textcb.getMessage());
    } else if (textcb.getMessageType() == TextOutputCallback.WARNING) {
      logger.warn(textcb.getMessage());
    } else if (textcb.getMessageType() == TextOutputCallback.ERROR) {
      logger.error(textcb.getMessage());
    } else {
      logger.debug("Auth message type {}, message {}", textcb.getMessageType(), textcb.getMessage());
    }
  } else {
    throw new UnsupportedCallbackException(callback);
  }
}
 
Example 2
@Override
public void handle(Callback[] callbacks)
{
    for (Callback callback : callbacks) {
        if (callback instanceof NameCallback) {
            ((NameCallback) callback).setName(username);
        }
        if (callback instanceof PasswordCallback) {
            ((PasswordCallback) callback).setPassword(password.toCharArray());
        }
        if (callback instanceof RealmCallback) {
            RealmCallback realmCallback = (RealmCallback) callback;
            realmCallback.setText(realmCallback.getDefaultText());
        }
    }
}
 
Example 3
Source Project: netbeans   Source File: Authentication.java    License: Apache License 2.0 6 votes vote down vote up
private CallbackHandler createCallBackHandler(){
    return new CallbackHandler() {

        @Override
        public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
            for (Callback current : callbacks) {
                if (current instanceof NameCallback) {
                    NameCallback nameCallback = (NameCallback) current;
                    nameCallback.setName(username);
                } else if (current instanceof PasswordCallback) {
                    PasswordCallback pwdCallback = (PasswordCallback) current;
                    pwdCallback.setPassword(password);
                } else if (current instanceof RealmCallback) {
                    RealmCallback realmCallback = (RealmCallback) current;
                    realmCallback.setText(realmCallback.getDefaultText());
                } else {
                    throw new UnsupportedCallbackException(current);
                }
            }
        }
    };
}
 
Example 4
Source Project: registry   Source File: AbstractLogin.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void handle(Callback[] callbacks) throws UnsupportedCallbackException {
    for (Callback callback : callbacks) {
        if (callback instanceof NameCallback) {
            NameCallback nc = (NameCallback) callback;
            nc.setName(nc.getDefaultName());
        } else if (callback instanceof PasswordCallback) {
            String errorMessage = "Could not login: the client is being asked for a password, but this " +
                    " module does not currently support obtaining a password from the user.";
            throw new UnsupportedCallbackException(callback, errorMessage);
        } else if (callback instanceof RealmCallback) {
            RealmCallback rc = (RealmCallback) callback;
            rc.setText(rc.getDefaultText());
        } else {
            throw new UnsupportedCallbackException(callback, "Unrecognized Login callback");
        }
    }
}
 
Example 5
Source Project: ARCHIVE-wildfly-swarm   Source File: AuthCallbackHandler.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (Callback current : callbacks) {
        if (current instanceof NameCallback) {
            NameCallback ncb = (NameCallback) current;
            ncb.setName(this.userName);
        } else if (current instanceof PasswordCallback) {
            PasswordCallback pcb = (PasswordCallback) current;
            pcb.setPassword(this.password.toCharArray());
        } else if (current instanceof RealmCallback) {
            RealmCallback rcb = (RealmCallback) current;
            rcb.setText(rcb.getDefaultText());
        } else {
            throw new UnsupportedCallbackException(current);
        }
    }

}
 
Example 6
Source Project: hbase   Source File: TestHBaseSaslRpcClient.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testDigestSaslClientCallbackHandler() throws UnsupportedCallbackException {
  final Token<? extends TokenIdentifier> token = createTokenMock();
  when(token.getIdentifier()).thenReturn(Bytes.toBytes(DEFAULT_USER_NAME));
  when(token.getPassword()).thenReturn(Bytes.toBytes(DEFAULT_USER_PASSWORD));

  final NameCallback nameCallback = mock(NameCallback.class);
  final PasswordCallback passwordCallback = mock(PasswordCallback.class);
  final RealmCallback realmCallback = mock(RealmCallback.class);

  // We can provide a realmCallback, but HBase presently does nothing with it.
  Callback[] callbackArray = {nameCallback, passwordCallback, realmCallback};
  final DigestSaslClientCallbackHandler saslClCallbackHandler =
      new DigestSaslClientCallbackHandler(token);
  saslClCallbackHandler.handle(callbackArray);
  verify(nameCallback).setName(anyString());
  verify(passwordCallback).setPassword(any());
}
 
Example 7
Source Project: glowroot   Source File: SaslCallbackHandler.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (Callback callback : callbacks) {
        if (callback instanceof AuthorizeCallback) {
            AuthorizeCallback authorizeCallback = (AuthorizeCallback) callback;
            String authenticationId = authorizeCallback.getAuthenticationID();
            String authorizationId = authorizeCallback.getAuthorizationID();
            authorizeCallback.setAuthorized(authenticationId.equals(authorizationId));
        } else if (callback instanceof NameCallback) {
            ((NameCallback) callback).setName("glowroot");
        } else if (callback instanceof PasswordCallback) {
            ((PasswordCallback) callback).setPassword(password);
        } else if (callback instanceof RealmCallback) {
            ((RealmCallback) callback).setText("glowroot");
        }
    }
}
 
Example 8
private CallbackHandler serverCallbackHandler(String username, String realm, String password) {
    return callbacks -> {
        for (Callback callback : callbacks) {
            if (callback instanceof NameCallback) {
                Assert.assertEquals(username, ((NameCallback) callback).getDefaultName());
            } else if (callback instanceof RealmCallback) {
                Assert.assertEquals(realm, ((RealmCallback) callback).getDefaultText());
            } else if (callback instanceof PasswordCallback) {
                ((PasswordCallback) callback).setPassword(password.toCharArray());
            } else if (callback instanceof AuthorizeCallback) {
                ((AuthorizeCallback) callback).setAuthorized(((AuthorizeCallback) callback).getAuthorizationID().equals(((AuthorizeCallback) callback).getAuthenticationID()));
            } else {
                throw new UnsupportedCallbackException(callback);
            }
        }
    };
}
 
Example 9
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (Callback current : callbacks) {
        if (current instanceof NameCallback) {
            NameCallback ncb = (NameCallback) current;
            ncb.setName(username);
        } else if (current instanceof PasswordCallback) {
            PasswordCallback pcb = (PasswordCallback) current;
            pcb.setPassword(password.toCharArray());
        } else if (current instanceof RealmCallback) {
            RealmCallback rcb = (RealmCallback) current;
            rcb.setText(rcb.getDefaultText());
        } else {
            throw new UnsupportedCallbackException(current);
        }
    }
}
 
Example 10
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (Callback current : callbacks) {
        if (current instanceof NameCallback) {
            NameCallback ncb = (NameCallback) current;
            ncb.setName(username);
        } else if (current instanceof PasswordCallback) {
            PasswordCallback pcb = (PasswordCallback) current;
            pcb.setPassword(password.toCharArray());
        } else if (current instanceof RealmCallback) {
            RealmCallback rcb = (RealmCallback) current;
            rcb.setText(rcb.getDefaultText());
        } else {
            throw new UnsupportedCallbackException(current);
        }
    }
}
 
Example 11
@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (Callback current : callbacks) {
        if (current instanceof NameCallback) {
            NameCallback ncb = (NameCallback) current;
            ncb.setName(userName);
            System.out.println("set user " + userName);
        } else if (current instanceof PasswordCallback) {
            PasswordCallback pcb = (PasswordCallback) current;
            pcb.setPassword(password.toCharArray());
            System.out.println("set password " + password);
        } else if (current instanceof RealmCallback) {
            RealmCallback rcb = (RealmCallback) current;
            rcb.setText(rcb.getDefaultText());
        } else {
            throw new UnsupportedCallbackException(current);
        }
    }
}
 
Example 12
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (Callback current : callbacks) {
        if (current instanceof NameCallback) {
            NameCallback ncb = (NameCallback) current;
            ncb.setName(username);
        } else if (current instanceof PasswordCallback) {
            PasswordCallback pcb = (PasswordCallback) current;
            pcb.setPassword(password.toCharArray());
        } else if (current instanceof RealmCallback) {
            RealmCallback rcb = (RealmCallback) current;
            rcb.setText(realm != null ? realm : rcb.getDefaultText());
        } else {
            throw new UnsupportedCallbackException(current);
        }
    }
}
 
Example 13
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (Callback current : callbacks) {
        if (current instanceof RealmCallback) {
            RealmCallback rcb = (RealmCallback) current;
            String defaultText = rcb.getDefaultText();
            rcb.setText(defaultText); // For now just use the realm suggested.
        } else if (current instanceof RealmChoiceCallback) {
            throw DomainManagementLogger.ROOT_LOGGER.realmNotSupported(current);
        } else if (current instanceof OptionalNameCallback) {
            // Do nothing as we don't want to set a name for local authentication.
        } else if (current instanceof NameCallback) {
            NameCallback ncb = (NameCallback) current;
            ncb.setName(userName);
        } else if (current instanceof PasswordCallback) {
            PasswordCallback pcb = (PasswordCallback) current;
            pcb.setPassword(password);
        } else {
            throw new UnsupportedCallbackException(current);
        }
    }
}
 
Example 14
@Test
public void testVerifyGoodPasswordReferral() throws Exception {
    AuthorizingCallbackHandler cbh = securityRealm.getAuthorizingCallbackHandler(AuthMechanism.PLAIN);

    NameCallback ncb = new NameCallback("Username", USER_THREE_REFERRAL);
    RealmCallback rcb = new RealmCallback("Realm", TEST_REALM);
    EvidenceVerifyCallback evc = new EvidenceVerifyCallback(new PasswordGuessEvidence(USER_THREE_PASSWORD.toCharArray()));

    cbh.handle(new Callback[] { ncb, rcb, evc });

    assertTrue("Password Verified", evc.isVerified());

    Collection<Principal> principals = Collections.emptyList();
    SubjectUserInfo userInfo = cbh.createSubjectUserInfo(principals);
    principals = userInfo.getPrincipals();
    assertEquals("Principal Count", 1, principals.size());
    RealmUser user = (RealmUser) principals.iterator().next();
    assertEquals("Expected Username", "user_three", user.getName());
}
 
Example 15
@Test
public void testVerifyGoodPassword_UserTwo() throws Exception {
    /*
     * Essentially a duplicate of the previous test but we want to be sure this works as we later
     * test that this user can be excluded using an advanced filter.
     */
    AuthorizingCallbackHandler cbh = securityRealm.getAuthorizingCallbackHandler(AuthMechanism.PLAIN);

    NameCallback ncb = new NameCallback("Username", USER_TWO);
    RealmCallback rcb = new RealmCallback("Realm", TEST_REALM);
    EvidenceVerifyCallback evc = new EvidenceVerifyCallback(new PasswordGuessEvidence(USER_TWO_PASSWORD.toCharArray()));

    cbh.handle(new Callback[] { ncb, rcb, evc });

    assertTrue("Password Verified", evc.isVerified());
}
 
Example 16
@Override
public void handle(Callback[] callbacks) throws IOException,
        UnsupportedCallbackException {
    for (Callback callback : callbacks) {
        if (callback instanceof NameCallback) {
            System.out.println("NameCallback");
            ((NameCallback) callback).setName(userId);
        } else if (callback instanceof PasswordCallback) {
            System.out.println("PasswordCallback");
            ((PasswordCallback) callback).setPassword(passwd);
        } else if (callback instanceof RealmCallback) {
            System.out.println("RealmCallback");
            ((RealmCallback) callback).setText(realm);
        } else if (callback instanceof RealmChoiceCallback) {
            System.out.println("RealmChoiceCallback");
            RealmChoiceCallback choice = (RealmChoiceCallback) callback;
            if (realm == null) {
                choice.setSelectedIndex(choice.getDefaultChoice());
            } else {
                String[] choices = choice.getChoices();
                for (int j = 0; j < choices.length; j++) {
                    if (realm.equals(choices[j])) {
                        choice.setSelectedIndex(j);
                        break;
                    }
                }
            }
        } else if (callback instanceof AuthorizeCallback) {
            System.out.println("AuthorizeCallback");
            ((AuthorizeCallback) callback).setAuthorized(true);
            if (authId == null || authId.trim().length() == 0) {
                authId = userId;
            }
            ((AuthorizeCallback) callback).setAuthorizedID(authId);
        } else {
            throw new UnsupportedCallbackException(callback);
        }
    }
}
 
Example 17
@Override
public void handle(Callback[] callbacks) throws IOException,
        UnsupportedCallbackException {
    for (Callback callback : callbacks) {
        if (callback instanceof NameCallback) {
            System.out.println("NameCallback");
            ((NameCallback) callback).setName(userId);
        } else if (callback instanceof PasswordCallback) {
            System.out.println("PasswordCallback");
            ((PasswordCallback) callback).setPassword(passwd);
        } else if (callback instanceof RealmCallback) {
            System.out.println("RealmCallback");
            ((RealmCallback) callback).setText(realm);
        } else if (callback instanceof RealmChoiceCallback) {
            System.out.println("RealmChoiceCallback");
            RealmChoiceCallback choice = (RealmChoiceCallback) callback;
            if (realm == null) {
                choice.setSelectedIndex(choice.getDefaultChoice());
            } else {
                String[] choices = choice.getChoices();
                for (int j = 0; j < choices.length; j++) {
                    if (realm.equals(choices[j])) {
                        choice.setSelectedIndex(j);
                        break;
                    }
                }
            }
        } else if (callback instanceof AuthorizeCallback) {
            System.out.println("AuthorizeCallback");
            ((AuthorizeCallback) callback).setAuthorized(true);
            if (authId == null || authId.trim().length() == 0) {
                authId = userId;
            }
            ((AuthorizeCallback) callback).setAuthorizedID(authId);
        } else {
            throw new UnsupportedCallbackException(callback);
        }
    }
}
 
Example 18
@Override
public void handle(Callback[] callbacks) throws IOException,
        UnsupportedCallbackException {
    for (Callback callback : callbacks) {
        if (callback instanceof NameCallback) {
            System.out.println("NameCallback");
            ((NameCallback) callback).setName(userId);
        } else if (callback instanceof PasswordCallback) {
            System.out.println("PasswordCallback");
            ((PasswordCallback) callback).setPassword(passwd);
        } else if (callback instanceof RealmCallback) {
            System.out.println("RealmCallback");
            ((RealmCallback) callback).setText(realm);
        } else if (callback instanceof RealmChoiceCallback) {
            System.out.println("RealmChoiceCallback");
            RealmChoiceCallback choice = (RealmChoiceCallback) callback;
            if (realm == null) {
                choice.setSelectedIndex(choice.getDefaultChoice());
            } else {
                String[] choices = choice.getChoices();
                for (int j = 0; j < choices.length; j++) {
                    if (realm.equals(choices[j])) {
                        choice.setSelectedIndex(j);
                        break;
                    }
                }
            }
        } else if (callback instanceof AuthorizeCallback) {
            System.out.println("AuthorizeCallback");
            ((AuthorizeCallback) callback).setAuthorized(true);
            if (authId == null || authId.trim().length() == 0) {
                authId = userId;
            }
            ((AuthorizeCallback) callback).setAuthorizedID(authId);
        } else {
            throw new UnsupportedCallbackException(callback);
        }
    }
}
 
Example 19
@Override
public void handle(Callback[] callbacks) throws IOException,
        UnsupportedCallbackException {
    for (Callback callback : callbacks) {
        if (callback instanceof NameCallback) {
            System.out.println("NameCallback");
            ((NameCallback) callback).setName(userId);
        } else if (callback instanceof PasswordCallback) {
            System.out.println("PasswordCallback");
            ((PasswordCallback) callback).setPassword(passwd);
        } else if (callback instanceof RealmCallback) {
            System.out.println("RealmCallback");
            ((RealmCallback) callback).setText(realm);
        } else if (callback instanceof RealmChoiceCallback) {
            System.out.println("RealmChoiceCallback");
            RealmChoiceCallback choice = (RealmChoiceCallback) callback;
            if (realm == null) {
                choice.setSelectedIndex(choice.getDefaultChoice());
            } else {
                String[] choices = choice.getChoices();
                for (int j = 0; j < choices.length; j++) {
                    if (realm.equals(choices[j])) {
                        choice.setSelectedIndex(j);
                        break;
                    }
                }
            }
        } else if (callback instanceof AuthorizeCallback) {
            System.out.println("AuthorizeCallback");
            ((AuthorizeCallback) callback).setAuthorized(true);
            if (authId == null || authId.trim().length() == 0) {
                authId = userId;
            }
            ((AuthorizeCallback) callback).setAuthorizedID(authId);
        } else {
            throw new UnsupportedCallbackException(callback);
        }
    }
}
 
Example 20
@Override
public void handle(Callback[] callbacks) throws IOException,
        UnsupportedCallbackException {
    for (Callback callback : callbacks) {
        if (callback instanceof NameCallback) {
            System.out.println("NameCallback");
            ((NameCallback) callback).setName(userId);
        } else if (callback instanceof PasswordCallback) {
            System.out.println("PasswordCallback");
            ((PasswordCallback) callback).setPassword(passwd);
        } else if (callback instanceof RealmCallback) {
            System.out.println("RealmCallback");
            ((RealmCallback) callback).setText(realm);
        } else if (callback instanceof RealmChoiceCallback) {
            System.out.println("RealmChoiceCallback");
            RealmChoiceCallback choice = (RealmChoiceCallback) callback;
            if (realm == null) {
                choice.setSelectedIndex(choice.getDefaultChoice());
            } else {
                String[] choices = choice.getChoices();
                for (int j = 0; j < choices.length; j++) {
                    if (realm.equals(choices[j])) {
                        choice.setSelectedIndex(j);
                        break;
                    }
                }
            }
        } else if (callback instanceof AuthorizeCallback) {
            System.out.println("AuthorizeCallback");
            ((AuthorizeCallback) callback).setAuthorized(true);
            if (authId == null || authId.trim().length() == 0) {
                authId = userId;
            }
            ((AuthorizeCallback) callback).setAuthorizedID(authId);
        } else {
            throw new UnsupportedCallbackException(callback);
        }
    }
}
 
Example 21
Source Project: jdk8u-jdk   Source File: ClientServerTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public void handle(Callback[] callbacks) throws IOException,
        UnsupportedCallbackException {
    for (Callback callback : callbacks) {
        if (callback instanceof NameCallback) {
            System.out.println("NameCallback");
            ((NameCallback) callback).setName(userId);
        } else if (callback instanceof PasswordCallback) {
            System.out.println("PasswordCallback");
            ((PasswordCallback) callback).setPassword(passwd);
        } else if (callback instanceof RealmCallback) {
            System.out.println("RealmCallback");
            ((RealmCallback) callback).setText(realm);
        } else if (callback instanceof RealmChoiceCallback) {
            System.out.println("RealmChoiceCallback");
            RealmChoiceCallback choice = (RealmChoiceCallback) callback;
            if (realm == null) {
                choice.setSelectedIndex(choice.getDefaultChoice());
            } else {
                String[] choices = choice.getChoices();
                for (int j = 0; j < choices.length; j++) {
                    if (realm.equals(choices[j])) {
                        choice.setSelectedIndex(j);
                        break;
                    }
                }
            }
        } else if (callback instanceof AuthorizeCallback) {
            System.out.println("AuthorizeCallback");
            ((AuthorizeCallback) callback).setAuthorized(true);
            if (authId == null || authId.trim().length() == 0) {
                authId = userId;
            }
            ((AuthorizeCallback) callback).setAuthorizedID(authId);
        } else {
            throw new UnsupportedCallbackException(callback);
        }
    }
}
 
Example 22
Source Project: hottub   Source File: ClientServerTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public void handle(Callback[] callbacks) throws IOException,
        UnsupportedCallbackException {
    for (Callback callback : callbacks) {
        if (callback instanceof NameCallback) {
            System.out.println("NameCallback");
            ((NameCallback) callback).setName(userId);
        } else if (callback instanceof PasswordCallback) {
            System.out.println("PasswordCallback");
            ((PasswordCallback) callback).setPassword(passwd);
        } else if (callback instanceof RealmCallback) {
            System.out.println("RealmCallback");
            ((RealmCallback) callback).setText(realm);
        } else if (callback instanceof RealmChoiceCallback) {
            System.out.println("RealmChoiceCallback");
            RealmChoiceCallback choice = (RealmChoiceCallback) callback;
            if (realm == null) {
                choice.setSelectedIndex(choice.getDefaultChoice());
            } else {
                String[] choices = choice.getChoices();
                for (int j = 0; j < choices.length; j++) {
                    if (realm.equals(choices[j])) {
                        choice.setSelectedIndex(j);
                        break;
                    }
                }
            }
        } else if (callback instanceof AuthorizeCallback) {
            System.out.println("AuthorizeCallback");
            ((AuthorizeCallback) callback).setAuthorized(true);
            if (authId == null || authId.trim().length() == 0) {
                authId = userId;
            }
            ((AuthorizeCallback) callback).setAuthorizedID(authId);
        } else {
            throw new UnsupportedCallbackException(callback);
        }
    }
}
 
Example 23
Source Project: herddb   Source File: SaslNettyServer.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void handle(Callback[] callbacks) throws
        UnsupportedCallbackException {
    for (Callback callback : callbacks) {
        if (callback instanceof NameCallback) {
            NameCallback nc = (NameCallback) callback;
            nc.setName(nc.getDefaultName());
        } else {
            if (callback instanceof PasswordCallback) {
                PasswordCallback pc = (PasswordCallback) callback;
                if (password != null) {
                    pc.setPassword(this.password.toCharArray());
                }
            } else {
                if (callback instanceof RealmCallback) {
                    RealmCallback rc = (RealmCallback) callback;
                    rc.setText(rc.getDefaultText());
                } else {
                    if (callback instanceof AuthorizeCallback) {
                        AuthorizeCallback ac = (AuthorizeCallback) callback;
                        String authid = ac.getAuthenticationID();
                        String authzid = ac.getAuthorizationID();
                        if (authid.equals(authzid)) {
                            ac.setAuthorized(true);
                        } else {
                            ac.setAuthorized(false);
                        }
                        if (ac.isAuthorized()) {
                            ac.setAuthorizedID(authzid);
                        }
                    } else {
                        throw new UnsupportedCallbackException(callback, "Unrecognized SASL ClientCallback");
                    }
                }
            }
        }
    }
}
 
Example 24
Source Project: herddb   Source File: SaslNettyServer.java    License: Apache License 2.0 5 votes vote down vote up
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);
        }
    }
}
 
Example 25
Source Project: Krackle   Source File: Login.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void handle(Callback[] callbacks) throws
	 UnsupportedCallbackException {
	for (Callback callback : callbacks) {
		if (callback instanceof NameCallback) {
			NameCallback nc = (NameCallback) callback;
			nc.setName(nc.getDefaultName());
		} else {
			if (callback instanceof PasswordCallback) {
				LOG.warn("Could not login: the client is being asked for a password");
			} else {
				if (callback instanceof RealmCallback) {
					RealmCallback rc = (RealmCallback) callback;
					rc.setText(rc.getDefaultText());
				} else {
					if (callback instanceof AuthorizeCallback) {
						AuthorizeCallback ac = (AuthorizeCallback) callback;
						String authid = ac.getAuthenticationID();
						String authzid = ac.getAuthorizationID();
						if (authid.equals(authzid)) {
							ac.setAuthorized(true);
						} else {
							ac.setAuthorized(false);
						}
						if (ac.isAuthorized()) {
							ac.setAuthorizedID(authzid);
						}
					} else {
						throw new UnsupportedCallbackException(callback, "Unrecognized SASL ClientCallback");
					}
				}
			}
		}
	}
}
 
Example 26
Source Project: herddb   Source File: SaslNettyClient.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void handle(Callback[] callbacks) throws
        UnsupportedCallbackException {
    for (Callback callback : callbacks) {
        if (callback instanceof NameCallback) {
            NameCallback nc = (NameCallback) callback;
            nc.setName(nc.getDefaultName());
        } else {
            if (callback instanceof PasswordCallback) {
                PasswordCallback pc = (PasswordCallback) callback;
                if (password != null) {
                    pc.setPassword(this.password.toCharArray());
                }
            } else {
                if (callback instanceof RealmCallback) {
                    RealmCallback rc = (RealmCallback) callback;
                    rc.setText(rc.getDefaultText());
                } else {
                    if (callback instanceof AuthorizeCallback) {
                        AuthorizeCallback ac = (AuthorizeCallback) callback;
                        String authid = ac.getAuthenticationID();
                        String authzid = ac.getAuthorizationID();
                        if (authid.equals(authzid)) {
                            ac.setAuthorized(true);
                        } else {
                            ac.setAuthorized(false);
                        }
                        if (ac.isAuthorized()) {
                            ac.setAuthorizedID(authzid);
                        }
                    } else {
                        throw new UnsupportedCallbackException(callback, "Unrecognized SASL ClientCallback");
                    }
                }
            }
        }
    }
}
 
Example 27
Source Project: jdk8u_jdk   Source File: ClientServerTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public void handle(Callback[] callbacks) throws IOException,
        UnsupportedCallbackException {
    for (Callback callback : callbacks) {
        if (callback instanceof NameCallback) {
            System.out.println("NameCallback");
            ((NameCallback) callback).setName(userId);
        } else if (callback instanceof PasswordCallback) {
            System.out.println("PasswordCallback");
            ((PasswordCallback) callback).setPassword(passwd);
        } else if (callback instanceof RealmCallback) {
            System.out.println("RealmCallback");
            ((RealmCallback) callback).setText(realm);
        } else if (callback instanceof RealmChoiceCallback) {
            System.out.println("RealmChoiceCallback");
            RealmChoiceCallback choice = (RealmChoiceCallback) callback;
            if (realm == null) {
                choice.setSelectedIndex(choice.getDefaultChoice());
            } else {
                String[] choices = choice.getChoices();
                for (int j = 0; j < choices.length; j++) {
                    if (realm.equals(choices[j])) {
                        choice.setSelectedIndex(j);
                        break;
                    }
                }
            }
        } else if (callback instanceof AuthorizeCallback) {
            System.out.println("AuthorizeCallback");
            ((AuthorizeCallback) callback).setAuthorized(true);
            if (authId == null || authId.trim().length() == 0) {
                authId = userId;
            }
            ((AuthorizeCallback) callback).setAuthorizedID(authId);
        } else {
            throw new UnsupportedCallbackException(callback);
        }
    }
}
 
Example 28
Source Project: hawkular-agent   Source File: AbstractITest.java    License: Apache License 2.0 5 votes vote down vote up
protected static ModelControllerClient newModelControllerClient(final String host, final int managementPort) {
    final CallbackHandler callbackHandler = new CallbackHandler() {
        @Override
        public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
            for (Callback current : callbacks) {
                if (current instanceof NameCallback) {
                    NameCallback ncb = (NameCallback) current;
                    log.fine("ModelControllerClient is sending a username [" + managementUser + "]");
                    ncb.setName(managementUser);
                } else if (current instanceof PasswordCallback) {
                    PasswordCallback pcb = (PasswordCallback) current;
                    log.fine("ModelControllerClient is sending a password [" + managementPasword + "]");
                    pcb.setPassword(managementPasword.toCharArray());
                } else if (current instanceof RealmCallback) {
                    RealmCallback rcb = (RealmCallback) current;
                    log.fine("ModelControllerClient is sending a realm [" + rcb.getDefaultText() + "]");
                    rcb.setText(rcb.getDefaultText());
                } else {
                    throw new UnsupportedCallbackException(current);
                }
            }
        }
    };

    try {
        InetAddress inetAddr = InetAddress.getByName(host);
        log.fine("Connecting a ModelControllerClient to [" + host + ":" + managementPort + "]");
        return ModelControllerClient.Factory.create(inetAddr, managementPort, callbackHandler);
    } catch (Exception e) {
        throw new RuntimeException("Failed to create management client", e);
    }
}
 
Example 29
protected ModelControllerClient createClient(final MonitoredEndpoint endpoint) {
    final ConnectionData cnData = endpoint.getConnectionData();
    final CallbackHandler callbackHandler = new CallbackHandler() {
        public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
            for (Callback current : callbacks) {
                if (current instanceof NameCallback) {
                    NameCallback ncb = (NameCallback) current;
                    ncb.setName(cnData.getUsername());
                } else if (current instanceof PasswordCallback) {
                    PasswordCallback pcb = (PasswordCallback) current;
                    pcb.setPassword(cnData.getPassword().toCharArray());
                } else if (current instanceof RealmCallback) {
                    RealmCallback rcb = (RealmCallback) current;
                    rcb.setText(rcb.getDefaultText());
                } else {
                    throw new UnsupportedCallbackException(current);
                }
            }
        }
    };
    final URI uri = cnData.getUri();
    try {
        ModelControllerClientConfiguration config = new ModelControllerClientConfiguration.Builder()
                .setProtocol(uri.getScheme())
                .setHostName(uri.getHost())
                .setPort(uri.getPort())
                .setSslContext(endpoint.getSSLContext())
                .setHandler(callbackHandler)
                .build();

        return ModelControllerClient.Factory.create(config);
    } catch (Exception e) {
        throw new RuntimeException("Failed to create management client", e);
    }
}
 
Example 30
Source Project: blazingcache   Source File: SaslNettyServer.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void handle(Callback[] callbacks) throws
    UnsupportedCallbackException {
    for (Callback callback : callbacks) {
        if (callback instanceof NameCallback) {
            NameCallback nc = (NameCallback) callback;
            nc.setName(nc.getDefaultName());
        } else {
            if (callback instanceof PasswordCallback) {
                PasswordCallback pc = (PasswordCallback) callback;
                if (password != null) {
                    pc.setPassword(this.password.toCharArray());
                }
            } else {
                if (callback instanceof RealmCallback) {
                    RealmCallback rc = (RealmCallback) callback;
                    rc.setText(rc.getDefaultText());
                } else {
                    if (callback instanceof AuthorizeCallback) {
                        AuthorizeCallback ac = (AuthorizeCallback) callback;
                        String authid = ac.getAuthenticationID();
                        String authzid = ac.getAuthorizationID();
                        if (authid.equals(authzid)) {
                            ac.setAuthorized(true);
                        } else {
                            ac.setAuthorized(false);
                        }
                        if (ac.isAuthorized()) {
                            ac.setAuthorizedID(authzid);
                        }
                    } else {
                        throw new UnsupportedCallbackException(callback, "Unrecognized SASL ClientCallback");
                    }
                }
            }
        }
    }
}