aspnetcore/shared/Mocks/Twitter/TestTwitterEvents.cs

52 lines
2.2 KiB
C#

#if TESTING
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Authentication;
using Microsoft.AspNet.Authentication.Twitter;
using Microsoft.AspNet.Identity;
using MusicStore.Mocks.Common;
namespace MusicStore.Mocks.Twitter
{
internal class TestTwitterEvents
{
internal static Task OnCreatingTicket(TwitterCreatingTicketContext context)
{
if (context.Principal != null)
{
Helpers.ThrowIfConditionFailed(() => context.UserId == "valid_user_id", "UserId is not valid");
Helpers.ThrowIfConditionFailed(() => context.ScreenName == "valid_screen_name", "ScreenName is not valid");
Helpers.ThrowIfConditionFailed(() => context.AccessToken == "valid_oauth_token", "AccessToken is not valid");
Helpers.ThrowIfConditionFailed(() => context.AccessTokenSecret == "valid_oauth_token_secret", "AccessTokenSecret is not valid");
context.Principal.Identities.First().AddClaim(new Claim("ManageStore", "false"));
}
return Task.FromResult(0);
}
internal static Task OnTicketReceived(TicketReceivedContext context)
{
if (context.Principal != null && context.Options.SignInScheme == new IdentityCookieOptions().ExternalCookieAuthenticationScheme)
{
//This way we will know all Events were fired.
var identity = context.Principal.Identities.First();
var manageStoreClaim = identity?.Claims.Where(c => c.Type == "ManageStore" && c.Value == "false").FirstOrDefault();
if (manageStoreClaim != null)
{
identity.RemoveClaim(manageStoreClaim);
identity.AddClaim(new Claim("ManageStore", "Allowed"));
}
}
return Task.FromResult(0);
}
internal static Task RedirectToAuthorizationEndpoint(TwitterRedirectToAuthorizationEndpointContext context)
{
context.Response.Redirect(context.RedirectUri + "&custom_redirect_uri=custom");
return Task.FromResult(0);
}
}
}
#endif