Search in sources :

Example 6 with JWTUser

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);
}
Also used : AuditSection(com.salesmanager.core.model.common.audit.AuditSection) JWTUser(com.salesmanager.shop.store.security.user.JWTUser) Date(java.util.Date)

Example 7 with JWTUser

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);
    }
}
Also used : JWTUser(com.salesmanager.shop.store.security.user.JWTUser) AuthenticationResponse(com.salesmanager.shop.store.security.AuthenticationResponse) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 8 with JWTUser

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));
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) Authentication(org.springframework.security.core.Authentication) JWTUser(com.salesmanager.shop.store.security.user.JWTUser) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) GenericRuntimeException(com.salesmanager.shop.store.api.exception.GenericRuntimeException) AuthenticationResponse(com.salesmanager.shop.store.security.AuthenticationResponse) AuthenticationException(org.apache.http.auth.AuthenticationException) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) UnauthorizedException(com.salesmanager.shop.store.api.exception.UnauthorizedException) GenericRuntimeException(com.salesmanager.shop.store.api.exception.GenericRuntimeException) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

JWTUser (com.salesmanager.shop.store.security.user.JWTUser)8 AuthenticationResponse (com.salesmanager.shop.store.security.AuthenticationResponse)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 Date (java.util.Date)3 AuthenticationException (org.apache.http.auth.AuthenticationException)3 ResponseEntity (org.springframework.http.ResponseEntity)3 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)3 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)3 Authentication (org.springframework.security.core.Authentication)3 AuditSection (com.salesmanager.core.model.common.audit.AuditSection)2 GenericRuntimeException (com.salesmanager.shop.store.api.exception.GenericRuntimeException)2 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)2 UnauthorizedException (com.salesmanager.shop.store.api.exception.UnauthorizedException)2 ApiOperation (io.swagger.annotations.ApiOperation)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 ApiImplicitParams (io.swagger.annotations.ApiImplicitParams)1 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)1