Esempio n. 1
0
        public void ToCookie(DashboardSettingsViewModel dashboardSettingsViewModel)
        {
            if (dashboardSettingsViewModel == null)
            {
                throw new ArgumentNullException(nameof(dashboardSettingsViewModel));
            }

            StoreCookie(DashboardSettingsViewModel.CookieName, dashboardSettingsViewModel,
                        viewModel => _contentHelper.ToBase64String(viewModel),
                        (viewModel, secureHttpRequest) =>
            {
                DateTime expires = DateTime.Now.AddHours(8);
                if (string.IsNullOrWhiteSpace(viewModel.RedditAccessToken) == false)
                {
                    IRedditAccessToken redditAccessToken = RedditAccessToken.Create(viewModel.RedditAccessToken);
                    if (redditAccessToken != null && redditAccessToken.Expires <= expires)
                    {
                        expires = redditAccessToken.Expires;
                    }
                }

                return(new CookieOptions
                {
                    Expires = expires,
                    Secure = secureHttpRequest,
                    SameSite = SameSiteMode.None
                });
            });
        }
Esempio n. 2
0
        public async Task <IActionResult> Export(int numberOfNews = 100, [FromHeader] string redditAccessToken = null, bool includeNsfwContent = false, bool onlyNsfwContent = false)
        {
            if (numberOfNews < 0)
            {
                return(BadRequest($"The submitted value for '{nameof(numberOfNews)}' cannot be lower than 0."));
            }
            if (numberOfNews > 250)
            {
                return(BadRequest($"The submitted value for '{nameof(numberOfNews)}' cannot be greater than 250."));
            }

            IRedditAccessToken token = null;

            if (string.IsNullOrWhiteSpace(redditAccessToken) == false)
            {
                try
                {
                    token = RedditAccessToken.Create(redditAccessToken);
                }
                catch (SerializationException)
                {
                    return(BadRequest($"The submitted value for '{nameof(redditAccessToken)}' is invalid."));
                }
                catch (FormatException)
                {
                    return(BadRequest($"The submitted value for '{nameof(redditAccessToken)}' is invalid."));
                }
            }

            DashboardSettingsViewModel dashboardSettingsViewModel = new DashboardSettingsViewModel
            {
                NumberOfNews                  = numberOfNews,
                UseReddit                     = token != null,
                AllowNsfwContent              = token != null ? includeNsfwContent || onlyNsfwContent : false,
                IncludeNsfwContent            = token != null ? includeNsfwContent : false,
                NotNullableIncludeNsfwContent = token != null ? includeNsfwContent : false,
                OnlyNsfwContent               = token != null ? onlyNsfwContent : false,
                NotNullableOnlyNsfwContent    = token != null ? onlyNsfwContent : false,
                RedditAccessToken             = token?.ToBase64(),
                ExportData                    = true
            };
            IDashboard dashboard = await _dashboardFactory.BuildAsync(dashboardSettingsViewModel.ToDashboardSettings());

            DashboardExportModel dashboardExportModel = await _dashboardModelExporter.ExportAsync(dashboard);

            return(Ok(dashboardExportModel));
        }
        public Task <IRedditAccessToken> GetRedditAccessTokenAsync(string clientId, string clientSecret, string code, Uri redirectUri)
        {
            if (string.IsNullOrWhiteSpace(clientId))
            {
                throw new ArgumentNullException(nameof(clientId));
            }
            if (string.IsNullOrWhiteSpace(clientSecret))
            {
                throw new ArgumentNullException(nameof(clientSecret));
            }
            if (string.IsNullOrWhiteSpace(code))
            {
                throw new ArgumentNullException(nameof(code));
            }
            if (redirectUri == null)
            {
                throw new ArgumentNullException(nameof(redirectUri));
            }

            return(Task.Run <IRedditAccessToken>(async() =>
            {
                IDictionary <string, string> formFields = new Dictionary <string, string>
                {
                    { "grant_type", "authorization_code" },
                    { "code", code },
                    { "redirect_uri", redirectUri.AbsoluteUri }
                };

                IRedditResponse <RedditAccessToken> redditResponse = await RedditRepository.PostAsync <RedditAccessToken>(
                    new Uri("https://www.reddit.com/api/v1/access_token"),
                    "Basic",
                    Convert.ToBase64String(Encoding.ASCII.GetBytes($"{clientId}:{clientSecret}")),
                    formFields);

                RedditAccessToken redditAccessToken = redditResponse.Data;
                if (string.IsNullOrWhiteSpace(redditAccessToken.Error))
                {
                    return redditAccessToken;
                }
                throw new Exception($"Unable to get the access token from Reddit: {redditAccessToken.Error}");
            }));
        }