|
| 1 | +0007: Enforcement mechanisms - communication with MFEs and other clients |
| 2 | +######################################################################### |
| 3 | + |
| 4 | +Status |
| 5 | +******** |
| 6 | + |
| 7 | +**Draft** |
| 8 | + |
| 9 | +Context |
| 10 | +********* |
| 11 | + |
| 12 | +Authorization (AuthZ) decisions need to be enforced not only on the |
| 13 | +backend services, but also on the frontend applications, including |
| 14 | +Micro-Frontends (MFEs) and mobile apps. This ensures that users only see |
| 15 | +and can interact with the UI elements they are permitted to access based |
| 16 | +on their roles and permissions. |
| 17 | + |
| 18 | +To achieve this, we need to establish effective communication mechanisms |
| 19 | +between the backend services and the frontend clients. This involves |
| 20 | +determining how AuthZ data is transmitted, how often it is updated, and |
| 21 | +how the frontend applications can use this data to enforce access |
| 22 | +control. |
| 23 | + |
| 24 | +We want an approach that: |
| 25 | + |
| 26 | +- Minimizes the amount of requests and data transferred between the |
| 27 | + backend and frontend. |
| 28 | +- Is granular enough to match the functionality available on the |
| 29 | + backend. |
| 30 | +- Is easy to implement and maintain across different frontend |
| 31 | + applications. |
| 32 | + |
| 33 | +Decision |
| 34 | +********** |
| 35 | + |
| 36 | +I. REST API for authorization queries |
| 37 | +===================================== |
| 38 | + |
| 39 | +We will implement a dedicated REST API endpoint that frontend |
| 40 | +applications can use to query for specific permissions. Queries would be |
| 41 | +at the Subject-Action-Object-Context level, being the Subject always the |
| 42 | +current authenticated user. This allows the frontend to check if a user |
| 43 | +has permission to perform a specific action on a specific object within |
| 44 | +a given context. |
| 45 | + |
| 46 | +To optimize performance and reduce latency, the API will support batch |
| 47 | +queries, allowing multiple permission checks in a single request. It |
| 48 | +will also have caching mechanisms in place. |
| 49 | + |
| 50 | +API Definition |
| 51 | +-------------- |
| 52 | + |
| 53 | +POST /api/authz/v1/permissions/validate |
| 54 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 55 | + |
| 56 | +Validate if the current user has specific permissions. |
| 57 | + |
| 58 | +Request Body: |
| 59 | +""""""""""""" |
| 60 | + |
| 61 | +Format: |
| 62 | + |
| 63 | +.. code:: ts |
| 64 | +
|
| 65 | + Array<{ |
| 66 | + id: number |
| 67 | + action: string |
| 68 | + object?: string |
| 69 | + scope?: string |
| 70 | + }> |
| 71 | +
|
| 72 | +Example: |
| 73 | + |
| 74 | +.. code:: json |
| 75 | +
|
| 76 | + [ |
| 77 | + { |
| 78 | + "id": 1, |
| 79 | + "action": "act:read", |
| 80 | + "object": "lib:test-lib", |
| 81 | + "scope": "org:OpenedX" |
| 82 | + }, |
| 83 | + { |
| 84 | + "id": 2, |
| 85 | + "action": "act:edit", |
| 86 | + "object": "lib:test-lib", |
| 87 | + "scope": "org:OpenedX" |
| 88 | + } |
| 89 | + ] |
| 90 | +
|
| 91 | +**Please Note:** |
| 92 | + |
| 93 | +- id would be arbitrarily defined by the frontend/client as a way to |
| 94 | + keep track of each requested permission and match with the response. |
| 95 | +- The user (subject) would be inferred from the authenticated user |
| 96 | + making the request. |
| 97 | + |
| 98 | +Response Body: |
| 99 | +"""""""""""""" |
| 100 | + |
| 101 | +Format: |
| 102 | + |
| 103 | +.. code:: ts |
| 104 | +
|
| 105 | + Array<{ |
| 106 | + id: number |
| 107 | + allowed: boolean |
| 108 | + }> |
| 109 | +
|
| 110 | +Example: |
| 111 | + |
| 112 | +.. code:: json |
| 113 | +
|
| 114 | + [ |
| 115 | + { |
| 116 | + "id": 1, |
| 117 | + "allowed": true |
| 118 | + }, |
| 119 | + { |
| 120 | + "id": 2, |
| 121 | + "allowed": false |
| 122 | + } |
| 123 | + ] |
| 124 | +
|
| 125 | +Possible response codes: |
| 126 | +"""""""""""""""""""""""" |
| 127 | + |
| 128 | +- 200: Ok, includes the Response Body defined above. |
| 129 | +- 400: Bad Request, happens when the request body doesn't match the |
| 130 | + required format. |
| 131 | +- 401: Unauthorized, happens when the user is not authorized/logged in. |
| 132 | + |
| 133 | +**Please note:** There is no “404 not found” case here, if the action, |
| 134 | +object or scope doesn't exist, the “allowed” value in the response will |
| 135 | +be whatever Casbin evaluates in this case. |
| 136 | + |
| 137 | +II. Frontend integration |
| 138 | +======================== |
| 139 | + |
| 140 | +Frontend applications will integrate with the REST API to enforce |
| 141 | +authorization decisions. This will involve: |
| 142 | + |
| 143 | +#. Querying the API for permissions when rendering UI components. |
| 144 | +#. Using the API response to conditionally render or style UI elements |
| 145 | + based on the user's permissions. |
| 146 | +#. Implementing a caching strategy on the frontend to minimize API calls |
| 147 | + and improve performance. |
| 148 | + |
| 149 | +The specifics on when and how to query the API will depend on the |
| 150 | +application's architecture and user interaction patterns. |
| 151 | + |
| 152 | +Consequences |
| 153 | +************** |
| 154 | + |
| 155 | +- The REST API approach provides a flexible and scalable way to enforce |
| 156 | + AuthZ decisions across different frontend applications. |
| 157 | + |
| 158 | +- It allows for real-time updates to permissions, as the frontend can |
| 159 | + query the API as needed. |
| 160 | + |
| 161 | +- The batch query and caching mechanisms help mitigate performance |
| 162 | + concerns, ensuring that the user experience remains smooth. |
| 163 | + |
| 164 | +- Frontend developers will need to implement the necessary logic to |
| 165 | + interact with the REST API and enforce AuthZ decisions. |
| 166 | + |
| 167 | +- The approach is adaptable to various frontend architectures, |
| 168 | + including MFEs and mobile apps, making it a versatile solution for |
| 169 | + the Open edX platform. |
| 170 | + |
| 171 | +Rejected Alternatives |
| 172 | +*********************** |
| 173 | + |
| 174 | +- Embedding AuthZ data in JWT tokens: As discussed in `0003-jwt-usage`, |
| 175 | + embedding AuthZ data in JWT tokens can lead to large token sizes and |
| 176 | + stale permissions, in addition to having to re-implement Casbin model |
| 177 | + logic in the frontend. |
| 178 | + |
| 179 | +- Depending solely on backend enforcement on resource endpoints: |
| 180 | + Relying solely on backend enforcement can lead to a poor user |
| 181 | + experience, as users may see UI elements they cannot interact with, |
| 182 | + leading to confusion and frustration. |
| 183 | + |
| 184 | +References |
| 185 | +************ |
| 186 | + |
| 187 | +- `Open edX REST API Conventions |
| 188 | + <https://openedx.atlassian.net/wiki/spaces/AC/pages/18350757/Open+edX+REST+API+Conventions>`_ |
0 commit comments