Set user in OAuthContext

This commit is contained in:
Hao Kung 2015-07-01 19:01:44 -07:00
parent 9bb8b61146
commit 61bbe4cf52
2 changed files with 88 additions and 2 deletions

View File

@ -52,6 +52,7 @@ namespace Microsoft.AspNet.Authentication.OAuth
{
TokenResponse = tokens;
Backchannel = backchannel;
User = user;
}
/// <summary>

View File

@ -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);