Ejemplo n.º 1
0
        public static async Task <ApiSession> OpenSessionMultiplexedAsync(
            SpaceEnumerationItem desiredSpace,
            OpenSessionAuthenticatorContext context,
            OpenSessionRequest openSessionRequest,
            CancellationToken cancellationToken)
        {
            // space access restriction is supported since server 3.9.2
            // for previous versions api will return SpaceAccessRestriction.NotSupported
            // a special fall-back mechanize need to be used to open session in such case
            switch (desiredSpace.SpaceAccessRestriction)
            {
            // anon space
            case SpaceAccessRestriction.None:
                return(ApiSession.Anonymous(context.MorphServerApiClient, openSessionRequest.SpaceName));

            // password protected space
            case SpaceAccessRestriction.BasicPassword:
                return(await OpenSessionViaSpacePasswordAsync(context, openSessionRequest.SpaceName, openSessionRequest.Password, cancellationToken));

            // windows authentication
            case SpaceAccessRestriction.WindowsAuthentication:
                return(await OpenSessionViaWindowsAuthenticationAsync(context, openSessionRequest.SpaceName, cancellationToken));

            // fallback
            case SpaceAccessRestriction.NotSupported:

                //  if space is public or password is not set - open anon session
                if (desiredSpace.IsPublic || string.IsNullOrWhiteSpace(openSessionRequest.Password))
                {
                    return(ApiSession.Anonymous(context.MorphServerApiClient, openSessionRequest.SpaceName));
                }
                // otherwise open session via space password
                else
                {
                    return(await OpenSessionViaSpacePasswordAsync(context, openSessionRequest.SpaceName, openSessionRequest.Password, cancellationToken));
                }

            default:
                throw new Exception("Space access restriction method is not supported by this client.");
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Opens session based on required authentication mechanism
        /// </summary>
        /// <param name="openSessionRequest"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <ApiSession> OpenSessionAsync(OpenSessionRequest openSessionRequest, CancellationToken ct)
        {
            if (openSessionRequest == null)
            {
                throw new ArgumentNullException(nameof(openSessionRequest));
            }
            if (string.IsNullOrWhiteSpace(openSessionRequest.SpaceName))
            {
                throw new ArgumentException("Space name is not set.", nameof(openSessionRequest.SpaceName));
            }

            using (var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(ct))
            {
                // no more than 20 sec for session opening
                var timeout = TimeSpan.FromSeconds(20);
                linkedTokenSource.CancelAfter(timeout);
                var cancellationToken = linkedTokenSource.Token;
                try
                {
                    var spacesListResult = await GetSpacesListAsync(cancellationToken);

                    var desiredSpace = spacesListResult.Items.FirstOrDefault(x => x.SpaceName.Equals(openSessionRequest.SpaceName, StringComparison.OrdinalIgnoreCase));
                    if (desiredSpace == null)
                    {
                        throw new Exception($"Server has no space '{openSessionRequest.SpaceName}'");
                    }
                    // space access restriction is supported since server 3.9.2
                    // for previous versions api will return SpaceAccessRestriction.NotSupported
                    // a special fall-back mechanize need to be used to open session in such case
                    switch (desiredSpace.SpaceAccessRestriction)
                    {
                    // anon space
                    case SpaceAccessRestriction.None:
                        return(ApiSession.Anonymous(openSessionRequest.SpaceName));

                    // password protected space
                    case SpaceAccessRestriction.BasicPassword:
                        return(await OpenSessionViaSpacePasswordAsync(openSessionRequest.SpaceName, openSessionRequest.Password, cancellationToken));

                    // windows authentication
                    case SpaceAccessRestriction.WindowsAuthentication:
                        return(await OpenSessionViaWindowsAuthenticationAsync(openSessionRequest.SpaceName, cancellationToken));

                    // fallback
                    case SpaceAccessRestriction.NotSupported:

                        //  if space is public or password is not set - open anon session
                        if (desiredSpace.IsPublic || string.IsNullOrWhiteSpace(openSessionRequest.Password))
                        {
                            return(ApiSession.Anonymous(openSessionRequest.SpaceName));
                        }
                        // otherwise open session via space password
                        else
                        {
                            return(await OpenSessionViaSpacePasswordAsync(openSessionRequest.SpaceName, openSessionRequest.Password, cancellationToken));
                        }

                    default:
                        throw new Exception("Space access restriction method is not supported by this client.");
                    }
                }
                catch (OperationCanceledException) when(!ct.IsCancellationRequested && linkedTokenSource.IsCancellationRequested)
                {
                    throw new Exception($"Can't connect to host {_apiHost}.  Operation timeout ({timeout})");
                }
            }
        }