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()
{