Пример #1
0
        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);
            }
        }
Пример #2
0
 /// <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;
 }
Пример #3
0
 internal static string ToPartitionKeyBy(this string userId, PermissionModeKind permissionMode) => permissionMode switch
 {
Пример #4
0
        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);
        }
Пример #5
0
        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));
        }
Пример #6
0
 /// <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;
 }