use of com.salesmanager.shop.store.security.user.JWTUser in project shopizer by shopizer-ecommerce.
the class JWTCustomerServicesImpl method userDetails.
@Override
protected UserDetails userDetails(String userName, Customer customer, Collection<GrantedAuthority> authorities) {
AuditSection section = null;
section = customer.getAuditSection();
Date lastModified = null;
return new JWTUser(customer.getId(), userName, customer.getBilling().getFirstName(), customer.getBilling().getLastName(), customer.getEmailAddress(), customer.getPassword(), authorities, true, lastModified);
}
use of com.salesmanager.shop.store.security.user.JWTUser in project shopizer by shopizer-ecommerce.
the class AuthenticateCustomerApi method refreshToken.
@RequestMapping(value = "/auth/customer/refresh", method = RequestMethod.GET, produces = { "application/json" })
public ResponseEntity<?> refreshToken(HttpServletRequest request) {
String token = request.getHeader(tokenHeader);
String username = jwtTokenUtil.getUsernameFromToken(token);
JWTUser user = (JWTUser) jwtCustomerDetailsService.loadUserByUsername(username);
if (jwtTokenUtil.canTokenBeRefreshed(token, user.getLastPasswordResetDate())) {
String refreshedToken = jwtTokenUtil.refreshToken(token);
return ResponseEntity.ok(new AuthenticationResponse(user.getId(), refreshedToken));
} else {
return ResponseEntity.badRequest().body(null);
}
}
use of com.salesmanager.shop.store.security.user.JWTUser in project shopizer by shopizer-ecommerce.
the class AuthenticateCustomerApi method register.
/**
* Create new customer for a given MerchantStore, then authenticate that customer
*/
@RequestMapping(value = { "/customer/register" }, method = RequestMethod.POST, produces = { "application/json" })
@ResponseStatus(HttpStatus.CREATED)
@ApiOperation(httpMethod = "POST", value = "Registers a customer to the application", notes = "Used as self-served operation", response = AuthenticationResponse.class)
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "string", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "string", defaultValue = "en") })
@ResponseBody
public ResponseEntity<?> register(@Valid @RequestBody PersistableCustomer customer, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language) throws Exception {
customer.setUserName(customer.getEmailAddress());
if (customerFacade.checkIfUserExists(customer.getUserName(), merchantStore)) {
// 409 Conflict
throw new GenericRuntimeException("409", "Customer with email [" + customer.getEmailAddress() + "] is already registered");
}
Validate.notNull(customer.getUserName(), "Username cannot be null");
Validate.notNull(customer.getBilling(), "Requires customer Country code");
Validate.notNull(customer.getBilling().getCountry(), "Requires customer Country code");
customerFacade.registerCustomer(customer, merchantStore, language);
// Perform the security
Authentication authentication = null;
try {
authentication = jwtCustomerAuthenticationManager.authenticate(new UsernamePasswordAuthenticationToken(customer.getUserName(), customer.getPassword()));
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
if (authentication == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
SecurityContextHolder.getContext().setAuthentication(authentication);
// Reload password post-security so we can generate token
final JWTUser userDetails = (JWTUser) jwtCustomerDetailsService.loadUserByUsername(customer.getUserName());
final String token = jwtTokenUtil.generateToken(userDetails);
// Return the token
return ResponseEntity.ok(new AuthenticationResponse(customer.getId(), token));
}
Aggregations