- added /login endpoint

- added authorization test
This commit is contained in:
Main
2016-02-15 23:18:28 +03:00
parent 23ffa96f47
commit 8c342e40ce
21 changed files with 199 additions and 61 deletions

View File

@@ -5,10 +5,7 @@ import net.chrisrichardson.eventstore.EventStore;
import net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.customers.Customer;
import net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.customers.CustomerService;
import net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.accounts.AccountInfo;
import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Address;
import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo;
import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Name;
import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.ToAccountInfo;
import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.*;
import net.chrisrichardson.eventstorestore.javaexamples.testutil.Producer;
import net.chrisrichardson.eventstorestore.javaexamples.testutil.Verifier;
import org.junit.Assert;

View File

@@ -1,13 +0,0 @@
apply plugin: 'java'
dependencies {
compile project(":common-auth")
compile "org.springframework.boot:spring-boot-starter-web:$springBootVersion"
compile "org.springframework.security:spring-security-config:4.0.2.RELEASE"
compile "org.springframework.security:spring-security-web:4.0.2.RELEASE"
testCompile "junit:junit:4.11"
}

View File

@@ -1,23 +0,0 @@
package net.chrisrichardson.eventstore.javaexamples.banking.commonauth.model;
/**
* Created by popikyardo on 21.09.15.
*/
public class AuthResponse {
private String token;
public AuthResponse() {
}
public AuthResponse(String token) {
this.token = token;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
}

View File

@@ -0,0 +1,13 @@
apply plugin: 'java'
dependencies {
compile project(":common-auth")
compile project(":common-customers")
compile project(":common-web")
compile "org.springframework.boot:spring-boot-starter-web:$springBootVersion"
compile "org.springframework.boot:spring-boot-starter-security:$springBootVersion"
compile "org.springframework.boot:spring-boot-starter-data-mongodb:$springBootVersion"
testCompile "junit:junit:4.11"
}

View File

@@ -0,0 +1,21 @@
package net.chrisrichardson.eventstore.javaexamples.banking.commonauth;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
/**
* Created by Main on 04.02.2016.
*/
@Configuration
@EnableMongoRepositories
@ComponentScan
public class CustomerAuthConfiguration {
@Bean
public CustomerAuthService customerAuthService(CustomerAuthRepository customerAuthRepository) {
return new CustomerAuthService(customerAuthRepository);
}
}

View File

@@ -0,0 +1,11 @@
package net.chrisrichardson.eventstore.javaexamples.banking.commonauth;
import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.QuerySideCustomer;
import org.springframework.data.mongodb.repository.MongoRepository;
import java.util.List;
interface CustomerAuthRepository extends MongoRepository<QuerySideCustomer, String> {
List<QuerySideCustomer> findByEmail(String email);
}

View File

@@ -0,0 +1,28 @@
package net.chrisrichardson.eventstore.javaexamples.banking.commonauth;
import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.QuerySideCustomer;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import java.util.List;
/**
* Created by Main on 15.02.2016.
*/
public class CustomerAuthService {
private CustomerAuthRepository customerAuthRepository;
public CustomerAuthService(CustomerAuthRepository customerAuthRepository) {
this.customerAuthRepository = customerAuthRepository;
}
public QuerySideCustomer findByEmail(String email){
List<QuerySideCustomer> customers = customerAuthRepository.findByEmail(email);
if (customers.isEmpty())
throw new EmptyResultDataAccessException(1);
else if(customers.size()>1)
throw new IncorrectResultSizeDataAccessException(1, customers.size());
else
return customers.get(0);
}
}

View File

@@ -1,8 +1,10 @@
package net.chrisrichardson.eventstore.javaexamples.banking.commonauth.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerResponse;
import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.QuerySideCustomer;
import net.chrisrichardson.eventstore.javaexamples.banking.commonauth.CustomerAuthService;
import net.chrisrichardson.eventstore.javaexamples.banking.commonauth.model.AuthRequest;
import net.chrisrichardson.eventstore.javaexamples.banking.commonauth.model.AuthResponse;
import net.chrisrichardson.eventstore.javaexamples.banking.commonauth.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
@@ -29,16 +31,18 @@ public class AuthController {
@Autowired
private TokenService tokenService;
@Autowired
private CustomerAuthService customerAuthService;
private static ObjectMapper objectMapper = new ObjectMapper();
@RequestMapping(value = "/login", method = POST)
public ResponseEntity<AuthResponse> doAuth(@RequestBody @Valid AuthRequest request) throws IOException {
User user = new User();
user.setEmail(request.getEmail());
public ResponseEntity<CustomerResponse> doAuth(@RequestBody @Valid AuthRequest request) throws IOException {
QuerySideCustomer customer = customerAuthService.findByEmail(request.getEmail());
Token token = tokenService.allocateToken(objectMapper.writeValueAsString(user));
return ResponseEntity.status(HttpStatus.OK)
.body(new AuthResponse(token.getKey()));
Token token = tokenService.allocateToken(objectMapper.writeValueAsString(new User(request.getEmail())));
return ResponseEntity.status(HttpStatus.OK).header("access-token", token.getKey())
.body(new CustomerResponse(customer.getId(), customer));
}

View File

@@ -52,11 +52,10 @@ public class AuthConfiguration extends WebSecurityConfigurerAdapter {
.antMatchers("/health").permitAll()
.antMatchers("/swagger-ui.html").permitAll()
.antMatchers("/v2/api-docs").permitAll()
.antMatchers("/js/**").permitAll()
.antMatchers("/styles/**").permitAll()
.antMatchers("/views/**").permitAll()
.antMatchers(HttpMethod.POST, "/register/step_1").permitAll()
.antMatchers(HttpMethod.POST, "/register/step_2").permitAll()
.antMatchers("/js*//**").permitAll()
.antMatchers("/styles*//**").permitAll()
.antMatchers("/views*//**").permitAll()
.antMatchers(HttpMethod.POST, "/customers").permitAll()
.antMatchers(HttpMethod.POST, "/login").permitAll()
.anyRequest().authenticated().and()
.addFilterBefore(new StatelessAuthenticationFilter(tokenAuthenticationService), UsernamePasswordAuthenticationFilter.class);

View File

@@ -18,6 +18,13 @@ public class User implements UserDetails {
private String email;
public User() {
}
public User(String email) {
this.email = email;
}
public void setUsername(String username) {
this.email = username;
}

View File

@@ -0,0 +1,2 @@
auth.serverSecret=the_cake_is_a_lie
auth.serverInteger=1

View File

@@ -1,9 +1,4 @@
package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers;
import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Address;
import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo;
import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Name;
import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.ToAccountInfo;
package net.chrisrichardson.eventstore.javaexamples.banking.common.customers;
import java.util.Map;

View File

@@ -1,6 +1,7 @@
package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers;
import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo;
import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.QuerySideCustomer;
import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.ToAccountInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

View File

@@ -1,6 +1,7 @@
package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers;
import net.chrisrichardson.eventstore.EntityIdentifier;
import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.QuerySideCustomer;
import org.springframework.dao.EmptyResultDataAccessException;
import rx.Observable;

View File

@@ -1,5 +1,6 @@
package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers;
import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.QuerySideCustomer;
import org.springframework.data.mongodb.repository.MongoRepository;
import java.util.List;

View File

@@ -2,8 +2,8 @@ package net.chrisrichardson.eventstore.javaexamples.banking.web.queryside.custom
import net.chrisrichardson.eventstore.EntityIdentifier;
import net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers.CustomerQueryService;
import net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers.QuerySideCustomer;
import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerResponse;
import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.QuerySideCustomer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.http.HttpStatus;

View File

@@ -8,6 +8,7 @@ dependencies {
compile project(":transactions-command-side-web")
compile project(":customers-command-side-web")
compile project(":customers-query-side-web")
compile project(":common-auth-web")
compile "org.springframework.boot:spring-boot-starter-web"
compile "org.springframework.boot:spring-boot-starter-actuator"

View File

@@ -1,5 +1,6 @@
package net.chrisrichardson.eventstore.javaexamples.banking.web;
import net.chrisrichardson.eventstore.javaexamples.banking.commonauth.CustomerAuthConfiguration;
import net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.accounts.CommandSideWebAccountsConfiguration;
import net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.customers.CustomersCommandSideWebConfiguration;
import net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.transactions.CommandSideWebTransactionsConfiguration;
@@ -16,7 +17,7 @@ import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
@Configuration
@Import({CommandSideWebAccountsConfiguration.class, CommandSideWebTransactionsConfiguration.class, JdbcEventStoreConfiguration.class, QuerySideWebConfiguration.class, CustomersQuerySideWebConfiguration.class, CustomersCommandSideWebConfiguration.class})
@Import({CommandSideWebAccountsConfiguration.class, CommandSideWebTransactionsConfiguration.class, JdbcEventStoreConfiguration.class, QuerySideWebConfiguration.class, CustomersQuerySideWebConfiguration.class, CustomersCommandSideWebConfiguration.class, CustomerAuthConfiguration.class})
@EnableAutoConfiguration
@ComponentScan
public class BankingWebConfiguration {

View File

@@ -0,0 +1,92 @@
package net.chrisrichardson.eventstore.javaexamples.banking.web;
import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Address;
import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo;
import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerResponse;
import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Name;
import net.chrisrichardson.eventstore.javaexamples.banking.commonauth.model.AuthRequest;
import net.chrisrichardson.eventstorestore.javaexamples.testutil.Producer;
import net.chrisrichardson.eventstorestore.javaexamples.testutil.Verifier;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.client.RestTemplate;
import rx.Observable;
import static net.chrisrichardson.eventstorestore.javaexamples.testutil.TestUtil.eventually;
/**
* Created by Main on 15.02.2016.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = BankingWebTestConfiguration.class)
@WebAppConfiguration
@IntegrationTest({"server.port=0", "management.port=0"})
public class BankingAuthTest {
@Value("${local.server.port}")
private int port;
@Autowired
RestTemplate restTemplate;
private String baseUrl(String path) {
return "http://localhost:" + port + "/" + path;
}
@Test
public void shouldCreateCustomerAndLogin() {
CustomerInfo customerInfo = generateCustomerInfo();
final CustomerResponse customerResponse = restTemplate.postForEntity(baseUrl("/customers"), customerInfo, CustomerResponse.class).getBody();
final String customerId = customerResponse.getId();
Assert.assertNotNull(customerId);
Assert.assertEquals(customerInfo, customerResponse.getCustomerInfo());
assertCustomerResponse(customerId, customerInfo);
AuthRequest authRequest = new AuthRequest("current@email.com");
final CustomerResponse loginCustomerResponse = restTemplate.postForEntity(baseUrl("/login"), authRequest, CustomerResponse.class).getBody();
Assert.assertEquals(customerResponse, loginCustomerResponse);
}
private void assertCustomerResponse(final String customerId, final CustomerInfo customerInfo) {
eventually(
new Producer<CustomerResponse>() {
@Override
public Observable<CustomerResponse> produce() {
return Observable.just(restTemplate.getForEntity(baseUrl("/customers/" + customerId), CustomerResponse.class).getBody());
}
},
new Verifier<CustomerResponse>() {
@Override
public void verify(CustomerResponse customerResponse) {
Assert.assertEquals(customerId, customerResponse.getId());
Assert.assertEquals(customerInfo, customerResponse.getCustomerInfo());
}
});
}
private CustomerInfo generateCustomerInfo() {
return new CustomerInfo(
new Name("John", "Doe"),
"current@email.com",
"000-00-0000",
"1-111-111-1111",
new Address("street 1",
"street 2",
"City",
"State",
"1111111")
);
}
}

View File

@@ -32,5 +32,5 @@ include 'customers-query-side-web'
include 'common-customers'
include 'customers-command-side-service'
include 'customers-query-side-service'
include 'common-auth-controller'
include 'common-auth-web'