Skip to content

Commit

Permalink
#4 : support auth-flows + spring-security
Browse files Browse the repository at this point in the history
  • Loading branch information
OhadR committed May 2, 2014
1 parent e80dca7 commit 9032248
Show file tree
Hide file tree
Showing 18 changed files with 756 additions and 16 deletions.
16 changes: 14 additions & 2 deletions coffee-req/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@
<version>${appengine.target.version}</version>
</dependency>

<dependency>
<groupId>com.ohadr</groupId>
<artifactId>authentication-flows</artifactId>
<version>1.5.1-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
Expand All @@ -51,7 +63,7 @@
<version>${worldtime.spring.version}</version>
</dependency>

<!-- dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>${ohadr.spring.security.version}</version>
Expand All @@ -67,7 +79,7 @@
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${ohadr.spring.security.version}</version>
</dependency-->
</dependency>



Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
package com.ohadr.dictionary.gae;

import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.NoSuchElementException;

import org.apache.log4j.Logger;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.google.appengine.api.datastore.*;
import com.ohadr.auth_flows.core.AbstractAuthenticationAccountRepository;
import com.ohadr.auth_flows.interfaces.AuthenticationUser;
import com.ohadr.auth_flows.mocks.InMemoryAuthenticationUserImpl;


public class GAEAuthenticationAccountRepositoryImpl extends
AbstractAuthenticationAccountRepository
{
private static final String PASSWORD_PROP_NAME = "password";
private static final String LOGIN_ATTEMPTS_LEFT_PROP_NAME = "loginAttemptsLeft";
private static final String ENABLED_PROP_NAME = "enabled";
private static final String LAST_PSWD_CHANGE_DATE_PROP_NAME = "lastPasswordChangeDate";
private static final String AUTHORITIES_PROP_NAME = "authorities";

private static final String USER_DB_KIND = "User";


private static Logger log = Logger.getLogger(GAEAuthenticationAccountRepositoryImpl.class);

private DatastoreService datastore;

public GAEAuthenticationAccountRepositoryImpl()
{
datastore = DatastoreServiceFactory.getDatastoreService();
}

@Override
public void setDisabled(String email)
{
setEnabledFlag(email, false);
}

@Override
public void setEnabled(String email)
{
setEnabledFlag(email, true);
}

@Override
public void setPassword(String username, String newEncodedPassword)
{
changePassword(username, newEncodedPassword);
}

@Override
public void createUser(UserDetails user)
{
AuthenticationUser authUser = (AuthenticationUser) user;

Entity dbUser = new Entity(USER_DB_KIND, user.getUsername()); //the username is the key

dbUser.setProperty("username", user.getUsername());
dbUser.setProperty(PASSWORD_PROP_NAME, user.getPassword());
dbUser.setProperty(ENABLED_PROP_NAME, user.isEnabled());
dbUser.setProperty(LOGIN_ATTEMPTS_LEFT_PROP_NAME, authUser.getLoginAttemptsLeft());
dbUser.setProperty(LAST_PSWD_CHANGE_DATE_PROP_NAME, new Date( System.currentTimeMillis()) );
dbUser.setProperty(AUTHORITIES_PROP_NAME, "ROLE_USER" );

datastore.put(dbUser);
}

@Override
public void updateUser(UserDetails user) {
// TODO Auto-generated method stub

}

@Override
public void deleteUser(String username)
{
Key userKey = KeyFactory.createKey(USER_DB_KIND, username);
datastore.delete(userKey);

}


@Override
public boolean userExists(String username) {
// TODO Auto-generated method stub
return false;
}


@Override
public UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException
{
Key userKey = KeyFactory.createKey(USER_DB_KIND, username);
Entity entity;
try
{
entity = datastore.get(userKey);
log.debug("got entity of " + username + ": " + entity);
}
catch (EntityNotFoundException e)
{
log.error("entity of " + username + " not found");
throw new UsernameNotFoundException(username, e);
}

boolean isEnabled = false;
Object isEnabledObj = entity.getProperty(ENABLED_PROP_NAME);
if( null != isEnabledObj )
{
isEnabled = (Boolean)isEnabledObj;
}
int loginAttemptsLeft = 0;
Object loginAttemptsLeftObj = entity.getProperty(LOGIN_ATTEMPTS_LEFT_PROP_NAME);
if( null != loginAttemptsLeftObj )
{
//"hack" convert Object to int:
loginAttemptsLeft = new Integer(loginAttemptsLeftObj.toString());
}

String roleName = (String)entity.getProperty(AUTHORITIES_PROP_NAME);
GrantedAuthority userAuth = new SimpleGrantedAuthority(roleName);
Collection<GrantedAuthority> authSet = new HashSet<GrantedAuthority>();
authSet.add(userAuth);

return new InMemoryAuthenticationUserImpl(
username,
(String)entity.getProperty(PASSWORD_PROP_NAME),
isEnabled,
loginAttemptsLeft,
(Date)entity.getProperty(LAST_PSWD_CHANGE_DATE_PROP_NAME),
authSet);

}


@Override
protected void setEnabledFlag(String username, boolean flag) throws NoSuchElementException
{
Key userKey = KeyFactory.createKey(USER_DB_KIND, username);
Entity entity;
try
{
entity = datastore.get(userKey);
log.debug("got entity of " + username + ": " + entity);
}
catch (EntityNotFoundException e)
{
log.error("entity of " + username + " not found");
throw new NoSuchElementException(e.getMessage());
}

entity.setProperty(ENABLED_PROP_NAME, flag);
datastore.put(entity);
}

@Override
protected void updateLoginAttemptsCounter(String username, int attempts) throws NoSuchElementException
{
// FlowsUtil.logStackTrace( log );

Key userKey = KeyFactory.createKey(USER_DB_KIND, username);
Entity entity;
try
{
entity = datastore.get(userKey);
log.debug("got entity of " + username + ": " + entity);
}
catch (EntityNotFoundException e)
{
log.error("entity of " + username + " not found");
throw new NoSuchElementException(e.getMessage());
}

entity.setProperty(LOGIN_ATTEMPTS_LEFT_PROP_NAME, attempts);
datastore.put(entity);
}

@Override
public void changePassword(String username, String newEncodedPassword)
{
Key userKey = KeyFactory.createKey(USER_DB_KIND, username);
Entity entity;
try
{
entity = datastore.get(userKey);
log.debug("got entity of " + username + ": " + entity);
}
catch (EntityNotFoundException e)
{
log.error("entity of " + username + " not found");
throw new NoSuchElementException(e.getMessage());
}

entity.setProperty(LAST_PSWD_CHANGE_DATE_PROP_NAME, new Date( System.currentTimeMillis()));
entity.setProperty(PASSWORD_PROP_NAME, newEncodedPassword);
datastore.put(entity);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.ohadr.dictionary.gae;


import java.io.UnsupportedEncodingException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.log4j.Logger;
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;


/**
* this implementation uses pure javax.mail to send mail.
* Deprecated because newer version uses Spring @link:JavaMailSenderImpl
* @author OhadR
*
*/

public class GAEMailSenderImpl implements MailSender
{
private static Logger log = Logger.getLogger(GAEMailSenderImpl.class);

private Session session;


public GAEMailSenderImpl()
{
final String username = "[email protected]";
final String password = "theheatison";

Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.port", "587");
// props.put("mail.smtp.port", "465");

// session = Session.getDefaultInstance(props, null);
session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(username, password);
}
});

}


@Override
public void send(SimpleMailMessage msg) throws MailException
{
log.info(msg.toString());

try
{
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]", "ohadr.com Admin"));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress( msg.getTo()[0] )); //Spring's getTo returns String[]

/* message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse( msg.getTo().toString() ));
*/ message.setSubject( msg.getSubject() );
message.setText( msg.getText() );

Transport.send(message);
}
catch (MessagingException e)
{
log.error("MessagingException: ", e);
throw new RuntimeException(e);
}
catch (UnsupportedEncodingException e)
{
log.error("UnsupportedEncodingException: ", e);
e.printStackTrace();
}
}

@Override
public void send(SimpleMailMessage[] simpleMessages) throws MailException
{
// TODO Auto-generated method stub

}

}
19 changes: 19 additions & 0 deletions coffee-req/src/main/resources/client.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@


com.ohadr.oauth2.db.username=root
com.ohadr.oauth2.db.password=ohadrbmc
com.ohadr.oauth2.db.host=ohadr.com
com.ohadr.oauth2.db.port=3306
com.ohadr.oauth2.db.schema=auth-flows



com.ohadr.auth-flows.linksExpirationMinutes=60
com.ohadr.auth-flows.maxAttempts=5


# Crypto settings
com.ohadr.crypto.keystore=./irrelevant
com.ohadr.crypto.password=kspass
com.ohadr.crypto.keyAlias=ohadr
com.ohadr.crypto.createKeystoreFileIfNotExist=false
Loading

0 comments on commit 9032248

Please sign in to comment.