Esempio n. 1
0
    public void process(final HttpRequest request, final HttpContext context)
        throws HttpException, IOException {

      AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);

      if (authState.getAuthScheme() != null || authState.hasAuthOptions()) {
        return;
      }

      // If no authState has been established and this is a PUT or POST request, add preemptive
      // authorisation
      String requestMethod = request.getRequestLine().getMethod();
      if (requestMethod.equals(HttpPut.METHOD_NAME) || requestMethod.equals(HttpPost.METHOD_NAME)) {
        CredentialsProvider credentialsProvider =
            (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
        HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        Credentials credentials =
            credentialsProvider.getCredentials(
                new AuthScope(targetHost.getHostName(), targetHost.getPort()));
        if (credentials == null) {
          throw new HttpException("No credentials for preemptive authentication");
        }
        authState.update(authScheme, credentials);
      }
    }
  /**
   * Processes the given collection of challenges and updates the {@link AuthState state} of the
   * authentication process.
   *
   * @param challenges the collection of authentication challenges
   * @return the {@link AuthScheme authentication scheme} used to process the challenge
   * @throws AuthChallengeException if authentication challenges cannot be successfully processed or
   *     the preferred authentication scheme cannot be determined
   */
  public AuthScheme processChallenge(final AuthState state, final Map challenges)
      throws MalformedChallengeException, AuthenticationException {
    if (state == null) {
      throw new IllegalArgumentException("Authentication state may not be null");
    }
    if (challenges == null) {
      throw new IllegalArgumentException("Challenge map may not be null");
    }

    if (state.isPreemptive() || state.getAuthScheme() == null) {
      // Authentication not attempted before
      state.setAuthScheme(selectAuthScheme(challenges));
    }
    AuthScheme authscheme = state.getAuthScheme();
    String id = authscheme.getSchemeName();
    //       //  if (LOG.isDebugEnabled()) {
    //           //   LOG.debug("Using authentication scheme: " + id);
    //        }
    String challenge = (String) challenges.get(id.toLowerCase());
    if (challenge == null) {
      throw new AuthenticationException(id + " authorization challenge expected, but not found");
    }
    authscheme.processChallenge(challenge);
    // LOG.debug("Authorization challenge processed");
    return authscheme;
  }
 public AuthScheme processChallenge(AuthState paramAuthState, Map paramMap)
     throws MalformedChallengeException, AuthenticationException {
   if (paramAuthState == null)
     throw new IllegalArgumentException("Authentication state may not be null");
   if (paramMap == null) throw new IllegalArgumentException("Challenge map may not be null");
   if ((paramAuthState.isPreemptive()) || (paramAuthState.getAuthScheme() == null))
     paramAuthState.setAuthScheme(selectAuthScheme(paramMap));
   AuthScheme localAuthScheme = paramAuthState.getAuthScheme();
   String str1 = localAuthScheme.getSchemeName();
   if (LOG.isDebugEnabled()) LOG.debug("Using authentication scheme: " + str1);
   String str2 = (String) paramMap.get(str1.toLowerCase());
   if (str2 == null)
     throw new AuthenticationException(str1 + " authorization challenge expected, but not found");
   localAuthScheme.processChallenge(str2);
   LOG.debug("Authorization challenge processed");
   return localAuthScheme;
 }
Esempio n. 4
0
    @Override
    public void process(final HttpRequest request, final HttpContext context) {
      AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);

      // If no auth scheme avaialble yet, try to initialize it
      // preemptively
      if (authState.getAuthScheme() == null) {
        AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth");
        if (authScheme != null) {
          CredentialsProvider credsProvider =
              (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
          HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
          Credentials creds =
              credsProvider.getCredentials(
                  new AuthScope(targetHost.getHostName(), targetHost.getPort()));
          if (creds == null) {
            throw new RuntimeException("No credentials for preemptive authentication");
          }
          authState.setAuthScheme(authScheme);
          authState.setCredentials(creds);
        }
      }
    }