<commit_before>﻿using LinqToTwitter.Common;
using LinqToTwitter.Net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace LinqToTwitter.OAuth
{
    public class OAuth2Authorizer : OAuth2AuthorizerBase, IOAuth2Authorizer
    {
        public const string CredentialStoreMessage = "You must assign the CredentialStore property (with required values).";

        public const string ValidCharacterString = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~";
        public static readonly char[] ValidCharacters = ValidCharacterString.ToCharArray();

        public string AuthorizeUrl { get; set; }
        public string AccessTokenUrl { get; set; }
        public string RefreshTokenUrl { get; set; }
        public string RevokeTokenUrl { get; set; }

        public string? HtmlResponseString { get; set; }

        /// <summary>
        /// This is a hook where you can assign
        /// an <see cref="Action"/> to perform the technology
        /// specific redirection action.
        /// 
        /// The string passed as the <see cref="Action"/> paramter
        /// is the Twitter authorization URL.
        /// </summary>
        public Action<string>? GoToTwitterAuthorization { get; set; }

        public OAuth2Authorizer()
        {
            AuthorizeUrl = "https://twitter.com/i/oauth2/authorize";
            AccessTokenUrl = "https://api.twitter.com/2/oauth2/token";
            RefreshTokenUrl = "https://api.twitter.com/2/oauth2/token";
            RevokeTokenUrl = "https://api.twitter.com/2/oauth2/revoke";

            SupportsCompression = true;

            if (string.IsNullOrWhiteSpace(UserAgent))
                UserAgent = L2TKeys.DefaultUserAgent;
        }

        void HandleAuthorizationHeader(HttpRequestMessage req, string url)
        {
            if (CredentialStore is not IOAuth2CredentialStore credStore)
                throw new NullReferenceException(CredentialStoreMessage);

            bool isAuthorizationRequest(string url) => url.StartsWith(AuthorizeUrl);
            bool isPublicClient() => !string.IsNullOrWhiteSpace(credStore.ClientSecret);
<commit_msg>Oauth2 desktop (#254)<commit_after>﻿using LinqToTwitter.Common;
using LinqToTwitter.Net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace LinqToTwitter.OAuth
{
    public class OAuth2Authorizer : OAuth2AuthorizerBase, IOAuth2Authorizer
    {
        public const string CredentialStoreMessage = "You must assign the CredentialStore property (with required values).";

        public const string ValidCharacterString = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~";
        public static readonly char[] ValidCharacters = ValidCharacterString.ToCharArray();

        public string AuthorizeUrl { get; set; }
        public string AccessTokenUrl { get; set; }
        public string RefreshTokenUrl { get; set; }
        public string RevokeTokenUrl { get; set; }

        public string? HtmlResponseString { get; set; }

        /// <summary>
        /// This is a hook where you can assign
        /// an <see cref="Action"/> to perform the technology
        /// specific redirection action.
        /// 
        /// The string passed as the <see cref="Action"/> paramter
        /// is the Twitter authorization URL.
        /// </summary>
        public Action<string>? GoToTwitterAuthorization { get; set; }

        public OAuth2Authorizer()
        {
            AuthorizeUrl = "https://twitter.com/i/oauth2/authorize";
            AccessTokenUrl = "https://api.twitter.com/2/oauth2/token";
            RefreshTokenUrl = "https://api.twitter.com/2/oauth2/token";
            RevokeTokenUrl = "https://api.twitter.com/2/oauth2/revoke";

            SupportsCompression = true;

            if (string.IsNullOrWhiteSpace(UserAgent))
                UserAgent = L2TKeys.DefaultUserAgent;
        }

        void HandleAuthorizationHeader(HttpRequestMessage req, string url)
        {
            if (CredentialStore is not IOAuth2CredentialStore credStore)
                throw new NullReferenceException(CredentialStoreMessage);

            bool isAuthorizationRequest(string url) => url.StartsWith(AuthorizeUrl);
            bool isPublicClient() => string.IsNullOrWhiteSpace(credStore.ClientSecret);

{'fraction_non_alphanumeric': 0.0936483849989161, 'fraction_numerical': 0.010838933448948623, 'mean_word_length': 2.797530864197531, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1690679', 'n_tokens_mistral': 1289, 'n_tokens_neox': 1240, 'n_words': 308}