-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DUOS-2873][risk=no] Cache request headers and populate auth user wit…
…h them (#2233)
- Loading branch information
Showing
15 changed files
with
191 additions
and
285 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 0 additions & 129 deletions
129
src/main/java/org/broadinstitute/consent/http/authentication/GenericUser.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/main/java/org/broadinstitute/consent/http/filters/ClaimsCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.broadinstitute.consent.http.filters; | ||
|
||
import com.google.common.cache.Cache; | ||
import com.google.common.cache.CacheBuilder; | ||
import jakarta.ws.rs.core.MultivaluedMap; | ||
import java.util.AbstractMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Manage a cache of bearer token to map of `OAUTH2_CLAIM` headers for every request. This is | ||
* useful in cases where components need, but do not have access to, the full request context. | ||
*/ | ||
public class ClaimsCache { | ||
|
||
private static ClaimsCache INSTANCE; | ||
public final Cache<String, Map<String, String>> cache; | ||
public final static String OAUTH2_CLAIM_email = "OAUTH2_CLAIM_email"; | ||
public final static String OAUTH2_CLAIM_name = "OAUTH2_CLAIM_name"; | ||
public final static String OAUTH2_CLAIM_access_token = "OAUTH2_CLAIM_access_token"; | ||
public final static String OAUTH2_CLAIM_aud = "OAUTH2_CLAIM_aud"; | ||
|
||
private ClaimsCache() { | ||
cache = CacheBuilder | ||
.newBuilder() | ||
.expireAfterWrite(5, TimeUnit.MINUTES) | ||
.build(); | ||
} | ||
|
||
public static ClaimsCache getInstance() { | ||
if (INSTANCE == null) { | ||
INSTANCE = new ClaimsCache(); | ||
} | ||
return INSTANCE; | ||
} | ||
|
||
private String getFirst(List<String> headerValues) { | ||
if (headerValues == null) { | ||
return null; | ||
} | ||
return headerValues.stream().findFirst().orElse(null); | ||
} | ||
|
||
public void loadCache(String token, MultivaluedMap<String, String> headers) { | ||
if (this.cache.getIfPresent(token) == null) { | ||
Map<String, String> claimsMap = headers.entrySet() | ||
.stream() | ||
.filter(e -> e.getKey().startsWith("OAUTH2_CLAIM")) | ||
.map(e -> new AbstractMap.SimpleEntry<>(e.getKey(), getFirst(e.getValue()))) | ||
.collect(Collectors.toMap(Entry::getKey, Entry::getValue)); | ||
this.cache.put(token, claimsMap); | ||
this.cache.cleanUp(); | ||
} | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/org/broadinstitute/consent/http/filters/RequestHeaderCacheFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package org.broadinstitute.consent.http.filters; | ||
|
||
import jakarta.annotation.Priority; | ||
import jakarta.ws.rs.container.ContainerRequestContext; | ||
import jakarta.ws.rs.container.ContainerRequestFilter; | ||
import jakarta.ws.rs.core.HttpHeaders; | ||
import jakarta.ws.rs.ext.Provider; | ||
import java.io.IOException; | ||
|
||
@Provider | ||
@Priority(Integer.MIN_VALUE) | ||
public class RequestHeaderCacheFilter implements ContainerRequestFilter { | ||
|
||
private final ClaimsCache claimsCache = ClaimsCache.getInstance(); | ||
|
||
@Override | ||
public void filter(ContainerRequestContext containerRequestContext) throws IOException { | ||
var headers = containerRequestContext.getHeaders(); | ||
var token = headers.getFirst(HttpHeaders.AUTHORIZATION); | ||
if (token != null) { | ||
var bearer = token.replace("Bearer ", ""); | ||
claimsCache.loadCache(bearer, headers); | ||
} | ||
} | ||
} |
Oops, something went wrong.