Package javax.security.sasl

Examples of javax.security.sasl.SaslException


        {
            return Sasl.createSaslServer(mechanism, "AMQP", localFQDN, null, new SimpleCramMd5CallbackHandler());
        }
        else
        {
            throw new SaslException("Unknown mechanism: " + mechanism);
        }
    }
View Full Code Here


                        String password = _users.get(username);
                        ((PasswordCallback) callback).setPassword(password.toCharArray());
                    }
                    else
                    {
                        throw new SaslException("Authentication failed");
                    }
                }
                else if (callback instanceof AuthorizeCallback)
                {
                    ((AuthorizeCallback) callback).setAuthorized(true);
View Full Code Here

    public byte[] evaluateResponse(byte[] response) throws SaslException
    {
        int authzidNullPosition = findNullPosition(response, 0);
        if (authzidNullPosition < 0)
        {
            throw new SaslException("Invalid PLAIN encoding, authzid null terminator not found");
        }
        int authcidNullPosition = findNullPosition(response, authzidNullPosition + 1);
        if (authcidNullPosition < 0)
        {
            throw new SaslException("Invalid PLAIN encoding, authcid null terminator not found");
        }

        PlainPasswordCallback passwordCb;
        AuthorizeCallback authzCb;

        try
        {
            // we do not currently support authcid in any meaningful way
            String authzid = new String(response, authzidNullPosition + 1, authcidNullPosition - authzidNullPosition - 1, "utf8");

            // TODO: should not get pwd as a String but as a char array...
            int passwordLen = response.length - authcidNullPosition - 1;
            String pwd = new String(response, authcidNullPosition + 1, passwordLen, "utf8");

            // we do not care about the prompt but it throws if null
            NameCallback nameCb = new NameCallback("prompt", authzid);
            passwordCb = new PlainPasswordCallback("prompt", false, pwd);
            authzCb = new AuthorizeCallback(authzid, authzid);

            Callback[] callbacks = new Callback[]{nameCb, passwordCb, authzCb};
            _cbh.handle(callbacks);

        }
        catch (IOException e)
        {
            if(e instanceof SaslException)
            {
                throw (SaslException) e;
            }
            throw new SaslException("Error processing data: " + e, e);
        }
        catch (UnsupportedCallbackException e)
        {
            throw new SaslException("Unable to obtain data from callback handler: " + e, e);
        }

        if (passwordCb.isAuthenticated())
        {
            _complete = true;
        }

        if (authzCb.isAuthorized() && _complete)
        {
            _authorizationId = authzCb.getAuthenticationID();
            return null;
        }
        else
        {
            throw new SaslException("Authentication failed");
        }
    }
View Full Code Here

        return _authorizationId;
    }

    public byte[] unwrap(byte[] incoming, int offset, int len) throws SaslException
    {
        throw new SaslException("Unsupported operation");
    }
View Full Code Here

        throw new SaslException("Unsupported operation");
    }

    public byte[] wrap(byte[] outgoing, int offset, int len) throws SaslException
    {
        throw new SaslException("Unsupported operation");
    }
View Full Code Here

            bch = buf[i];

            if (gettingKey) {
                if (bch == ',') {
                    if (key.size() != 0) {
                        throw new SaslException("Directive key contains a ',':"
                                + key);
                    }

                    // Empty element, skip separator and lws
                    i = skipLws(buf, i + 1);
                } else if (bch == '=') {
                    if (key.size() == 0) {
                        throw new SaslException("Empty directive key");
                    }

                    gettingKey = false; // Termination of key
                    i = skipLws(buf, i + 1); // Skip to next non whitespace

                    // Check whether value is quoted
                    if (i < buf.length) {
                        if (buf[i] == '"') {
                            gettingQuotedValue = true;
                            ++i; // Skip quote
                        }
                    } else {
                        throw new SaslException("Valueless directive found: "
                                + key.toString());
                    }
                } else if (isLws(bch)) {
                    // LWS that occurs after key
                    i = skipLws(buf, i + 1);

                    // Expecting '='
                    if (i < buf.length) {
                        if (buf[i] != '=') {
                            throw new SaslException("'=' expected after key: "
                                    + key.toString());
                        }
                    } else {
                        throw new SaslException("'=' expected after key: "
                                + key.toString());
                    }
                } else {
                    key.write(bch); // Append to key
                    ++i; // Advance
                }
            } else if (gettingQuotedValue) {
                // Getting a quoted value
                if (bch == '\\') {
                    // quoted-pair = "\" CHAR ==> CHAR
                    ++i; // Skip escape
                    if (i < buf.length) {
                        value.write(buf[i]);
                        ++i; // Advance
                    } else {
                        // Trailing escape in a quoted value
                        throw new SaslException(
                                "Unmatched quote found for directive: "
                                        + key.toString() + " with value: "
                                        + value.toString());
                    }
                } else if (bch == '"') {
                    // closing quote
                    ++i; // Skip closing quote
                    gettingQuotedValue = false;
                    expectSeparator = true;
                } else {
                    value.write(bch);
                    ++i; // Advance
                }
            } else if (isLws(bch) || bch == ',') {
                // Value terminated
                extractDirective(map, key.toString(), value.toString());
                key.reset();
                value.reset();
                gettingKey = true;
                gettingQuotedValue = expectSeparator = false;
                i = skipLws(buf, i + 1); // Skip separator and LWS
            } else if (expectSeparator) {
                throw new SaslException(
                        "Expecting comma or linear whitespace after quoted string: \""
                                + value.toString() + "\"");
            } else {
                value.write(bch); // Unquoted value
                ++i; // Advance
            }
        }

        if (gettingQuotedValue) {
            throw new SaslException("Unmatched quote found for directive: "
                    + key.toString() + " with value: " + value.toString());
        }

        // Get last pair
        if (key.size() > 0) {
View Full Code Here

     * if the key already has a value.
     */
    private static void extractDirective(HashMap<String, String> map,
            String key, String value) throws SaslException {
        if (map.get(key) != null) {
            throw new SaslException("Peer sent more than one " + key
                    + " directive");
        }

        map.put(key, value);
    }
View Full Code Here

        new TestPlainCallbacks.Server("user", "pass"));
  }

  @Test
  public void testSaslPlainServerBadPassword() {
    SaslException e = null;
    try {
      runNegotiation(
          new TestPlainCallbacks.Client("user", "pass1"),
          new TestPlainCallbacks.Server("user", "pass2"));
    } catch (SaslException se) {
      e = se;
    }
    assertNotNull(e);
    assertEquals("PLAIN auth failed: wrong password", e.getMessage());
  }
View Full Code Here

      switch (saslMessage.getState()) {
        case WRAP: {
          if (!saslContextEstablished || !useWrap) {
            throw new WrappedRpcServerException(
                RpcErrorCodeProto.FATAL_INVALID_RPC_HEADER,
                new SaslException("Server is not wrapping data"));
          }
          // loops over decoded data and calls processOneRpc
          unwrapPacketAndProcessRpcs(saslMessage.getToken().toByteArray());
          break;
        }
View Full Code Here

    private void saslProcess(RpcSaslProto saslMessage)
        throws WrappedRpcServerException, IOException, InterruptedException {
      if (saslContextEstablished) {
        throw new WrappedRpcServerException(
            RpcErrorCodeProto.FATAL_INVALID_RPC_HEADER,
            new SaslException("Negotiation is already complete"));
      }
      RpcSaslProto saslResponse = null;
      try {
        try {
          saslResponse = processSaslMessage(saslMessage);
View Full Code Here

TOP

Related Classes of javax.security.sasl.SaslException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.