public DiscordOAuthProvider(
     HttpClient http,
     OAuthConfigurationProvider cfgProvider)
 {
     this.Http          = http;
     this.Configuration = cfgProvider.GetById(ProviderType);
 }
        private static LoginProviderPreview ProviderToPreview(ConfigurationOAuthProvider provider, OAuthProviderSelector selector)
        {
            var id  = provider.Id ?? provider.Type;
            var prv = selector.GetById(id);

            return(new LoginProviderPreview(id, prv.GetName(id), prv.GetColour(id)));
        }
Esempio n. 3
0
 private static bool IsValidConfigurationItem(ConfigurationOAuthProvider cfg)
 => cfg.Id != null &&
 cfg.ClientId != null &&
 cfg.ClientSecret != null &&
 cfg.Scopes?.Any() == true &&
 cfg.AuthorizeUrl != null &&
 cfg.TokenUrl != null &&
 cfg.UserUrl != null &&
 cfg.Mappings != null;
Esempio n. 4
0
        private async Task <OAuthResult> ExchangeTokenAsync(AuthenticationContext ctx, string credential, string grantType, ConfigurationOAuthProvider cfg, string url, CancellationToken cancellationToken = default)
        {
            using (var post = new HttpRequestMessage(HttpMethod.Post, url))
            {
                var postData = new Dictionary <string, string>(6)
                {
                    ["client_id"]     = cfg.ClientId,
                    ["client_secret"] = cfg.ClientSecret,
                    ["grant_type"]    = grantType,
                    ["redirect_uri"]  = this.GetRedirectUrl(ctx),
                    ["scope"]         = string.Join(" ", cfg.Scopes)
                };

                switch (grantType)
                {
                case GrantTypeToken:
                    postData["code"] = credential;
                    break;

                case GrantTypeRefresh:
                    postData["refresh_token"] = credential;
                    break;
                }

                post.Content = new FormUrlEncodedContent(postData);

                using (var res = await this.Http.SendAsync(post, cancellationToken))
                {
                    if (!res.IsSuccessStatusCode)
                    {
                        return(null);
                    }

                    using (var dat = await res.Content.ReadAsStreamAsync())
                    {
                        var result = await JsonSerializer.DeserializeAsync <OAuthResult>(dat, AbstractionUtilities.SnakeCaseJsonOptions, cancellationToken);

                        result.IsSuccess = true;
                        return(result);
                    }
                }
            }
        }