// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Text; using System.Threading.Tasks; using Microsoft.AspNet.Builder; using Microsoft.AspNet.Http; using Microsoft.AspNet.TestHost; using Microsoft.Framework.DependencyInjection; using Shouldly; using Xunit; namespace Microsoft.AspNet.Authentication.Twitter { public class TwitterMiddlewareTests { [Fact] public async Task ChallengeWillTriggerApplyRedirectEvent() { var server = CreateServer( app => app.UseTwitterAuthentication(options => { options.ConsumerKey = "Test Consumer Key"; options.ConsumerSecret = "Test Consumer Secret"; options.Notifications = new TwitterAuthenticationNotifications { OnApplyRedirect = context => { context.Response.Redirect(context.RedirectUri + "&custom=test"); } }; options.BackchannelHttpHandler = new TestHttpMessageHandler { Sender = req => { if (req.RequestUri.AbsoluteUri == "https://api.twitter.com/oauth/request_token") { return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("oauth_callback_confirmed=true&oauth_token=test_oauth_token&oauth_token_secret=test_oauth_token_secret", Encoding.UTF8, "application/x-www-form-urlencoded") }; } return null; } }; options.BackchannelCertificateValidator = null; }), context => { context.Authentication.Challenge("Twitter"); return true; }); var transaction = await SendAsync(server, "http://example.com/challenge"); transaction.Response.StatusCode.ShouldBe(HttpStatusCode.Redirect); var query = transaction.Response.Headers.Location.Query; query.ShouldContain("custom=test"); } [Fact] public async Task ChallengeWillTriggerRedirection() { var server = CreateServer( app => app.UseTwitterAuthentication(options => { options.ConsumerKey = "Test Consumer Key"; options.ConsumerSecret = "Test Consumer Secret"; options.BackchannelHttpHandler = new TestHttpMessageHandler { Sender = req => { if (req.RequestUri.AbsoluteUri == "https://api.twitter.com/oauth/request_token") { return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("oauth_callback_confirmed=true&oauth_token=test_oauth_token&oauth_token_secret=test_oauth_token_secret", Encoding.UTF8, "application/x-www-form-urlencoded") }; } return null; } }; options.BackchannelCertificateValidator = null; }), context => { context.Authentication.Challenge("Twitter"); return true; }); var transaction = await SendAsync(server, "http://example.com/challenge"); transaction.Response.StatusCode.ShouldBe(HttpStatusCode.Redirect); var location = transaction.Response.Headers.Location.AbsoluteUri; location.ShouldContain("https://twitter.com/oauth/authenticate?oauth_token="); } private static TestServer CreateServer(Action configure, Func handler) { return TestServer.Create(app => { app.UseCookieAuthentication(options => { options.AuthenticationScheme = "External"; }); if (configure != null) { configure(app); } app.Use(async (context, next) => { if (handler == null || !handler(context)) { await next(); } }); }, services => { services.AddAuthentication(); services.Configure(options => { options.SignInScheme = "External"; }); }); } private static async Task SendAsync(TestServer server, string uri, string cookieHeader = null) { var request = new HttpRequestMessage(HttpMethod.Get, uri); if (!string.IsNullOrEmpty(cookieHeader)) { request.Headers.Add("Cookie", cookieHeader); } var transaction = new Transaction { Request = request, Response = await server.CreateClient().SendAsync(request), }; if (transaction.Response.Headers.Contains("Set-Cookie")) { transaction.SetCookie = transaction.Response.Headers.GetValues("Set-Cookie").ToList(); } transaction.ResponseText = await transaction.Response.Content.ReadAsStringAsync(); return transaction; } private class TestHttpMessageHandler : HttpMessageHandler { public Func Sender { get; set; } protected override Task SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) { if (Sender != null) { return Task.FromResult(Sender(request)); } return Task.FromResult(null); } } private class Transaction { public HttpRequestMessage Request { get; set; } public HttpResponseMessage Response { get; set; } public IList SetCookie { get; set; } public string ResponseText { get; set; } } } }