Example #1
0
        public IHttpActionResult CheckActiveSession()
        {
            var currentTokenParts = HttpTokenHelper.GetTokenParts(HttpContext.Current.Request);

            if (currentTokenParts == null) return Unauthorized();

            var request = new GetActiveSessionRequest
            {
                SessionId = currentTokenParts.SessionId,
                UserName = currentTokenParts.UserName
            };

            var response = GetActiveSessionAction.Handle(ActionContext, request);
            if (response != null && response.Account != null && response.Session != null)
            {
                response.Token = HttpTokenHelper.GenerateToken(response.Account.Name, response.Session.ExternalId);

                return Ok(response);
            }

            return Unauthorized();
        }
        private Account GetAccount(HttpActionContext actionContext)
        {
            var token = GetAuthorizationToken(actionContext);
            if (string.IsNullOrWhiteSpace(token))
            {
                var basic = ParseAuthorizationHeader(actionContext);
                if (basic == null || string.IsNullOrEmpty(basic.Username)) return null;

                var account = AuthenticationService.GetAccount(basic.Username, basic.Password);
                if (account == null) return null;

                using (var gateway = AccountsGatewayFactory())
                {
                    return gateway.GetAccountByEmail(account.Mail);
                }
            }

            var parts = HttpTokenHelper.GetTokenParts(token);
            if (parts == null)
                return null;
            var request = new GetActiveSessionRequest { SessionId = parts.SessionId, UserName = parts.UserName };
            var response = ActiveSessionQuery.Handle(request);
            if (response != null)
            {
                CommandDispatcher.Send(new ProlongSessionCommand
                {
                    SessionId = request.SessionId,
                    CommandContext = new CommandContext
                    {
                        Id = Guid.NewGuid()
                    }
                });
            }

            return response != null ? response.Account : null;
        }