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

The following examples show how to use javax.security.auth.callback.NameCallback#setName() . 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: DefaultCallbackHandler.java    From attic-apex-core with 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
Source File: AbstractLogin.java    From registry with 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 3
Source File: Authentication.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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 4
Source File: UsernamePasswordCallback.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
public void handle(Callback[] callbacks) throws IOException,
		UnsupportedCallbackException {
	for (Callback callback : callbacks) {
		if (callback instanceof NameCallback) {
			NameCallback nameCallback = (NameCallback) callback;
			nameCallback.setName(username);
		} else if (callback instanceof PasswordCallback) {
			PasswordCallback passwordCallback = (PasswordCallback) callback;
			passwordCallback.setPassword(password.toCharArray());
		} else {
			throw new UnsupportedCallbackException(callback,
					"Only username and password supported.");
		}
	}

}
 
Example 5
Source File: DynamicConfigurationTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@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");
        }
    }
}
 
Example 6
Source File: JAASAuthenticator.java    From jcifs-ng with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @see javax.security.auth.callback.CallbackHandler#handle(javax.security.auth.callback.Callback[])
 */
@Override
public void handle ( Callback[] callbacks ) throws IOException, UnsupportedCallbackException {
    for ( Callback cb : callbacks ) {
        if ( log.isDebugEnabled() ) {
            log.debug("Got callback " + cb.getClass().getName());
        }

        if ( cb instanceof NameCallback ) {
            NameCallback nc = (NameCallback) cb;
            String userDomain = this.getSpecifiedUserDomain();
            if ( this.getUsername() != null && userDomain != null ) {
                nc.setName(this.getUsername() + "@" + userDomain);
            }
        }
        else if ( cb instanceof PasswordCallback ) {
            PasswordCallback pc = (PasswordCallback) cb;
            if ( this.getPassword() != null ) {
                pc.setPassword(this.getPassword().toCharArray());
            }
        }
    }
}
 
Example 7
Source File: AuthCallbackHandler.java    From ARCHIVE-wildfly-swarm with 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 8
Source File: SSLAndKerberosTest.java    From incubator-atlas with Apache License 2.0 6 votes vote down vote up
protected Subject loginTestUser() throws LoginException, IOException {
    LoginContext lc = new LoginContext(TEST_USER_JAAS_SECTION, new CallbackHandler() {

        @Override
        public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
            for (Callback callback : callbacks) {
                if (callback instanceof PasswordCallback) {
                    PasswordCallback passwordCallback = (PasswordCallback) callback;
                    passwordCallback.setPassword(TESTPASS.toCharArray());
                }
                if (callback instanceof NameCallback) {
                    NameCallback nameCallback = (NameCallback) callback;
                    nameCallback.setName(TESTUSER);
                }
            }
        }
    });
    // attempt authentication
    lc.login();
    return lc.getSubject();
}
 
Example 9
Source File: MockCallbackHandler.java    From datawave with Apache License 2.0 5 votes vote down vote up
@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    assertEquals(2, callbacks.length);
    assertEquals(NameCallback.class, callbacks[0].getClass());
    assertEquals(ObjectCallback.class, callbacks[1].getClass());
    
    NameCallback nc = (NameCallback) callbacks[0];
    ObjectCallback oc = (ObjectCallback) callbacks[1];
    
    assertEquals(nameCallbackPrompt, nc.getPrompt());
    assertEquals(credentialsCallbackPrompt, oc.getPrompt());
    
    nc.setName(name);
    oc.setCredential(credential);
}
 
Example 10
Source File: UsernameAndPasswordCallbackHandler.java    From iaf with Apache License 2.0 5 votes vote down vote up
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
	for (Callback callback : callbacks) {
		if (callback instanceof NameCallback) {
			NameCallback nc = (NameCallback) callback;
			nc.setName(user);
		} else if (callback instanceof PasswordCallback) {
			PasswordCallback pc = (PasswordCallback) callback;
			pc.setPassword(password.toCharArray());
		} else {
			throw new UnsupportedCallbackException(callback, "Unknown Callback");
		}
	}
}
 
Example 11
Source File: JmxProxyImpl.java    From cassandra-reaper with Apache License 2.0 5 votes vote down vote up
public void handle(javax.security.auth.callback.Callback[] callbacks)
        throws IOException, javax.security.auth.callback.UnsupportedCallbackException {
  for (int i = 0; i < callbacks.length; i++) {
    if (callbacks[i] instanceof PasswordCallback) {
      PasswordCallback pcb = (PasswordCallback) callbacks[i];
      pcb.setPassword(password);
    } else if (callbacks[i] instanceof javax.security.auth.callback.NameCallback) {
      NameCallback ncb = (NameCallback) callbacks[i];
      ncb.setName(username);
    } else {
      throw new UnsupportedCallbackException(callbacks[i]);
    }
  }
}
 
Example 12
Source File: KerberosAuth.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException
{
  for (Callback callback : callbacks) {
    if (callback instanceof NameCallback) {
      NameCallback nameCallback = (NameCallback)callback;
      nameCallback.setName(principal);
    } else if (callback instanceof PasswordCallback) {
      PasswordCallback passwordCallback = (PasswordCallback)callback;
      passwordCallback.setPassword(password);
    }
  }
}
 
Example 13
Source File: ClientCallbackHandler.java    From jstorm with Apache License 2.0 5 votes vote down vote up
/**
 * This method is invoked by SASL for authentication challenges
 * 
 * @param callbacks a collection of challenge callbacks
 */
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (Callback c : callbacks) {
        if (c instanceof NameCallback) {
            LOG.debug("name callback");
            NameCallback nc = (NameCallback) c;
            nc.setName(_username);
        } else if (c instanceof PasswordCallback) {
            LOG.debug("password callback");
            PasswordCallback pc = (PasswordCallback) c;
            if (_password != null) {
                pc.setPassword(_password.toCharArray());
            }
        } else if (c instanceof AuthorizeCallback) {
            LOG.debug("authorization callback");
            AuthorizeCallback ac = (AuthorizeCallback) c;
            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 if (c instanceof RealmCallback) {
            RealmCallback rc = (RealmCallback) c;
            ((RealmCallback) c).setText(rc.getDefaultText());
        } else {
            throw new UnsupportedCallbackException(c);
        }
    }
}
 
Example 14
Source File: SaslNettyServer.java    From blazingcache with Apache License 2.0 5 votes vote down vote up
private void handleNameCallback(NameCallback nc) {
    // check to see if this user is in the user password database.
    if (credentials.get(nc.getDefaultName()) == null) {
        LOG.severe("User '" + nc.getDefaultName() + "' not found in list of JAAS DIGEST-MD5 users.");
        return;
    }
    nc.setName(nc.getDefaultName());
    userName = nc.getDefaultName();
}
 
Example 15
Source File: UsernamePasswordHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/** Sets any NameCallback name property to the instance username,
 sets any PasswordCallback password property to the instance, and any
 password.
 @exception UnsupportedCallbackException, thrown if any callback of
 type other than NameCallback or PasswordCallback are seen.
 */
public void handle(Callback[] callbacks) throws
      UnsupportedCallbackException
{
   for (int i = 0; i < callbacks.length; i++)
   {
      Callback c = callbacks[i];
      if (c instanceof NameCallback)
      {
         NameCallback nc = (NameCallback) c;
         nc.setName(username);
      }
      else if (c instanceof PasswordCallback)
      {
         PasswordCallback pc = (PasswordCallback) c;
         if( password == null )
         {
            // We were given an opaque Object credential but a char[] is requested?
            if( credential != null )
            {
               String tmp = credential.toString();
               password = tmp.toCharArray();
            }
         }
         pc.setPassword(password);
      }
      else if (c instanceof ObjectCallback)
      {
         ObjectCallback oc = (ObjectCallback) c;
         oc.setCredential(credential);
      }
      else
      {
         throw PicketBoxMessages.MESSAGES.unableToHandleCallback(c, this.getClass().getName(), c.getClass().getCanonicalName());
      }
   }
}
 
Example 16
Source File: SaslNettyClient.java    From herddb with 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 17
Source File: KerberosClientPasswordCallback.java    From cxf with Apache License 2.0 5 votes vote down vote up
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (int i = 0; i < callbacks.length; i++) {
        if (callbacks[i] instanceof NameCallback) {
            NameCallback nameCallback = (NameCallback)callbacks[i];
            nameCallback.setName(username);
        } else if (callbacks[i] instanceof PasswordCallback) {
            PasswordCallback passwordCallback = (PasswordCallback)callbacks[i];
            passwordCallback.setPassword(password.toCharArray());
        } else if (callbacks[i] instanceof WSPasswordCallback) {
            WSPasswordCallback wsPasswordCallback = (WSPasswordCallback)callbacks[i];
            // Get a custom (Kerberos) token directly using the WSS4J APIs
            if (wsPasswordCallback.getUsage() == WSPasswordCallback.CUSTOM_TOKEN) {
                KerberosSecurity kerberosSecurity = new KerberosSecurity(DOMUtils.getEmptyDocument());

                try {
                    kerberosSecurity.retrieveServiceTicket(username, this, servicePrincipal,
                                              false, false, null);
                    kerberosSecurity.addWSUNamespace();
                    WSSConfig wssConfig = WSSConfig.getNewInstance();
                    kerberosSecurity.setID(wssConfig.getIdAllocator().createSecureId("BST-", kerberosSecurity));

                    wsPasswordCallback.setCustomToken(kerberosSecurity.getElement());
                } catch (WSSecurityException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}
 
Example 18
Source File: KerberosClientPasswordCallback.java    From cxf-fediz with Apache License 2.0 5 votes vote down vote up
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (int i = 0; i < callbacks.length; i++) {
        if (callbacks[i] instanceof NameCallback) {
            NameCallback nameCallback = (NameCallback)callbacks[i];
            nameCallback.setName("alice");
        } else if (callbacks[i] instanceof PasswordCallback) {
            PasswordCallback passwordCallback = (PasswordCallback)callbacks[i];
            passwordCallback.setPassword("alice".toCharArray());
        }
    }
}
 
Example 19
Source File: PlainSaslServer.java    From incubator-retired-blur with Apache License 2.0 4 votes vote down vote up
public byte[] evaluateResponse(byte[] response) throws SaslException {
  try {
    // parse the response
    // message   = [authzid] UTF8NUL authcid UTF8NUL passwd'

    Deque<String> tokenList = new ArrayDeque<String>();
    StringBuilder messageToken = new StringBuilder();
    for (byte b : response) {
      if (b == 0) {
        tokenList.addLast(messageToken.toString());
        messageToken = new StringBuilder();
      } else {
        messageToken.append((char)b);
      }
    }
    tokenList.addLast(messageToken.toString());

    // validate response
    if ((tokenList.size() < 2) || (tokenList.size() > 3)) {
      throw new SaslException("Invalid message format");
    }
    _passwd = tokenList.removeLast();
    _user = tokenList.removeLast();
    // optional authzid
    if (!tokenList.isEmpty()) {
      _authzId = tokenList.removeLast();
    } else {
      _authzId = _user;
    }
    if (_user == null || _user.isEmpty()) {
      throw new SaslException("No user name provide");
    }
    if (_passwd == null || _passwd.isEmpty()) {
      throw new SaslException("No password name provide");
    }

    NameCallback nameCallback = new NameCallback("User");
    nameCallback.setName(_user);
    PasswordCallback pcCallback = new PasswordCallback("Password", false);
    pcCallback.setPassword(_passwd.toCharArray());
    AuthorizeCallback acCallback = new AuthorizeCallback(_user, _authzId);

    Callback[] cbList = new Callback[] {nameCallback, pcCallback, acCallback};
    _handler.handle(cbList);
    if (!acCallback.isAuthorized()) {
      throw new SaslException("Authentication failed");
    }
  } catch (IllegalStateException eL) {
    throw new SaslException("Invalid message format", eL);
  } catch (IOException eI) {
    throw new SaslException("Error validating the login", eI);
  } catch (UnsupportedCallbackException eU) {
    throw new SaslException("Error validating the login", eU);
  }
  return null;
}
 
Example 20
Source File: SecurityAssociationHandler.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/** Handles SecurityAssociationCallback, ObjectCallback, NameCallback and
 PasswordCallback types. A NameCallback name property is set to
 the Prinicpal.getName() value. A PasswordCallback password property is
 set to the getPassword() value. The preferred SecurityAssociationCallback
 has its principal and credential properties set to the instance principal
 and credential. An ObjectCallback has its credential set to the credential
 value.

 @see #getPassword()
 @exception UnsupportedCallbackException - thrown if any callback of
 type other than SecurityAssociationCallback, ObjectCallback, NameCallback
 or PasswordCallback are seen.
 */
public void handle(Callback[] callbacks) throws
   UnsupportedCallbackException, IOException
{
   for (int i = 0; i < callbacks.length; i++)
   {
      Callback c = callbacks[i];
      if (c instanceof SecurityAssociationCallback)
      {
         SecurityAssociationCallback sac = (SecurityAssociationCallback) c;
         sac.setPrincipal(principal);
         sac.setCredential(credential);
      }
      else if (c instanceof ObjectCallback)
      {
         ObjectCallback oc = (ObjectCallback) c;
         oc.setCredential(credential);
      }
      else if (c instanceof NameCallback)
      {
         NameCallback nc = (NameCallback) c;
         if (principal != null)
            nc.setName(principal.getName());
      }
      else if (c instanceof PasswordCallback)
      {
         PasswordCallback pc = (PasswordCallback) c;
         char[] password = getPassword();
         if (password != null)
            pc.setPassword(password);
      }
      else
      {
         // Try the JACC context CallbackHandler 
         try
         {
            CallbackHandler handler = SecurityActions.getContextCallbackHandler();
            if( handler != null )
            {
               Callback[] unknown = {c};
               handler.handle(unknown);
               return;
            }
         }
         catch (Exception e)
         {
         }
         throw PicketBoxMessages.MESSAGES.unableToHandleCallback(c, this.getClass().getName(), c.getClass().getCanonicalName());
      }
   }
}