Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Seray Uzgur committed Nov 10, 2015
2 parents 039fecd + 1078d0b commit 3465431
Show file tree
Hide file tree
Showing 23 changed files with 166 additions and 78 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>io.robe</groupId>
<artifactId>robe-parent</artifactId>
<version>0.4.5.3</version>
<version>0.4.5.4</version>
<packaging>pom</packaging>
<name>Robe Project</name>
<url>www.robe.io</url>
Expand Down Expand Up @@ -305,7 +305,7 @@
</dependencies>


<profiles>
<profiles>
<profile>
<id>release-sign-artifacts</id>
<activation>
Expand Down Expand Up @@ -433,7 +433,7 @@
</execution>
</executions>
</plugin>
<plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
Expand Down
2 changes: 1 addition & 1 deletion robe-admin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>io.robe</groupId>
<artifactId>robe-parent</artifactId>
<version>0.4.5.3</version>
<version>0.4.5.4</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public class ActionLog extends BaseEntity {
@Column
private boolean positive;

public ActionLog() {

}

public ActionLog(String actionType, boolean positive) {
this.actionType = actionType;
this.positive = positive;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ public AuthResource(UserDao userDao) {
@Timed
public Response login(@Context HttpServletRequest request, Map<String, String> credentials) throws Exception {


Optional<User> user = userDao.findByUsername(credentials.get("username"));
if (!user.isPresent()) {
throw new WebApplicationException(Response.Status.UNAUTHORIZED);
Expand Down Expand Up @@ -105,7 +104,7 @@ public Response login(@Context HttpServletRequest request, Map<String, String> c
}
int failCount = user.get().getFailCount() + 1;
user.get().setFailCount(failCount);
boolean block = failCount >= ((Integer) SystemParameterCache.get("USER_BLOCK_FAIL_LIMIT", 3));
boolean block = failCount >= Integer.valueOf((String) SystemParameterCache.get("USER_BLOCK_FAIL_LIMIT", "3"));
if (block)
user.get().setActive(false);

Expand All @@ -126,17 +125,12 @@ private void logAction(ActionLog login) {
@Path("logout")
@Timed
public User logout(@Auth Credentials credentials) throws Exception {


Optional<User> user = userDao.findByUsername(credentials.getUsername());
if (!user.isPresent()) {
throw new WebApplicationException(Response.Status.UNAUTHORIZED);
} else {

BasicToken.clearPermissionCache(credentials.getUsername());

user.get().setLastLogoutTime(DateTime.now().toDate());

return user.get();
}
}
Expand Down
65 changes: 65 additions & 0 deletions robe-admin/src/main/java/io/robe/admin/resources/BaseResource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package io.robe.admin.resources;

import com.google.inject.Inject;
import io.dropwizard.auth.Auth;
import io.dropwizard.hibernate.UnitOfWork;
import io.robe.auth.Credentials;
import io.robe.hibernate.dao.BaseDao;
import io.robe.hibernate.entity.BaseEntity;
import org.hibernate.CacheMode;
import org.hibernate.FlushMode;

import javax.validation.Valid;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import java.lang.reflect.ParameterizedType;
import java.util.List;

public abstract class BaseResource<T extends BaseEntity> {

@Inject
private BaseDao<T> dao;

private final Class<T> entity;

@SuppressWarnings("unchecked")
protected BaseResource() {
this.entity = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}

@Path("all")
@GET
@UnitOfWork
public List<T> getAll(@Auth Credentials credentials) {
return dao.findAll(entity);
}

@Path("{modelOid}")
@GET
@UnitOfWork
public T get(@PathParam("modelOid") String modelOid, @Auth Credentials credentials) {
return dao.findById(entity, modelOid);
}

@PUT
@UnitOfWork
public T create(@Valid T model, @Auth Credentials credentials) {
return dao.create(model);
}

@POST
@UnitOfWork
public T update(@Valid T model, @Auth Credentials credentials) {
return dao.update(model);
}

@DELETE
@UnitOfWork
public T delete(@Valid T model, @Auth Credentials credentials) {
return dao.delete(model);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.dropwizard.hibernate.UnitOfWork;
import io.robe.admin.hibernate.dao.SystemParameterDao;
import io.robe.admin.hibernate.entity.SystemParameter;
import io.robe.admin.util.SystemParameterCache;
import io.robe.auth.Credentials;
import io.robe.common.exception.RobeRuntimeException;
import org.hibernate.FlushMode;
Expand Down Expand Up @@ -59,4 +60,13 @@ public SystemParameter update(@Auth Credentials credentials, @Valid SystemParame
public SystemParameter delete(@Auth Credentials credentials, @Valid SystemParameter systemParameter) {
return systemParameterDao.delete(systemParameter);
}

@POST
@Path("clearcache")
@UnitOfWork(readOnly = true, flushMode = FlushMode.MANUAL)
public void clearCache() {
SystemParameterCache.fillCache();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.robe.admin.util.TemplateManager;
import io.robe.auth.AbstractAuthResource;
import io.robe.auth.Credentials;
import io.robe.auth.tokenbased.BasicToken;
import io.robe.common.exception.RobeRuntimeException;
import io.robe.mail.MailItem;
import io.robe.mail.MailManager;
Expand Down Expand Up @@ -179,6 +180,8 @@ public UserDTO update(@Auth Credentials credentials, @Valid UserDTO user) {
entity = userDao.update(entity);
userDao.flush();

BasicToken.clearPermissionCache(entity.getUsername());

return new UserDTO(entity);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public static void fillCache() {


public static Object get(String key, Object defaultValue) {
if (cache.isEmpty()) {
fillCache();
}
Object value = cache.get(key);
return (value == null) ? defaultValue : value;
}
Expand Down
2 changes: 1 addition & 1 deletion robe-assets/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>robe-parent</artifactId>
<groupId>io.robe</groupId>
<version>0.4.5.3</version>
<version>0.4.5.4</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion robe-auth/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>robe-parent</artifactId>
<groupId>io.robe</groupId>
<version>0.4.5.3</version>
<version>0.4.5.4</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
90 changes: 44 additions & 46 deletions robe-auth/src/main/java/io/robe/auth/tokenbased/BasicToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,6 @@ public class BasicToken implements Token {
private int maxAge;


/**
* Configure method for Token generation configurations and ENCRYPTOR configure
*
* @param configuration
*/
public static void configure(TokenBasedAuthConfiguration configuration) {
ENCRYPTOR.setPoolSize(configuration.getPoolSize()); // This would be a good value for a 4-core system
if (configuration.getServerPassword().equals("auto")) {
ENCRYPTOR.setPassword(UUID.randomUUID().toString());
} else {
ENCRYPTOR.setPassword(configuration.getServerPassword());
}
ENCRYPTOR.setAlgorithm(configuration.getAlgorithm());
ENCRYPTOR.initialize();
BasicToken.defaultMaxAge = configuration.getMaxage();

//Create cache for permissions.
cache = CacheBuilder.newBuilder()
.expireAfterAccess(defaultMaxAge, TimeUnit.SECONDS)
.expireAfterWrite(defaultMaxAge, TimeUnit.SECONDS)
.build();

}

/**
* Creates an access token with the given parameters.
*
Expand All @@ -91,6 +67,7 @@ public BasicToken(String userId, String username, DateTime expireAt, Map<String,
*/
public BasicToken(String tokenString) throws Exception {
try {
tokenString = tokenString.replaceAll("\"", "");
tokenString = new String(Hex.decodeHex(tokenString.toCharArray()));
String[] parts = ENCRYPTOR.decrypt(tokenString).split(SEPARATOR);
this.userId = parts[0];
Expand All @@ -104,6 +81,39 @@ public BasicToken(String tokenString) throws Exception {

}

/**
* Configure method for Token generation configurations and ENCRYPTOR configure
*
* @param configuration
*/
public static void configure(TokenBasedAuthConfiguration configuration) {
ENCRYPTOR.setPoolSize(configuration.getPoolSize()); // This would be a good value for a 4-core system
if (configuration.getServerPassword().equals("auto")) {
ENCRYPTOR.setPassword(UUID.randomUUID().toString());
} else {
ENCRYPTOR.setPassword(configuration.getServerPassword());
}
ENCRYPTOR.setAlgorithm(configuration.getAlgorithm());
ENCRYPTOR.initialize();
BasicToken.defaultMaxAge = configuration.getMaxage();

//Create cache for permissions.
cache = CacheBuilder.newBuilder()
.expireAfterAccess(defaultMaxAge, TimeUnit.SECONDS)
.expireAfterWrite(defaultMaxAge, TimeUnit.SECONDS)
.build();

}

public static void clearPermissionCache(String username) {
cache.invalidate(username);
cache.cleanUp();
}

public static Set<String> getCurrentUsernames() {
cache.cleanUp();
return cache.asMap().keySet();
}

@Override
public String getUserId() {
Expand Down Expand Up @@ -184,6 +194,16 @@ public int getMaxAge() {
return maxAge < 1 ? defaultMaxAge : maxAge;
}

public void setMaxAge(int maxAge) {
if (maxAge < 1) maxAge = defaultMaxAge;
this.maxAge = maxAge;
}

@Override
public Set<String> getPermissions() {
return cache.getIfPresent(getUsername());
}

/**
* Sets permissions to the cache with current username
*
Expand All @@ -194,17 +214,6 @@ public void setPermissions(Set<String> permissions) {
cache.put(getUsername(), permissions);
}

@Override
public Set<String> getPermissions() {
return cache.getIfPresent(getUsername());
}

public void setMaxAge(int maxAge) {
if (maxAge < 1) maxAge = defaultMaxAge;
this.maxAge = maxAge;
}


private void resetTokenString() {
tokenString = null;
}
Expand All @@ -229,15 +238,4 @@ public int hashCode() {
result = 31 * result + attributesHash.hashCode();
return result;
}


public static void clearPermissionCache(String username) {
cache.invalidate(username);
cache.cleanUp();
}

public static Set<String> getCurrentUsernames() {
cache.cleanUp();
return cache.asMap().keySet();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public TokenBasedAuthenticator(UserStore userStore, ServiceStore serviceStore) {
*/
@Override
public Optional<Token> authenticate(String tokenString) throws AuthenticationException {
tokenString = tokenString.replaceAll("\"", "");
LOGGER.debug("Authenticating from database: " + tokenString);
try {
// Decode tokenString and get user
Expand Down
2 changes: 1 addition & 1 deletion robe-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>robe-parent</artifactId>
<groupId>io.robe</groupId>
<version>0.4.5.3</version>
<version>0.4.5.4</version>
</parent>

<modelVersion>4.0.0</modelVersion>
Expand Down
Loading

0 comments on commit 3465431

Please sign in to comment.