Skip to content

Commit

Permalink
improved login response deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
devhl-labs committed Aug 13, 2023
1 parent f9b881d commit caaa98c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 25 deletions.
4 changes: 4 additions & 0 deletions docs/templates/netcore_project.additions.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.32.1" />
</ItemGroup>
4 changes: 4 additions & 0 deletions src/CocApi.Rest/CocApi.Rest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.32.1" />
</ItemGroup></Project>
35 changes: 16 additions & 19 deletions src/CocApi.Rest/Models/LoginResponse.Manual.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Text.Json.Serialization;

namespace CocApi.Rest.Models
{
public partial class LoginResponse
{
public string IpAddress()
internal class IpAddressLimit
{
string part = TemporaryAPIToken.Split(".")[1];
string decoded = Encoding.UTF8.GetString(Convert.FromBase64String(part));
var o = System.Text.Json.JsonSerializer.Deserialize<TokenDetails>(decoded)!;
var result = o.Limits[1].Cidrs[0].Split("/")[0];
return result;
}
}
[JsonPropertyName("cidrs")]
public string[] Cidrs { get; set; } = Array.Empty<string>();

internal class TokenLimits
{
[JsonPropertyName("cidrs")]
public List<string> Cidrs { get; set; } = new();
}
[JsonPropertyName("type")]
public string Type { get; set; } = string.Empty;
}

internal class TokenDetails
{
[JsonPropertyName("limits")]
public List<TokenLimits> Limits { get; set; } = new();
public string[] IpAddresses()
{
JwtSecurityTokenHandler handler = new();
JwtSecurityToken jwtSecurityToken = handler.ReadJwtToken(TemporaryAPIToken);
System.Security.Claims.Claim limits = jwtSecurityToken.Claims.First(c => c.Value.Contains("cidrs"));
IpAddressLimit? ipAddressLimit = System.Text.Json.JsonSerializer.Deserialize<IpAddressLimit>(limits.Value);
return ipAddressLimit == null ? throw new Exception("Could not deserialize the response.") : ipAddressLimit.Cidrs;
}
}
}
2 changes: 1 addition & 1 deletion src/CocApi.Rest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ Authentication schemes defined for the API:

## Build
- SDK version: 2.8.1
- Build date: 2023-07-16T15:58:57.341934-04:00[America/New_York]
- Build date: 2023-08-13T16:25:25.138597100-04:00[America/New_York]
- Build package: org.openapitools.codegen.languages.CSharpClientCodegen

## Api Information
Expand Down
12 changes: 7 additions & 5 deletions src/CocApi.Test/TokenService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,24 @@ public TokenService(
public async Task StartAsync(CancellationToken cancellationToken)
{
// login and save the cookie to the CookieContainer
var login = await DeveloperApi.LoginAsync(Options.Value);
var login = await DeveloperApi.LoginAsync(Options.Value, cancellationToken);
var rawValue = login.Headers.GetValues("set-cookie").Single();
var encodedValue = System.Web.HttpUtility.UrlEncode(rawValue, Encoding.UTF8);
var domain = new System.Uri("https://developer.clashofclans.com/api");
var cookie = new System.Net.Cookie("session", encodedValue, null, domain.Host);
CookieContainer.Value.Add(domain, cookie);

// Create a testing token
var token = new CreateTokenRequest(new List<string> { login.AsModel()!.IpAddress() }, "test description", "test name");
var createResponse = await DeveloperApi.CreateAsync(token);
string ipAddressWithMask = login.AsModel()!.IpAddresses()[0];
string ipAddress = ipAddressWithMask[..ipAddressWithMask.IndexOf("/")];
var token = new CreateTokenRequest(new List<string> { ipAddress }, "test description", "test name");
var createResponse = await DeveloperApi.CreateAsync(token, cancellationToken);

// delete that testing token
var deleteResponse = await DeveloperApi.RevokeAsync(createResponse.AsModel()!.Key!);
var deleteResponse = await DeveloperApi.RevokeAsync(createResponse.AsModel()!.Key!, cancellationToken);

// query all keys
var keys = await DeveloperApi.KeysAsync();
var keys = await DeveloperApi.KeysAsync(cancellationToken);
}

public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
Expand Down

0 comments on commit caaa98c

Please sign in to comment.