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

TB Reach Cache authentication #453

Merged
merged 15 commits into from
Jun 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
57 changes: 52 additions & 5 deletions opensrp-core/src/main/resources/applicationContext-opensrp.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
<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" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
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
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
Expand All @@ -27,9 +28,6 @@

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

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

<task:scheduler id="applicationScheduler" pool-size="10" />

<task:scheduled-tasks scheduler="applicationScheduler">
Expand All @@ -47,5 +45,54 @@
fixed-delay="120000" />
</task:scheduled-tasks>

<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>
78 changes: 73 additions & 5 deletions opensrp-web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,60 @@
<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>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>3.1.1.RELEASE</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 +103,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 @@ -267,9 +332,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