-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiBearerTokenAuthenticationConverter.java
More file actions
46 lines (36 loc) · 1.71 KB
/
ApiBearerTokenAuthenticationConverter.java
File metadata and controls
46 lines (36 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package org.openpodcastapi.opa.auth;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.server.resource.web.authentication.BearerTokenAuthenticationConverter;
import org.springframework.security.web.authentication.AuthenticationConverter;
import org.springframework.stereotype.Component;
import static org.slf4j.LoggerFactory.getLogger;
/// A converter that handles JWT-based auth for API requests.
///
/// This converter targets only the API endpoints at `/api`.
/// Auth for the frontend is handled by Spring's form login.
@Component
public class ApiBearerTokenAuthenticationConverter implements AuthenticationConverter {
private static final Logger log = getLogger(ApiBearerTokenAuthenticationConverter.class);
private final BearerTokenAuthenticationConverter delegate =
new BearerTokenAuthenticationConverter();
@Override
public Authentication convert(HttpServletRequest request) {
final var path = request.getRequestURI();
// Don't authenticate the auth endpoints
if (path.startsWith("/api/auth/")) {
log.debug("Bypassing token check for auth endpoint");
return null;
}
// If the request has no Bearer token, return null
final var header = request.getHeader("Authorization");
if (header == null || !header.startsWith("Bearer ")) {
log.debug("Request with no auth header sent to {}", request.getRequestURI());
return null;
}
log.debug("Converting request");
// Task Spring Boot with handling the request
return delegate.convert(request);
}
}