|
6 | 6 | using System.Text.Json; |
7 | 7 | using System.Text.Json.Nodes; |
8 | 8 | using System.Text.Json.Serialization; |
| 9 | +using Azure.DataApiBuilder.Auth; |
| 10 | +using Azure.DataApiBuilder.Config.ObjectModel; |
9 | 11 | using Azure.DataApiBuilder.Core.Configurations; |
10 | 12 | using Azure.DataApiBuilder.Core.Models; |
11 | 13 | using Azure.DataApiBuilder.Core.Parsers; |
@@ -155,15 +157,7 @@ private static JsonObject CreatePaginationConnection(JsonElement root, Paginatio |
155 | 157 | { |
156 | 158 | JsonElement lastElemInRoot = rootEnumerated.ElementAtOrDefault(returnedElementCount - 1); |
157 | 159 | connection.Add(QueryBuilder.PAGINATION_TOKEN_FIELD_NAME, |
158 | | - MakeCursorFromJsonElement( |
159 | | - lastElemInRoot, |
160 | | - paginationMetadata.Structure!.PrimaryKey(), |
161 | | - paginationMetadata.Structure!.OrderByColumns, |
162 | | - paginationMetadata.Structure!.EntityName, |
163 | | - paginationMetadata.Structure!.DatabaseObject.SchemaName, |
164 | | - paginationMetadata.Structure!.DatabaseObject.Name, |
165 | | - paginationMetadata.Structure!.MetadataProvider, |
166 | | - paginationMetadata.RequestedGroupBy)); |
| 160 | + paginationMetadata.Structure!.CreatePaginationCursor(lastElemInRoot, paginationMetadata.RequestedGroupBy)); |
167 | 161 | } |
168 | 162 | } |
169 | 163 |
|
@@ -203,19 +197,36 @@ public NextLinkField( |
203 | 197 |
|
204 | 198 | /// <summary> |
205 | 199 | /// Extracts the columns from the JsonElement needed for pagination, represents them as a string in json format and base64 encodes. |
206 | | - /// The JSON is encoded in base64 for opaqueness. The cursor should function as a token that the user copies and pastes |
207 | | - /// without needing to understand how it works. |
| 200 | + /// Base64 encoding does not provide confidentiality, so every cursor field must be readable by the caller. |
208 | 201 | /// </summary> |
209 | 202 | public static string MakeCursorFromJsonElement( |
210 | 203 | JsonElement element, |
211 | 204 | List<string> primaryKey, |
212 | 205 | List<OrderByColumn>? orderByColumns, |
| 206 | + IAuthorizationResolver authorizationResolver, |
| 207 | + string roleName, |
213 | 208 | string entityName = "", |
214 | 209 | string schemaName = "", |
215 | 210 | string tableName = "", |
216 | 211 | ISqlMetadataProvider? sqlMetadataProvider = null, |
217 | 212 | bool isGroupByQuery = false) |
218 | 213 | { |
| 214 | + IEnumerable<string> cursorColumns = orderByColumns?.Select(column => column.ColumnName) ?? Enumerable.Empty<string>(); |
| 215 | + if (!isGroupByQuery) |
| 216 | + { |
| 217 | + cursorColumns = cursorColumns.Concat(primaryKey); |
| 218 | + } |
| 219 | + |
| 220 | + IEnumerable<string> exposedCursorColumns = cursorColumns.Distinct() |
| 221 | + .Select(column => GetExposedColumnName(entityName, column, sqlMetadataProvider)); |
| 222 | + if (!authorizationResolver.AreColumnsAllowedForOperation(entityName, roleName, EntityActionOperation.Read, exposedCursorColumns)) |
| 223 | + { |
| 224 | + throw new DataApiBuilderException( |
| 225 | + message: DataApiBuilderException.PAGINATION_FIELD_AUTHZ_FAILURE, |
| 226 | + statusCode: HttpStatusCode.Forbidden, |
| 227 | + subStatusCode: DataApiBuilderException.SubStatusCodes.AuthorizationCheckFailed); |
| 228 | + } |
| 229 | + |
219 | 230 | List<NextLinkField> cursorJson = new(); |
220 | 231 | JsonSerializerOptions options = new() { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }; |
221 | 232 | // Hash set is used here to maintain linear runtime |
|
0 commit comments