/// <summary>
        /// Generates a list of `role` claims for the provided identity roles.
        /// </summary>
        /// <param name="roles">Roles to generate claims for.</param>
        /// <returns>Returns the newly generated claims.</returns>
        /// <exception cref="EntityNotFoundException">Thrown if any of the referenced domains could be found.</exception>
        private async Task <IEnumerable <Claim> > GenerateRoleClaims(IEnumerable <Role> roles)
        {
            Dictionary <Guid, Domain> domains = new Dictionary <Guid, Domain>();
            List <Claim> claims = new List <Claim>();

            // For each role, retrieve domain if neccessary, and set claim with role name `[<domainName>.]<roleName>`.
            foreach (Role role in roles)
            {
                string roleName = "";
                if (role.DomainId.HasValue)
                {
                    Guid domainId = role.DomainId.Value;
                    if (!domains.ContainsKey(domainId))
                    {
                        domains.Add(domainId, await DomainService.GetDomain(domainId));
                    }
                    roleName += domains[domainId].Name + ".";
                }
                roleName += role.Name;
                claims.Add(new Claim(MisConstants.JWT_ROLES, roleName));
            }

            // Done, return role claims
            return(claims);
        }
        /// <summary>
        /// Sets up the service with all needed dependencies.
        /// </summary>
        /// <param name="loggerFactory">Factory to create loggers from.</param>
        /// <param name="passwordHashingService">Provides password hashing functionalities.</param>
        /// <param name="identityService">Provides identities.</param>
        /// <param name="domainService">Provides domain names.</param>
        /// <param name="configuration">App configuration for JWT signing information.</param>
        public AuthenticationService(ILoggerFactory loggerFactory,
                                     PasswordHashingService passwordHashingService,
                                     IdentityService identityService,
                                     DomainService domainService,
                                     ApiKeyService apiKeyService,
                                     IConfiguration configuration)
        {
            Logger = loggerFactory.CreateLogger <AuthenticationService>();
            PasswordHashingService = passwordHashingService;
            IdentityService        = identityService;
            DomainService          = domainService;
            ApiKeyService          = apiKeyService;

            // JWT-related configuration
            SymmetricSecurityKey securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration.GetValue <string>(ConfigurationPaths.JWT_SECRET)));

            SigningCredentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
            JwtLifetime        = TimeSpan.FromMinutes(configuration.GetValue <int>(ConfigurationPaths.JWT_LIFETIME_IN_MINUTES));
            JwtIssuer          = configuration.GetValue <string>(ConfigurationPaths.JWT_ISSUER);
        }
        /// <summary>
        /// Gets roles, optionally filtered to the roles that belong to a given domain.
        /// </summary>
        /// <param name="domainId">ID of the domain to get identity roles for.</param>
        /// <returns>Returns a list of roles.</returns>
        /// <exception cref="EntityNotFoundException">Thrown if a domain ID was provided but no matching domain could be found.</exception>
        public async Task <IEnumerable <Role> > GetRoles(string filter = null, Guid?domainId = null)
        {
            if (filter == null && domainId == null)
            {
                return(await RoleRepository.GetRoles());
            }
            else if (filter == null && domainId != null)
            {
                await DomainService.GetDomain(domainId.Value); // Throws an EntityNotFoundException if domain doesn't exist

                return(await RoleRepository.GetDomainRoles(domainId.Value));
            }
            else if (filter != null && domainId == null)
            {
                return(await RoleRepository.SearchRolesByName(filter));
            }
            else
            {
                await DomainService.GetDomain(domainId.Value); // Throws an EntityNotFoundException if domain doesn't exist

                return(await RoleRepository.SearchDomainRolesByName(domainId.Value, filter));
            }
        }
 /// <summary>
 /// Sets up the service with all needed dependencies.
 /// </summary>
 /// <param name="logger">A logger for local logging needs.</param>
 /// <param name="roleRepository">A repository of identity roles.</param>
 /// <param name="domainService">Provides domains.</param>
 public RoleService(ILogger <RoleService> logger, IRoleRepository roleRepository, DomainService domainService)
 {
     RoleRepository = roleRepository;
     DomainService  = domainService;
     Logger         = logger;
 }