public void OnAuthorization(AuthorizationFilterContext context)
        {
            var pathsegments = context.HttpContext.Request.Path.Value.Split("/");

            if (pathsegments.Length < 2)
            {
                context.Result = new StatusCodeResult(400);
                return;
            }

            //user is second from last
            var userSlug = pathsegments[pathsegments.Length - 2];
            //podcast slug is last
            var podcastSlug = pathsegments[pathsegments.Length - 1];

            //get the podcast
            var podcast = _podcastRepository
                          .GetAll().FirstOrDefault(
                p => p.AppUser.Slug == userSlug &&
                p.Slug == podcastSlug && (p.Private));

            if (podcast is null)
            {
                return;
            }

            string authHeader = context.HttpContext.Request.Headers["Authorization"];

            if (authHeader != null && authHeader.StartsWith("Basic "))
            {
                // Get the encoded username and password
                var encodedUsernamePassword =
                    authHeader.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries)[1]?.Trim();
                // Decode from Base64 to string
                var decodedUsernamePassword =
                    Encoding.UTF8.GetString(Convert.FromBase64String(encodedUsernamePassword));
                // Split username and password
                var username = decodedUsernamePassword.Split(':', 2)[0];
                var password = decodedUsernamePassword.Split(':', 2)[1];
                // Check if login is correct
                if (IsAuthorized(podcast, username, password))
                {
                    return;
                }
            }

            // Return authentication type (causes browser to show login dialog)
            context.HttpContext.Response.Headers["WWW-Authenticate"] = "Basic";
            // Add realm if it is not null
            context.HttpContext.Response.Headers["WWW-Authenticate"] += $" realm=\"{Realm}\"";

            // Return unauthorized
            context.Result = new UnauthorizedResult();
        }
예제 #2
0
        public async Task <ActionResult <List <SearchResultsViewModel> > > DoSearch(string query)
        {
            var podcastResults = await _podcastRepository
                                 .GetAll()
                                 .Where(p => p.AppUser.Id == _applicationUser.Id)
                                 .Where(p => p.Title.Contains(query) || p.Description.Contains(query))
                                 .Select(p => new SearchResultsViewModel {
                Title       = p.Title,
                Description = HtmlUtils.FormatLineBreaks(p.Description).Truncate(100, true),
                ImageUrl    = p.GetImageUrl(_storageSettings.CdnUrl, _imageFileStorageSettings.ContainerName),
                Url         = p.Slug,
                Type        = "Podcast",
                DateCreated = p.CreateDate
            }).ToListAsync();

            var entryResults = await _entryRepository
                               .GetAll()
                               .Include(x => x.Podcast)
                               .Where(p => p.Podcast.AppUser.Id == _applicationUser.Id)
                               .Where(p => p.Title.Contains(query) || p.Description.Contains(query))
                               .Select(p => new SearchResultsViewModel {
                Title       = p.Title,
                Description = HtmlUtils.FormatLineBreaks(p.Description).Truncate(100, true),
                ImageUrl    = p.GetImageUrl(_storageSettings.CdnUrl, _imageFileStorageSettings.ContainerName),
                Url         = p.Podcast.Slug,
                Type        = "Entry",
                DateCreated = p.CreateDate
            }).ToListAsync();

            var mergedResults = podcastResults.Union(entryResults);

            return(Ok(mergedResults));
        }
예제 #3
0
        public async Task <ActionResult <PodcastViewModel> > Query()
        {
            var podcast = await _podcastRepository.GetAll()
                          .Where(p => p.Id == Guid.Parse("54f5ea27-9dff-41cf-9944-08d600a180a2"))
                          .Include(p => p.Notifications)
                          .FirstOrDefaultAsync();

            var response = _mapper.Map <Podcast, PodcastViewModel>(podcast);

            return(response);
        }
        public async Task <ActionResult <PodcastEntryViewModel> > GetFeaturedEpisode(string userSlug, string podcastSlug)
        {
            var podcast = await _podcastRepository.GetAll()
                          .OrderByDescending(p => p.CreateDate)
                          .Include(p => p.PodcastEntries)
                          .Include(p => p.AppUser)
                          .SingleOrDefaultAsync(r => r.AppUser.Slug == userSlug && r.Slug == podcastSlug);

            var result = await _entryRepository.GetFeaturedEpisode(podcast);

            if (result is null)
            {
                return(NotFound());
            }

            return(_mapper.Map <PodcastEntry, PodcastEntryViewModel>(result));
        }
 public async Task<ActionResult<PublicDomainViewModel>> ResolveDomain([FromQuery] string domain) {
     try {
         var cleanedDomain = domain.Split(":")[0]; //remove port
         var podcastUrl = string.Empty;
         var podcast = await _podcastRepository
             .GetAll()
             .Include(p => p.AppUser)
             .SingleOrDefaultAsync(r => r.CustomDomain == cleanedDomain);
         if (podcast != null) {
             podcastUrl = Flurl.Url.Combine(podcast.AppUser.Slug, podcast.Slug);
             return Ok(new PublicDomainViewModel {
                 Domain = cleanedDomain,
                 PodcastId = podcast.Id.ToString(),
                 PodcastSlug = podcast.Slug,
                 UserSlug = podcast.AppUser.Slug,
                 Url = podcastUrl
             });
         }
     } catch (Exception e) {
         _logger.LogError($"Error resolving custom domain: {domain}\n{e.Message}");
     }
     return Ok();
 }
예제 #6
0
 public List <Podcast> GetAllPodcasts()
 {
     return(podcastRepository.GetAll());
 }
        public IReadOnlyCollection <PodcastModel> GetPodcasts()
        {
            IReadOnlyCollection <Podcast> podcasts = _podcastRepository.GetAll();

            return(_mapper.Map <IReadOnlyCollection <PodcastModel> >(podcasts));
        }