-
Notifications
You must be signed in to change notification settings - Fork 50
Record and enforce consent through the Chat API #4306
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
Open
barry47products
wants to merge
17
commits into
main
Choose a base branch
from
bt/consent-backend-3682
base: main
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.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
4060392
Record consent with a timestamp on participant data
barry47products 2fb95c7
Key consent enforcement on the widget release that collects it
barry47products ecade0f
Describe a session's consent state for the Chat API
barry47products cd31928
Report the consent state when a chat session starts
barry47products 9c166cc
Report the consent state on every poll
barry47products 6dd1948
Regenerate the schema without the local OIDC scopes
barry47products c1fdb4f
Record consent through the Chat API
barry47products cacce4f
Refuse consent on a completed session like the other chat endpoints
barry47products a97f4cd
Hold messages and uploads until consent is recorded
barry47products 8443ea1
Skip the participant data query when a version has no consent form
barry47products 615306a
Cover the frozen consent form and the consent endpoint's access control
barry47products a503dd6
Key consent on the accepted form version
barry47products b66ff35
Gate consent enforcement on widget 0.13.0
barry47products 5171cd0
Merge branch 'main' into bt/consent-backend-3682
barry47products 86bf68a
Keep the first acceptance time on a repeated consent POST
barry47products 6f41d28
Merge branch 'main' into bt/consent-backend-3682
barry47products 33b05ad
Merge remote-tracking branch 'origin/main' into bt/consent-backend-3682
barry47products 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| """Consent as the Chat API reports and enforces it (public channel design, D7). | ||
|
|
||
| The store is ``ParticipantData.system_metadata["consent"]``, shared with CommCare Connect and read | ||
| by ``ConsentCheckStage``, plus the accepted ``consent_form_version_id``. The text is the frozen | ||
| ``ConsentForm`` on the version the session runs against; a republished form has a new id, so the | ||
| participant is prompted again until they accept it. | ||
| """ | ||
|
|
||
| from rest_framework import status | ||
| from rest_framework.response import Response | ||
|
|
||
| from apps.channels.widget_versions import WIDGET_VERSION_HEADER, widget_enforces_consent | ||
| from apps.experiments.models import Experiment, ExperimentSession, ParticipantData | ||
|
|
||
|
|
||
| def participant_data_for(session: ExperimentSession) -> ParticipantData | None: | ||
| return ParticipantData.objects.for_experiment(session.experiment).filter(participant=session.participant).first() | ||
|
|
||
|
|
||
| def consent_block(version: Experiment, participant_data: ParticipantData | None) -> dict: | ||
| form = version.consent_form | ||
| if form is None: | ||
| return {"required": False, "form_version_id": None, "text": None} | ||
| consented = participant_data is not None and participant_data.has_consented_to(form.id) | ||
| return { | ||
| "required": not consented, | ||
| "form_version_id": form.id, | ||
| "text": None if consented else form.get_rendered_content(), | ||
| } | ||
|
|
||
|
|
||
| def session_consent_block(session: ExperimentSession, version: Experiment) -> dict: | ||
| """The consent block for `session` on `version`, without a participant-data query when there is no form.""" | ||
| if version.consent_form_id is None: | ||
| return consent_block(version, None) | ||
| return consent_block(version, participant_data_for(session)) | ||
|
|
||
|
|
||
| def consent_refusal(request, session: ExperimentSession, version: Experiment) -> Response | None: | ||
| """The 403 that holds a message until consent is recorded, or None. | ||
|
|
||
| Only widgets from ``CONSENT_INTRODUCED`` on are refused: older widgets treat every 403 as a | ||
| dead session, and non-widget API callers have no consent surface. | ||
| """ | ||
| if not widget_enforces_consent(request.headers.get(WIDGET_VERSION_HEADER)): | ||
| return None | ||
| block = session_consent_block(session, version) | ||
| if not block["required"]: | ||
| return None | ||
| return Response( | ||
| {"error": "Consent is required before chatting", "code": "consent_required", "consent": block}, | ||
| status=status.HTTP_403_FORBIDDEN, | ||
| ) |
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
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.
Uh oh!
There was an error while loading. Please reload this page.