Example usage for javax.security.sasl SaslClient getMechanismName

List of usage examples for javax.security.sasl SaslClient getMechanismName

Introduction

In this page you can find the example usage for javax.security.sasl SaslClient getMechanismName.

Prototype

public abstract String getMechanismName();

Source Link

Document

Returns the IANA-registered mechanism name of this SASL client.

Usage

From source file:com.fluffypeople.managesieve.ManageSieveClient.java

/**
 * Authenticate against the remote server using SASL.
 *
 * The CallbackHandler should be setup appropriately, for example:
 * <pre>//w w w  . ja v a 2  s.  c  om
 * <code>
 *
 * CallbackHandler cbh = new CallbackHandler() {
 *     public void handle(Callback[] clbcks) throws IOException,  UnsupportedCallbackException {
 *         for (Callback cb : clbcks) {
 *             if (cb instanceof NameCallback) {
 *                 NameCallback name = (NameCallback) cb;
 *                 name.setName("user");
 *             } else if (cb instanceof PasswordCallback) {
 *                 PasswordCallback passwd = (PasswordCallback) cb;
 *                 passwd.setPassword("secret".toCharArray());
 *             }
 *         }
 *     }
 * };
 * </code>
 * </pre>
 *
 * @param cbh CallbackHandler[] list of call backs that will be called by
 * the SASL code
 * @return ManageSieveResponse from the server, OK is authenticated, NO
 * means a problem
 * @throws SaslException
 * @throws IOException
 * @throws ParseException
 */
public synchronized ManageSieveResponse authenticate(final CallbackHandler cbh)
        throws SaslException, IOException, ParseException {

    SaslClient sc = Sasl.createSaslClient(cap.getSASLMethods(), null, "sieve", hostname, null, cbh);

    String mechanism = escapeString(sc.getMechanismName());
    if (sc.hasInitialResponse()) {
        byte[] ir = sc.evaluateChallenge(new byte[0]);
        String ready = new String(Base64.encodeBase64(ir));
        ready = encodeString(ready.trim());
        sendCommand("AUTHENTICATE", mechanism, ready);
    } else {
        sendCommand("AUTHENTICATE", mechanism);
    }

    int token;
    ManageSieveResponse resp = null;
    do {
        token = in.nextToken();
        if (token == DQUOTE) {
            // String - so more data for the auth sequence
            in.pushBack();
            String msg = parseString();
            byte[] response = sc.evaluateChallenge(msg.getBytes());
            sendLine(encodeString(new String(response)));
        } else if (token == StreamTokenizer.TT_WORD) {
            in.pushBack();
            resp = parseResponse();
            break;
        } else {
            throw new ParseException(
                    "Expecting DQUOTE/WORD, got " + tokenToString(token) + " at line " + in.lineno());
        }
    } while (!sc.isComplete());

    // Complete
    sc.dispose();
    return resp;
}

From source file:org.wildfly.security.sasl.entity.EntityTest.java

@Test
public void testServerAuthIndirect_Client() throws Exception {
    Map<String, Object> props = new HashMap<String, Object>();

    // No properties are set, an appropriate EntitySaslClient should be returned
    SaslClient client = Sasl.createSaslClient(
            new String[] { SaslMechanismInformation.Names.IEC_ISO_9798_U_RSA_SHA1_ENC }, "TestUser",
            "TestProtocol", "TestServer", props, null);
    assertEquals(EntitySaslClient.class, client.getClass());
    assertEquals(SaslMechanismInformation.Names.IEC_ISO_9798_U_RSA_SHA1_ENC, client.getMechanismName());

    // If we set SERVER_AUTH to true even though only unilateral mechanisms are specified, no client should be returned
    props.put(Sasl.SERVER_AUTH, Boolean.toString(true));
    client = Sasl.createSaslClient(
            new String[] { SaslMechanismInformation.Names.IEC_ISO_9798_U_RSA_SHA1_ENC,
                    SaslMechanismInformation.Names.IEC_ISO_9798_U_DSA_SHA1,
                    SaslMechanismInformation.Names.IEC_ISO_9798_U_ECDSA_SHA1 },
            "TestUser", "TestProtocol", "TestServer", props, null);
    assertNull(client);//from www.j  a  v  a 2 s  .  co  m

    // If we set SERVER_AUTH to true, an appropriate EntitySaslClient should be returned
    props.put(Sasl.SERVER_AUTH, Boolean.toString(true));
    client = Sasl.createSaslClient(
            new String[] { SaslMechanismInformation.Names.IEC_ISO_9798_U_RSA_SHA1_ENC,
                    SaslMechanismInformation.Names.IEC_ISO_9798_U_DSA_SHA1,
                    SaslMechanismInformation.Names.IEC_ISO_9798_U_ECDSA_SHA1,
                    SaslMechanismInformation.Names.IEC_ISO_9798_M_RSA_SHA1_ENC,
                    SaslMechanismInformation.Names.IEC_ISO_9798_M_DSA_SHA1,
                    SaslMechanismInformation.Names.IEC_ISO_9798_M_ECDSA_SHA1 },
            "TestUser", "TestProtocol", "TestServer", props, null);
    assertEquals(EntitySaslClient.class, client.getClass());
    assertEquals(SaslMechanismInformation.Names.IEC_ISO_9798_M_RSA_SHA1_ENC, client.getMechanismName());
}