mirror of
https://github.com/Sarsoo/IF.Lastfm.git
synced 2024-10-17 07:13:09 +01:00
Moved authentication into GetMobileSessionCommand. Resolves #6
This commit is contained in:
parent
b249ea7fa8
commit
85d8edad3b
@ -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
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
@ -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();
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
|
||||
|
@ -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" />
|
||||
|
@ -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";
|
||||
|
Loading…
Reference in New Issue
Block a user