def require_touchstone_login(strategy, backend, flow=None, **kwargs): # pylint: disable=unused-argument """ If the user is attempting to log in via email and has authenticated via Touchstone/SAML, require them to log in that way. Args: strategy (social_django.strategy.DjangoStrategy): the strategy used to authenticate backend (social_core.backends.base.BaseAuth): the backend being used to authenticate flow (str): the type of flow (login or register) """ if (backend.name != EmailAuth.name or flow != SocialAuthState.FLOW_LOGIN or not features.is_enabled(features.SAML_AUTH)): return {} data = strategy.request_data() email = data.get("email") if not email: return {} user_storage = strategy.storage.user saml_auth = user_storage.get_social_auth( SAMLAuth.name, "{}:{}".format(settings.SOCIAL_AUTH_DEFAULT_IDP_KEY, email)) if saml_auth: raise RequireProviderException(backend, saml_auth) return {}
def post(self, request, *args, **kwargs): """Execute a related posts search""" if not features.is_enabled(features.RELATED_POSTS_UI): return Response(status=HTTP_405_METHOD_NOT_ALLOWED) response = find_related_documents(user=request.user, post_id=self.kwargs["post_id"]) return Response(response)
def test_is_enabled(settings, value_in_settings, default, default_in_settings, expected): """Tests that is_enabled returns expected values""" key = "feature_key_we_will_never_use" settings.OPEN_DISCUSSIONS_FEATURES_DEFAULT = default_in_settings if value_in_settings is not None: settings.FEATURES[key] = value_in_settings assert features.is_enabled(key, default=default) is expected
def saml_metadata(request): """ Display SAML configuration metadata as XML """ if not features.is_enabled(features.SAML_AUTH): raise Http404("Page not found") complete_url = reverse("social:complete", args=("saml", )) saml_backend = load_backend(load_strategy(request), "saml", redirect_uri=complete_url) metadata, _ = saml_backend.generate_metadata_xml() return HttpResponse(content=metadata, content_type="text/xml")
def update_moira_lists(strategy, backend, user=None, **kwargs): # pylint: disable=unused-argument """ Update a user's moira lists Args: strategy (social_django.strategy.DjangoStrategy): the strategy used to authenticate backend (social_core.backends.base.BaseAuth): the backend being used to authenticate user (User): the current user """ if features.is_enabled(features.MOIRA) and user and user.is_active: update_user_moira_lists.delay(user.id, update_memberships=True) return {}
def handle_create_course_run_certificate( sender, instance, created, **kwargs ): # pylint: disable=unused-argument """ Update moira lists when a ChannelMembershipConfig model is saved. """ if "moira_lists" in instance.query and features.is_enabled(features.MOIRA): update_moira_list_users.delay( instance.query.get("moira_lists", []), channel_ids=[ channel_id for channel_id in instance.channels.values_list("id", flat=True) ], )
def can_notify(self, last_notification): """ Returns true if we can notify this user based on their settings and when the last notification occurred Args: last_notification (NotificationBase): last notification that was triggered for this NotificationSettings Raises: InvalidTriggerFrequencyError: if the frequency is invalid Returns: bool: True if we're due to send another notification """ return features.is_enabled(features.COMMENT_NOTIFICATIONS) and super( ).can_notify(last_notification)
def ckeditor_view(request, **kwargs): """get the JWT to authenticate for CKEditor""" if (settings.CKEDITOR_SECRET_KEY and settings.CKEDITOR_ENVIRONMENT_ID and is_enabled(ARTICLE_UI)): payload = { "iss": settings.CKEDITOR_ENVIRONMENT_ID, "iat": math.floor(time()) } token = jwt.encode(payload, settings.CKEDITOR_SECRET_KEY, algorithm="HS256") return HttpResponse(token) else: return HttpResponse(status=status.HTTP_503_SERVICE_UNAVAILABLE)
def can_notify(self, last_notification): """ Returns true if we can notify this user based on their settings and when the last notification occurred Args: last_notification (NotificationBase): last notification that was triggered for this NotificationSettings Raises: InvalidTriggerFrequencyError: if the frequency is invalid Returns: bool: True if we're due to send another notification """ if features.is_enabled(features.FRONTPAGE_EMAIL_DIGESTS) and super().can_notify( last_notification ): # do this last as it's expensive if the others are False anyway # check if we have posts since the last notification return bool( _posts_since_notification(self.notification_settings, last_notification) ) return False
def post(self, request): """Process webhook request""" if not compare_digest(request.GET.get("webhook_key", ""), settings.OCW_WEBHOOK_KEY): raise WebhookException("Incorrect webhook key") content = rapidjson.loads(request.body.decode()) records = content.get("Records") if features.is_enabled(features.WEBHOOK_OCW) and records is not None: blacklist = load_course_blacklist() for record in content.get("Records"): s3_key = record.get("s3", {}).get("object", {}).get("key") prefix = s3_key.split("0/1.json")[0] get_ocw_courses.apply_async( countdown=settings.OCW_WEBHOOK_DELAY, kwargs={ "course_prefixes": [prefix], "blacklist": blacklist, "force_overwrite": False, "upload_to_s3": True, }, ) else: log.error("No records found in webhook: %s", request.body.decode()) return Response({})
def index(request, **kwargs): # pylint: disable=unused-argument """Render the react app""" if request.META.get("HTTP_USER_AGENT", "").startswith("facebookexternalhit"): return render( request, "social.html", context={ "url": request.build_absolute_uri(), "description_value": "MIT Open Learning's discussion platform", }, ) username = None user_full_name = None user_email = None user_is_superuser = False user_id = None if request.user.is_authenticated: user = request.user username = user.username user_full_name = user.profile.name user_email = user.email user_is_superuser = user.is_superuser user_id = user.id site = get_default_site() article_ui_enabled = (features.is_enabled(features.ARTICLE_UI) if settings.CKEDITOR_ENVIRONMENT_ID and settings.CKEDITOR_SECRET_KEY and settings.CKEDITOR_UPLOAD_URL else False) livestream_ui_enabled = (features.is_enabled(features.LIVESTREAM_UI) if settings.LIVESTREAM_ACCOUNT_ID and settings.LIVESTREAM_SECRET_KEY else False) js_settings = { "gaTrackingID": settings.GA_TRACKING_ID, "environment": settings.ENVIRONMENT, "sentry_dsn": settings.SENTRY_DSN, "release_version": settings.VERSION, "public_path": public_path(request), "site_url": settings.SITE_BASE_URL, "max_comment_depth": settings.OPEN_DISCUSSIONS_MAX_COMMENT_DEPTH, "username": username, "user_full_name": user_full_name, "user_email": user_email, "user_id": user_id, "is_admin": user_is_superuser, "authenticated_site": { "title": site.title, "base_url": site.base_url, "login_url": site.login_url, "session_url": site.session_url, "tos_url": site.tos_url, }, "support_email": settings.EMAIL_SUPPORT, "is_authenticated": bool(request.user.is_authenticated), "profile_ui_enabled": features.is_enabled(features.PROFILE_UI), "allow_saml_auth": features.is_enabled(features.SAML_AUTH), "allow_related_posts_ui": features.is_enabled(features.RELATED_POSTS_UI), "embedlyKey": settings.EMBEDLY_KEY, "recaptchaKey": settings.RECAPTCHA_SITE_KEY, "search_page_size": settings.ELASTICSEARCH_DEFAULT_PAGE_SIZE, "search_min_length": settings.ELASTICSEARCH_MIN_QUERY_SIZE, "accepted_social_sites": list(SOCIAL_SITE_NAME_MAP.values()), "article_ui_enabled": article_ui_enabled, "ckeditor_upload_url": settings.CKEDITOR_UPLOAD_URL, "algolia_appId": settings.ALGOLIA_APP_ID, "algolia_apiKey": settings.ALGOLIA_API_KEY, "course_ui_enabled": features.is_enabled(features.COURSE_UI), "file_search_enabled": features.is_enabled(features.COURSE_FILE_SEARCH), "livestream_ui_enabled": livestream_ui_enabled, "podcast_frontpage_enabled": features.is_enabled(features.PODCAST_FRONTPAGE), } return render(request, "index.html", context={"js_settings_json": json.dumps(js_settings)})
def has_permission(self, request, view): """Check that the feature flag is enabled""" return features.is_enabled(features.PODCAST_APIS)