Skip to content

Commit

Permalink
Implement command auth.getToken (inflatablefriends#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
tolbxela committed Mar 29, 2020
1 parent cf34acc commit 9dd2ef9
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
42 changes: 42 additions & 0 deletions src/IF.Lastfm.Core/Api/Commands/Auth/GetTokenCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using IF.Lastfm.Core.Api.Enums;
using IF.Lastfm.Core.Api.Helpers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace IF.Lastfm.Core.Api.Commands.Auth
{
[ApiMethodName("auth.getToken")]
internal class GetTokenCommand : UnauthenticatedPostAsyncCommandBase<LastResponse<string>>
{
public GetTokenCommand(ILastAuth auth) : base(auth)
{
}

protected override Uri BuildRequestUrl()
{
return new Uri(LastFm.ApiRootSsl, UriKind.Absolute);
}

public override void SetParameters()
{
}

public override async Task<LastResponse<string>> HandleResponse(HttpResponseMessage response)
{
var json = await response.Content.ReadAsStringAsync();

if (LastFm.IsResponseValid(json, out LastResponseStatus status) && response.IsSuccessStatusCode)
{
var token = JsonConvert.DeserializeObject<JObject>(json).GetValue("token");
return LastResponse<string>.CreateSuccessResponse(token.Value<string>());
}
else
{
return LastResponse.CreateErrorResponse<LastResponse<string>>(status);
}
}
}
}
11 changes: 10 additions & 1 deletion src/IF.Lastfm.Core/Api/ILastAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,19 @@ public interface ILastAuth
/// Authentication Token from the Web Authentication 3.1 (https://www.last.fm/api/webauth)
/// </summary>
/// <param name="authToken">Authentication Token</param>
/// <returns>Session token used to authenticate calls to last.fm</returns>
/// <returns>Session token used to authenticate calls to Last.fm</returns>
/// <remarks>API: Auth.getSession</remarks>
Task<LastResponse> GetSessionTokenAsync(string authToken);

/// <summary>
/// Fetch an unathorized request token for an API account.
/// This is step 2 of the authentication process for desktop applications. (https://www.last.fm/api/desktopauth)
/// </summary>
/// <returns>Authentication Token used to get Last.fm Session.
/// Authentication tokens are user and API account specific. They are valid for 60 minutes from the moment they are granted.</returns>
/// <remarks>API: Auth.getToken</remarks>
Task<LastResponse> GetAuthTokenAsync();

/// <summary>
/// Adds the api_key, method and session key to the provided params dictionary, then generates an MD5 hash.
/// Parameters contained in the hash must also be exactly the parameters sent to the API.
Expand Down
9 changes: 9 additions & 0 deletions src/IF.Lastfm.Core/Api/LastAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ public async Task<LastResponse> GetSessionTokenAsync(string authToken)
}
}

public async Task<LastResponse> GetAuthTokenAsync()
{
var command = new GetTokenCommand(this)
{
HttpClient = HttpClient
};
return await command.ExecuteAsync();
}

public string GenerateMethodSignature(string method, Dictionary<string, string> parameters = null)
{
if (parameters == null)
Expand Down

0 comments on commit 9dd2ef9

Please sign in to comment.