Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow OAuth2Client refresh token to be updated when grant_type is "refresh_token" #151

Merged
merged 4 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions OAuth2.Tests/Client/OAuth2ClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,45 @@ public async Task Should_IssueCorrectRequestForUserInfo_When_GetUserInfoIsCalled
_restClient.Authenticator.Should().BeOfType<OAuth2UriQueryParameterAuthenticator>();
}

[Test]
public async Task Should_Update_RefreshToken_When_New_Token_Is_Provided_in_GetCurrentTokenAsync()
{
// arrange
var oldRefreshToken = "old-refresh-token";
var newRefreshToken = "new-refresh-token";
var response = @$"{{""access_token"": ""abc123"", ""refresh_token"": ""{newRefreshToken}""}}";
_restResponse.Content.Returns(response);

// act
await _descendant.GetCurrentTokenAsync(oldRefreshToken);

// assert
Assert.That(_descendant.RefreshToken, Is.EqualTo(newRefreshToken));
}

[Test]
public async Task Should_Not_Modify_RefreshToken_When_Not_Included_In_Response_From_GetCurrentTokenAsync()
{
// arrange
var currentRefreshToken = "refresh-token";
var initialTokenResponse = @$"{{""access_token"": ""abc123"", ""refresh_token"": ""{currentRefreshToken}""}}";
var refreshTokenResponse = @"{""access_token"": ""abc123""}";

// simulate getting the initial token (to populate refresh token)
_restResponse.Content.Returns(initialTokenResponse);
await _descendant.GetTokenAsync(new NameValueCollection {{"code", "auth-code"}});
Assert.That(_descendant.RefreshToken, Is.EqualTo(currentRefreshToken));

// setup response for refresh token request
_restResponse.Content.Returns(refreshTokenResponse);

// act
await _descendant.GetCurrentTokenAsync(currentRefreshToken);

// assert
Assert.That(_descendant.RefreshToken, Is.EqualTo(currentRefreshToken));
}

class OAuth2ClientDescendant : OAuth2Client
{
public OAuth2ClientDescendant(IRequestFactory factory, IClientConfiguration configuration)
Expand Down
5 changes: 3 additions & 2 deletions OAuth2/Client/OAuth2Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,9 @@ private async Task QueryAccessTokenAsync(NameValueCollection parameters, Cancell
if (String.IsNullOrEmpty(AccessToken))
throw new UnexpectedResponseException(AccessTokenKey);

if (GrantType != "refresh_token")
RefreshToken = ParseTokenResponse(response.Content, RefreshTokenKey);
string refreshToken = ParseTokenResponse(response.Content, RefreshTokenKey);
if (!String.IsNullOrWhiteSpace(refreshToken))
RefreshToken = refreshToken;

TokenType = ParseTokenResponse(response.Content, TokenTypeKey);

Expand Down