Author: dimuthul
Date: Sun Feb 24 22:44:36 2008
New Revision: 14140

Log:

Adding email verifier code to identity.



Added:
   trunk/registry/modules/core/src/main/java/org/wso2/registry/users/verifier/
   
trunk/registry/modules/core/src/main/java/org/wso2/registry/users/verifier/EmailVerifier.java
   
trunk/registry/modules/core/src/main/java/org/wso2/registry/users/verifier/EmailVerifierConfig.java

Added: 
trunk/registry/modules/core/src/main/java/org/wso2/registry/users/verifier/EmailVerifier.java
==============================================================================
--- (empty file)
+++ 
trunk/registry/modules/core/src/main/java/org/wso2/registry/users/verifier/EmailVerifier.java
       Sun Feb 24 22:44:36 2008
@@ -0,0 +1,339 @@
+package org.wso2.registry.users.verifier;
+
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.Driver;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Random;
+import java.util.Map.Entry;
+
+import javax.mail.Address;
+import javax.mail.Message;
+import javax.mail.MessagingException;
+import javax.mail.Session;
+import javax.mail.Transport;
+import javax.mail.internet.AddressException;
+import javax.mail.internet.InternetAddress;
+import javax.mail.internet.MimeMessage;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.wso2.registry.users.UserRealm;
+import org.wso2.registry.users.UserStoreAdmin;
+import org.wso2.registry.users.UserStoreException;
+import org.wso2.registry.users.def.DefaultRealm;
+
+import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
+
+public class EmailVerifier {
+
+    private static Log log = LogFactory.getLog(DefaultRealm.class);
+
+    private static UserRealm realm = null;
+    private static EmailVerifierConfig config = null;
+
+    public static final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
+    public static final String PROTOCOL = "jdbc:derby:";
+    public static final String CONF_STRING = "confirmation";
+
+    private static final String ADD_USER_SQL = "insert into 
PENDING_USERS(username, email_address, password, confirmation) values(?, ?, ?, 
?)";
+    private static final String GET_USER_BY_CONF_SQL = "select * from 
PENDING_USERS where confirmation=?";
+    private static final String GET_USER_BY_USER_SQL = "select * from 
PENDING_USERS where username=?";
+    private static final String DELETE_USER_SQL = "delete from PENDING_USERS 
where username=?";
+    private static final String GET_USER_PROPERTY_SQL = "select * from 
PENDING_USER_PROPERTIES where username=?";
+    private static final String DELETE_USER_PROPERTY = "delete from 
PENDING_USER_PROPERTIES where username=?";
+    private static final String ADD_USER_PROPERTY_SQL = "insert into 
PENDING_USER_PROPERTIES(username, property_name, property_value) values(?, ?, 
?)";
+
+    private static Connection dbConnection = null;
+
+    /**
+     * Configures the Email Verifier
+     * 
+     * @param realmInstance -
+     *            The realm instance where the email verifier is going to call
+     *            addUser
+     * @param verifierConfig -
+     *            Email Verifierification config
+     */
+    public static void init(UserRealm realmInstance,
+            EmailVerifierConfig verifierConfig) throws UserStoreException {
+
+        if ((realmInstance == null) && (verifierConfig == null)) {
+            throw new RuntimeException("Invalid or null data provided");
+        }
+
+        realm = realmInstance;
+        config = verifierConfig;
+        createDatabase();
+    }
+
+    /**
+     * Returns the user name for matching config string. This can be used to 
get
+     * the user name at the time where user is confirmed by the email.
+     * 
+     * @param confString
+     * @return
+     * @throws UserStoreException
+     */
+    public String getUserName(String confString) throws UserStoreException {
+
+        try {
+            PreparedStatement getUserByConfStmt = dbConnection
+                    .prepareStatement(GET_USER_BY_CONF_SQL);
+            getUserByConfStmt.setString(1, confString);
+            ResultSet rs = getUserByConfStmt.executeQuery();
+            if (rs.next()) {
+                String username = rs.getString("username");
+                getUserByConfStmt.close();
+                return username;
+            }
+        } catch (SQLException e) {
+            throw new UserStoreException("error", e);
+        }
+
+        return null;
+    }
+
+    public boolean confirmUser(String confString) throws UserStoreException {
+        if (realm == null) {
+            throw new UserStoreException("initVerifier");
+        }
+
+        boolean isConfirmed = false;
+
+        try {
+            PreparedStatement getUserByConfStmt = dbConnection
+                    .prepareStatement(GET_USER_BY_CONF_SQL);
+            PreparedStatement getUserPropertyStmt = dbConnection
+                    .prepareStatement(GET_USER_PROPERTY_SQL);
+
+            PreparedStatement deleteUserStmt = dbConnection
+                    .prepareStatement(DELETE_USER_SQL);
+            PreparedStatement deleteUserPropertyStmt = dbConnection
+                    .prepareStatement(DELETE_USER_PROPERTY);
+
+            getUserByConfStmt.setString(1, confString);
+            ResultSet rs = getUserByConfStmt.executeQuery();
+            if (rs.next()) {
+                String username = rs.getString("username");
+                String password = rs.getString("password");
+                UserStoreAdmin usAdmin = realm.getUserStoreAdmin();
+                usAdmin.addUser(username, password);
+                isConfirmed = true;
+
+                getUserPropertyStmt.setString(1, username);
+                ResultSet propRS = getUserPropertyStmt.executeQuery();
+                Map props = new HashMap();
+                while (propRS.next()) {
+                    String key = propRS.getString("property_name");
+                    String value = propRS.getString("property_value");
+                    props.put(key, value);
+                }
+
+                usAdmin.setUserProperties(username, props);
+                deleteUserPropertyStmt.setString(1, username);
+                deleteUserPropertyStmt.executeUpdate();
+                deleteUserStmt.setString(1, username);
+                deleteUserStmt.executeUpdate();
+
+                dbConnection.commit();
+
+                getUserByConfStmt.close();
+                getUserPropertyStmt.close();
+                deleteUserStmt.close();
+                deleteUserPropertyStmt.close();
+            }
+        } catch (SQLException e) {
+            throw new UserStoreException("error", e);
+        }
+
+        return isConfirmed;
+    }
+
+    public void requestUserVerification(String username, String emailAddress,
+            String password, Map properties) throws UserStoreException {
+        if (config == null) {
+            throw new UserStoreException("initVerifier");
+        }
+
+        if (username == null || emailAddress == null || password == null) {
+            throw new UserStoreException("invalidData");
+        }
+
+        username = username.trim();
+        emailAddress = emailAddress.trim();
+        password = password.trim();
+
+        if (username.length() == 0 || emailAddress.length() == 0
+                || password.length() == 0) {
+            throw new UserStoreException("invalidData");
+        }
+
+        if (realm.getUserStoreReader().isExistingUser(username)) {
+            throw new UserStoreException("duplicateUser");
+        }
+
+        try {
+
+            PreparedStatement getUser = dbConnection
+                    .prepareStatement(GET_USER_BY_USER_SQL);
+            getUser.setString(1, username);
+            ResultSet rs = getUser.executeQuery();
+            if (rs.next()) {
+                throw new UserStoreException("duplicateUser");
+            }
+
+            PreparedStatement addUserStmt = dbConnection
+                    .prepareStatement(ADD_USER_SQL);
+            PreparedStatement addUserPropertyStmt = dbConnection
+                    .prepareStatement(ADD_USER_PROPERTY_SQL);
+
+            Random random = new Random();
+            byte[] temp = new byte[16];
+            random.nextBytes(temp);
+            String confString = Base64.encode(temp);
+            confString = confString.replaceAll("/", "a");
+            confString = confString.replaceAll("\\+", "b");
+            confString = confString.replaceAll("=", "c");
+            confString = confString.trim();
+            // TODO check whether user is there
+            String epr = config.getRegistrationServiceEPR();
+            sendEmail(confString, emailAddress, epr);
+            addUserStmt.setString(1, username);
+            addUserStmt.setString(2, emailAddress);
+            addUserStmt.setString(3, password);
+            addUserStmt.setString(4, confString);
+
+            addUserStmt.executeUpdate();
+
+            if (properties != null) {
+                Iterator ite = properties.entrySet().iterator();
+
+                while (ite.hasNext()) {
+                    Entry entry = (Entry) ite.next();
+                    String key = (String) entry.getKey();
+                    String value = (String) entry.getValue();
+                    addUserPropertyStmt.setString(1, username);
+                    addUserPropertyStmt.setString(2, key);
+                    addUserPropertyStmt.setString(3, value);
+                    addUserPropertyStmt.executeUpdate();
+                }
+
+                dbConnection.commit();
+                addUserStmt.close();
+                addUserPropertyStmt.close();
+            }
+        } catch (SQLException e) {
+            throw new UserStoreException("pendingAdd", e);
+        }
+
+    }
+
+    protected void sendEmail(String confString, String emailAddr, String epr)
+            throws UserStoreException {
+        // TODO :: Use a java thread here
+
+        Properties props = new Properties();
+        props.put(EmailVerifierConfig.HOST, config.getHost());
+        props.put(EmailVerifierConfig.PORT, 
Integer.toString(config.getPort()));
+        Session session = Session.getDefaultInstance(props, null);
+
+        try {
+            // Construct the message
+            Message msg = new MimeMessage(session);
+            msg.setFrom(new InternetAddress(config.getFromAddress()));
+            msg.setRecipient(Message.RecipientType.TO, new InternetAddress(
+                    emailAddr));
+            
+            if(config.getReplyTo()!= null){
+                msg.setReplyTo(new Address[]{new 
InternetAddress(config.getReplyTo()) });
+            }
+            
+            if (config.getSubject().length() == 0) {
+                msg.setSubject(EmailVerifierConfig.DEFAULT_VALUE_SUBJECT);
+            } else {
+                msg.setSubject(config.getSubject());
+            }
+
+            String responseMessage = getResponseMessage(confString, epr);
+            msg.setText(responseMessage);
+
+            log.debug("Sending confirmation mail to " + emailAddr);
+            log.debug("Verification url : " + responseMessage);
+            // Send the message
+            Transport.send(msg);
+            log.debug("Sending confirmation mail to " + emailAddr + "DONE");
+        } catch (AddressException e) {
+            throw new UserStoreException("sendingMailProblems", e);
+        } catch (MessagingException e) {
+            throw new UserStoreException("sendingMailProblems", e);
+        }
+    }
+
+    protected static void createDatabase() throws UserStoreException {
+        try {
+            Class clazz = Class.forName(DRIVER);
+            Driver driver = (Driver) clazz.newInstance();
+
+            Properties props = new Properties();
+
+            String connectionURL = PROTOCOL
+                    + "UnVerifiedUserDatabase;create=true";
+            dbConnection = driver.connect(connectionURL, props);
+
+            Statement stmt = dbConnection.createStatement(
+                    ResultSet.TYPE_SCROLL_INSENSITIVE,
+                    ResultSet.CONCUR_UPDATABLE);
+
+            DatabaseMetaData dbmd = dbConnection.getMetaData();
+            ResultSet rs = dbmd.getTables(null, null, "PENDING_USERS", null);
+
+            if (rs.next() == false) {
+                log.debug("Creating a new table in the database.");
+                stmt = dbConnection.createStatement(
+                        ResultSet.TYPE_SCROLL_INSENSITIVE,
+                        ResultSet.CONCUR_UPDATABLE);
+                stmt
+                        .executeUpdate("create table PENDING_USERS(username 
varchar(255) not null, email_address varchar(255) not null, password 
varchar(255) not null, confirmation varchar(255) not null unique, primary key 
(username))");
+                stmt
+                        .executeUpdate("create table 
PENDING_USER_PROPERTIES(username varchar(255) not null, property_name 
varchar(255) not null, property_value varchar(255) not null, primary key 
(username, property_name))");
+
+                stmt
+                        .executeUpdate("insert into PENDING_USERS(username, 
email_address, password, confirmation) values('admin', 'admin', 'dummyPass', 
'adminConfirmation')");
+                stmt
+                        .executeUpdate("insert into PENDING_USERS(username, 
email_address, password, confirmation) values('root', 'root', 'dummyPass', 
'rootConfirmation')");
+                dbConnection.commit();
+            } else {
+                log.debug("Database and table already found.");
+
+            }
+            dbConnection.commit();
+        } catch (Exception e) {
+            throw new UserStoreException("verifierUserDatabaseInit", e);
+        }
+    }
+
+    private String getResponseMessage(String confString, String epr) {
+        String msg = null;
+        if (config.getEmailBody().length() == 0) {
+            msg = EmailVerifierConfig.DEFAULT_VALUE_MESSAGE + "\n" + epr + "?"
+                    + CONF_STRING + "=" + confString+"\n";
+        } else {
+            msg = config.getEmailBody()+"\n"+ epr + "?" + CONF_STRING + "="
+                    + confString+"\n";
+        }
+        
+        if(config.getEmailFooter() != null){
+            msg = msg + "\n"+ config.getEmailFooter();
+        }
+        return msg;
+    }
+
+}

Added: 
trunk/registry/modules/core/src/main/java/org/wso2/registry/users/verifier/EmailVerifierConfig.java
==============================================================================
--- (empty file)
+++ 
trunk/registry/modules/core/src/main/java/org/wso2/registry/users/verifier/EmailVerifierConfig.java
 Sun Feb 24 22:44:36 2008
@@ -0,0 +1,132 @@
+package org.wso2.registry.users.verifier;
+
+public class EmailVerifierConfig {
+        
+    public final static String PORT = "mail.smtp.port";
+    public final static String HOST = "mail.smtp.host";
+    public final static String FROM_ADDRESS = "from";
+    public final static String USERNAME = "username";
+    public final static String PASSWORD = "password";
+      
+    public final static String DEFAULT_VALUE_SUBJECT = "EmailVerification";
+    public final static String DEFAULT_VALUE_MESSAGE = "Please point your 
browser to : ";
+    /**
+     * Mail server port
+     */
+    private int port = 25;
+    
+    /**
+     * Mail server IP or host name
+     */
+    private String host;
+    
+    /**
+     * The from address
+     */
+    private String fromAddress = "[EMAIL PROTECTED]";
+    
+    /**
+     * Username of the mail server account
+     * This is optional
+     */
+    private String username;
+    
+    /**
+     * Password of the mail server account
+     * This is optional
+     */
+    private String password;
+
+    private String registrationServiceEPR;
+    
+    private String subject = DEFAULT_VALUE_SUBJECT;
+    
+    private String emailBody = DEFAULT_VALUE_MESSAGE;
+    
+    private String replyTo = null;
+    
+    private String emailFooter = null;
+    
+       public String getFromAddress() {
+               return fromAddress;
+       }
+
+       public String getHost() {
+               return host;
+       }
+
+       public String getPassword() {
+               return password;
+       }
+
+       public int getPort() {
+               return port;
+       }
+
+       public String getUsername() {
+               return username;
+       }
+
+       public void setFromAddress(String fromAddress) {
+               this.fromAddress = fromAddress;
+       }
+
+       public void setHost(String host) {
+               this.host = host;
+       }
+
+       public void setPassword(String password) {
+               this.password = password;
+       }
+
+       public void setPort(int port) {
+               this.port = port;
+       }
+
+       public void setUsername(String username) {
+               this.username = username;
+       }
+
+       public String getRegistrationServiceEPR() {
+               return registrationServiceEPR;
+       }
+
+       public void setRegistrationServiceEPR(String registrationServiceEPR) {
+               this.registrationServiceEPR = registrationServiceEPR;
+       }
+
+    public String getSubject() {
+        return subject;
+    }
+
+    public void setSubject(String subject) {
+        this.subject = subject.trim();
+    }
+
+    public String getEmailBody() {
+        return emailBody;
+    }
+
+    public void setEmailBody(String emailMessage) {
+        this.emailBody = emailMessage.trim();
+    }
+
+    public String getReplyTo() {
+        return replyTo;
+    }
+
+    public void setReplyTo(String replyTo) {
+        this.replyTo = replyTo;
+    }
+
+    public String getEmailFooter() {
+        return emailFooter;
+    }
+
+    public void setEmailFooter(String emailFooter) {
+        this.emailFooter = emailFooter;
+    }
+    
+    
+
+}

_______________________________________________
Registry-dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/registry-dev

Reply via email to