I am implementing a custom auth server using spring's new authorization server. Our need was to implement a CustomAccessTokenResponseHandler for providing additional information in the http json response. I have achieved that by implementing AuthenticationSuccessHandler and overriding onAuthenticationSuccess method to add some extra user information. Also being used in the configuration are custom authentication grant type (authentication converter,provider and token) and TokenCustomizer to add extra claims to the token. The issue is wrt refresh token, i.e; when providing grant type as refresh token and while requesting for a new access token, as I am not providing username and password, the token is still getting generated if I dont use the customResponseHandler but not without it. Problem now is the authentication object contains the acccess token but in its principal now, I can not find the user details. I am at loss trying to understand how the securityContext is having the user information while creating the access-token from refresh-token and where will I find those details. I just need the username to query db and fetch some extra information for it to provide in the response.
@Transactional
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException, ServletException {
Auth2AccessTokenAuthenticationToken accessTokenAuthentication =
(OAuth2AccessTokenAuthenticationToken) authentication;
OAuth2ClientAuthenticationToken oAuth2ClientAuthenticationToken = (OAuth2ClientAuthenticationToken) accessTokenAuthentication.getPrincipal();
CustomPasswordUser user = (CustomPasswordUser) oAuth2ClientAuthenticationToken.getDetails();
..................
..........................}```
This piece of code is giving error because oAuth2ClientAuthenticationToken.getDetails() now has the following structure
OAuth2ClientAuthenticationToken [Principal=eQCLrL7JVHw1GRzP, Credentials=[PROTECTED], Authenticated=true, Details=WebAuthenticationDetails [RemoteIpAddress=0:0:0:0:0:0:0:1, SessionId=null], Granted Authorities=[]].
SecurityContextHolder.getContext().getAuthentication().getDetails() returns WebAuthenticationDetails [RemoteIpAddress=0:0:0:0:0:0:0:1, SessionId=null].
P.S : I can get the actual accesstoken from authentication but I am parallely working on creating custom JWTEncoder using JWE, and do not want to decode the token inside authorization server nor does I think it is the right approach.