Skip to content

Commit 2492342

Browse files
committed
Updating actor helper
1 parent 286cd6d commit 2492342

File tree

1 file changed

+61
-20
lines changed

1 file changed

+61
-20
lines changed

src/ActivityPubDotNet.Core/ActorHelper.cs

100644100755
Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,16 @@ public class ActorHelper(string privatePem, string keyId, ILogger? logger = null
1818
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
1919
};
2020

21-
public static async Task<Actor> FetchActorInformationAsync(string actorUrl)
21+
public async Task<ActivityPubActor> FetchActorInformationAsync(string actorUrl)
2222
{
23-
using (HttpClient httpClient = new HttpClient())
24-
{
25-
httpClient.DefaultRequestHeaders.Add("Accept", "application/activity+json");
26-
27-
HttpResponseMessage response = await httpClient.GetAsync(actorUrl);
28-
29-
var options = new JsonSerializerOptions
30-
{
31-
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
32-
};
23+
var jsonContent = await SendGetSignedRequest(new Uri(actorUrl));
3324

34-
if (response.IsSuccessStatusCode)
35-
{
36-
string jsonContent = await response.Content.ReadAsStringAsync();
37-
return JsonSerializer.Deserialize<Actor>(jsonContent, options)!;
38-
}
25+
var options = new JsonSerializerOptions
26+
{
27+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
28+
};
3929

40-
throw new Exception($"Failed to fetch information from {actorUrl}");
41-
}
30+
return JsonSerializer.Deserialize<ActivityPubActor>(jsonContent, options)!;
4231
}
4332

4433
static string CreateHashSha256(string input)
@@ -49,15 +38,65 @@ static string CreateHashSha256(string input)
4938
return Convert.ToBase64String(hashBytes);
5039
}
5140
}
41+
public async Task<string> SendGetSignedRequest(Uri url)
42+
{
43+
Console.WriteLine($"Sending GET request to {url}");
44+
45+
// Get current UTC date in HTTP format
46+
string date = DateTime.UtcNow.ToString("r");
47+
48+
// Load RSA private key from file
49+
using (RSA rsa = RSA.Create())
50+
{
51+
rsa.ImportFromPem(this._privatePem);
52+
53+
// Build the to-be-signed string
54+
string signedString = $"(request-target): get {url.AbsolutePath}\nhost: {url.Host}\ndate: {date}";
55+
56+
// Sign the to-be-signed string
57+
byte[] signatureBytes = rsa.SignData(Encoding.UTF8.GetBytes(signedString), HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
58+
59+
// Base64 encode the signature
60+
string signature = Convert.ToBase64String(signatureBytes);
5261

53-
public async Task SendSignedRequest(string document, Uri url)
62+
// Build the HTTP signature header
63+
string header = $"keyId=\"{this._keyId}\",headers=\"(request-target) host date\",signature=\"{signature}\",algorithm=\"rsa-sha256\"";
64+
65+
// Create HTTP client
66+
using (HttpClient client = new HttpClient())
67+
{
68+
// Set request headers
69+
client.DefaultRequestHeaders.Add("Host", url.Host);
70+
client.DefaultRequestHeaders.Add("Date", date);
71+
client.DefaultRequestHeaders.Add("Signature", header);
72+
client.DefaultRequestHeaders.Add("Accept", "application/activity+json");
73+
74+
// Make the GET request
75+
var response = await client.GetAsync(url);
76+
77+
response.EnsureSuccessStatusCode();
78+
79+
// Print the response
80+
var responseString = await response.Content.ReadAsStringAsync();
81+
82+
Logger?.LogInformation($"Response {response.StatusCode} - {responseString}");
83+
84+
return responseString;
85+
}
86+
}
87+
}
88+
89+
public async Task SendPostSignedRequest(string document, Uri url)
5490
{
91+
Console.WriteLine($"Sending POST request to {url}");
92+
5593
// Get current UTC date in HTTP format
5694
string date = DateTime.UtcNow.ToString("r");
5795

5896
// Load RSA private key from file
5997
using (RSA rsa = RSA.Create())
6098
{
99+
61100
rsa.ImportFromPem(this._privatePem);
62101

63102
string digest = $"SHA-256={CreateHashSha256(document)}";
@@ -93,6 +132,8 @@ public async Task SendSignedRequest(string document, Uri url)
93132
// Make the POST request
94133
var response = await client.PostAsync(url, new StringContent(document, Encoding.UTF8, "application/activity+json"));
95134

135+
response.EnsureSuccessStatusCode();
136+
96137
// Print the response
97138
var responseString = await response.Content.ReadAsStringAsync();
98139

@@ -101,4 +142,4 @@ public async Task SendSignedRequest(string document, Uri url)
101142
}
102143
}
103144
}
104-
}
145+
}

0 commit comments

Comments
 (0)