Skip to content

Commit 77717f4

Browse files
committed
Add auth middleware
1 parent f14a444 commit 77717f4

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package tv.codely.apps.backoffice.backend.config;
2+
3+
import org.springframework.boot.web.servlet.FilterRegistrationBean;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import tv.codely.shared.infrastructure.spring.BasicHttpAuthMiddleware;
7+
8+
@Configuration
9+
public class BackofficeBackendServerConfiguration {
10+
@Bean
11+
public FilterRegistrationBean<BasicHttpAuthMiddleware> basicHttpAuthMiddleware() {
12+
FilterRegistrationBean<BasicHttpAuthMiddleware> registrationBean = new FilterRegistrationBean<>();
13+
14+
registrationBean.setFilter(new BasicHttpAuthMiddleware());
15+
registrationBean.addUrlPatterns("/health-check");
16+
17+
return registrationBean;
18+
}
19+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package tv.codely.shared.infrastructure.spring;
2+
3+
import javax.servlet.*;
4+
import javax.servlet.http.HttpServletRequest;
5+
import javax.servlet.http.HttpServletResponse;
6+
import java.io.IOException;
7+
import java.util.Base64;
8+
import java.util.HashMap;
9+
10+
public final class BasicHttpAuthMiddleware implements Filter {
11+
private final HashMap<String, String> validUsers = new HashMap<String, String>() {{
12+
put("javi", "barbitas");
13+
put("rafa", "pelazo");
14+
}};
15+
16+
@Override
17+
public void doFilter(
18+
ServletRequest request,
19+
ServletResponse response,
20+
FilterChain chain
21+
) throws IOException, ServletException {
22+
String authorizationHeader = ((HttpServletRequest) request).getHeader("authorization");
23+
24+
if (hasIntroducedCredentials(authorizationHeader)) {
25+
authenticate(authorizationHeader, chain, request, response);
26+
} else {
27+
askForCredentials(response);
28+
}
29+
}
30+
31+
private boolean hasIntroducedCredentials(String authorizationHeader) {
32+
return null != authorizationHeader;
33+
}
34+
35+
private void authenticate(
36+
String authorizationHeader,
37+
FilterChain chain,
38+
ServletRequest request,
39+
ServletResponse response
40+
) throws IOException, ServletException {
41+
String[] auth = decodeAuth(authorizationHeader);
42+
String user = auth[0];
43+
String pass = auth[1];
44+
45+
if (isValid(user, pass)) {
46+
request.setAttribute("authentication_username", user);
47+
chain.doFilter(request, response);
48+
} else {
49+
setInvalidCredentials(response);
50+
}
51+
}
52+
53+
private boolean isValid(String user, String pass) {
54+
return validUsers.containsKey(user) && validUsers.get(user).equals(pass);
55+
}
56+
57+
private String[] decodeAuth(String authString) {
58+
return new String(Base64.getDecoder().decode(authString.split("\\s+")[1])).split(":");
59+
}
60+
61+
private void setInvalidCredentials(ServletResponse response) {
62+
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
63+
httpServletResponse.reset();
64+
httpServletResponse.setStatus(HttpServletResponse.SC_FORBIDDEN);
65+
}
66+
67+
private void askForCredentials(ServletResponse response) {
68+
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
69+
httpServletResponse.reset();
70+
httpServletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
71+
httpServletResponse.setHeader("WWW-Authenticate", "Basic realm=\"CodelyTV\"");
72+
}
73+
}

0 commit comments

Comments
 (0)