-
Notifications
You must be signed in to change notification settings - Fork 8
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
Refactor authentication for requests to CommCare #1575
base: master
Are you sure you want to change the base?
Conversation
This also adds support for POST requests
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1575 +/- ##
============================================
+ Coverage 70.49% 70.90% +0.40%
- Complexity 2025 2048 +23
============================================
Files 252 256 +4
Lines 7833 7856 +23
Branches 722 722
============================================
+ Hits 5522 5570 +48
+ Misses 2039 2012 -27
- Partials 272 274 +2 ☔ View full report in Codecov by Sentry. |
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.
Looks great!
src/main/java/org/commcare/formplayer/web/client/HmacRequestInterceptor.java
Outdated
Show resolved
Hide resolved
boolean asParamMissing = rawQuery == null || Arrays.stream(rawQuery.split("&")) | ||
.noneMatch(param -> param.startsWith("as=")); | ||
Optional<HqUserDetailsBean> userDetails = RequestUtils.getUserDetails(); | ||
if (asParamMissing && userDetails.isPresent()) { |
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.
maybe wrap all the request param logic in a parent if(userDetails.isPresent()
block as we simply want to return the request as it is otherwise
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.
I left this as is because it's a dual condition: missing param AND user details present. We also expect that user details will always be present, the check is just a safeguard.
@snopoke I haven't yet confirmed, but I suspect this is triggering an odd bug on staging (jira ticket). If this adds authentication credentials to all requests from formplayer to HQ, then it might affect this behavior: https://github.com/dimagi/commcare-hq/blob/master/corehq/apps/locations/middleware.py#L29-L32 At present, that exempts requests without auth from location restrictions, assuming that this is outside the scope of its concern (non-domain requests, pre-login, etc). I've long been uncomfortable with this code (which I wrote), as it's easy for stuff to fall through the cracks if you default to permissiveness, which is I think essentially what's happened here. It looks like the Aside: It'd be nice to explicitly mark views that don't require auth, both the location middleware can automatically exempt them, and to make it clear when no auth is needed, versus the current world, where it's implied only by the absence of auth decorators, which can be hard to see. |
@esoergel thanks for identifying this. I've tracked down two other places that could be an issue as well.
I'll have to look into what can be done make the auth opt-in instead of opt-out |
Technical Summary
This consolidates and standardises the auth and common headers which get added to requests that are made to CommCare.
Previously this code was in
RestoreFactory
but it does not belong there.Prior setup
UserRestoreAspect
grabs the user data from the request and the session cookie value and passes them toRestoreFactory
restoreFactory.getHeaders
which adds in the auth headers using the session cookie value (+ other headers).Change in new setup
RestoreFactory
RestTemplate
Other changes
Safety Assurance
Safety story
There are a decent set of tests for this and I have tested locally as well. I also plan to put this on staging for some time along with dimagi/commcare-hq#34441
Automated test coverage
Added / updated in the PR
QA Plan
QA on staging (regression tests): https://dimagi.atlassian.net/browse/QA-6505
Special deploy instructions
Rollback instructions
Review