internal CosmosClientStreamWrapper( IResourcePermissionResponse resourcePermissionResponse, PermissionModeKind permissionMode) { var currentPermission = resourcePermissionResponse?.ResourcePermissions? .FirstOrDefault(p => p?.PermissionMode == permissionMode); _partitionKey = new PartitionKey(currentPermission?.PartitionKey); _partitionKeyStr = currentPermission?.PartitionKey; _partitionKeyHeader = resourcePermissionResponse?.PartitionKeyHeader; try { _cosmosClient = new CosmosClient(resourcePermissionResponse?.EndpointUrl, currentPermission?.ResourceToken); _container = _cosmosClient .GetDatabase(resourcePermissionResponse?.DatabaseId) .GetContainer(resourcePermissionResponse?.CollectionId); } catch (Exception ex) { Debug.WriteLine($"Unable to instantiate {nameof(CosmosClientStreamWrapper)}. Unhandled exception {ex}"); throw new CosmosClientException($"Unable to instantiate {nameof(CosmosClientStreamWrapper)}. Unhandled exception {ex}", ex); } }
/// <summary> /// <para> /// Instance of resource permission /// </para> /// </summary> /// <param name="permissionMode">Permission mode.</param> /// <param name="scope">Permission scope.</param> /// <param name="resourceToken">Resource token.</param> /// <param name="id">Permission id.</param> /// <param name="partitionKey">Partition key.</param> /// <param name="expiresUtc">Permission expiration (UTC)</param> public ResourcePermission( PermissionModeKind permissionMode, string scope, string resourceToken, string id, string partitionKey, DateTime expiresUtc) { Scope = scope; PermissionMode = permissionMode; ResourceToken = resourceToken; Id = id; PartitionKey = partitionKey; ExpiresUtc = expiresUtc; }
internal static string ToPartitionKeyBy(this string userId, PermissionModeKind permissionMode) => permissionMode switch {
private async Task <IResourcePermissionResponse> GetResourcePermissionResponse(PermissionModeKind permissionMode, CancellationToken ct) { await ValidateLoginState(); var resourcePermissionResponse = await AcquireResourceToken(_authService.CurrentUserContext, ct); var resourceToken = resourcePermissionResponse?.ResourcePermissions? .FirstOrDefault(p => p?.PermissionMode == permissionMode)?.ResourceToken; if (string.IsNullOrEmpty(resourceToken)) { throw new CosmosClientException($"No Resource Token acquired for permission: {permissionMode.ToString()}"); } var endpointUrl = resourcePermissionResponse?.EndpointUrl; if (string.IsNullOrEmpty(endpointUrl) || !Uri.TryCreate(endpointUrl, UriKind.Absolute, out _)) { throw new CosmosClientException($"No or invalid endpoint received from broker: {endpointUrl}"); } var databaseId = resourcePermissionResponse?.DatabaseId; if (string.IsNullOrEmpty(databaseId)) { throw new CosmosClientException($"No or invalid database Id received from broker: {databaseId}"); } var collectionId = resourcePermissionResponse?.CollectionId; if (string.IsNullOrEmpty(collectionId)) { throw new CosmosClientException($"No or invalid database Id received from broker: {collectionId}"); } return(resourcePermissionResponse); }
public async Task <T> ExecuteCosmosCommand <T>(Func <IResourcePermissionResponse, Task <T> > cosmosFunc, PermissionModeKind permissionMode, CancellationToken ct) { var resourcePermissionResponse = await GetResourcePermissionResponse(permissionMode, ct); return(await cosmosFunc(resourcePermissionResponse)); }
/// <summary> /// <para> /// Instance of permission scope. /// </para> /// </summary> /// <param name="permissionMode">Permission mode enum.</param> /// <param name="scope">Permission scope.</param> public PermissionScope(PermissionModeKind permissionMode, string scope) { PermissionMode = permissionMode; Scope = scope; }