View Javadoc

1   package com.bradmcevoy.http.http11.auth;
2   
3   import com.bradmcevoy.http.Auth;
4   import com.bradmcevoy.http.Auth.Scheme;
5   import com.bradmcevoy.http.AuthenticationHandler;
6   import com.bradmcevoy.http.Request;
7   import com.bradmcevoy.http.Resource;
8   import org.slf4j.Logger;
9   import org.slf4j.LoggerFactory;
10  
11  /**
12   *
13   * @author brad
14   */
15  public class BasicAuthHandler implements AuthenticationHandler {
16  
17      private static final Logger log = LoggerFactory.getLogger( BasicAuthHandler.class );
18  
19      public boolean supports( Resource r, Request request ) {
20          Auth auth = request.getAuthorization();
21          if( auth == null ) {
22              return false;
23          }
24          log.trace( "supports: {}", auth.getScheme() );
25          return auth.getScheme().equals( Scheme.BASIC );
26      }
27  
28      public Object authenticate( Resource resource, Request request ) {
29          log.trace( "authenticate" );
30          Auth auth = request.getAuthorization();
31          Object o = resource.authenticate( auth.getUser(), auth.getPassword() );
32          log.trace( "result: {}", o );
33          return o;
34      }
35  
36      public String getChallenge( Resource resource, Request request ) {
37          return "Basic realm=\"" + resource.getRealm() + "\"";
38      }
39  
40      public boolean isCompatible( Resource resource ) {
41          return true;
42      }
43  }