-
Notifications
You must be signed in to change notification settings - Fork 20
WIP: feat: Add authorization context helpers and server-side filtering for timeseries #1461
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
vairav
wants to merge
4
commits into
develop
Choose a base branch
from
feature/auth-context-and-filters
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ad2564b
feat: add authorization helper classes for header parsing and JOOQ fi…
vairav a0702b1
feat: integrate authorization filtering in TimeSeriesController and T…
vairav c8fb177
Merge remote-tracking branch 'origin' into feature/auth-context-and-f…
vairav b544347
Return DSL.noCondition() instead of null from filter methods
vairav File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
159 changes: 159 additions & 0 deletions
159
cwms-data-api/src/main/java/cwms/cda/helpers/AuthorizationContextHelper.java
This file contains hidden or 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,159 @@ | ||
| package cwms.cda.helpers; | ||
|
|
||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import io.javalin.http.Context; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.logging.Level; | ||
| import java.util.logging.Logger; | ||
|
|
||
| public class AuthorizationContextHelper { | ||
| private static final Logger LOGGER = Logger.getLogger(AuthorizationContextHelper.class.getName()); | ||
| private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); | ||
| private static final String AUTH_CONTEXT_HEADER = "x-cwms-auth-context"; | ||
|
|
||
| private final Map<String, Object> authContext; | ||
| private final Map<String, Object> userContext; | ||
| private final Map<String, Object> constraints; | ||
|
|
||
| public AuthorizationContextHelper(Context ctx) { | ||
| this.authContext = parseAuthContextHeader(ctx); | ||
| this.userContext = extractUserContext(authContext); | ||
| this.constraints = extractConstraints(authContext); | ||
| } | ||
|
|
||
| private Map<String, Object> parseAuthContextHeader(Context ctx) { | ||
| String headerValue = ctx.header(AUTH_CONTEXT_HEADER); | ||
| if (headerValue == null || headerValue.isEmpty()) { | ||
| LOGGER.log(Level.FINE, "No authorization context header found"); | ||
| return Collections.emptyMap(); | ||
| } | ||
|
|
||
| try { | ||
| return OBJECT_MAPPER.readValue(headerValue, Map.class); | ||
| } catch (Exception e) { | ||
| LOGGER.log(Level.WARNING, "Failed to parse authorization context header", e); | ||
| return Collections.emptyMap(); | ||
| } | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| private Map<String, Object> extractUserContext(Map<String, Object> authContext) { | ||
| if (authContext.containsKey("user")) { | ||
| return (Map<String, Object>) authContext.get("user"); | ||
| } | ||
| return Collections.emptyMap(); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| private Map<String, Object> extractConstraints(Map<String, Object> authContext) { | ||
| if (authContext.containsKey("constraints")) { | ||
| return (Map<String, Object>) authContext.get("constraints"); | ||
| } | ||
| return Collections.emptyMap(); | ||
| } | ||
|
|
||
| public String getUserId() { | ||
| return (String) userContext.getOrDefault("id", null); | ||
| } | ||
|
|
||
| public String getUsername() { | ||
| return (String) userContext.getOrDefault("username", null); | ||
| } | ||
|
|
||
| public String getEmail() { | ||
| return (String) userContext.getOrDefault("email", null); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| public List<String> getRoles() { | ||
| Object roles = userContext.get("roles"); | ||
| if (roles instanceof List) { | ||
| return (List<String>) roles; | ||
| } | ||
| return Collections.emptyList(); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| public List<String> getOffices() { | ||
| Object offices = userContext.get("offices"); | ||
| if (offices instanceof List) { | ||
| return (List<String>) offices; | ||
| } | ||
| return Collections.emptyList(); | ||
| } | ||
|
|
||
| public String getPrimaryOffice() { | ||
| return (String) userContext.getOrDefault("primary_office", null); | ||
| } | ||
|
|
||
| public String getPersona() { | ||
| return (String) userContext.getOrDefault("persona", null); | ||
| } | ||
|
|
||
| public String getRegion() { | ||
| return (String) userContext.getOrDefault("region", null); | ||
| } | ||
|
|
||
| public String getAllowedOfficesConstraint() { | ||
| return (String) constraints.getOrDefault("allowed_offices", null); | ||
| } | ||
|
|
||
| public boolean isEmbargoExempt() { | ||
| Object exempt = constraints.get("embargo_exempt"); | ||
| return exempt != null && (boolean) exempt; | ||
| } | ||
|
|
||
| public String getTimezone() { | ||
| return (String) constraints.getOrDefault("timezone", null); | ||
| } | ||
|
|
||
| public boolean hasRole(String role) { | ||
| return getRoles().contains(role); | ||
| } | ||
|
|
||
| public boolean hasOfficeAccess(String office) { | ||
| if (office == null) { | ||
| return false; | ||
| } | ||
| List<String> userOffices = getOffices(); | ||
| return userOffices.contains(office); | ||
| } | ||
|
|
||
| public String buildOfficeFilter() { | ||
| String allowedOffices = getAllowedOfficesConstraint(); | ||
| if (allowedOffices != null && !allowedOffices.isEmpty()) { | ||
| if ("*".equals(allowedOffices)) { | ||
| return null; | ||
| } | ||
| return allowedOffices; | ||
| } | ||
|
|
||
| List<String> offices = getOffices(); | ||
| if (offices.isEmpty()) { | ||
| return null; | ||
| } | ||
|
|
||
| return String.join(",", offices); | ||
| } | ||
|
|
||
| public boolean isAuthorizationHeaderPresent() { | ||
| return !authContext.isEmpty(); | ||
| } | ||
|
|
||
| public Map<String, Object> getFullContext() { | ||
| return Collections.unmodifiableMap(authContext); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "AuthorizationContextHelper{" + | ||
| "userId='" + getUserId() + '\'' + | ||
| ", username='" + getUsername() + '\'' + | ||
| ", offices=" + getOffices() + | ||
| ", roles=" + getRoles() + | ||
| ", persona='" + getPersona() + '\'' + | ||
| '}'; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as another comments, favor returning noCondition over returning null.