Moved authentication into GetMobileSessionCommand. Resolves #6

This commit is contained in:
Rikki Tooley 2013-09-02 16:13:41 +01:00
parent b249ea7fa8
commit 85d8edad3b
6 changed files with 67 additions and 42 deletions

View File

@ -5,18 +5,11 @@ namespace IF.Lastfm.Core.Tests
{
public class MockLastFm
{
public LastFm Object { get; set; }
public Mock<IAuth> Auth { get; set; }
public MockLastFm()
{
Auth = new Mock<IAuth>();
Object = new LastFm
{
Auth = Auth.Object
};
}
}
}

View File

@ -1,24 +1,17 @@
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using IF.Lastfm.Core.Api.Enums;
using IF.Lastfm.Core.Api.Commands.AuthApi;
using IF.Lastfm.Core.Api.Helpers;
using IF.Lastfm.Core.Objects;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using xBrainLab.Security.Cryptography;
namespace IF.Lastfm.Core.Api
{
public class Auth : IAuth
{
private const string ApiAuthMethod = "auth.getMobileSession";
private readonly string _apiSecret;
private string _password;
private string _username;
public bool HasAuthenticated { get { return User != null; } }
public string ApiKey { get; private set; }
@ -43,35 +36,17 @@ public bool LoadSession(UserSession session)
public async Task<LastResponse> GetSessionTokenAsync(string username, string password)
{
const string apiMethod = "auth.getMobileSession";
var command = new GetMobileSessionCommand(this, username, password);
var response = await command.ExecuteAsync();
_password = password;
_username = username;
var apisig = GenerateMethodSignature(apiMethod);
var postContent = LastFm.CreatePostBody(apiMethod, ApiKey, apisig, new Dictionary<string, string>
{
{"password", password},
{"username", username}
});
var httpClient = new HttpClient();
HttpResponseMessage response = await httpClient.PostAsync("https://ws.audioscrobbler.com/2.0/", postContent);
string json = await response.Content.ReadAsStringAsync();
LastFmApiError error;
if (LastFm.IsResponseValid(json, out error) && response.IsSuccessStatusCode)
if (response.Success)
{
var sessionObject = JsonConvert.DeserializeObject<JObject>(json).GetValue("session");
User = JsonConvert.DeserializeObject<UserSession>(sessionObject.ToString());
User = response.Content;
return LastResponse.CreateSuccessResponse();
}
else
{
return LastResponse.CreateErrorResponse<LastResponse>(error);
return LastResponse.CreateErrorResponse<LastResponse>(response.Error);
}
}
@ -84,8 +59,6 @@ public string GenerateMethodSignature(string method, Dictionary<string, string>
parameters.Add("api_key", ApiKey);
parameters.Add("method", method);
parameters.Add("password", _password);
parameters.Add("username", _username);
var builder = new StringBuilder();

View File

@ -0,0 +1,54 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using IF.Lastfm.Core.Api.Enums;
using IF.Lastfm.Core.Api.Helpers;
using IF.Lastfm.Core.Objects;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace IF.Lastfm.Core.Api.Commands.AuthApi
{
public class GetMobileSessionCommand : PostAsyncCommandBase<LastResponse<UserSession>>
{
public string Username { get; set; }
public string Password { get; set; }
public GetMobileSessionCommand(IAuth auth, string username, string password) : base(auth)
{
Method = "auth.getMobileSession";
Username = username;
Password = password;
}
protected override Uri BuildRequestUrl()
{
return new Uri(LastFm.ApiRootSsl, UriKind.Absolute);
}
public override void SetParameters()
{
Parameters.Add("username", Username);
Parameters.Add("password", Password);
}
public async override Task<LastResponse<UserSession>> HandleResponse(HttpResponseMessage response)
{
string json = await response.Content.ReadAsStringAsync();
LastFmApiError error;
if (LastFm.IsResponseValid(json, out error) && response.IsSuccessStatusCode)
{
var sessionObject = JsonConvert.DeserializeObject<JObject>(json).GetValue("session");
var session = JsonConvert.DeserializeObject<UserSession>(sessionObject.ToString());
return LastResponse<UserSession>.CreateSuccessResponse(session);
}
else
{
return LastResponse.CreateErrorResponse<LastResponse<UserSession>>(error);
}
}
}
}

View File

@ -6,7 +6,7 @@
namespace IF.Lastfm.Core.Api.Commands
{
internal abstract class PostAsyncCommandBase<T> : LastAsyncCommandBase<T> where T : LastResponse, new()
public abstract class PostAsyncCommandBase<T> : LastAsyncCommandBase<T> where T : LastResponse, new()
{
protected PostAsyncCommandBase(IAuth auth)
{
@ -24,7 +24,10 @@ public override async Task<T> ExecuteAsync()
Url = BuildRequestUrl();
Parameters.Add("sk", Auth.User.Token);
if (Auth.HasAuthenticated)
{
Parameters.Add("sk", Auth.User.Token);
}
var apisig = Auth.GenerateMethodSignature(Method, Parameters);

View File

@ -47,6 +47,7 @@
<Compile Include="Api\Commands\ArtistApi\AddShoutCommand.cs" />
<Compile Include="Api\Commands\ArtistApi\GetArtistInfoCommand.cs" />
<Compile Include="Api\Commands\ArtistApi\GetArtistShoutsCommand.cs" />
<Compile Include="Api\Commands\AuthApi\GetMobileSessionCommand.cs" />
<Compile Include="Api\Commands\GetAsyncCommandBase.cs" />
<Compile Include="Api\Commands\LastAsyncCommandBase.cs" />
<Compile Include="Api\Commands\PostAsyncCommandBase.cs" />

View File

@ -14,6 +14,7 @@ public class LastFm
#region Constants
public const string ApiRoot = "http://ws.audioscrobbler.com/2.0/";
public const string ApiRootSsl = "https://ws.audioscrobbler.com/2.0/";
private const string ApiRootFormat = "{0}://ws.audioscrobbler.com/2.0/?method={1}&api_key={2}{3}";
private const string ResponseFormat = "json";