Skip to content
This repository has been archived by the owner on Jan 21, 2021. It is now read-only.

Commit

Permalink
Merge pull request #449 from OpenSRP/issue448-cache-authentication
Browse files Browse the repository at this point in the history
Cache authentication
  • Loading branch information
ndegwamartin authored Jun 8, 2018
2 parents 08ec4b4 + b4daded commit 464417f
Show file tree
Hide file tree
Showing 8 changed files with 693 additions and 491 deletions.
9 changes: 9 additions & 0 deletions assets/config/opensrp.properties
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,12 @@ opensrp.site.url=""

#search for missing clients
opensrp.sync.search.missing.client=false

#duration in seconds to cache authetication time to live
opensrp.authencation.cache.ttl=600

#redis settings
redis.host=localhost
redis.port=6379
redis.password=RedI$P@S5
redis.pool.max.connections=25
61 changes: 54 additions & 7 deletions opensrp-core/src/main/resources/applicationContext-opensrp.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:activemq.properties"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
Expand All @@ -22,11 +23,57 @@

<util:properties id="opensrp" location="classpath:/opensrp.properties"/>

<context:annotation-config />
<context:annotation-config/>

<bean id="statsdAgent" class="org.motechproject.metrics.impl.LoggingAgentBackendImpl" />
<bean id="statsdAgent" class="org.motechproject.metrics.impl.LoggingAgentBackendImpl"/>

<!-- <import resource="persistence_couchdb.xml"/> -->
<import resource="persistence_postgres.xml"/>
<beans profile="postgres">
<import resource="persistence_postgres.xml"/>
</beans>

<beans profile="couchDb">
<import resource="persistence_couchdb.xml"/>
</beans>

<beans profile="jedis">
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"
p:max-total="${redis.pool.max.connections}" p:test-on-borrow="true"
p:test-on-return="true"/>

<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.password}"
p:use-pool="true">
<constructor-arg ref="jedisPoolConfig"/>
</bean>

<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="jedisConnectionFactory"
p:enable-transaction-support="true"/>

</beans>

<beans profile="lettuce">
<bean id="lettucePool"
class="org.springframework.data.redis.connection.lettuce.DefaultLettucePool">
<property name="hostName" value="${redis.host}"/>
<property name="port" value="${redis.port}"/>
<property name="password" value="${redis.password}"/>
<property name="poolConfig" ref="lettucePoolConfiguration"/>
</bean>

<bean id="lettucePoolConfiguration" class="org.springframework.data.redis.connection.PoolConfig"
p:max-total="${redis.pool.max.connections}" p:test-on-borrow="true"
p:test-on-return="true"/>

<bean id="lettuceConnectionFactory"
class="org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory">
<constructor-arg index="0" ref="lettucePool"/>
</bean>

<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="lettuceConnectionFactory"
p:enable-transaction-support="true"/>

</beans>
</beans>
10 changes: 10 additions & 0 deletions opensrp-core/src/main/resources/persistence_postgres.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,14 @@
<property name="basePackage"
value="org.opensrp.repository.postgres.mapper;org.opensrp.repository.postgres.mapper.custom" />
</bean>
<!-- Add Couchdb connector because repository beans not migrated to Postgres -->
<bean id="opensrpDatabaseConnector" class="org.ektorp.impl.StdCouchDbConnector">
<constructor-arg value="${couchdb.db.opensrp}" />
<constructor-arg ref="couchDbInstance" />
</bean>

<bean class="org.opensrp.repository.couch.AllBaseEntities" />
<bean class="org.opensrp.repository.couch.AllLocations" />
<bean class="org.opensrp.repository.couch.AllProviders" />
<bean class="org.opensrp.repository.couch.AllUsers" />
</beans>
73 changes: 68 additions & 5 deletions opensrp-web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,55 @@
<hibernate-entitymanager.version>4.2.8.Final</hibernate-entitymanager.version>

</properties>

<profiles>
<profile>
<id>jedis</id>
<activation>
<activeByDefault>true</activeByDefault>
<property>
<name>opensrp.redis.client.type</name>
<value>jedis</value>
</property>
</activation>
<properties>
<opensrp.redis.client.profile>jedis</opensrp.redis.client.profile>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.4.1</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>lettuce</id>
<activation>
<property>
<name>opensrp.redis.client.type</name>
<value>lettuce</value>
</property>
</activation>
<properties>
<opensrp.redis.client.profile>lettuce</opensrp.redis.client.profile>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.lambdaworks/lettuce -->
<dependency>
<groupId>com.lambdaworks</groupId>
<artifactId>lettuce</artifactId>
<version>2.3.3</version>
</dependency>
</dependencies>
</profile>
</profiles>
<build>
<finalName>opensrp</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>../assets/config</directory>
Expand Down Expand Up @@ -55,6 +98,23 @@
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.5</version>
<configuration>
<webResources>
<webResource>
<filtering>true</filtering>
<directory>src/main/webapp</directory>
<includes>
<include>**/web.xml</include>
</includes>
</webResource>
</webResources>
<failOnMissingWebXml>true</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
Expand Down Expand Up @@ -278,9 +338,12 @@
<version>3.1</version>
</dependency>-->





<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.3.6.RELEASE</version>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.net.URLConnection;
import java.nio.charset.Charset;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.opensrp.domain.Multimedia;
Expand All @@ -26,6 +27,8 @@
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.WebAuthenticationDetails;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PathVariable;
Expand Down Expand Up @@ -68,11 +71,11 @@ public class MultimediaController {
@RequestMapping(value = "/download/{fileName:.+}", method = RequestMethod.GET)
public void downloadFile(HttpServletResponse response, @PathVariable("fileName") String fileName,
@RequestHeader(value = "username") String userName,
@RequestHeader(value = "password") String password)
@RequestHeader(value = "password") String password, HttpServletRequest request)
throws Exception {

try {
if (authenticate(userName, password).isAuthenticated()) {
if (authenticate(userName, password, request).isAuthenticated()) {
File file = new File(multiMediaDir + File.separator + "images" + File.separator + fileName);
if (fileName.endsWith("mp4")) {
file = new File(multiMediaDir + File.separator + "videos" + File.separator + fileName);
Expand All @@ -99,11 +102,11 @@ public void downloadFile(HttpServletResponse response, @PathVariable("fileName")
@RequestMapping(value = "/profileimage/{baseEntityId}", method = RequestMethod.GET)
public void downloadFileByClientId(HttpServletResponse response, @PathVariable("baseEntityId") String baseEntityId,
@RequestHeader(value = "username") String userName,
@RequestHeader(value = "password") String password)
@RequestHeader(value = "password") String password, HttpServletRequest request)
throws Exception {

try {
if (authenticate(userName, password).isAuthenticated()) {
if (authenticate(userName, password, request).isAuthenticated()) {

Multimedia multiMedia = multimediaService.findByCaseId(baseEntityId);
if (multiMedia == null || multiMedia.getFilePath() == null) {
Expand Down Expand Up @@ -148,10 +151,11 @@ public ResponseEntity<String> uploadFiles(@RequestParam("anm-id") String provide
return new ResponseEntity<>(new Gson().toJson(status), HttpStatus.OK);
}

private Authentication authenticate(String userName, String password) {
Authentication auth = new UsernamePasswordAuthenticationToken(userName, password);
auth = provider.authenticate(auth);
return auth;
private Authentication authenticate(String userName, String password, HttpServletRequest request) {
UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(userName, password);
WebAuthenticationDetails details = new WebAuthenticationDetailsSource().buildDetails(request);
auth.setDetails(details);
return provider.authenticate(auth);
}

private void downloadFile(File file, HttpServletResponse response) throws Exception {
Expand Down
Loading

0 comments on commit 464417f

Please sign in to comment.