Skip to content

Commit

Permalink
Merge branch 'master' into stage
Browse files Browse the repository at this point in the history
  • Loading branch information
GodCipher committed May 29, 2024
2 parents c5b1585 + 11e8ec7 commit 132834e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
Expand All @@ -20,20 +19,26 @@ public ApiKeyAuthFilter(String headerName, AuthenticationManager authenticationM

this.headerName = headerName;
setAuthenticationManager(authenticationManager);
setAuthenticationFailureHandler(
(request, response, exception) -> {
log.debug("Failed to authenticate API key", exception);
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
});
setAuthenticationSuccessHandler(
(request, response, authentication) -> {
log.debug("API key authenticated successfully");
response.setStatus(HttpServletResponse.SC_OK);
});
}

@Override
public Authentication attemptAuthentication(
HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
String apiKey = request.getHeader(headerName);
apiKey = apiKey == null ? null : apiKey.trim();
log.info("API Key: {}", apiKey);
if (apiKey == null || apiKey.isEmpty()) {
log.info("API Key not found in request header");
throw new BadCredentialsException("API Key not found in request header");
}
log.info("API Key found in request header, authenticating");
return getAuthenticationManager()
.authenticate(new UsernamePasswordAuthenticationToken(apiKey, null));
return getAuthenticationManager().authenticate(new ApiKeyAuthenticationToken(apiKey));
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package dev.luzifer.spring.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;

@Slf4j
public class ApiKeyAuthenticationProvider implements AuthenticationProvider {

private final String apiKey;
Expand All @@ -19,15 +17,12 @@ public ApiKeyAuthenticationProvider(String apiKey) {
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
ApiKeyAuthenticationToken authenticationToken = (ApiKeyAuthenticationToken) authentication;

log.info("Authenticating API key: {}", authenticationToken.getCredentials());
if (authenticationToken.getCredentials() instanceof String credentials) {
if (apiKey.equals(credentials)) {
log.info("API key authenticated successfully");
return new ApiKeyAuthenticationToken(apiKey);
}
}

log.info("API key was not found or not the expected value");
throw new BadCredentialsException("The API key was not found or not the expected value.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@
public class WebSecurityConfig {

private final String apiKey;
private final String apiKeyHeader;

public WebSecurityConfig(@Value("${api.key}") String apiKey) {
public WebSecurityConfig(
@Value("${api.key}") String apiKey, @Value("${api.key.header}") String apiKeyHeader) {
this.apiKey = apiKey;
this.apiKeyHeader = apiKeyHeader;
}

@Bean
public ApiKeyAuthFilter apiKeyAuthFilter(AuthenticationManager authenticationManager) {
return new ApiKeyAuthFilter("API-KEY-HEADER", authenticationManager);
return new ApiKeyAuthFilter(apiKeyHeader, authenticationManager);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ spring.data.redis.host=localhost
spring.data.redis.port=6379
# API-Requests
api.key=THV6aSBpc3QgZWluIFPDvMOfaQ==
api.key.header=API-KEY
# API-Paths
api.match=api/match
api.match.count=api/match/count

0 comments on commit 132834e

Please sign in to comment.