예제 #1
0
        public async Task <TokenDto> Authenticate(LoginData data)
        {
            var user = await _context
                       .Users
                       .Include(e => e.RefreshTokens)
                       .FirstOrDefaultAsync(e => e.Username == data.Username && e.Active)
                       ?? throw new NotFoundException($"No user for username {data.Username}");

            if (!VerifyPasswordHash(data.Password, user.PasswordHash, user.PasswordSalt))
            {
                throw new ArgumentException("Invalid password.");
            }

            var token = _jwtHelper.GenerateJwtToken(user);

            var referenceTime = _dateTime.Now;
            var refreshToken  = user.RefreshTokens
                                .FirstOrDefault(e => e.Revoked == null &&
                                                referenceTime < e.Expires);

            if (refreshToken == null)
            {
                refreshToken = CreateRefreshToken();
                user.RefreshTokens.Add(refreshToken);

                await _context.SaveChangesAsync();
            }

            token.RefreshToken           = refreshToken.Token;
            token.RefreshTokenValidUntil = refreshToken.Expires;

            return(token);
        }
예제 #2
0
        public async Task <WatchedDomainDto> CreateWatchedDomainAsync(CreateWatchedDomainData data)
        {
            var domain = _mapper.Map <WatchedDomain>(data);

            _context.WatchedDomains.Add(domain);
            await _context.SaveChangesAsync();

            return(_mapper.Map <WatchedDomainDto>(domain));
        }
예제 #3
0
        public async Task <DnsServerDto> CreateDnsServerAsync(CreateDnsServerData data)
        {
            var server = _mapper.Map <DnsServer>(data);

            _context.DnsServers.Add(server);
            await _context.SaveChangesAsync();

            return(_mapper.Map <DnsServerDto>(server));
        }
예제 #4
0
        public async Task <WatchedRecordDto> CreateWatchedRecordAsync(Guid domainId, CreateWatchedRecordData data)
        {
            var domain = await _domainsService.GetDomainAsync(domainId);

            var record = _mapper.Map <WatchedRecord>(data);

            domain.WatchedRecords.Add(record);
            await _context.SaveChangesAsync();

            return(_mapper.Map <WatchedRecordDto>(record));
        }
예제 #5
0
        public async Task UpdateDnsResultsForRecordAsync(WatchedRecord record)
        {
            var query = !string.IsNullOrWhiteSpace(record.Prefix)
                                ? $"{record.Prefix}.{record.WatchedDomain.DomainName}"
                                : record.WatchedDomain.DomainName;

            var servers = await _dnsServersService.GetDnsServersAsync();

            foreach (var server in servers.DnsServers)
            {
                var data = new DnsQueryData
                {
                    Domain     = query,
                    RecordType = record.RecordType,
                    Ip         = server.IpAddress,
                    Port       = server.Port
                };
                var result = await _dnsResolver.ResolveDnsQuery(data);

                var existingResult = record.Results
                                     .FirstOrDefault(e => e.DnsServerId == server.Id);
                if (existingResult != null)
                {
                    existingResult.Value      = result.Value;
                    existingResult.TimeToLive = result.TimeToLive;
                }
                else
                {
                    var newResult = new RecordServerResult
                    {
                        Value       = result.Value,
                        TimeToLive  = result.TimeToLive,
                        DnsServerId = server.Id
                    };
                    record.Results.Add(newResult);
                }
            }

            await _context.SaveChangesAsync();
        }