public async Task OAuth2Client_GetAuthorizationCodeAsync() { const string expectedAuthCode = "68c39cbd8d"; var baseUri = new Uri("https://example.com"); OAuth2ServerEndpoints endpoints = CreateEndpoints(baseUri); var httpHandler = new TestHttpMessageHandler { ThrowOnUnexpectedRequest = true }; string[] expectedScopes = { "read", "write", "delete" }; OAuth2Application app = CreateTestApplication(); var server = new TestOAuth2Server(endpoints); server.RegisterApplication(app); server.Bind(httpHandler); server.TokenGenerator.AuthCodes.Add(expectedAuthCode); IOAuth2WebBrowser browser = new TestOAuth2WebBrowser(httpHandler); OAuth2Client client = CreateClient(httpHandler, endpoints); OAuth2AuthorizationCodeResult result = await client.GetAuthorizationCodeAsync(expectedScopes, browser, CancellationToken.None); Assert.Equal(expectedAuthCode, result.Code); }
public async Task OAuth2Client_E2E_InteractiveWebFlowAndRefresh() { const string expectedAuthCode = "e78a711d11"; const string expectedAccessToken1 = "LET_ME_IN-1"; const string expectedAccessToken2 = "LET_ME_IN-2"; const string expectedRefreshToken1 = "REFRESH_ME-1"; const string expectedRefreshToken2 = "REFRESH_ME-2"; var baseUri = new Uri("https://example.com"); OAuth2ServerEndpoints endpoints = CreateEndpoints(baseUri); var httpHandler = new TestHttpMessageHandler { ThrowOnUnexpectedRequest = true }; string[] expectedScopes = { "read", "write", "delete" }; OAuth2Application app = CreateTestApplication(); var server = new TestOAuth2Server(endpoints); server.RegisterApplication(app); server.Bind(httpHandler); server.TokenGenerator.AuthCodes.Add(expectedAuthCode); server.TokenGenerator.AccessTokens.Add(expectedAccessToken1); server.TokenGenerator.RefreshTokens.Add(expectedRefreshToken1); IOAuth2WebBrowser browser = new TestOAuth2WebBrowser(httpHandler); OAuth2Client client = CreateClient(httpHandler, endpoints); OAuth2AuthorizationCodeResult authCodeResult = await client.GetAuthorizationCodeAsync( expectedScopes, browser, CancellationToken.None); OAuth2TokenResult result1 = await client.GetTokenByAuthorizationCodeAsync(authCodeResult, CancellationToken.None); Assert.NotNull(result1); Assert.Equal(expectedScopes, result1.Scopes); Assert.Equal(expectedAccessToken1, result1.AccessToken); Assert.Equal(expectedRefreshToken1, result1.RefreshToken); server.TokenGenerator.AccessTokens.Add(expectedAccessToken2); server.TokenGenerator.RefreshTokens.Add(expectedRefreshToken2); OAuth2TokenResult result2 = await client.GetTokenByRefreshTokenAsync(result1.RefreshToken, CancellationToken.None); Assert.NotNull(result2); Assert.Equal(expectedScopes, result2.Scopes); Assert.Equal(expectedAccessToken2, result2.AccessToken); Assert.Equal(expectedRefreshToken2, result2.RefreshToken); }