001    package org.hackystat.sensorbase.server;
002    
003    import org.hackystat.sensorbase.resource.users.UserManager;
004    import org.restlet.Context;
005    import org.restlet.Guard;
006    import org.restlet.data.ChallengeScheme;
007    import org.restlet.data.Request;
008    
009    /**
010     * Performs authentication of each HTTP request using HTTP Basic authentication. 
011     * 
012     * @author Philip Johnson
013     */
014    public class Authenticator extends Guard {
015      
016    
017      /**
018       * Initializes this Guard to do HTTP Basic authentication.
019       * @param context The server context.
020       */
021      public Authenticator (Context context) {
022        super(context, ChallengeScheme.HTTP_BASIC,  "SensorBase");
023      }
024      
025      /**
026       * Returns true if the passed credentials are OK.
027       * @param request The request. 
028       * @param identifier The account name.
029       * @param secret The password. 
030       * @return If the credentials are valid.
031       */
032      @Override public boolean checkSecret(Request request, String identifier, char[] secret) {
033        UserManager manager = (UserManager)getContext().getAttributes().get("UserManager");
034        //SensorBaseLogger.getLogger().info("Authenticating: " + identifier + " " + new String(secret));
035        return manager.isUser(identifier, new String(secret));
036      }
037    }