diff --git a/src/Microsoft.AspNet.Authentication.OAuth/Notifications/OAuthAuthenticatedContext.cs b/src/Microsoft.AspNet.Authentication.OAuth/Notifications/OAuthAuthenticatedContext.cs index e59f04d07b..e423e9244f 100644 --- a/src/Microsoft.AspNet.Authentication.OAuth/Notifications/OAuthAuthenticatedContext.cs +++ b/src/Microsoft.AspNet.Authentication.OAuth/Notifications/OAuthAuthenticatedContext.cs @@ -52,6 +52,7 @@ namespace Microsoft.AspNet.Authentication.OAuth { TokenResponse = tokens; Backchannel = backchannel; + User = user; } /// diff --git a/test/Microsoft.AspNet.Authentication.Test/Google/GoogleMiddlewareTests.cs b/test/Microsoft.AspNet.Authentication.Test/Google/GoogleMiddlewareTests.cs index ee55456dc4..e1b8edfec5 100644 --- a/test/Microsoft.AspNet.Authentication.Test/Google/GoogleMiddlewareTests.cs +++ b/test/Microsoft.AspNet.Authentication.Test/Google/GoogleMiddlewareTests.cs @@ -246,7 +246,7 @@ namespace Microsoft.AspNet.Authentication.Google return ReturnJsonResponse(new { access_token = "Test Access Token", - expire_in = 3600, + expires_in = 3600, token_type = "Bearer" }); } @@ -384,7 +384,7 @@ namespace Microsoft.AspNet.Authentication.Google return ReturnJsonResponse(new { access_token = "Test Access Token", - expire_in = 3600, + expires_in = 3600, token_type = "Bearer", refresh_token = "Test Refresh Token" }); @@ -446,6 +446,91 @@ namespace Microsoft.AspNet.Authentication.Google transaction.FindClaimValue("RefreshToken").ShouldBe("Test Refresh Token"); } + [Fact] + public async Task ValidateAuthenticatedContext() + { + var stateFormat = new PropertiesDataFormat(new EphemeralDataProtectionProvider().CreateProtector("GoogleTest")); + var server = CreateServer(options => + { + options.ClientId = "Test Id"; + options.ClientSecret = "Test Secret"; + options.StateDataFormat = stateFormat; + options.AccessType = "offline"; + options.Notifications = new OAuthAuthenticationNotifications() + { + OnAuthenticated = context => + { + Assert.NotNull(context.User); + Assert.Equal(context.AccessToken, "Test Access Token"); + Assert.Equal(context.RefreshToken, "Test Refresh Token"); + Assert.Equal(context.ExpiresIn, TimeSpan.FromSeconds(3600)); + Assert.Equal(GoogleAuthenticationHelper.GetEmail(context.User), "Test email"); + Assert.Equal(GoogleAuthenticationHelper.GetId(context.User), "Test User ID"); + Assert.Equal(GoogleAuthenticationHelper.GetName(context.User), "Test Name"); + Assert.Equal(GoogleAuthenticationHelper.GetFamilyName(context.User), "Test Family Name"); + Assert.Equal(GoogleAuthenticationHelper.GetGivenName(context.User), "Test Given Name"); + return Task.FromResult(0); + } + }; + options.BackchannelHttpHandler = new TestHttpMessageHandler + { + Sender = req => + { + if (req.RequestUri.AbsoluteUri == "https://accounts.google.com/o/oauth2/token") + { + return ReturnJsonResponse(new + { + access_token = "Test Access Token", + expires_in = 3600, + token_type = "Bearer", + refresh_token = "Test Refresh Token" + }); + } + else if (req.RequestUri.GetLeftPart(UriPartial.Path) == "https://www.googleapis.com/plus/v1/people/me") + { + return ReturnJsonResponse(new + { + id = "Test User ID", + displayName = "Test Name", + name = new + { + familyName = "Test Family Name", + givenName = "Test Given Name" + }, + url = "Profile link", + emails = new[] + { + new + { + value = "Test email", + type = "account" + } + } + }); + } + + return null; + } + }; + }); + + var properties = new AuthenticationProperties(); + var correlationKey = ".AspNet.Correlation.Google"; + var correlationValue = "TestCorrelationId"; + properties.Items.Add(correlationKey, correlationValue); + properties.RedirectUri = "/foo"; + var state = stateFormat.Protect(properties); + + //Post a message to the Google middleware + var transaction = await server.SendAsync( + "https://example.com/signin-google?code=TestCode&state=" + UrlEncoder.Default.UrlEncode(state), + correlationKey + "=" + correlationValue); + + transaction.Response.StatusCode.ShouldBe(HttpStatusCode.Redirect); + transaction.Response.Headers.Location.ToString().ShouldBe("/foo"); + } + + private static HttpResponseMessage ReturnJsonResponse(object content) { var res = new HttpResponseMessage(HttpStatusCode.OK);