From 43db99a5686471641eed7913d71dab2dcf0d2464 Mon Sep 17 00:00:00 2001 From: Saravanan Date: Mon, 14 Aug 2017 22:59:02 +0530 Subject: [PATCH] =?UTF-8?q?Updated=20for=20the=20TwitterOptions=20Validati?= =?UTF-8?q?on=20for=20the=20ConsumerKey=20and=20Con=E2=80=A6=20(#1337)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TwitterOptions.cs | 18 +++++++++++ .../TwitterTests.cs | 32 ++++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.AspNetCore.Authentication.Twitter/TwitterOptions.cs b/src/Microsoft.AspNetCore.Authentication.Twitter/TwitterOptions.cs index 0190f21a6b..86919d0925 100644 --- a/src/Microsoft.AspNetCore.Authentication.Twitter/TwitterOptions.cs +++ b/src/Microsoft.AspNetCore.Authentication.Twitter/TwitterOptions.cs @@ -3,6 +3,7 @@ using System; using System.Security.Claims; +using System.Globalization; using Microsoft.AspNetCore.Authentication.OAuth.Claims; using Microsoft.AspNetCore.Http; @@ -86,6 +87,23 @@ namespace Microsoft.AspNetCore.Authentication.Twitter set => _stateCookieBuilder = value ?? throw new ArgumentNullException(nameof(value)); } + /// + /// Added the validate method to ensure that the customer key and customer secret values are not not empty for the twitter authentication middleware + /// + public override void Validate() + { + base.Validate(); + if (string.IsNullOrEmpty(ConsumerKey)) + { + throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.Exception_OptionMustBeProvided, nameof(ConsumerKey)), nameof(ConsumerKey)); + } + + if (string.IsNullOrEmpty(ConsumerSecret)) + { + throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.Exception_OptionMustBeProvided, nameof(ConsumerSecret)), nameof(ConsumerSecret)); + } + } + private class TwitterCookieBuilder : CookieBuilder { private readonly TwitterOptions _twitterOptions; diff --git a/test/Microsoft.AspNetCore.Authentication.Test/TwitterTests.cs b/test/Microsoft.AspNetCore.Authentication.Test/TwitterTests.cs index 1c387d889a..746dfee6ab 100644 --- a/test/Microsoft.AspNetCore.Authentication.Test/TwitterTests.cs +++ b/test/Microsoft.AspNetCore.Authentication.Test/TwitterTests.cs @@ -63,7 +63,7 @@ namespace Microsoft.AspNetCore.Authentication.Twitter } }; }, - context => + context => { // REVIEW: Gross context.ChallengeAsync("Twitter").GetAwaiter().GetResult(); @@ -75,6 +75,36 @@ namespace Microsoft.AspNetCore.Authentication.Twitter Assert.Contains("custom=test", query); } + /// + /// Validates the Twitter Options to check if the Consumer Key is missing in the TwitterOptions and if so throws the ArgumentException + /// + /// + [Fact] + public async Task ThrowsIfClientIdMissing() + { + var server = CreateServer(o => + { + o.ConsumerSecret = "Test Consumer Secret"; + }); + + await Assert.ThrowsAsync("ConsumerKey", async () => await server.SendAsync("http://example.com/challenge")); + } + + /// + /// Validates the Twitter Options to check if the Consumer Secret is missing in the TwitterOptions and if so throws the ArgumentException + /// + /// + [Fact] + public async Task ThrowsIfClientSecretMissing() + { + var server = CreateServer(o => + { + o.ConsumerKey = "Test Consumer Key"; + }); + + await Assert.ThrowsAsync("ConsumerSecret", async () => await server.SendAsync("http://example.com/challenge")); + } + [Fact] public async Task BadSignInWillThrow() {