var when = require("when");
var pam = require("authenticate-pam");

module.exports = {
  type: "credentials",
  users: function(username){
    //returns a promise that checks the authorisation for a given user
    return when.promise(function(resolve) {
      resolve({username: username, permissions: "*"});
    });
  },
  authenticate: function(username, password) {
    //returns a promise that completes when the user has been authenticated
    return when.promise(function(resolve) {
      pam.authenticate(username, password,
                       function(err) {
                         if(err) {
                           resolve(null);
                         } else {
                           //Authenticated
                           resolve({username: username, permissions: "*"});
                         }
                       });

    });
  },
  default: function() {
    // Resolve with the user object for the default user.
    // If no default user exists, resolve with null.
    return when.promise(function(resolve) {
      resolve(null);
    });
  }
};
