/**
  * @see org.eclipse.jetty.server.Authentication.Deferred#login(java.lang.String, java.lang.String)
  */
 public Authentication login(String username, Object password, ServletRequest request) {
   UserIdentity identity = _authenticator.login(username, password, request);
   if (identity != null) {
     IdentityService identity_service = _authenticator.getLoginService().getIdentityService();
     UserAuthentication authentication = new UserAuthentication("API", identity);
     if (identity_service != null) _previousAssociation = identity_service.associate(identity);
     return authentication;
   }
   return null;
 }
  /**
   * @see
   *     org.eclipse.jetty.server.Authentication.Deferred#authenticate(javax.servlet.ServletRequest,
   *     javax.servlet.ServletResponse)
   */
  public Authentication authenticate(ServletRequest request, ServletResponse response) {
    try {
      LoginService login_service = _authenticator.getLoginService();
      IdentityService identity_service = login_service.getIdentityService();

      Authentication authentication = _authenticator.validateRequest(request, response, true);
      if (authentication instanceof Authentication.User && identity_service != null)
        _previousAssociation =
            identity_service.associate(((Authentication.User) authentication).getUserIdentity());
      return authentication;
    } catch (ServerAuthException e) {
      LOG.debug(e);
    }
    return this;
  }
  public UserIdentity login(String userName, Object credential) {
    // AuthFactory supports both a bare username, as well as user@domain. However, UserManager only
    // accepts the bare
    // username. If the provided value includes a domain, use only the node-part (after verifying
    // that it's actually
    // a user of our domain).
    final String[] parts = userName.split("@", 2);
    if (parts.length > 1) {
      if (XMPPServer.getInstance().getServerInfo().getXMPPDomain().equals(parts[1])) {
        userName = parts[0];
      } else {
        Log.error("access denied, unknown domain" + userName);
        return null;
      }
    }

    UserIdentity identity = null;

    if (identities.containsKey(userName)) {
      identity = identities.get(userName);

      if (authTokens.containsKey(userName) == false) {
        Log.debug("UserIdentity login " + userName + " ");

        try {

          AuthToken authToken = AuthFactory.authenticate(userName, (String) credential);
          authTokens.put(userName, authToken);

        } catch (UnauthorizedException e) {
          Log.error("access denied, bad password " + userName);
          return null;

        } catch (Exception e) {
          Log.error("access denied " + userName);
          return null;
        }
      }

    } else {

      Log.debug("UserIdentity login " + userName + " ");

      try {
        userManager.getUser(userName);
      } catch (UserNotFoundException e) {
        Log.error("user not found " + userName, e);
        return null;
      }

      try {

        AuthToken authToken = AuthFactory.authenticate(userName, (String) credential);
        authTokens.put(userName, authToken);

      } catch (UnauthorizedException e) {
        Log.error("access denied, bad password " + userName);
        return null;

      } catch (Exception e) {
        Log.error("access denied " + userName);
        return null;
      }

      Principal userPrincipal = new KnownUser(userName, credential);
      Subject subject = new Subject();
      subject.getPrincipals().add(userPrincipal);
      subject.getPrivateCredentials().add(credential);
      subject.getPrincipals().add(new RolePrincipal("ofmeet"));
      subject.setReadOnly();

      identity = _identityService.newUserIdentity(subject, userPrincipal, new String[] {"ofmeet"});
      identities.put(userName, identity);
    }

    return identity;
  }
Exemplo n.º 4
0
  /* ------------------------------------------------------------ */
  public UserIdentity login(final String username, final Object credentials) {
    try {
      CallbackHandler callbackHandler = null;

      if (_callbackHandlerClass == null) {
        callbackHandler =
            new CallbackHandler() {
              public void handle(Callback[] callbacks)
                  throws IOException, UnsupportedCallbackException {
                for (Callback callback : callbacks) {
                  if (callback instanceof NameCallback) {
                    ((NameCallback) callback).setName(username);
                  } else if (callback instanceof PasswordCallback) {
                    ((PasswordCallback) callback)
                        .setPassword((char[]) credentials.toString().toCharArray());
                  } else if (callback instanceof ObjectCallback) {
                    ((ObjectCallback) callback).setObject(credentials);
                  } else if (callback instanceof RequestParameterCallback) {
                    HttpChannel channel = HttpChannel.getCurrentHttpChannel();

                    if (channel == null) return;
                    Request request = channel.getRequest();

                    if (request != null) {
                      RequestParameterCallback rpc = (RequestParameterCallback) callback;
                      rpc.setParameterValues(
                          Arrays.asList(request.getParameterValues(rpc.getParameterName())));
                    }
                  } else throw new UnsupportedCallbackException(callback);
                }
              }
            };
      } else {
        Class clazz = Loader.loadClass(getClass(), _callbackHandlerClass);
        callbackHandler = (CallbackHandler) clazz.newInstance();
      }
      // set up the login context
      // TODO jaspi requires we provide the Configuration parameter
      Subject subject = new Subject();
      LoginContext loginContext = new LoginContext(_loginModuleName, subject, callbackHandler);

      loginContext.login();

      // login success
      JAASUserPrincipal userPrincipal =
          new JAASUserPrincipal(getUserName(callbackHandler), subject, loginContext);
      subject.getPrincipals().add(userPrincipal);

      return _identityService.newUserIdentity(subject, userPrincipal, getGroups(subject));
    } catch (LoginException e) {
      LOG.warn(e);
    } catch (IOException e) {
      LOG.warn(e);
    } catch (UnsupportedCallbackException e) {
      LOG.warn(e);
    } catch (InstantiationException e) {
      LOG.warn(e);
    } catch (IllegalAccessException e) {
      LOG.warn(e);
    } catch (ClassNotFoundException e) {
      LOG.warn(e);
    }
    return null;
  }