/// <summary>
 /// 配置刷新令牌
 /// </summary>
 /// <param name="builder">授权参数生成器</param>
 /// <param name="token">访问令牌参数</param>
 /// <param name="config">授权配置</param>
 protected virtual void ConfigRefreshToken(AuthorizationParameterBuilder builder, string token,
                                           TAuthorizationConfig config)
 {
     builder.GatewayUrl(config.AccessTokenUrl)
     .GrantType(OAuthConst.RefreshToken)
     .ClientId(config.AppId)
     .ClientSecret(config.AppKey)
     .RefreshToken(token);
 }
 /// <summary>
 /// 配置生成授权地址
 /// </summary>
 /// <param name="builder">授权参数生成器</param>
 /// <param name="param">授权参数</param>
 /// <param name="config">授权配置</param>
 protected virtual void ConfigGenerateUrl(AuthorizationParameterBuilder builder, AuthorizationParam param,
                                          TAuthorizationConfig config)
 {
     builder.GatewayUrl(config.AuthorizationUrl)
     .ClientId(config.AppId)
     .ResponseType(param.ResponseType)
     .State(param.State)
     .RedirectUri(string.IsNullOrWhiteSpace(param.RedirectUri) ? config.CallbackUrl : param.RedirectUri);
 }
        /// <summary>
        /// 获取访问令牌
        /// </summary>
        /// <param name="param">访问令牌参数</param>
        /// <returns></returns>
        public virtual async Task <AuthorizationResult> GetTokenAsync(AccessTokenParam param)
        {
            var config = await ConfigProvider.GetConfigAsync();

            Validate(config, param);
            var builder = new AuthorizationParameterBuilder();

            ConfigGetToken(builder, param, config);
            return(await RequestResult(config, builder, ParameterParserType.Url, Success));
        }
        /// <summary>
        /// 生成授权地址
        /// </summary>
        /// <param name="param">授权参数</param>
        /// <returns></returns>
        public virtual async Task <string> GenerateUrlAsync(AuthorizationParam param)
        {
            var config = await ConfigProvider.GetConfigAsync();

            Validate(config, param);
            var builder = new AuthorizationParameterBuilder();

            ConfigGenerateUrl(builder, param, config);
            return(HandlerUrl(builder));
        }
        /// <summary>
        /// Post请求结果
        /// </summary>
        /// <param name="config">授权配置</param>
        /// <param name="builder">授权参数生成器</param>
        /// <param name="type">参数解析器类型</param>
        /// <param name="success">请求成功条件</param>
        /// <returns></returns>
        protected virtual async Task <AuthorizationResult> PostRequestResult(TAuthorizationConfig config,
                                                                             AuthorizationParameterBuilder builder, ParameterParserType type, Func <AuthorizationResult, bool> success)
        {
            var result = new AuthorizationResult(await PostRequest(builder), success, type);

            result.Parameter = builder.ToString();
            result.Message   = GetMessage(result);
            WriteLog(config, builder, result);
            return(result);
        }
 /// <summary>
 /// 配置获取访问令牌
 /// </summary>
 /// <param name="builder">授权参数生成器</param>
 /// <param name="param">访问令牌参数</param>
 /// <param name="config">授权配置</param>
 protected virtual void ConfigGetToken(AuthorizationParameterBuilder builder, AccessTokenParam param,
                                       TAuthorizationConfig config)
 {
     builder.GatewayUrl(config.AccessTokenUrl)
     .GrantType(OAuthConst.AuthorizationCode)
     .ClientId(config.AppId)
     .ClientSecret(config.AppKey)
     .Code(param.Code)
     .RedirectUri(string.IsNullOrWhiteSpace(param.RedirectUri) ? config.CallbackUrl : param.RedirectUri);
 }
        /// <summary>
        /// 刷新令牌
        /// </summary>
        /// <param name="token">刷新令牌</param>
        /// <returns></returns>
        public virtual async Task <AuthorizationResult> RefreshTokenAsync(string token)
        {
            if (string.IsNullOrWhiteSpace(token))
            {
                throw new ArgumentNullException(nameof(token));
            }
            var config = await ConfigProvider.GetConfigAsync();

            Validate(config);
            var builder = new AuthorizationParameterBuilder();

            ConfigRefreshToken(builder, token, config);
            return(await RequestResult(config, builder, ParameterParserType.Url, Success));
        }
        /// <summary>
        /// 写日志
        /// </summary>
        /// <param name="config">授权配置</param>
        /// <param name="builder">授权参数生成器</param>
        /// <param name="result">授权结果</param>
        protected void WriteLog(TAuthorizationConfig config, AuthorizationParameterBuilder builder,
                                AuthorizationResult result)
        {
            var log = GetLog();

            if (log.IsTraceEnabled == false)
            {
                return;
            }

            log.Class(GetType().FullName)
            .Caption("OAuth授权登录")
            .Content($"授权渠道 : {GetOAuthWay().Description()}")
            .Content("请求参数")
            .Content(builder.ToString())
            .Content()
            .Content("返回结果:")
            .Content(result.GetDictionary())
            .Content("原始响应:")
            .Content(result.Raw)
            .Trace();
        }
 /// <summary>
 /// 处理授权地址
 /// </summary>
 /// <param name="builder">授权参数生成器</param>
 /// <returns></returns>
 protected virtual string HandlerUrl(AuthorizationParameterBuilder builder)
 {
     return(builder.ToString());
 }
 /// <summary>
 /// 发送Post请求
 /// </summary>
 /// <param name="builder">授权参数生成器</param>
 /// <returns></returns>
 protected virtual async Task <string> PostRequest(AuthorizationParameterBuilder builder)
 {
     return(await Web.Client().Post(builder.GetGatewayUrl()).Data(builder.GetDictionary()).ResultAsync());
 }
 /// <summary>
 /// 发送请求
 /// </summary>
 /// <param name="builder">授权参数生成器</param>
 /// <returns></returns>
 protected virtual async Task <string> Request(AuthorizationParameterBuilder builder)
 {
     return(await Web.Client().Get(builder.ToString()).ResultAsync());
 }