-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improved login response deserialization
- Loading branch information
1 parent
f9b881d
commit caaa98c
Showing
5 changed files
with
32 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters