public UserResponse Execute(string accessKey)
        {
            var session = _sessionsGateway.GetSessionByToken(accessKey);

            if (session != null)
            {
                return(session.User == null ? null : _mapper.ToDomain(session.User).ToResponse());
            }
            return(null);
        }
        public OrganisationResponse ExecuteCreate(string accessToken, OrganisationRequest requestParams)
        {
            var gatewayResponse = _organisationsGateway.CreateOrganisation(requestParams.ToEntity());

            if (gatewayResponse != null)
            {
                var session        = _sessionsGateway.GetSessionByToken(accessToken);
                var userQueryParam = new UserQueryParam {
                    Sort = "Name", Direction = "asc"
                };
                var adminUsers = _usersGateway.GetAllUsers(userQueryParam).Result
                                 .Where(u => u.UserRoles.Any(ur => ur.Role.Name == "Admin"));
                var adminEmails = adminUsers.Select(au => au.Email).ToArray();
                _notifyGateway.SendMessage(NotifyMessageTypes.AdminNotification, adminEmails, requestParams.StatusMessage);
            }
            return(gatewayResponse == null ? new OrganisationResponse() : gatewayResponse.ToResponse());
        }
        protected override async Task <AuthenticateResult> HandleAuthenticateAsync()
        {
            // skip authentication if request method is OPTIONS
            if (Context.Request.Method == "OPTIONS")
            {
                return(AuthenticateResult.NoResult());
            }
            // skip authentication if endpoint has [AllowAnonymous] attribute
            var endpoint = Context.GetEndpoint();

            if (endpoint?.Metadata?.GetMetadata <IAllowAnonymous>() != null)
            {
                return(AuthenticateResult.NoResult());
            }

            var accessToken = Request.Cookies[Cookies.AccessTokenName];

            if (accessToken == null)
            {
                return(AuthenticateResult.Fail("Missing 'access_token' cookie"));
            }

            var session = _sessionsGateway.GetSessionByToken(accessToken);

            if (session?.User == null)
            {
                return(AuthenticateResult.Fail("Invalid session key"));
            }

            if (session.LastAccessAt == null)
            {
                return(AuthenticateResult.Fail("Invalid session"));
            }
            TimeSpan duration = DateTime.Now - session.LastAccessAt.Value;

            if (duration.TotalHours > _sessionDuration)
            {
                _sessionsGateway.RemoveSessions(session.Payload);
                return(AuthenticateResult.Fail("Session expired"));
            }
            _sessionsGateway.RefreshSessionExpiry(session.Id);

            if (session.User.UserRoles == null || !session.User.UserRoles.Any())
            {
                return(AuthenticateResult.Fail("No roles have been assigned to the user"));
            }

            var userRoleNames = session.User.UserRoles
                                .Where(ur => ur.Role != null)
                                .Select(ur => ur.Role.Name)
                                .ToList();

            var claims = userRoleNames.SelectMany(r => new[] {
                new Claim(ClaimTypes.NameIdentifier, $"{session.User.Id}"),
                new Claim(ClaimTypes.Role, r)
            })
                         .ToArray();

            var identity  = new ClaimsIdentity(claims, Scheme.Name);
            var principal = new ClaimsPrincipal(identity);
            var ticket    = new AuthenticationTicket(principal, Scheme.Name);

            LoggingHandler.LogInfo("Authentication Success!!!!");
            return(AuthenticateResult.Success(ticket));
        }