package com.dbxml.db.common.security;

/*
 * dbXML - Native XML Database
 * Copyright (C) 1999-2004  The dbXML Group, L.L.C.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: SimpleSecurityManager.java,v 1.2 2004/02/12 00:17:56 bradford Exp $
 */

import com.dbxml.db.core.security.InvalidCredentialsException;
import com.dbxml.db.core.security.InvalidUserException;
import com.dbxml.db.core.security.SecurityException;
import com.dbxml.util.Configuration;
import com.dbxml.util.dbXMLException;

/**
 * SimpleSecurityManager is an incredibly simple SecurityManager
 * implementation that allows a single User ID and password to be
 * defined for the entire database.  The User ID and password are
 * defined in the system.xml file instead of being stored in the
 * database itself.
 */

public final class SimpleSecurityManager extends SecurityManagerBase {
   private static final String USERID = "userid";
   private static final String PASSWORD = "password";

   private String userID;
   private String password;

   public void setConfig(Configuration config) throws dbXMLException {
      super.setConfig(config);
      userID = config.getAttribute(USERID);
      password = config.getAttribute(PASSWORD);
   }

   public void authenticate(String userID, String password) throws SecurityException {
      if ( !this.userID.equals(userID) )
         throw new InvalidUserException("Unknown User '"+userID+"'");
      if ( !this.password.equals(password) )
         throw new InvalidCredentialsException("Invalid credentials for User '"+userID+"'");
   }

   public void access(String path, int mask) throws SecurityException {
   }
}
