Add template for assignment 2
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>at.ac.tuwien.infosys.dst</groupId>
|
||||
<artifactId>dst</artifactId>
|
||||
<version>2021.1</version>
|
||||
<relativePath>../..</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>ass2-service-auth</artifactId>
|
||||
|
||||
<name>DST :: Assignment 2 :: Service :: Auth Server</name>
|
||||
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>at.ac.tuwien.infosys.dst</groupId>
|
||||
<artifactId>ass1-jpa</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>at.ac.tuwien.infosys.dst</groupId>
|
||||
<artifactId>ass2-service-api</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.grpc</groupId>
|
||||
<artifactId>grpc-protobuf</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.grpc</groupId>
|
||||
<artifactId>grpc-stub</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.grpc</groupId>
|
||||
<artifactId>grpc-netty</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>at.ac.tuwien.infosys.dst</groupId>
|
||||
<artifactId>ass1-jpa</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<type>test-jar</type>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-orm</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<extensions>
|
||||
<extension>
|
||||
<groupId>kr.motd.maven</groupId>
|
||||
<artifactId>os-maven-plugin</artifactId>
|
||||
</extension>
|
||||
</extensions>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.xolstice.maven.plugins</groupId>
|
||||
<artifactId>protobuf-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
|
||||
</build>
|
||||
|
||||
</project>
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package dst.ass2.service.auth;
|
||||
|
||||
import dst.ass2.service.api.auth.AuthenticationException;
|
||||
import dst.ass2.service.api.auth.IAuthenticationService;
|
||||
import dst.ass2.service.api.auth.NoSuchUserException;
|
||||
|
||||
public interface ICachingAuthenticationService extends IAuthenticationService {
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>
|
||||
* Instead of checking database records directly, the method first checks the cache for existing users. If the user
|
||||
* is not in the cache, then the service checks the database for the given email address, and updates the cache if
|
||||
* necessary.
|
||||
* </p>
|
||||
*/
|
||||
@Override
|
||||
String authenticate(String email, String password) throws NoSuchUserException, AuthenticationException;
|
||||
|
||||
/**
|
||||
* Loads user data from the database into memory.
|
||||
*/
|
||||
void loadData();
|
||||
|
||||
/**
|
||||
* Clears the data cached from the database.
|
||||
*/
|
||||
void clearCache();
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package dst.ass2.service.auth.grpc;
|
||||
|
||||
/**
|
||||
* This class holds the port value used to bind the gRPC server. The CDI context provides an instance that you can
|
||||
* inject into your implementation of {@link IGrpcServerRunner}. The config values are loaded from the
|
||||
* grpc.properties.
|
||||
*/
|
||||
public class GrpcServerProperties {
|
||||
|
||||
private int port;
|
||||
|
||||
public GrpcServerProperties() {
|
||||
}
|
||||
|
||||
public GrpcServerProperties(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package dst.ass2.service.auth.grpc;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* An implementation of this interface is expected by the application to start the grpc server. Inject get
|
||||
* {@link GrpcServerProperties} to access the configuration.
|
||||
*/
|
||||
public interface IGrpcServerRunner {
|
||||
/**
|
||||
* Starts the gRPC server.
|
||||
*
|
||||
* @throws IOException start error
|
||||
*/
|
||||
void run() throws IOException;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<configuration>
|
||||
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
|
||||
<encoder>
|
||||
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} - %highlight(%5p) [%12.12thread] %cyan(%-40.40logger{39}): %m%n</pattern>
|
||||
</encoder>
|
||||
|
||||
</appender>
|
||||
|
||||
<root level="${log.level:-INFO}">
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</root>
|
||||
</configuration>
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package dst.ass2.service.auth;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableTransactionManagement
|
||||
public class AuthenticationServiceApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(AuthenticationServiceApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+117
@@ -0,0 +1,117 @@
|
||||
package dst.ass2.service.auth;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.boot.SpringBootConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||
import org.springframework.orm.jpa.LocalEntityManagerFactoryBean;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
|
||||
import dst.ass1.jpa.dao.IDAOFactory;
|
||||
import dst.ass1.jpa.dao.impl.DAOFactory;
|
||||
import dst.ass1.jpa.model.IModelFactory;
|
||||
import dst.ass1.jpa.model.impl.ModelFactory;
|
||||
import dst.ass1.jpa.tests.TestData;
|
||||
import dst.ass1.jpa.util.Constants;
|
||||
import dst.ass2.service.auth.grpc.GrpcServerProperties;
|
||||
|
||||
@SpringBootConfiguration
|
||||
@PropertySource("classpath:/dst/ass2/service/auth/grpc.properties")
|
||||
public class AuthenticationServiceApplicationConfig {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Bean
|
||||
public IModelFactory modelFactory() {
|
||||
return new ModelFactory();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public IDAOFactory daoFactory() {
|
||||
return new DAOFactory(em);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public GrpcServerProperties grpcServerProperties(@Value("${grpc.port}") int port) {
|
||||
return new GrpcServerProperties(port);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Profile("grpc")
|
||||
public SpringGrpcServerRunner springGrpcServerRunner() {
|
||||
return new SpringGrpcServerRunner();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LocalEntityManagerFactoryBean entityManagerFactoryBean() {
|
||||
LocalEntityManagerFactoryBean bean = new LocalEntityManagerFactoryBean();
|
||||
bean.setPersistenceUnitName(Constants.JPA_PERSISTENCE_UNIT);
|
||||
// fixes collection proxy problem when using jersey
|
||||
bean.getJpaPropertyMap().put("hibernate.enable_lazy_load_no_trans", true);
|
||||
return bean;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PlatformTransactionManager transactionManager(LocalEntityManagerFactoryBean entityManagerFactoryBean) {
|
||||
JpaTransactionManager transactionManager = new JpaTransactionManager();
|
||||
transactionManager.setPersistenceUnitName(Constants.JPA_PERSISTENCE_UNIT);
|
||||
transactionManager.setEntityManagerFactory(entityManagerFactoryBean.getObject());
|
||||
return transactionManager;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Profile("testdata")
|
||||
public TestData testData() {
|
||||
return new TestData();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Profile("testdata")
|
||||
public TestDataInserter testDataInserter(TestData testData, IModelFactory modelFactory, PlatformTransactionManager transactionManager) {
|
||||
return new TestDataInserter(testData, modelFactory, transactionManager);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Profile("testdata")
|
||||
public AuthServiceDataInjector dataInjector(TestDataInserter testDataInserter) {
|
||||
return new AuthServiceDataInjector(em, testDataInserter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes sure data is in the database before the {@link ICachingAuthenticationService} is initialized.
|
||||
*/
|
||||
public static class AuthServiceDataInjector implements BeanPostProcessor {
|
||||
private boolean dataInjected = false;
|
||||
|
||||
private EntityManager em;
|
||||
private TestDataInserter testDataInserter;
|
||||
|
||||
public AuthServiceDataInjector(EntityManager em, TestDataInserter testDataInserter) {
|
||||
this.em = em;
|
||||
this.testDataInserter = testDataInserter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
||||
if (!dataInjected && (bean instanceof ICachingAuthenticationService)) {
|
||||
testDataInserter.insertTestData(em);
|
||||
dataInjected = true;
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
||||
return bean;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package dst.ass2.service.auth;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
|
||||
import dst.ass2.service.auth.grpc.IGrpcServerRunner;
|
||||
|
||||
/**
|
||||
* This class loads the {@link IGrpcServerRunner} from the application context and runs it after the application starts.
|
||||
*/
|
||||
public class SpringGrpcServerRunner implements CommandLineRunner, ApplicationContextAware {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(SpringGrpcServerRunner.class);
|
||||
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(String... args) throws Exception {
|
||||
LOG.info("Getting instance of GrpcServerRunner");
|
||||
IGrpcServerRunner bean = applicationContext.getBean(IGrpcServerRunner.class);
|
||||
LOG.info("Starting IGrpcServerRunner instance {}", bean);
|
||||
bean.run();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package dst.ass2.service.auth;
|
||||
|
||||
import dst.ass1.jpa.model.IModelFactory;
|
||||
import dst.ass1.jpa.tests.TestData;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
public class TestDataInserter {
|
||||
|
||||
private PlatformTransactionManager transactionManager;
|
||||
private IModelFactory modelFactory;
|
||||
private TestData testData;
|
||||
|
||||
public TestDataInserter(TestData testData, IModelFactory modelFactory, PlatformTransactionManager transactionManager) {
|
||||
this.testData = testData;
|
||||
this.modelFactory = modelFactory;
|
||||
this.transactionManager = transactionManager;
|
||||
}
|
||||
|
||||
public void insertTestData(EntityManager em) {
|
||||
TransactionTemplate tx = new TransactionTemplate(transactionManager);
|
||||
tx.execute(status -> {
|
||||
testData.insert(modelFactory, em);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
+143
@@ -0,0 +1,143 @@
|
||||
package dst.ass2.service.auth.tests;
|
||||
|
||||
import dst.ass1.jpa.model.IModelFactory;
|
||||
import dst.ass1.jpa.model.IPreferences;
|
||||
import dst.ass1.jpa.model.IRider;
|
||||
import dst.ass1.jpa.tests.TestData;
|
||||
import dst.ass2.service.api.auth.AuthenticationException;
|
||||
import dst.ass2.service.api.auth.NoSuchUserException;
|
||||
import dst.ass2.service.auth.AuthenticationServiceApplication;
|
||||
import dst.ass2.service.auth.ICachingAuthenticationService;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.transaction.Transactional;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = AuthenticationServiceApplication.class)
|
||||
@Transactional
|
||||
@ActiveProfiles("testdata")
|
||||
public class AuthenticationServiceTest implements ApplicationContextAware {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
private IModelFactory modelFactory;
|
||||
private ICachingAuthenticationService authenticationService;
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
|
||||
applicationContext = ctx;
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
modelFactory = applicationContext.getBean(IModelFactory.class);
|
||||
authenticationService = applicationContext.getBean(ICachingAuthenticationService.class);
|
||||
|
||||
// reload the data before each test
|
||||
authenticationService.loadData();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void authenticate_existingUser_createsTokenCorrectly() throws Exception {
|
||||
String token = authenticationService.authenticate(TestData.RIDER_1_EMAIL, TestData.RIDER_1_PW);
|
||||
assertNotNull(token);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void authenticate_existingUserNotInCache_createsTokenCorrectly() throws Exception {
|
||||
IRider p = modelFactory.createRider();
|
||||
|
||||
p.setEmail("non-cached@example.com");
|
||||
try {
|
||||
p.setPassword(MessageDigest.getInstance("SHA1").digest("somepw".getBytes()));
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
p.setName("non-cached");
|
||||
p.setAccountNo("accountno");
|
||||
p.setBankCode("bankcode");
|
||||
p.setTel("+43158801");
|
||||
IPreferences prefs = modelFactory.createPreferences();
|
||||
p.setPreferences(prefs);
|
||||
|
||||
em.persist(prefs);
|
||||
em.persist(p);
|
||||
|
||||
String token = authenticationService.authenticate(p.getEmail(), "somepw");
|
||||
assertNotNull(token);
|
||||
}
|
||||
|
||||
@Test(expected = NoSuchUserException.class)
|
||||
public void authenticate_invalidUser_throwsException() throws Exception {
|
||||
authenticationService.authenticate("nonexisting@example.com", "foo");
|
||||
}
|
||||
|
||||
@Test(expected = AuthenticationException.class)
|
||||
public void authenticate_invalidPassword_throwsException() throws Exception {
|
||||
authenticationService.authenticate(TestData.RIDER_1_EMAIL, "foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void changePassword_existingUser_passwordChanged() throws Exception {
|
||||
authenticationService.changePassword(TestData.RIDER_1_EMAIL, "newPwd");
|
||||
assertNotNull(authenticationService.authenticate(TestData.RIDER_1_EMAIL, "newPwd"));
|
||||
}
|
||||
|
||||
@Test(expected = NoSuchUserException.class)
|
||||
public void changePassword_nonExistingUser_throwsException() throws Exception {
|
||||
authenticationService.changePassword("nonexisting@example.com", "foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getUser_existingToken_returnsUser() throws Exception {
|
||||
String token = authenticationService.authenticate(TestData.RIDER_1_EMAIL, TestData.RIDER_1_PW);
|
||||
assertEquals(TestData.RIDER_1_EMAIL, authenticationService.getUser(token));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getUser_nonExistingToken_returnsNull() throws Exception {
|
||||
assertNull(authenticationService.getUser("invalidToken"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isValid_existingToken_returnsTrue() throws Exception {
|
||||
String token = authenticationService.authenticate(TestData.RIDER_1_EMAIL, TestData.RIDER_1_PW);
|
||||
assertTrue(authenticationService.isValid(token));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isValid_nonExistingToken_returnsFalse() throws Exception {
|
||||
assertFalse(authenticationService.isValid("invalidToken"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invalidate_validToken_tokenInvalidatedReturnsTrue() throws Exception {
|
||||
String token = authenticationService.authenticate(TestData.RIDER_1_EMAIL, TestData.RIDER_1_PW);
|
||||
assertTrue(authenticationService.invalidate(token));
|
||||
assertFalse(authenticationService.isValid(token));
|
||||
assertNull(authenticationService.getUser(token));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invalidate_invalidToken_returnsFalse() throws Exception {
|
||||
assertFalse(authenticationService.invalidate("invalidToken"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package dst.ass2.service.auth.tests;
|
||||
|
||||
import dst.ass2.service.auth.AuthenticationServiceApplication;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.net.Socket;
|
||||
|
||||
@ActiveProfiles({"testdata", "grpc"})
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = AuthenticationServiceApplication.class)
|
||||
public class GrpcServerRunnerTest {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(GrpcServerRunnerTest.class);
|
||||
|
||||
@Value("${grpc.port}")
|
||||
private int port;
|
||||
|
||||
@Test
|
||||
public void canConnectToGrpcSocketAfterApplicationInitialization() throws Exception {
|
||||
int n = 4;
|
||||
|
||||
while (true) {
|
||||
LOG.info("Tyring to connect to TCP socket on localhost:{}", port);
|
||||
try (Socket socket = new Socket("localhost", port)) {
|
||||
return;
|
||||
} catch (Exception e) {
|
||||
if (n == 0) {
|
||||
throw new AssertionError("Expected gRPC server to run on port " + port, e);
|
||||
} else {
|
||||
Thread.sleep(250);
|
||||
}
|
||||
}
|
||||
n--;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
grpc.port=50051
|
||||
Reference in New Issue
Block a user