Skip to content

Commit

Permalink
change name UserProfile, UserUUID -> PlayerProfile, PlayerUUID
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaBs committed Feb 11, 2021
1 parent ef8ad55 commit 8fa5ac3
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace MojangAPI.Model
{
public class UserProfile : MojangAPIResponse
public class PlayerProfile : MojangAPIResponse
{
public string UUID { get; set; } = "";
public string Name { get; set; } = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace MojangAPI.Model
{
public class UserUUID : MojangAPIResponse
public class PlayerUUID : MojangAPIResponse
{
public override bool IsSuccess
=> base.IsSuccess
Expand Down
58 changes: 29 additions & 29 deletions MojangAPI/Mojang.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ public Mojang(HttpClient client)
this.client = client;
}

public Task<UserUUID> GetUUID(string username) =>
client.SendActionAsync(new HttpAction<UserUUID>
public Task<PlayerUUID> GetUUID(string username) =>
client.SendActionAsync(new HttpAction<PlayerUUID>
{
Method = HttpMethod.Get,
Host = "https://api.mojang.com",
Path = $"users/profiles/minecraft/{username ?? throw new ArgumentNullException(nameof(username))}"
});

public Task<UserUUID> GetUUID(string username, DateTimeOffset timestamp) =>
public Task<PlayerUUID> GetUUID(string username, DateTimeOffset timestamp) =>
GetUUID(username, timestamp.ToUnixTimeSeconds());

public Task<UserUUID> GetUUID(string username, long timestamp) =>
client.SendActionAsync(new HttpAction<UserUUID>
public Task<PlayerUUID> GetUUID(string username, long timestamp) =>
client.SendActionAsync(new HttpAction<PlayerUUID>
{
Method = HttpMethod.Get,
Host = "https://api.mojang.com",
Expand All @@ -71,27 +71,27 @@ public Task<UserUUID> GetUUID(string username, long timestamp) =>
ErrorHandler = (res, ex) => Task.FromResult<NameHistory[]?>(null)
});

public Task<UserUUID[]?> GetUUIDs(string[] usernames) =>
client.SendActionAsync(new HttpAction<UserUUID[]?>
public Task<PlayerUUID[]?> GetUUIDs(string[] usernames) =>
client.SendActionAsync(new HttpAction<PlayerUUID[]?>
{
Method = HttpMethod.Post,
Host = "https://api.mojang.com",
Path = "profiles/minecraft",
Content = new JsonHttpContent(usernames ?? throw new ArgumentNullException()),
ResponseHandler = HttpResponseHandlers.GetJsonArrayHandler<UserUUID>(),
ErrorHandler = (res, ex) => Task.FromResult<UserUUID[]?>(null)
ResponseHandler = HttpResponseHandlers.GetJsonArrayHandler<PlayerUUID>(),
ErrorHandler = (res, ex) => Task.FromResult<PlayerUUID[]?>(null)
});

public Task<UserProfile> GetProfileUsingUUID(string uuid) =>
client.SendActionAsync(new HttpAction<UserProfile>
public Task<PlayerProfile> GetProfileUsingUUID(string uuid) =>
client.SendActionAsync(new HttpAction<PlayerProfile>
{
Method = HttpMethod.Get,
Host = "https://sessionserver.mojang.com",
Path = $"session/minecraft/profile/{uuid ?? throw new ArgumentNullException()}",
ResponseHandler = uuidProfileResponseHandler
});

private async Task<UserProfile> uuidProfileResponseHandler(HttpResponseMessage response)
private async Task<PlayerProfile> uuidProfileResponseHandler(HttpResponseMessage response)
{
string responseContent = await response.Content.ReadAsStringAsync();
JObject job = JObject.Parse(responseContent);
Expand Down Expand Up @@ -128,7 +128,7 @@ private async Task<UserProfile> uuidProfileResponseHandler(HttpResponseMessage r
skin = new Skin(null, defaultSkinType);
}

return new UserProfile
return new PlayerProfile
{
UUID = uuid ?? "",
Name = name,
Expand All @@ -137,8 +137,8 @@ private async Task<UserProfile> uuidProfileResponseHandler(HttpResponseMessage r
};
}

public Task<UserProfile> GetProfileUsingAccessToken(string accessToken) =>
client.SendActionAsync(new HttpAction<UserProfile>
public Task<PlayerProfile> GetProfileUsingAccessToken(string accessToken) =>
client.SendActionAsync(new HttpAction<PlayerProfile>
{
Method = HttpMethod.Get,
Host = "https://api.minecraftservices.com",
Expand All @@ -152,14 +152,14 @@ public Task<UserProfile> GetProfileUsingAccessToken(string accessToken) =>
ResponseHandler = atProfileResponseHandler
});

private async Task<UserProfile> atProfileResponseHandler(HttpResponseMessage response)
private async Task<PlayerProfile> atProfileResponseHandler(HttpResponseMessage response)
{
string responseContent = await response.Content.ReadAsStringAsync();
JObject job = JObject.Parse(responseContent);

var skinObj = job["skins"]?[0];

return new UserProfile
return new PlayerProfile
{
UUID = job["id"]?.ToString() ?? "",
Name = job["name"]?.ToString() ?? "",
Expand All @@ -172,12 +172,12 @@ private async Task<UserProfile> atProfileResponseHandler(HttpResponseMessage res
};
}

public Task<UserProfile> ChangeName(string accessToken, string newName) =>
client.SendActionAsync(new HttpAction<UserProfile>
public Task<PlayerProfile> ChangeName(string accessToken, string newName) =>
client.SendActionAsync(new HttpAction<PlayerProfile>
{
Method = HttpMethod.Put,
//Host = "https://api.minecraftservices.com",
Host = "http://localhost:7777",
Host = "https://api.minecraftservices.com",
//Host = "http://localhost:7777",
Path = $"minecraft/profile/name/{newName}",

RequestHeaders = new HttpHeaderCollection
Expand All @@ -186,7 +186,7 @@ public Task<UserProfile> ChangeName(string accessToken, string newName) =>
},

ResponseHandler = atProfileResponseHandler,
ErrorHandler = HttpResponseHandlers.GetJsonErrorHandler<UserProfile>(),
ErrorHandler = HttpResponseHandlers.GetJsonErrorHandler<PlayerProfile>(),

CheckValidation = (h) =>
{
Expand All @@ -200,8 +200,8 @@ public Task<MojangAPIResponse> ChangeSkin(string uuid, string accessToken, SkinT
client.SendActionAsync(new HttpAction<MojangAPIResponse>
{
Method = HttpMethod.Post,
//Host = "https://api.mojang.com",
Host = "http://localhost:7777",
Host = "https://api.mojang.com",
//Host = "http://localhost:7777",
Path = $"user/profile/{uuid}/skin",

RequestHeaders = new HttpHeaderCollection
Expand Down Expand Up @@ -237,13 +237,13 @@ public Task<MojangAPIResponse> UploadSkin(string accessToken, SkinType skinType,
Stream fileStream = File.OpenRead(skinPath);
return UploadSkin(accessToken, skinType, fileStream, filename);
}

public Task<MojangAPIResponse> UploadSkin(string accessToken, SkinType skinType, Stream skinStream, string filename) =>
client.SendActionAsync(new HttpAction<MojangAPIResponse>
{
Method = HttpMethod.Put,
//Host = "https://api.minecraftservices.com",
Host = "http://localhost:7777",
Host = "https://api.minecraftservices.com",
//Host = "http://localhost:7777",
Path = "minecraft/profile/skins",

RequestHeaders = new HttpHeaderCollection
Expand Down Expand Up @@ -277,8 +277,8 @@ public Task<MojangAPIResponse> ResetSkin(string uuid, string accessToken) =>
client.SendActionAsync(new HttpAction<MojangAPIResponse>
{
Method = HttpMethod.Delete,
//Host = "https://api.mojang.com",
Host = "http://localhost:7777",
Host = "https://api.mojang.com",
//Host = "http://localhost:7777",
Path = $"user/profile/{uuid}/skin",

RequestHeaders = new HttpHeaderCollection
Expand Down
2 changes: 1 addition & 1 deletion MojangAPI/MojangAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<LangVersion>8.0</LangVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Description>Mojang API, Authenticate</Description>
<PackageLicenseExpression>mit</PackageLicenseExpression>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryUrl>https://github.com/AlphaBs/MojangAPI</RepositoryUrl>
</PropertyGroup>

Expand Down
4 changes: 2 additions & 2 deletions MojangAPI/MojangAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public Task<MojangAuthResponse> Signout(string email, string password) =>
ErrorHandler = errorResponseHandler
});

public Task<MojangAuthResponse> Invalidte()
public Task<MojangAuthResponse> Invalidate()
{
Session? cachedSession = cacheManager?.ReadCache();
if (cachedSession == null || string.IsNullOrEmpty(cachedSession.AccessToken))
Expand Down Expand Up @@ -326,7 +326,7 @@ public async Task<MojangAuthResponse> RequestSessionWithXbox(string uhs, string
if (!await mojang.CheckGameOwnership(authResponse.AccessToken!))
return new MojangAuthResponse(MojangAuthResult.NoProfile);

UserProfile profile = await mojang.GetProfileUsingAccessToken(authResponse.AccessToken!);
PlayerProfile profile = await mojang.GetProfileUsingAccessToken(authResponse.AccessToken!);
Session session = new Session
{
Username = profile.Name,
Expand Down
21 changes: 10 additions & 11 deletions MojangAPISample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace MojangAPISample
{
class Program
{
static HttpClient httpClient = new HttpClient();
static readonly HttpClient httpClient = new HttpClient();

static async Task Main(string[] args)
{
Expand All @@ -35,7 +35,7 @@ static async Task Main(string[] args)
static async Task testMojangAPIs(Mojang mojang, Session session)
{
Console.WriteLine("GetUUID");
UserUUID uuid = await mojang.GetUUID(session.Username);
PlayerUUID uuid = await mojang.GetUUID(session.Username);
printResponse(uuid);
Console.WriteLine($"UUID: {uuid.UUID}, IsLegacy: {uuid.IsLegacy}, IsDemo: {uuid.IsDemo}\n");

Expand All @@ -53,13 +53,13 @@ static async Task testMojangAPIs(Mojang mojang, Session session)
Console.WriteLine();

Console.WriteLine("GetProfileUsingUUID");
UserProfile uuidProfile = await mojang.GetProfileUsingUUID(session.UUID);
PlayerProfile uuidProfile = await mojang.GetProfileUsingUUID(session.UUID);
printResponse(uuidProfile);
printProfile(uuidProfile);
Console.WriteLine();

Console.WriteLine("GetProfileUsingAccessToken");
UserProfile atProfile = await mojang.GetProfileUsingAccessToken(session.AccessToken);
PlayerProfile atProfile = await mojang.GetProfileUsingAccessToken(session.AccessToken);
printResponse(atProfile);
printProfile(atProfile);
Console.WriteLine();
Expand Down Expand Up @@ -145,15 +145,14 @@ static async Task testSecurityFlow(QuestionFlow q, Session session)
throw new Exception("failed to get questions");

QuestionList questions = res.Questions;
int qCount = 1;
foreach (var item in questions)
for (int i = 0; i < questions.Count; i++)
{
Console.WriteLine($"Q{qCount}. [{item.QuestionId}] {item.QuestionMessage}");
Question question = questions[i];
Console.WriteLine($"Q{i + 1}. [{question.QuestionId}] {question.QuestionMessage}");
Console.Write("Answer? : ");

var answer = Console.ReadLine();
item.Answer = answer;
qCount++;
question.Answer = answer;
Console.WriteLine();
}

Expand All @@ -169,7 +168,7 @@ static async Task testSecurityFlow(QuestionFlow q, Session session)
static async Task test_DANGEROUS_apis(Mojang mojang, Session session)
{
Console.WriteLine("ChangeName");
UserProfile changeNameProfile = await mojang.ChangeName(session.AccessToken, "NEWNAME");
PlayerProfile changeNameProfile = await mojang.ChangeName(session.AccessToken, "NEWNAME");
printResponse(changeNameProfile);
printProfile(changeNameProfile);

Expand All @@ -196,7 +195,7 @@ static void printResponse(MojangAPIResponse res)

}

static void printProfile(UserProfile profile)
static void printProfile(PlayerProfile profile)
{
if (!profile.IsSuccess)
return;
Expand Down

0 comments on commit 8fa5ac3

Please sign in to comment.