|
| 1 | +package sanchez.sergio.security.filter; |
| 2 | + |
| 3 | +import java.io.ByteArrayOutputStream; |
| 4 | +import java.io.IOException; |
| 5 | + |
| 6 | +import javax.servlet.Filter; |
| 7 | +import javax.servlet.FilterChain; |
| 8 | +import javax.servlet.FilterConfig; |
| 9 | +import javax.servlet.ServletException; |
| 10 | +import javax.servlet.ServletOutputStream; |
| 11 | +import javax.servlet.ServletRequest; |
| 12 | +import javax.servlet.ServletResponse; |
| 13 | +import javax.servlet.http.HttpServletResponse; |
| 14 | +import javax.servlet.http.HttpServletResponseWrapper; |
| 15 | + |
| 16 | +import org.apache.commons.io.output.TeeOutputStream; |
| 17 | +import org.apache.log4j.Logger; |
| 18 | +import org.springframework.mock.web.DelegatingServletOutputStream; |
| 19 | +import org.springframework.security.core.Authentication; |
| 20 | +import org.springframework.security.core.context.SecurityContextHolder; |
| 21 | +import org.springframework.security.oauth2.common.OAuth2AccessToken; |
| 22 | +import org.springframework.security.oauth2.common.util.OAuth2Utils; |
| 23 | +import org.springframework.security.oauth2.provider.OAuth2Authentication; |
| 24 | +import org.springframework.security.oauth2.provider.token.DefaultTokenServices; |
| 25 | + |
| 26 | +/** |
| 27 | + * @author Sergio Sánchez |
| 28 | + * Filter to handle authentication of server |
| 29 | + * |
| 30 | + */ |
| 31 | +public abstract class OAuth2ServerAuthenticationFilter implements Filter { |
| 32 | + |
| 33 | + private static Logger log = Logger |
| 34 | + .getLogger(OAuth2ServerAuthenticationFilter.class); |
| 35 | + |
| 36 | + private DefaultTokenServices tokenServices; |
| 37 | + |
| 38 | + private OAuth2AccessTokenExtractor tokenExtractor; |
| 39 | + |
| 40 | + public OAuth2ServerAuthenticationFilter(DefaultTokenServices tokenServices) { |
| 41 | + this.tokenServices = tokenServices; |
| 42 | + this.tokenExtractor = new DefaultOAuth2AccessTokenExtractor(); |
| 43 | + } |
| 44 | + |
| 45 | + public OAuth2ServerAuthenticationFilter(DefaultTokenServices tokenServices, |
| 46 | + OAuth2AccessTokenExtractor tokenExtractor) { |
| 47 | + this.tokenServices = tokenServices; |
| 48 | + this.tokenExtractor = tokenExtractor; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void doFilter(ServletRequest request, ServletResponse response, |
| 53 | + FilterChain chain) throws IOException, ServletException { |
| 54 | + |
| 55 | + // create wrapper to read response body |
| 56 | + ByteArrayResponseWrapper responseWraper = new ByteArrayResponseWrapper( |
| 57 | + response); |
| 58 | + |
| 59 | + // led them go |
| 60 | + chain.doFilter(request, responseWraper); |
| 61 | + |
| 62 | + // get ClientAuthentication |
| 63 | + Authentication clientAuthentication = SecurityContextHolder |
| 64 | + .getContext().getAuthentication(); |
| 65 | + |
| 66 | + // is authenticated or not to proceed |
| 67 | + if (clientAuthentication != null |
| 68 | + && clientAuthentication.isAuthenticated()) { |
| 69 | + |
| 70 | + // callBack client authenticated successfully |
| 71 | + onSuccessfulClientAuthentication(request, response, |
| 72 | + clientAuthentication); |
| 73 | + |
| 74 | + // check response status is success of failure |
| 75 | + if (responseWraper.getStatus() == 200) { |
| 76 | + |
| 77 | + // extract accessToken from response |
| 78 | + String token = tokenExtractor |
| 79 | + .getAccessTokenValue(responseWraper.getByteArray()); |
| 80 | + |
| 81 | + if (token != null && !token.isEmpty()) { |
| 82 | + |
| 83 | + // load authentication from token |
| 84 | + OAuth2Authentication oAuth2Authentication = this.tokenServices |
| 85 | + .loadAuthentication(token); |
| 86 | + OAuth2AccessToken actualAccessToken = this.tokenServices |
| 87 | + .getAccessToken(oAuth2Authentication); |
| 88 | + |
| 89 | + // callBack user authenticated successfully |
| 90 | + onSuccessfulUserAuthentication(request, response, |
| 91 | + clientAuthentication, oAuth2Authentication, |
| 92 | + actualAccessToken); |
| 93 | + } else { |
| 94 | + log.error("access token is empty from extractor"); |
| 95 | + } |
| 96 | + } else { |
| 97 | + // callBack user authenticated failure |
| 98 | + onFailureUserAuthentication(request, response, |
| 99 | + clientAuthentication, request.getParameter("username")); |
| 100 | + } |
| 101 | + } else { |
| 102 | + // callBack client authenticated failure |
| 103 | + onFailClientAuthentication(request, response, |
| 104 | + request.getParameter(OAuth2Utils.CLIENT_ID)); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + protected void onSuccessfulClientAuthentication(ServletRequest request, |
| 109 | + ServletResponse response, Authentication authentication) { |
| 110 | + } |
| 111 | + |
| 112 | + protected void onFailClientAuthentication(ServletRequest request, |
| 113 | + ServletResponse response, String clientId) { |
| 114 | + } |
| 115 | + |
| 116 | + protected void onSuccessfulUserAuthentication(ServletRequest request, |
| 117 | + ServletResponse response, Authentication clientAuthentication, |
| 118 | + OAuth2Authentication userOAuth2Authentication, |
| 119 | + OAuth2AccessToken token) { |
| 120 | + } |
| 121 | + |
| 122 | + protected void onFailureUserAuthentication(ServletRequest request, |
| 123 | + ServletResponse response, Authentication clientAuthentication, |
| 124 | + String username) { |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public void init(FilterConfig filterConfig) throws ServletException { |
| 129 | + |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public void destroy() { |
| 134 | + |
| 135 | + } |
| 136 | + |
| 137 | + public DefaultTokenServices getTokenServices() { |
| 138 | + return tokenServices; |
| 139 | + } |
| 140 | + |
| 141 | + public void setTokenServices(DefaultTokenServices tokenServices) { |
| 142 | + this.tokenServices = tokenServices; |
| 143 | + } |
| 144 | + |
| 145 | + public OAuth2AccessTokenExtractor getTokenExtractor() { |
| 146 | + return tokenExtractor; |
| 147 | + } |
| 148 | + |
| 149 | + public void setTokenExtractor(OAuth2AccessTokenExtractor tokenExtractor) { |
| 150 | + this.tokenExtractor = tokenExtractor; |
| 151 | + } |
| 152 | + |
| 153 | + // response wrapper |
| 154 | + private class ByteArrayResponseWrapper extends HttpServletResponseWrapper { |
| 155 | + |
| 156 | + public ByteArrayResponseWrapper(ServletResponse response) { |
| 157 | + super((HttpServletResponse) response); |
| 158 | + } |
| 159 | + |
| 160 | + private ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
| 161 | + |
| 162 | + @Override |
| 163 | + public ServletOutputStream getOutputStream() throws IOException { |
| 164 | + return new DelegatingServletOutputStream(new TeeOutputStream( |
| 165 | + super.getOutputStream(), byteArrayOutputStream)); |
| 166 | + } |
| 167 | + |
| 168 | + public byte[] getByteArray() { |
| 169 | + return this.byteArrayOutputStream.toByteArray(); |
| 170 | + } |
| 171 | + } |
| 172 | +} |
0 commit comments