Search in sources :

Example 1 with JWTUser

use of com.salesmanager.shop.store.security.user.JWTUser in project shopizer by shopizer-ecommerce.

the class JWTTokenUtil method validateToken.

public Boolean validateToken(String token, UserDetails userDetails) {
    JWTUser user = (JWTUser) userDetails;
    final String username = getUsernameFromToken(token);
    final Date created = getIssuedAtDateFromToken(token);
    // final Date expiration = getExpirationDateFromToken(token);
    boolean usernameEquals = username.equals(user.getUsername());
    boolean isTokenExpired = isTokenExpired(token);
    boolean isTokenCreatedBeforeLastPasswordReset = isCreatedBeforeLastPasswordReset(created, user.getLastPasswordResetDate());
    return (usernameEquals && !isTokenExpired && !isTokenCreatedBeforeLastPasswordReset);
}
Also used : JWTUser(com.salesmanager.shop.store.security.user.JWTUser) Date(java.util.Date)

Example 2 with JWTUser

use of com.salesmanager.shop.store.security.user.JWTUser in project shopizer by shopizer-ecommerce.

the class AuthenticateCustomerApi method authenticate.

/**
 * Authenticate a customer using username & password
 * @param authenticationRequest
 * @param device
 * @return
 * @throws AuthenticationException
 */
@RequestMapping(value = "/customer/login", method = RequestMethod.POST, produces = { "application/json" })
@ApiOperation(httpMethod = "POST", value = "Authenticates a customer to the application", notes = "Customer can authenticate after registration, request is {\"username\":\"admin\",\"password\":\"password\"}", response = ResponseEntity.class)
@ResponseBody
public ResponseEntity<?> authenticate(@RequestBody @Valid AuthenticationRequest authenticationRequest) throws AuthenticationException {
    // TODO SET STORE in flow
    // Perform the security
    Authentication authentication = null;
    try {
        // to be used when username and password are set
        authentication = jwtCustomerAuthenticationManager.authenticate(new UsernamePasswordAuthenticationToken(authenticationRequest.getUsername(), authenticationRequest.getPassword()));
    } catch (BadCredentialsException unn) {
        return new ResponseEntity<>("{\"message\":\"Bad credentials\"}", HttpStatus.UNAUTHORIZED);
    } catch (Exception e) {
        return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
    }
    if (authentication == null) {
        return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
    }
    SecurityContextHolder.getContext().setAuthentication(authentication);
    // Reload password post-security so we can generate token
    // todo create one for social
    final JWTUser userDetails = (JWTUser) jwtCustomerDetailsService.loadUserByUsername(authenticationRequest.getUsername());
    final String token = jwtTokenUtil.generateToken(userDetails);
    // Return the token
    return ResponseEntity.ok(new AuthenticationResponse(userDetails.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) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) 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) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 3 with JWTUser

use of com.salesmanager.shop.store.security.user.JWTUser in project shopizer by shopizer-ecommerce.

the class AuthenticateUserApi method authenticate.

/**
 * Authenticate a user using username & password
 * @param authenticationRequest
 * @param device
 * @return
 * @throws AuthenticationException
 */
@RequestMapping(value = "/private/login", method = RequestMethod.POST)
public ResponseEntity<?> authenticate(@RequestBody @Valid AuthenticationRequest authenticationRequest) throws AuthenticationException {
    // TODO SET STORE in flow
    // Perform the security
    Authentication authentication = null;
    try {
        // to be used when username and password are set
        authentication = jwtAdminAuthenticationManager.authenticate(new UsernamePasswordAuthenticationToken(authenticationRequest.getUsername(), authenticationRequest.getPassword()));
    } catch (Exception e) {
        if (e instanceof BadCredentialsException) {
            return new ResponseEntity<>("{\"message\":\"Bad credentials\"}", HttpStatus.UNAUTHORIZED);
        }
        LOGGER.error("Error during authentication " + e.getMessage());
        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) jwtAdminDetailsService.loadUserByUsername(authenticationRequest.getUsername());
    final String token = jwtTokenUtil.generateToken(userDetails);
    // Return the token
    return ResponseEntity.ok(new AuthenticationResponse(userDetails.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) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) AuthenticationResponse(com.salesmanager.shop.store.security.AuthenticationResponse) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) AuthenticationException(org.apache.http.auth.AuthenticationException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with JWTUser

use of com.salesmanager.shop.store.security.user.JWTUser in project shopizer by shopizer-ecommerce.

the class AuthenticateUserApi method refreshAndGetAuthenticationToken.

@RequestMapping(value = "/auth/refresh", method = RequestMethod.GET)
public ResponseEntity<AuthenticationResponse> refreshAndGetAuthenticationToken(HttpServletRequest request) {
    String token = request.getHeader(tokenHeader);
    if (token != null && token.contains("Bearer")) {
        token = token.substring("Bearer ".length(), token.length());
    }
    String username = jwtTokenUtil.getUsernameFromToken(token);
    JWTUser user = (JWTUser) jwtAdminDetailsService.loadUserByUsername(username);
    if (jwtTokenUtil.canTokenBeRefreshedWithGrace(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 5 with JWTUser

use of com.salesmanager.shop.store.security.user.JWTUser in project shopizer by shopizer-ecommerce.

the class JWTAdminServicesImpl method userDetails.

private UserDetails userDetails(String userName, User user, Collection<GrantedAuthority> authorities) {
    AuditSection section = null;
    section = user.getAuditSection();
    Date lastModified = null;
    return new JWTUser(user.getId(), userName, user.getFirstName(), user.getLastName(), user.getAdminEmail(), user.getAdminPassword(), 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)

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