javax.security.auth.callback.NameCallback Java Examples

The following examples show how to use javax.security.auth.callback.NameCallback. 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: DynamicConfigurationTest.java    From TencentKona-8 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 #2
Source File: NegotiateCallbackHandler.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
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");
        }
    }
}
 
Example #3
Source File: NegotiateCallbackHandler.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
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");
        }
    }
}
 
Example #4
Source File: TestHBaseSaslRpcClient.java    From hbase with 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 #5
Source File: ServiceProviderLoginModule.java    From tomee with Apache License 2.0 6 votes vote down vote up
private UserData getUserData() throws LoginException {
    final Callback[] callbacks = new Callback[2];

    callbacks[0] = new NameCallback("Username: ");
    callbacks[1] = new PasswordCallback("Password: ", false);
    try {
        this.callbackHandler.handle(callbacks);
    } catch (final IOException ioe) {
        throw new LoginException(ioe.getMessage());
    } catch (final UnsupportedCallbackException uce) {
        throw new LoginException(uce.getMessage() + " not available to obtain information from user");
    }

    final String user = ((NameCallback) callbacks[0]).getName();

    char[] tmpPassword = ((PasswordCallback) callbacks[1]).getPassword();
    if (tmpPassword == null) {
        tmpPassword = new char[0];
    }

    final String password = new String(tmpPassword);

    return new UserData(user, password);
}
 
Example #6
Source File: NegotiateCallbackHandler.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
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");
        }
    }
}
 
Example #7
Source File: MockLoginModule.java    From springboot-shiro-cas-mybatis with MIT License 6 votes vote down vote up
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();
}
 
Example #8
Source File: AuthentWithRequestTest.java    From tomee with Apache License 2.0 6 votes vote down vote up
@Override
public boolean login() throws LoginException {
    assertNull(SystemInstance.get().getComponent(SecurityService.class).currentState()); // check the user was not logged at lookup()

    final NameCallback nameCallback = new NameCallback("name?", "dummy");
    try {
        callbackHandler.handle(new Callback[]{nameCallback});
    } catch (final Exception e) {
        throw new LoginException(e.getMessage());
    }
    if (!"foo".equals(nameCallback.getName())) {
        throw new IllegalArgumentException("Not an Error/assert cause in java 9 jaas doesnt capture it anymore");
    }
    RemoteWithSecurity.name.set(nameCallback.getName());
    return true;
}
 
Example #9
Source File: AtlasAuthenticationKerberosFilterTest.java    From 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 #10
Source File: DynamicConfigurationTest.java    From dragonwell8_jdk 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 #11
Source File: DynamicConfigurationTest.java    From openjdk-jdk8u-backup 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 #12
Source File: UserCredentialsHandler.java    From development with Apache License 2.0 6 votes vote down vote up
/**
 * 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");
        }
    }
}
 
Example #13
Source File: JavaCallbackHandler.java    From neoscada with Eclipse Public License 1.0 6 votes vote down vote up
private javax.security.auth.callback.Callback convert ( final Callback cb )
{
    if ( cb instanceof UserNameCallback )
    {
        return new NameCallback ( ( (UserNameCallback)cb ).getLabel () );
    }
    else if ( cb instanceof TextCallback )
    {
        return new TextInputCallback ( ( (TextCallback)cb ).getLabel () );
    }
    if ( cb instanceof PasswordCallback )
    {
        return new javax.security.auth.callback.PasswordCallback ( ( (PasswordCallback)cb ).getLabel (), false );
    }
    return null;
}
 
Example #14
Source File: HostControllerConnection.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 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 new UnsupportedCallbackException(current, "Realm choice not currently supported.");
        } else if (current instanceof NameCallback) {
            NameCallback ncb = (NameCallback) current;
            ncb.setName(userName);
        } else if (current instanceof PasswordCallback) {
            PasswordCallback pcb = (PasswordCallback) current;
            pcb.setPassword(authKey.toCharArray());
        } else {
            throw new UnsupportedCallbackException(current);
        }
    }
}
 
Example #15
Source File: JAASAuthenticator.java    From jcifs 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 #16
Source File: NegotiateCallbackHandler.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
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");
        }
    }
}
 
Example #17
Source File: MockLoginModule.java    From cas4.0.x-server-wechat with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example #18
Source File: UsernamePasswordCallbackHandler.java    From tomee with Apache License 2.0 6 votes vote down vote up
public void handle(final Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (final Callback callback : callbacks) {
        if (callback instanceof PasswordCallback) {
            final PasswordCallback passwordCallback = (PasswordCallback) callback;
            if (password == null) {
                passwordCallback.setPassword(null);
            } else {
                passwordCallback.setPassword(password.toCharArray());
            }
        } else if (callback instanceof NameCallback) {
            final NameCallback nameCallback = (NameCallback) callback;
            if (username == null) {
                nameCallback.setName(null);
            } else {
                nameCallback.setName(username);
            }
        }
    }
}
 
Example #19
Source File: NegotiateCallbackHandler.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
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");
        }
    }
}
 
Example #20
Source File: NegotiateCallbackHandler.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
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");
        }
    }
}
 
Example #21
Source File: NegotiateCallbackHandler.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
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");
        }
    }
}
 
Example #22
Source File: RbacAdminCallbackHandler.java    From wildfly-core with GNU Lesser General Public License v2.1 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(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 #23
Source File: NegotiateCallbackHandler.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
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");
        }
    }
}
 
Example #24
Source File: PlainFactory.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override
public SaslClient createSaslClient(final UserGroupInformation ugi, final Map<String, ?> properties)
    throws SaslException {
  final String userName = (String) properties.get(DrillProperties.USER);
  final String password = (String) properties.get(DrillProperties.PASSWORD);

  return FastSaslClientFactory.getInstance().createSaslClient(new String[]{SIMPLE_NAME},
      null /** authorization ID */, null, null, properties, new CallbackHandler() {
        @Override
        public void handle(final Callback[] callbacks) throws IOException, UnsupportedCallbackException {
          for (final Callback callback : callbacks) {
            if (callback instanceof NameCallback) {
              NameCallback.class.cast(callback).setName(userName);
              continue;
            }
            if (callback instanceof PasswordCallback) {
              PasswordCallback.class.cast(callback).setPassword(password.toCharArray());
              continue;
            }
            throw new UnsupportedCallbackException(callback);
          }
        }
      });
}
 
Example #25
Source File: GssapiMechanism.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (int i = 0; i < callbacks.length; i++) {
        Callback cb = callbacks[i];
        if (cb instanceof NameCallback) {
            ((NameCallback) cb).setName(getUsername());
        } else if (cb instanceof PasswordCallback) {
            String pass = getPassword();
            if (pass != null) {
                ((PasswordCallback) cb).setPassword(pass.toCharArray());
            }
        } else {
            throw new UnsupportedCallbackException(cb);
        }
    }
}
 
Example #26
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 #27
Source File: LoginModuleOptions.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public void handle(Callback[] callbacks) {
    for (Callback callback : callbacks) {
        System.err.println(callback);
        if (callback instanceof NameCallback) {
            System.err.println("name is " + name);
            ((NameCallback) callback).setName(name);
        }
        if (callback instanceof PasswordCallback) {
            System.err.println("pass is " + new String(password));
            ((PasswordCallback) callback).setPassword(password);
        }
    }
}
 
Example #28
Source File: HttpRequestBasedCallbackHandlerTest.java    From incubator-retired-wave with Apache License 2.0 5 votes vote down vote up
public void testBindsUsernameAndPassword() throws IOException, UnsupportedCallbackException {
  MultiMap<String> args = new MultiMap<String>();
  args.add("address", "[email protected]");
  args.add("password", "internet");

  CallbackHandler handler = new HttpRequestBasedCallbackHandler(args);
  Callback[] callbacks =
      new Callback[] {new NameCallback("ignored"), new PasswordCallback("ignored", false),};

  handler.handle(callbacks);

  assertEquals("[email protected]", ((NameCallback) callbacks[0]).getName());
  assertEquals("internet", new String(((PasswordCallback) callbacks[1]).getPassword()));
}
 
Example #29
Source File: CleanState.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
void go() throws Exception {
    Krb5LoginModule krb5 = new Krb5LoginModule();

    final String name = OneKDC.USER;
    final char[] password = OneKDC.PASS;
    char[] badpassword = "hellokitty".toCharArray();

    Map<String,String> map = new HashMap<>();
    map.put("useTicketCache", "false");
    map.put("doNotPrompt", "false");
    map.put("tryFirstPass", "true");
    Map<String,Object> shared = new HashMap<>();
    shared.put("javax.security.auth.login.name", name);
    shared.put("javax.security.auth.login.password", badpassword);

    krb5.initialize(new Subject(), new CallbackHandler() {
        @Override
        public void handle(Callback[] callbacks) {
            for(Callback callback: callbacks) {
                if (callback instanceof NameCallback) {
                    ((NameCallback)callback).setName(name);
                }
                if (callback instanceof PasswordCallback) {
                    ((PasswordCallback)callback).setPassword(password);
                }
            }
        }
    }, shared, map);
    krb5.login();
}
 
Example #30
Source File: KerberosAuth.java    From attic-apex-core 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);
    }
  }
}