Java Code Examples for javax.security.auth.callback.NameCallback#getName()

The following examples show how to use javax.security.auth.callback.NameCallback#getName() . 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 check out the related API usage on the sidebar.
Example 1
Source File: TestUserPasswordLoginModule.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Override
public boolean login() throws LoginException {
    NameCallback nameCallback = new NameCallback("User");
    PasswordCallback passwordCallback = new PasswordCallback("Password", false);
    Callback[] callbacks = new Callback[] {nameCallback, passwordCallback};
    try {
        this.callbackHandler.handle(callbacks);
    } catch (IOException | UnsupportedCallbackException e) {
        throw new LoginException(e.getMessage());
    }
    String userName = nameCallback.getName();
    String password = new String(passwordCallback.getPassword());
    if (!TESTUSER.equals(userName)) {
        throw new LoginException("wrong username");
    }
    if (!TESTPASS.equals(password)) {
        throw new LoginException("wrong password");
    }
    subject.getPrincipals().add(new SimplePrincipal(userName));
    subject.getPrincipals().add(new SimpleGroup(TESTGROUP));
    return true;
}
 
Example 2
Source File: AbstractCallbackHandler.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Given the callbacks, look for {@code NameCallback}
 * @param callbacks
 * @return
 */
protected String getUserName(Callback[] callbacks)
{
	if(userName == null)
	{ 
		for (int i = 0; i < callbacks.length; i++)
		{
			Callback callback = callbacks[i];
			if(callback instanceof NameCallback)
			{
				NameCallback nc = (NameCallback) callback;
				userName = nc.getName();
				break;
			}  
		}
	}
	return userName;
}
 
Example 3
Source File: SimpleServerAuthModule.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected boolean validate(Subject clientSubject, MessageInfo messageInfo) throws AuthException
{
 //Construct Callbacks
   NameCallback nc = new NameCallback("Dummy");
   PasswordCallback pc = new PasswordCallback("B" , true);
   try
   {
      this.callbackHandler.handle(new Callback[]{nc,pc});
      String userName = nc.getName();
      String pwd = new String(pc.getPassword());
      
      //Check the options
      if(!(userName.equals(options.get("principal"))
            && (pwd.equals(options.get("pass")))))
      {
         return false;
      }
            
   }
   catch (Exception e)
   {
      throw new AuthException(e.getLocalizedMessage());
   } 
   return true;
}
 
Example 4
Source File: TestUserPasswordLoginModule.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Override
public boolean login() throws LoginException {
    NameCallback nameCallback = new NameCallback("User");
    PasswordCallback passwordCallback = new PasswordCallback("Password", false);
    Callback[] callbacks = new Callback[] {nameCallback, passwordCallback};
    try {
        this.callbackHandler.handle(callbacks);
    } catch (IOException | UnsupportedCallbackException e) {
        throw new LoginException(e.getMessage());
    }
    String userName = nameCallback.getName();
    String password = new String(passwordCallback.getPassword());
    if (!TESTUSER.equals(userName)) {
        throw new LoginException("wrong username");
    }
    if (!TESTPASS.equals(password)) {
        throw new LoginException("wrong password");
    }
    subject.getPrincipals().add(new SimplePrincipal(userName));
    subject.getPrincipals().add(new SimpleGroup(TESTGROUP));
    return true;
}
 
Example 5
Source File: TMLoginModule.java    From ontopia with Apache License 2.0 5 votes vote down vote up
/** 
 * Prompt the user for username and password, and verify those.
 */
@Override
public boolean login() throws LoginException {
  log.debug("TMLoginModule: login");
  
  if (callbackHandler == null)
    throw new LoginException("Error: no CallbackHandler available " +
            "to garner authentication information from the user");
  
  // prompt for a user name and password
  NameCallback nameCallback =  new NameCallback("user name: ");
  PasswordCallback passwordCallback = new PasswordCallback("password: ",
          false);
  
  try {
    callbackHandler.handle(new Callback[] {nameCallback, passwordCallback});

    this.username = nameCallback.getName();
    char[] charpassword = passwordCallback.getPassword();
    password = (charpassword == null ? "" : new String(charpassword));
    passwordCallback.clearPassword();
    
  } catch (java.io.IOException ioe) {
    throw new LoginException(ioe.toString());
  } catch (UnsupportedCallbackException uce) {
    throw new LoginException("Error: " + uce.getCallback() +
            " not available to garner authentication information " +
            "from the user");
  }
  // verify the username/password
  loginSucceeded = verifyUsernamePassword(username, password);
  return loginSucceeded;
}
 
Example 6
Source File: QuarkusDirContextFactory.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
public DirContext obtainDirContext(CallbackHandler handler, ReferralMode mode) throws NamingException {
    NameCallback nameCallback = new NameCallback("Principal Name");
    PasswordCallback passwordCallback = new PasswordCallback("Password", false);

    try {
        handler.handle(new Callback[] { nameCallback, passwordCallback });
    } catch (Exception e) {
        throw new RuntimeException("Could not obtain credential", e);
        //            throw log.couldNotObtainCredentialWithCause(e);
    }

    String securityPrincipal = nameCallback.getName();

    if (securityPrincipal == null) {
        throw new RuntimeException("Could not obtain principal");
        //            throw log.couldNotObtainPrincipal();
    }

    char[] securityCredential = passwordCallback.getPassword();

    if (securityCredential == null) {
        throw new RuntimeException("Could not obtain credential");
        //            throw log.couldNotObtainCredential();
    }

    return createDirContext(securityPrincipal, securityCredential, mode);
}
 
Example 7
Source File: CDILoginModuleTest.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(final Subject subject, final CallbackHandler callbackHandler,
                       final Map<String, ?> sharedState, final Map<String, ?> options) {
    final NameCallback nameCallback = new NameCallback("whatever");
    try {
        callbackHandler.handle(new Callback[]{nameCallback});
    } catch (final Exception e) {
        // no-op
    }
    name = nameCallback.getName();
    this.subject = subject;
}
 
Example 8
Source File: JAASLoginInterceptor.java    From cxf with Apache License 2.0 5 votes vote down vote up
private String getUsername(CallbackHandler handler) {
    if (handler == null) {
        return null;
    }
    try {
        NameCallback usernameCallBack = new NameCallback("user");
        handler.handle(new Callback[]{usernameCallBack });
        return usernameCallBack.getName();
    } catch (Exception e) {
        return null;
    }
}
 
Example 9
Source File: AltClientLoginModule.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Method to authenticate a Subject (phase 1).
 */
public boolean login() throws LoginException
{
   // If useFirstPass is true, look for the shared password
   if( useFirstPass == true )
   {
         return true;
   }

  /* There is no password sharing or we are the first login module. Get
      the username and password from the callback hander.
   */
   if (callbackHandler == null)
      throw PicketBoxMessages.MESSAGES.noCallbackHandlerAvailable();

   PasswordCallback pc = new PasswordCallback(PicketBoxMessages.MESSAGES.enterPasswordMessage(), false);
   NameCallback nc = new NameCallback(PicketBoxMessages.MESSAGES.enterUsernameMessage(), "guest");
   Callback[] callbacks = {nc, pc};
   try
   {
      char[] tmpPassword;
      
      callbackHandler.handle(callbacks);
      username = nc.getName();
      tmpPassword = pc.getPassword();
      if (tmpPassword != null)
      {
         password = new char[tmpPassword.length];
         System.arraycopy(tmpPassword, 0, password, 0, tmpPassword.length);
         pc.clearPassword();
      }
   }
   catch (java.io.IOException ioe)
   {
      throw new LoginException(ioe.toString());
   }
   catch (UnsupportedCallbackException uce)
   {
      LoginException le = new LoginException(uce.getLocalizedMessage());
      le.initCause(uce);
      throw le;
   }
   return true;
}
 
Example 10
Source File: KeyStoreLoginModule.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
private void saveAlias(NameCallback cb) {
    keyStoreAlias = cb.getName();
}
 
Example 11
Source File: KeyStoreLoginModule.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
private void saveAlias(NameCallback cb) {
    keyStoreAlias = cb.getName();
}
 
Example 12
Source File: KeyStoreLoginModule.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
private void saveAlias(NameCallback cb) {
    keyStoreAlias = cb.getName();
}
 
Example 13
Source File: KeyStoreLoginModule.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
private void saveAlias(NameCallback cb) {
    keyStoreAlias = cb.getName();
}
 
Example 14
Source File: KeyStoreLoginModule.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private void saveAlias(NameCallback cb) {
    keyStoreAlias = cb.getName();
}
 
Example 15
Source File: KeyStoreLoginModule.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private void saveAlias(NameCallback cb) {
    keyStoreAlias = cb.getName();
}
 
Example 16
Source File: KeyStoreLoginModule.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
private void saveAlias(NameCallback cb) {
    keyStoreAlias = cb.getName();
}
 
Example 17
Source File: KeyStoreLoginModule.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
private void saveAlias(NameCallback cb) {
    keyStoreAlias = cb.getName();
}
 
Example 18
Source File: JiveJdonLoginMoudle.java    From jivejdon with Apache License 2.0 4 votes vote down vote up
private void authenticate() throws IOException, UnsupportedCallbackException {

		NameCallback nameCallback = new NameCallback("name: ");
		PasswordCallback passwordCallback = new PasswordCallback("password: ", false);

		_callbackHandler.handle(new Callback[] { nameCallback, passwordCallback });

		username = nameCallback.getName();

		String password = null;
		char[] passwordChar = passwordCallback.getPassword();

		if (passwordChar != null) {
			password = new String(passwordChar);
			roles = rolesProvider.provideRoles(username, DigestUtil.hash(password));
		}

		if (roles.size() > 0)
			succeeded = true;

	}
 
Example 19
Source File: KeyStoreLoginModule.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
private void saveAlias(NameCallback cb) {
    keyStoreAlias = cb.getName();
}
 
Example 20
Source File: KeyStoreLoginModule.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
private void saveAlias(NameCallback cb) {
    keyStoreAlias = cb.getName();
}