Exemple #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.");
            }
        }
Exemple #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))
            {
                var timeout = clientConfiguration.SessionOpenTimeout;
                linkedTokenSource.CancelAfter(timeout);
                var cancellationToken = linkedTokenSource.Token;
                try
                {
                    // tring to resolve space and space auth method.
                    // Method 1. using get all spaces method
                    var spacesListApiResult = await _lowLevelApiClient.SpacesGetListAsync(cancellationToken);

                    SpaceEnumerationItem desiredSpace = null;
                    if (!spacesListApiResult.IsSucceed && spacesListApiResult.Error is MorphApiForbiddenException)
                    {
                        // space listing disabled has been disabled be server admin.
                        // Method 2. Using spaces lookup (new endpoint since next version of EM Server 4.3)
                        var lookupApiResult = await _lowLevelApiClient.SpacesLookupAsync(new SpacesLookupRequestDto()
                        {
                            SpaceNames = { openSessionRequest.SpaceName }
                        }, cancellationToken);

                        desiredSpace = MapOrFail(lookupApiResult,
                                                 (dto) =>
                        {
                            // response have at least 1 element with requested space.
                            var lookup = dto.Values.First();
                            if (lookup.Error != null)
                            {
                                // seems that space not found.
                                throw new Exception($"Unable to open session. {lookup.Error.message}");
                            }
                            else
                            {
                                // otherwise return space
                                return(SpacesEnumerationMapper.MapItemFromDto(lookup.Data));
                            }
                        }
                                                 );
                    }
                    else
                    {
                        var spacesListResult = MapOrFail(spacesListApiResult, (dto) => SpacesEnumerationMapper.MapFromDto(dto));
                        desiredSpace = spacesListResult.Items.FirstOrDefault(x => x.SpaceName.Equals(openSessionRequest.SpaceName, StringComparison.OrdinalIgnoreCase));
                    }

                    if (desiredSpace == null)
                    {
                        throw new Exception($"Unable to open session. Server has no space '{openSessionRequest.SpaceName}'");
                    }
                    var session = await MorphServerAuthenticator.OpenSessionMultiplexedAsync(desiredSpace,
                                                                                             new OpenSessionAuthenticatorContext(
                                                                                                 _lowLevelApiClient,
                                                                                                 this as ICanCloseSession,
                                                                                                 (handler) => ConstructRestApiClient(BuildHttpClient(clientConfiguration, handler))),
                                                                                             openSessionRequest, cancellationToken);

                    return(session);
                }
                catch (OperationCanceledException) when(!ct.IsCancellationRequested && linkedTokenSource.IsCancellationRequested)
                {
                    throw new Exception($"Can't connect to host {clientConfiguration.ApiUri}.  Operation timeout ({timeout})");
                }
            }
        }