Java Code Examples for javax.security.auth.callback.Callback

The following examples show how to use javax.security.auth.callback.Callback. 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: lams   Source File: LdapCallbackHandler.java    License: GNU General Public License v2.0 6 votes vote down vote up
public void handle(Callback[] callbacks) throws IOException,
UnsupportedCallbackException
{
	if(userName == null)
	{
		userName = getUserName(callbacks);
	}
	for (int i = 0; i < callbacks.length; i++)
	{
		Callback callback = callbacks[i];
		try
		{
			this.handleCallBack( callback );
		}
		catch (NamingException e)
		{
			throw new IOException(e);
		}
	}
}
 
Example 2
Source Project: steady   Source File: WSSUsernameCallbackHandler.java    License: Apache License 2.0 6 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 DelegationCallback) {
            DelegationCallback callback = (DelegationCallback) callbacks[i];
            Message message = callback.getCurrentMessage();
            
            String username = 
                (String)message.getContextualProperty(SecurityConstants.USERNAME);
            if (username != null) {
                Node contentNode = message.getContent(Node.class);
                Document doc = null;
                if (contentNode != null) {
                    doc = contentNode.getOwnerDocument();
                } else {
                    doc = DOMUtils.createDocument();
                }
                UsernameToken usernameToken = createWSSEUsernameToken(username, doc);
                callback.setToken(usernameToken.getElement());
            }
        } else {
            throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
        }
    }
}
 
Example 3
Source Project: steady   Source File: SAML1CallbackHandler.java    License: Apache License 2.0 6 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 SAMLCallback) {
            SAMLCallback callback = (SAMLCallback) callbacks[i];
            callback.setSamlVersion(SAMLVersion.VERSION_11);
            SubjectBean subjectBean = 
                new SubjectBean(
                    subjectName, subjectQualifier, confirmationMethod
                );
            if (SAML1Constants.CONF_HOLDER_KEY.equals(confirmationMethod)) {
                try {
                    KeyInfoBean keyInfo = createKeyInfo();
                    subjectBean.setKeyInfo(keyInfo);
                } catch (Exception ex) {
                    throw new IOException("Problem creating KeyInfo: " +  ex.getMessage());
                }
            }
            createAndSetStatement(subjectBean, callback);
        } else {
            throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
        }
    }
}
 
Example 4
Source Project: lams   Source File: DigestCallbackHandler.java    License: GNU General Public License v2.0 6 votes vote down vote up
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    boolean foundCallback = false;
    Callback firstUnknown = null;
    int count = callbacks != null ? callbacks.length : 0;
    for (int n = 0; n < count; n++) {
        Callback c = callbacks[n];
        if (c instanceof MapCallback) {
            MapCallback mc = (MapCallback) c;
            mc.setInfo(RFC2617Digest.USERNAME, username);
            mc.setInfo(RFC2617Digest.CNONCE, cnonce);
            mc.setInfo(RFC2617Digest.NONCE, nonce);
            mc.setInfo(RFC2617Digest.NONCE_COUNT, nc);
            mc.setInfo(RFC2617Digest.QOP, qop);
            mc.setInfo(RFC2617Digest.REALM, realm);
            mc.setInfo(RFC2617Digest.A2HASH, md5a2);
            foundCallback = true;
        } else if (firstUnknown == null) {
            firstUnknown = c;
        }
    }
    if (foundCallback == false)
        throw PicketBoxMessages.MESSAGES.unableToHandleCallback(firstUnknown, this.getClass().getName(),
                firstUnknown.getClass().getCanonicalName());
}
 
Example 5
Source Project: Bats   Source File: KerberosFactory.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void handle(final Callback[] callbacks) throws IOException, UnsupportedCallbackException {
  for (final Callback callback : callbacks) {
    if (callback instanceof AuthorizeCallback) {
      final AuthorizeCallback authorizeCallback = (AuthorizeCallback) callback;
      if (!authorizeCallback.getAuthenticationID()
          .equals(authorizeCallback.getAuthorizationID())) {
        throw new SaslException("Drill expects authorization ID and authentication ID to match. " +
            "Use inbound impersonation feature so one entity can act on behalf of another.");
      } else {
        authorizeCallback.setAuthorized(true);
      }
    } else {
      throw new UnsupportedCallbackException(callback);
    }
  }
}
 
Example 6
public void handle(Callback[] callbacks) throws IOException,
    UnsupportedCallbackException {
    for (int i = 0; i < callbacks.length; i++) {
        if (callbacks[i] instanceof WAuthCallback) {
            WAuthCallback callback = (WAuthCallback) callbacks[i];
            String authenticationType = (String)callback.getRequest().getAttribute("wauth");
            if (authenticationType == null || authenticationType.length() == 0) {
                LOG.debug("No authentication type found in request");
            } else {
                LOG.info("Authentication type '" + authenticationType + "' found in request");
                callback.setWauth(authenticationType);
            }

        } else {
            throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
        }
    }
}
 
Example 7
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 8
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 9
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (Callback current : callbacks) {
        if (current instanceof AuthorizeCallback) {
            AuthorizeCallback acb = (AuthorizeCallback) current;
            boolean authorized = acb.getAuthenticationID().equals(acb.getAuthorizationID());
            if (authorized == false) {
                SECURITY_LOGGER.tracef(
                        "Checking 'AuthorizeCallback', authorized=false, authenticationID=%s, authorizationID=%s.",
                        acb.getAuthenticationID(), acb.getAuthorizationID());
            }
            acb.setAuthorized(authorized);
        } else {
            throw new UnsupportedCallbackException(current);
        }
    }
}
 
Example 10
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 11
Source Project: cxf   Source File: TestUserPasswordLoginModule.java    License: 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 12
@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 13
Source Project: tomee   Source File: ScriptLoginModule.java    License: 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 14
Source Project: incubator-atlas   Source File: SSLAndKerberosTest.java    License: 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 15
Source Project: cxf-fediz   Source File: ClientIdHomeRealmDiscovery.java    License: Apache License 2.0 6 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 HomeRealmCallback) {
            HomeRealmCallback callback = (HomeRealmCallback) callbacks[i];

            HttpServletRequest request = callback.getRequest();
            String clientId = request.getParameter("client_id");

            if (clientId != null) {
                ApplicationContext ctx = ApplicationContextProvider.getApplicationContext();
                OAuthDataProvider dataManager = (OAuthDataProvider)ctx.getBean("oauthProvider");

                Client client = dataManager.getClient(clientId);
                if (client != null) {
                    callback.setHomeRealm(client.getHomeRealm());
                    LOG.debug("Retrieved home realm {}", callback.getHomeRealm());
                }

            }

        } else {
            LOG.warn("Callback is not an instance of HomeRealmCallback: {}", callbacks[i]);
        }
    }
}
 
Example 16
Source Project: ghidra   Source File: GhidraServer.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Callback[] getAuthenticationCallbacks() throws RemoteException {
	log.info("Authentication callbacks requested by " + RepositoryManager.getRMIClient());
	try {
		Callback[] callbacks =
			authModule != null ? authModule.getAuthenticationCallbacks() : null;
		if (sshAuthModule != null) {
			callbacks = sshAuthModule.addAuthenticationCallbacks(callbacks);
		}
		if (anonymousAuthModule != null &&
			(authModule == null || authModule.anonymousCallbacksAllowed())) {
			callbacks = anonymousAuthModule.addAuthenticationCallbacks(callbacks);
		}
		return callbacks;
	}
	catch (Throwable t) {
		log.error("Failed to generate authentication callbacks", t);
		throw new RemoteException("Failed to generate authentication callbacks", t);
	}
}
 
Example 17
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 18
Source Project: cxf   Source File: WSSUsernameCallbackHandler.java    License: Apache License 2.0 6 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 DelegationCallback) {
            DelegationCallback callback = (DelegationCallback) callbacks[i];
            Message message = callback.getCurrentMessage();

            String username =
                (String)SecurityUtils.getSecurityPropertyValue(SecurityConstants.USERNAME, message);
            if (username != null) {
                Node contentNode = message.getContent(Node.class);
                Document doc = null;
                if (contentNode != null) {
                    doc = contentNode.getOwnerDocument();
                } else {
                    doc = DOMUtils.getEmptyDocument();
                }
                UsernameToken usernameToken = createWSSEUsernameToken(username, doc);
                callback.setToken(usernameToken.getElement());
            }
        } else {
            throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
        }
    }
}
 
Example 19
Source Project: wildfly-samples   Source File: MyDeployer.java    License: MIT License 6 votes vote down vote up
static ModelControllerClient createClient(final InetAddress host, final int port,
        final String username, final String password) {

    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;
                    ncb.setName(username);
                } else if (current instanceof PasswordCallback) {
                    PasswordCallback pcb = (PasswordCallback) current;
                    pcb.setPassword(password.toCharArray());
                } else {
                    throw new UnsupportedCallbackException(current);
                }
            }
        }
    };

    return ModelControllerClient.Factory.create(host, port, callbackHandler);
}
 
Example 20
Source Project: MyVirtualDirectory   Source File: UpnPwdCallbackHandler.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 TextOutputCallback) {
			 TextOutputCallback tc = (TextOutputCallback) callback;
			 switch (tc.getMessageType()) {
			 	case TextOutputCallback.INFORMATION: logger.info(tc.getMessage()); break;
			 	case TextOutputCallback.ERROR: logger.error(tc.getMessage()); break;
			 	case TextOutputCallback.WARNING: logger.warn(tc.getMessage()); break;
			 }
		} else if (callback instanceof NameCallback) {
			NameCallback nc = (NameCallback) callback;
			nc.setName(upn);
		} else if (callback instanceof PasswordCallback) {
			PasswordCallback pc = (PasswordCallback) callback;
			pc.setPassword(this.password.toCharArray());
		}
	}

}
 
Example 21
Source Project: cxf   Source File: AbstractSTSClient.java    License: Apache License 2.0 6 votes vote down vote up
protected Element getDelegationSecurityToken(Object delegationObject) throws Exception {
    if (delegationObject != null) {
        final boolean isString = delegationObject instanceof String;
        final boolean isElement = delegationObject instanceof Element;
        final boolean isCallbackHandler = delegationObject instanceof CallbackHandler;
        if (isString) {
            final Document doc =
                StaxUtils.read(new StringReader((String) delegationObject));
            return doc.getDocumentElement();
        } else if (isElement) {
            return (Element) delegationObject;
        } else if (isCallbackHandler) {
            DelegationCallback callback = new DelegationCallback(message);
            ((CallbackHandler)delegationObject).handle(new Callback[]{callback});
            return callback.getToken();
        }
    }
    return null;
}
 
Example 22
Source Project: Tomcat8-Source-Read   Source File: CallbackHandlerImpl.java    License: MIT License 5 votes vote down vote up
@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {

    String name = null;
    Principal principal = null;
    Subject subject = null;
    String[] groups = null;

    if (callbacks != null) {
        // Need to combine data from multiple callbacks so use this to hold
        // the data
        // Process the callbacks
        for (Callback callback : callbacks) {
            if (callback instanceof CallerPrincipalCallback) {
                CallerPrincipalCallback cpc = (CallerPrincipalCallback) callback;
                name = cpc.getName();
                principal = cpc.getPrincipal();
                subject = cpc.getSubject();
            } else if (callback instanceof GroupPrincipalCallback) {
                GroupPrincipalCallback gpc = (GroupPrincipalCallback) callback;
                groups = gpc.getGroups();
            } else {
                // This is a singleton so need to get correct Logger for
                // current TCCL
                Log log = LogFactory.getLog(CallbackHandlerImpl.class);
                log.error(sm.getString("callbackHandlerImpl.jaspicCallbackMissing",
                        callback.getClass().getName()));
            }
        }

        // Create the GenericPrincipal
        Principal gp = getPrincipal(principal, name, groups);
        if (subject != null && gp != null) {
            subject.getPrivateCredentials().add(gp);
        }
    }
}
 
Example 23
Source Project: cxf-fediz   Source File: ClaimsCallbackHandler.java    License: 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 ClaimsCallback) {
            ClaimsCallback callback = (ClaimsCallback) callbacks[i];
            callback.setClaims(createClaims());

        } else {
            throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
        }
    }
}
 
Example 24
Source Project: knox   Source File: CredentialsProvider.java    License: 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 ) {
      String username = credentials.getUserPrincipal().getName();
      ((NameCallback)callback).setName( username );
    } else if ( callback instanceof PasswordCallback ) {
      String password = credentials.getPassword();
      ((PasswordCallback)callback).setPassword( password.toCharArray() );
    }
  }
}
 
Example 25
Source Project: j2objc   Source File: tmpCallbackHandler.java    License: Apache License 2.0 5 votes vote down vote up
public void handle(Callback[] callback) throws IOException,
        UnsupportedCallbackException {
    if (callback == null) {
        throw new UnsupportedCallbackException(null, "callback is null");
    }
    if (callback.length == 0) {
        throw new UnsupportedCallbackException(null, "callback is empty");
    }
}
 
Example 26
Source Project: cxf   Source File: SAMLTokenValidatorTest.java    License: 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 WSPasswordCallback) { // CXF
            WSPasswordCallback pc = (WSPasswordCallback) callbacks[i];
            if ("eve".equals(pc.getIdentifier())) {
                pc.setPassword("evekpass");
                break;
            }
        }
    }
}
 
Example 27
Source Project: openjdk-8   Source File: Confirm.java    License: GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    // Provide answer in an individual stream so that the program
    // does not block.
    System.setIn(new ByteArrayInputStream("1\n".getBytes()));

    new TextCallbackHandler().handle(new Callback[]{
        new ConfirmationCallback("Prince", ConfirmationCallback.INFORMATION,
                new String[]{"To be", "Not to be"}, 0)});
}
 
Example 28
Source Project: eet-client   Source File: ClientPasswordCallback.java    License: MIT License 5 votes vote down vote up
@Override
public void handle(final Callback[] callbacks) throws IOException, UnsupportedCallbackException {

    for (final Callback thisCallback : callbacks) {
        final WSPasswordCallback pwcb = (WSPasswordCallback) thisCallback;
        final String user = pwcb.getIdentifier();
        final int usage = pwcb.getUsage();
        if (usage == WSPasswordCallback.SIGNATURE && alias.equals(user)) {
            pwcb.setPassword(password);
        }
    }

}
 
Example 29
Source Project: roboconf-platform   Source File: AuthenticationManager.java    License: 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) callback).setName( this.username );
		else if( callback instanceof PasswordCallback )
			((PasswordCallback) callback).setPassword( this.password.toCharArray());
		else
			throw new UnsupportedCallbackException( callback );
	}
}
 
Example 30
private boolean checkLogin(AbstractKeycloakLoginModule loginModule, String username, String password, String clientId) {
    try {
        LOGGER.debug("Login for user {} ({})", username, clientId);
        Subject subject = new Subject();
        loginModule.initialize(
                subject,
                (Callback[] callbacks) -> {
                    ((NameCallback) callbacks[0]).setName(username);
                    ((PasswordCallback) callbacks[1]).setPassword(password.toCharArray());
                },
                SHARED_STATE,
                OPTIONS);
        boolean login = loginModule.login();
        if (login) {
            loginModule.commit();
            Client client = new Client(username);
            client.setLastSeen(Instant.now());
            client.setSubject(subject);
            CLIENTMAP.put(clientId, client);
        }
        return login;
    } catch (LoginException ex) {
        LOGGER.error("Login failed with exception: {}", ex.getMessage());
        LOGGER.debug("Exception:", ex);
        return false;
    }
}