|
| 1 | +"""Validation utilities for OpenedX AuthZ API. |
| 2 | +
|
| 3 | +This module provides validation functions for scope strings, particularly |
| 4 | +for glob patterns used in role assignments. |
| 5 | +""" |
| 6 | + |
| 7 | +from openedx_authz.api.data import ( |
| 8 | + EXTERNAL_KEY_SEPARATOR, |
| 9 | + GLOBAL_SCOPE_WILDCARD, |
| 10 | + ContentLibraryData, |
| 11 | + CourseOverviewData, |
| 12 | + ScopeData, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +def validate_scope_with_glob(scope: ScopeData) -> None: |
| 17 | + """Validate that a scope with glob patterns follows rules. |
| 18 | +
|
| 19 | + This function ensures that glob patterns (*) in scope strings are only |
| 20 | + allowed at the organization level and that the referenced organization |
| 21 | + exists to prevent overly broad or invalid permissions. |
| 22 | +
|
| 23 | + Rules: |
| 24 | + - For course scopes: Must have exactly the format "course-v1:ORG*" where ORG exists |
| 25 | + - For library scopes: Must have exactly the format "lib:ORG*" where ORG exists |
| 26 | + - Wildcards must only appear at the end of the string |
| 27 | + - Wildcards are only allowed at organization level (not at course, run, or slug level) |
| 28 | + - Cannot have wildcards before the org identifier |
| 29 | +
|
| 30 | + Args: |
| 31 | + scope (ScopeData): ScopeData instance to validate (e.g. ScopeData(external_key="course-v1:OpenedX*")) |
| 32 | +
|
| 33 | + Examples: |
| 34 | + Valid scopes: |
| 35 | + - CourseOverviewData(external_key="course-v1:OpenedX*") # org-level wildcard |
| 36 | + - ContentLibraryData(external_key="lib:DemoX*") # org-level wildcard |
| 37 | +
|
| 38 | + Invalid scopes: |
| 39 | + - course-v1* - wildcard before org |
| 40 | + - course-v1:* - wildcard without org prefix |
| 41 | + - course-v1:OpenedX+Course* - wildcard at course level (NOT allowed) |
| 42 | + - lib:DemoX:Slug* - wildcard at slug level (NOT allowed) |
| 43 | + """ |
| 44 | + external_key = scope.external_key |
| 45 | + |
| 46 | + if GLOBAL_SCOPE_WILDCARD not in external_key: |
| 47 | + return None |
| 48 | + |
| 49 | + # Get the scope string without the trailing wildcard |
| 50 | + scope_prefix = external_key[: -len(GLOBAL_SCOPE_WILDCARD)] |
| 51 | + |
| 52 | + if isinstance(scope, CourseOverviewData): |
| 53 | + return _validate_course_scope_glob(scope_prefix) |
| 54 | + if isinstance(scope, ContentLibraryData): |
| 55 | + return _validate_library_scope_glob(scope_prefix) |
| 56 | + |
| 57 | + raise ValueError(f"Invalid scope: {scope}") |
| 58 | + |
| 59 | + |
| 60 | +def _validate_org_identifier(scope_prefix: str) -> str: |
| 61 | + """Extract and structurally validate the organization identifier in a scope. |
| 62 | +
|
| 63 | + This helper only validates the structure (namespace and org position). It does |
| 64 | + not check whether the organization actually exists. That is the responsibility |
| 65 | + of the scope-type specific validators. |
| 66 | +
|
| 67 | + Args: |
| 68 | + scope_prefix (str): The scope without the trailing wildcard |
| 69 | +
|
| 70 | + Returns: |
| 71 | + str: The extracted organization identifier |
| 72 | +
|
| 73 | + Examples: |
| 74 | + >>> _validate_org_identifier("course-v1:OpenedX*") |
| 75 | + "OpenedX" |
| 76 | + >>> _validate_org_identifier("lib:DemoX*") |
| 77 | + "DemoX" |
| 78 | + """ |
| 79 | + parts = scope_prefix.split(EXTERNAL_KEY_SEPARATOR) |
| 80 | + |
| 81 | + if len(parts) != 2 or parts[1] == "": |
| 82 | + raise ValueError("Scope glob must include exactly one organization identifier.") |
| 83 | + |
| 84 | + return parts[1] |
| 85 | + |
| 86 | + |
| 87 | +def _course_org_exists(org: str) -> bool: |
| 88 | + """Check if there is at least one course with the given org. |
| 89 | +
|
| 90 | + Args: |
| 91 | + org (str): Organization identifier extracted from the course scope |
| 92 | +
|
| 93 | + Returns: |
| 94 | + bool: True if there is at least one CourseOverview whose org field matches |
| 95 | + the provided identifier in a case-sensitive way, False otherwise. |
| 96 | + """ |
| 97 | + from openedx_authz.models.scopes import CourseOverview # pylint: disable=import-outside-toplevel |
| 98 | + |
| 99 | + course_obj = CourseOverview.objects.filter(org=org).only("org").last() |
| 100 | + return course_obj is not None and course_obj.org == org |
| 101 | + |
| 102 | + |
| 103 | +def _library_org_exists(org: str) -> bool: |
| 104 | + """Check if there is at least one content library with the given org. |
| 105 | +
|
| 106 | + Args: |
| 107 | + org (str): Organization identifier extracted from the library scope |
| 108 | +
|
| 109 | + Returns: |
| 110 | + bool: True if there is at least one ContentLibrary whose related |
| 111 | + organization's short_name matches the provided identifier in a |
| 112 | + case-sensitive way, False otherwise. |
| 113 | + """ |
| 114 | + from openedx_authz.models.scopes import ContentLibrary # pylint: disable=import-outside-toplevel |
| 115 | + |
| 116 | + lib_obj = ContentLibrary.objects.filter(org__short_name=org).only("org").last() |
| 117 | + return lib_obj is not None and lib_obj.org.short_name == org |
| 118 | + |
| 119 | + |
| 120 | +def _validate_course_scope_glob(scope_prefix: str) -> None: |
| 121 | + """Validate a course scope with glob pattern. |
| 122 | +
|
| 123 | + Course keys have format: course-v1:ORG+COURSE+RUN |
| 124 | + We only allow wildcards at the organization level (course-v1:ORG*). |
| 125 | + Wildcards at course or run level are not allowed. |
| 126 | +
|
| 127 | + Args: |
| 128 | + scope_prefix (str): The course scope without the trailing wildcard |
| 129 | + """ |
| 130 | + org = _validate_org_identifier(scope_prefix) |
| 131 | + |
| 132 | + if not _course_org_exists(org): |
| 133 | + raise ValueError(f"Organization '{org}' does not exist for any course.") |
| 134 | + |
| 135 | + |
| 136 | +def _validate_library_scope_glob(scope_prefix: str) -> None: |
| 137 | + """Validate a library scope with glob pattern. |
| 138 | +
|
| 139 | + Library keys have format: lib:ORG:SLUG |
| 140 | + We only allow wildcards at the organization level (lib:ORG*). |
| 141 | + Wildcards at slug level are not allowed. |
| 142 | +
|
| 143 | + Args: |
| 144 | + scope_prefix (str): The library scope without the trailing wildcard |
| 145 | + """ |
| 146 | + org = _validate_org_identifier(scope_prefix) |
| 147 | + |
| 148 | + if not _library_org_exists(org): |
| 149 | + raise ValueError(f"Organization '{org}' does not exist for any library.") |
0 commit comments