Exemplo n.º 1
0
        public IAuthentificationToken Login(string pUsername, string pPassword)
        {
            if (string.IsNullOrWhiteSpace(pUsername))
            {
                throw new ArgumentNullException("pUsername");
            }
            if (string.IsNullOrWhiteSpace(pPassword))
            {
                throw new ArgumentNullException("pPassword");
            }

            var openSessionRequest = new OpenSessionRequest()
            {
                Username = pUsername, Password = pPassword
            };
            var openSessionResponse = _client.OpenSession(openSessionRequest);

            if (!openSessionResponse.IsAuthenticated)
            {
                return(null);
            }

            return(new AuthentificationToken()
            {
                Value = openSessionResponse.Token.Value, FleetOwnerID = openSessionResponse.FleetOwnerID
            });
        }
        public OpenSessionResponse OpenSession(OpenSessionRequest request)
        {
            string sessionId = RetailServiceBLL.NewSession(request.LoginId, request.ComputerName);

            return(new RetailService.OpenSessionResponse()
            {
                SessionId = sessionId
            });
        }
Exemplo n.º 3
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.");
            }
        }
Exemplo n.º 4
0
        public IActionResult OpenSession(OpenSessionRequest reqOpenSession)
        {
            IActionResult response = this.OpenSession(reqOpenSession.UserId);

            return(response);
        }
Exemplo n.º 5
0
        public HttpResponseMessage OpenSession(OpenSessionRequest reqOpenSession)
        {
            HttpResponseMessage response = this.OpenSession(reqOpenSession.UserId);

            return(response);
        }
Exemplo n.º 6
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})");
                }
            }
        }
Exemplo n.º 7
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})");
                }
            }
        }
Exemplo n.º 8
0
 public async Task <OpenSessionResp> OpenSessionAsync(OpenSessionRequest request)
 {
     return(await GenericRequestAsync <OpenSessionResp, OpenSessionRequest>(request));
 }