package com.technicalrex.springsecurityjwt.auth.jwt;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.User;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TokenAuthenticationService {
private static final String AUTH_HEADER_NAME = "X-AUTH-TOKEN";
private final TokenHandler tokenHandler;
public TokenAuthenticationService(String secret, UserService userService) {
tokenHandler = new TokenHandler(secret, userService);
}
public String addAuthentication(HttpServletResponse response, UserAuthentication authentication) {
final User user = authentication.getDetails();
String token = tokenHandler.createTokenForUser(user);
response.addHeader(AUTH_HEADER_NAME, token);
return token;
}
public Authentication getAuthentication(HttpServletRequest request) {
final String token = request.getHeader(AUTH_HEADER_NAME);
if (token != null) {
final User user = tokenHandler.parseUserFromToken(token);
if (user != null) {
return new UserAuthentication(user);
}
}
return null;
}
}