Package javax.security.sasl

Examples of javax.security.sasl.SaslException


      case START:
        String mechanism = toString(frame);
        frame = readFrame();
        if (!mechanism.equalsIgnoreCase(sasl.getMechanismName())) {
          write(Status.FAIL, "Wrong mechanism: "+mechanism);
          throw new SaslException("Wrong mechanism: "+mechanism);
        }
      case CONTINUE:
        byte[] response;
        try {
          response = sasl.evaluate(frame.array());
          status = sasl.isComplete() ? Status.COMPLETE : Status.CONTINUE;
        } catch (SaslException e) {
          response = e.toString().getBytes("UTF-8");
          status = Status.FAIL;
        }
        write(status, response!=null ? ByteBuffer.wrap(response) : EMPTY);
        break;
      case COMPLETE:
        sasl.evaluate(frame.array());
        if (!sasl.isComplete())
          throw new SaslException("Expected completion!");
        break;
      case FAIL:
        throw new SaslException("Fail: "+toString(frame));
      default:
        throw new IOException("Unexpected SASL status: "+status);
      }
    }
    LOG.debug("SASL opened");
View Full Code Here


    public boolean hasInitialResponse() { return true; }
    public byte[] evaluateChallenge(byte[] challenge) throws SaslException {
      try {
        return System.getProperty("user.name").getBytes("UTF-8");
      } catch (IOException e) {
        throw new SaslException(e.toString());
      }
    }
View Full Code Here

            public byte[] evaluateResponse(byte[] response) throws SaslException
            {
                if (throwSaslException)
                {
                    throw new SaslException("Mocked exception");
                }
                return null;
            }

            public boolean isComplete()
View Full Code Here

//           if (alg.equals(ENCRYPTION_LS_MD5)) {
//             passwd = ls_digest(passwd);
//           } // end of if (alg != null && !alg.equals())
        } catch (NoSuchAlgorithmException e) {
          throw
            new SaslException("Password encrypting algorithm is not supported.",
              e);
        } // end of try-catch
    }
    } // end of if (passwd != null)

    if (callbackHandler == null) {
      throw new SaslException("Error: no CallbackHandler available.");
    }
    Callback[] callbacks = new Callback[3];
    NameCallback nc = new NameCallback("User name", user_id);
    PasswordCallback pc = new PasswordCallback("User password", false);
    RealmCallback rc = new RealmCallback("Put domain as realm.");
    callbacks[0] = nc;
    callbacks[1] = pc;
    callbacks[2] = rc;
    try {
      callbackHandler.handle(callbacks);
      char[] real_password = pc.getPassword();
      if (!Arrays.equals(real_password, passwd.toCharArray())) {
        throw new SaslException("Password missmatch.");
      }
      if (authoriz != null && !authoriz.isEmpty()) {
        String realm = rc.getText();
        callbacks = new Callback[1];
        AuthorizeCallback ac =
          new AuthorizeCallback(JIDUtils.getNodeID(user_id, realm), authoriz);
        callbacks[0] = ac;
        callbackHandler.handle(callbacks);
        if (ac.isAuthorized()) {
          auth_ok = true;
        } else {
          throw new SaslException("Not authorized.");
        } // end of else
      } else {
        auth_ok = true;
      } // end of if (authoriz != null && !authoriz.empty()) else
    } catch (Exception e) {
      throw new SaslException("Authorization error.", e);
    } // end of try-catch

    return null;
  }
View Full Code Here

    NameCallback nc = new NameCallback("User name", UUID.randomUUID().toString());
    callbacks[0] = nc;
    try {
      callbackHandler.handle(callbacks);
    } catch (Exception e) {
      throw new SaslException("Authorization error.", e);
    }
    return null;
  }
View Full Code Here

      return new SaslPLAIN(props, callbackHandler);
    } // end of if (mechanism.equals("PLAIN"))
//     if (mechanism.equals("DIGEST-MD5")) {
//       return new SaslDigestMD5(props, callbackHandler);
//     } // end of if (mechanism.equals("PLAIN"))
    throw new SaslException("Mechanism not supported yet.");
  }
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");
        } else {
            map.put(key, value);
        }
    }
View Full Code Here

        try
        {
            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");
            }

            // we do not currently support authcid in any meaningful way
            // String authcid = new String(response, 0, authzidNullPosition, "utf8");
            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);
            PlainPasswordCallback passwordCb = new PlainPasswordCallback("prompt", false, pwd);
            AuthorizeCallback authzCb = new AuthorizeCallback(authzid, authzid);

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

            if (passwordCb.isAuthenticated())
            {
                _complete = true;
            }
            if (authzCb.isAuthorized() && _complete)
            {
                _authorizationId = authzCb.getAuthenticationID();
                return null;
            }
            else
            {
                throw new SaslException("Authentication failed");
            }
        }
        catch (IOException e)
        {
            throw new SaslException("Error processing data: " + e, e);
        }
        catch (UnsupportedCallbackException e)
        {
            throw new SaslException("Unable to obtain data from callback handler: " + e, e);
        }
    }
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

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.