Skip to content

Commit

Permalink
GH-37: Update post message body for revoke request
Browse files Browse the repository at this point in the history
  • Loading branch information
henrikfroehling committed Jul 28, 2018
1 parent 453ac53 commit a429cde
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 16 deletions.
8 changes: 5 additions & 3 deletions Source/Lib/Trakt.NET/Modules/TraktAuthenticationModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public Task<TraktResponse<ITraktAuthorization>> RefreshAuthorizationAsync(string
}

public Task<TraktNoContentResponse> RevokeAuthorizationAsync(CancellationToken cancellationToken = default)
=> RevokeAuthorizationAsync(Authorization.AccessToken, cancellationToken);
=> RevokeAuthorizationAsync(Authorization?.AccessToken, cancellationToken);

public Task<TraktNoContentResponse> RevokeAuthorizationAsync(string accessToken, CancellationToken cancellationToken = default)
=> RevokeAuthorizationAsync(accessToken, ClientId, cancellationToken);
Expand All @@ -199,12 +199,14 @@ public Task<TraktNoContentResponse> RevokeAuthorizationAsync(string accessToken,
{
RequestBody = new AuthorizationRevokeRequestBody
{
AccessToken = accessToken
AccessToken = accessToken,
ClientId = clientId,
ClientSecret = ClientSecret
}
};

var requestHandler = new AuthenticationRequestHandler(Client);
return requestHandler.RevokeAuthorizationAsync(request, clientId, cancellationToken);
return requestHandler.RevokeAuthorizationAsync(request, cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

internal sealed class AuthorizationRevokeRequest : APostRequest<AuthorizationRevokeRequestBody>
{
public override AuthorizationRequirement AuthorizationRequirement => AuthorizationRequirement.NotRequired;

public override string UriTemplate => "oauth/revoke";

public override AuthorizationRevokeRequestBody RequestBody { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,26 @@ internal sealed class AuthorizationRevokeRequestBody : IRequestBody
{
internal string AccessToken { get; set; }

internal string ClientId { get; set; }

internal string ClientSecret { get; set; }

public Task<string> ToJson(CancellationToken cancellationToken = default)
{
return Task.FromResult(HttpContentAsString);
}
=> Task.FromResult(HttpContentAsString);

private string HttpContentAsString => $"token={AccessToken}";
private string HttpContentAsString => $"{{ \"token\": \"{AccessToken}\", \"client_id\": \"{ClientId}\"," +
$" \"client_secret\": \"{ClientSecret}\" }}";

public void Validate()
{
if (string.IsNullOrEmpty(AccessToken) || AccessToken.ContainsSpace())
throw new ArgumentException("access token not valid", nameof(AccessToken));

if (string.IsNullOrEmpty(ClientId) || ClientId.ContainsSpace())
throw new ArgumentException("client id not valid", nameof(ClientId));

if (string.IsNullOrEmpty(ClientSecret) || ClientSecret.ContainsSpace())
throw new ArgumentException("client secret not valid", nameof(ClientSecret));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -264,16 +264,10 @@ await _requestMessageBuilder.Reset(request)
public Task<TraktResponse<ITraktAuthorization>> RefreshAuthorizationAsync(AuthorizationRefreshRequest request, CancellationToken cancellationToken = default)
=> ExecuteAuthorizationRequestAsync(request, true, cancellationToken);

public async Task<TraktNoContentResponse> RevokeAuthorizationAsync(AuthorizationRevokeRequest request, string clientId, CancellationToken cancellationToken = default)
public async Task<TraktNoContentResponse> RevokeAuthorizationAsync(AuthorizationRevokeRequest request, CancellationToken cancellationToken = default)
{
try
{
if (!_client.Authentication.IsAuthorized)
throw new TraktAuthorizationException("not authorized");

if (string.IsNullOrEmpty(clientId) || clientId.ContainsSpace())
throw new ArgumentException("client id not valid", nameof(clientId));

request.Validate();

ExtendedHttpRequestMessage requestMessage =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ internal interface IAuthenticationRequestHandler

Task<TraktResponse<ITraktAuthorization>> RefreshAuthorizationAsync(AuthorizationRefreshRequest request, CancellationToken cancellationToken = default);

Task<TraktNoContentResponse> RevokeAuthorizationAsync(AuthorizationRevokeRequest request, string clientId, CancellationToken cancellationToken = default);
Task<TraktNoContentResponse> RevokeAuthorizationAsync(AuthorizationRevokeRequest request, CancellationToken cancellationToken = default);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public TraktAuthenticationModule_Tests()
$"\"client_secret\": \"{TraktClientSecret}\", \"redirect_uri\": " +
$"\"{TraktRedirectUri}\", \"grant_type\": \"refresh_token\" }}";

MockAuthorizationRevokePostContent = $"token={TestConstants.MOCK_ACCESS_TOKEN}";
MockAuthorizationRevokePostContent = $"{{ \"token\": \"{TestConstants.MOCK_ACCESS_TOKEN}\", \"client_id\": \"{TraktClientId}\"," +
$" \"client_secret\": \"{TraktClientSecret}\" }}";

MockAuthorizationPollingPostContent = $"{{ \"code\": \"{MOCK_DEVICE_CODE}\", \"client_id\": \"{TraktClientId}\", \"client_secret\": \"{TraktClientSecret}\" }}";

Expand Down

0 comments on commit a429cde

Please sign in to comment.