-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#4 : support auth-flows + spring-security
- Loading branch information
Showing
18 changed files
with
756 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
208 changes: 208 additions & 0 deletions
208
...ee-req/src/main/java/com/ohadr/dictionary/gae/GAEAuthenticationAccountRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
99 changes: 99 additions & 0 deletions
99
coffee-req/src/main/java/com/ohadr/dictionary/gae/GAEMailSenderImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.