Skip to content

Commit

Permalink
Merge pull request #7 from maliming/0.4.0-net5
Browse files Browse the repository at this point in the history
Update version to 0.4.0 & Use system.text.json instead of newtonsoft.
  • Loading branch information
maliming authored Nov 11, 2020
2 parents 722e15a + f262926 commit 45297b2
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 51 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dotnetcore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.100
dotnet-version: 5.0.100
- name: Build with dotnet
run: dotnet build -c Release
- name: Test with dotnet
Expand Down
13 changes: 8 additions & 5 deletions demo/reCAPTCHA.Demo/Pages/V2_Checkbox.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Threading.Tasks;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Newtonsoft.Json;
using Owl.reCAPTCHA;
using Owl.reCAPTCHA.v2;

Expand All @@ -22,10 +22,13 @@ public async Task OnPostAsync(string token)
var response = await _siteVerify.Verify(new reCAPTCHASiteVerifyRequest
{
Response = token,
RemoteIp = HttpContext.Connection.RemoteIpAddress.ToString()
RemoteIp = HttpContext.Connection.RemoteIpAddress?.ToString()
});

Result = JsonConvert.SerializeObject(response, Formatting.Indented);
Result = JsonSerializer.Serialize(response, new JsonSerializerOptions
{
WriteIndented = true
});
}
}
}
}
13 changes: 8 additions & 5 deletions demo/reCAPTCHA.Demo/Pages/V2_Invisible.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Threading.Tasks;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Newtonsoft.Json;
using Owl.reCAPTCHA;
using Owl.reCAPTCHA.v2;

Expand All @@ -22,10 +22,13 @@ public async Task OnPostAsync(string token)
var response = await _siteVerify.Verify(new reCAPTCHASiteVerifyRequest
{
Response = token,
RemoteIp = HttpContext.Connection.RemoteIpAddress.ToString()
RemoteIp = HttpContext.Connection.RemoteIpAddress?.ToString()
});

Result = JsonConvert.SerializeObject(response, Formatting.Indented);
Result = JsonSerializer.Serialize(response, new JsonSerializerOptions
{
WriteIndented = true
});
}
}
}
}
13 changes: 8 additions & 5 deletions demo/reCAPTCHA.Demo/Pages/V3.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Threading.Tasks;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Newtonsoft.Json;
using Owl.reCAPTCHA;
using Owl.reCAPTCHA.v3;

Expand All @@ -22,10 +22,13 @@ public async Task OnPostAsync(string token)
var response = await _siteVerify.Verify(new reCAPTCHASiteVerifyRequest
{
Response = token,
RemoteIp = HttpContext.Connection.RemoteIpAddress.ToString()
RemoteIp = HttpContext.Connection.RemoteIpAddress?.ToString()
});

Result = JsonConvert.SerializeObject(response, Formatting.Indented);
Result = JsonSerializer.Serialize(response, new JsonSerializerOptions
{
WriteIndented = true
});
}
}
}
}
11 changes: 7 additions & 4 deletions demo/reCAPTCHA.Demo/Pages/V3_Programmatically.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Threading.Tasks;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Newtonsoft.Json;
using Owl.reCAPTCHA;
using Owl.reCAPTCHA.v3;

Expand All @@ -22,10 +22,13 @@ public async Task OnPostAsync(string token)
var response = await _siteVerify.Verify(new reCAPTCHASiteVerifyRequest
{
Response = token,
RemoteIp = HttpContext.Connection.RemoteIpAddress.ToString()
RemoteIp = HttpContext.Connection.RemoteIpAddress?.ToString()
});

Result = JsonConvert.SerializeObject(response, Formatting.Indented);
Result = JsonSerializer.Serialize(response, new JsonSerializerOptions
{
WriteIndented = true
});
}
}
}
2 changes: 1 addition & 1 deletion demo/reCAPTCHA.Demo/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public Startup(IConfiguration configuration)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages().AddRazorRuntimeCompilation();
services.AddRazorPages();

services.AddreCAPTCHAV3(x =>
{
Expand Down
10 changes: 0 additions & 10 deletions demo/reCAPTCHA.Demo/reCAPTCHA.Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="5.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Owl.reCAPTCHA\Owl.reCAPTCHA.csproj" />
</ItemGroup>
Expand Down
12 changes: 4 additions & 8 deletions src/Owl.reCAPTCHA/Owl.reCAPTCHA.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,20 @@
<RootNamespace>Owl.reCAPTCHA</RootNamespace>
<PackageId>Owl.reCAPTCHA</PackageId>
<Authors>maliming</Authors>
<Description>reCAPTCHA for Asp Net Core 3.0</Description>
<Description>reCAPTCHA for Asp Net Core 5.0</Description>
<PackageProjectUrl>https://github.com/maliming/reCAPTCHA</PackageProjectUrl>
<RepositoryUrl>https://github.com/maliming/reCAPTCHA</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<Version>0.3.0</Version>
<Version>0.4.0</Version>
<IsPackable>true</IsPackable>
<OutputType>Library</OutputType>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<!-- including PDB files in NuGet for source link because symbolsource.org does not support portable PDBs -->
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
<PackageVersion>0.3.1</PackageVersion>
<PackageVersion>0.4.0</PackageVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.0-beta-20204-02" PrivateAssets="All" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="5.0.0" />
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

</Project>
5 changes: 1 addition & 4 deletions src/Owl.reCAPTCHA/reCAPTCHASiteVerifyResponse.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Newtonsoft.Json;

namespace Owl.reCAPTCHA
{
Expand All @@ -9,13 +8,11 @@ public class reCAPTCHASiteVerifyResponse
public bool Success { get; set; }

// timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)
[JsonProperty("challenge_ts")]
public DateTime ChallengeTs { get; set; }

// the hostname of the site where the reCAPTCHA was solved
public string HostName { get; set; }

[JsonProperty("error-codes")]
public string[] ErrorCodes { get; set; }
}
}
}
60 changes: 60 additions & 0 deletions src/Owl.reCAPTCHA/reCAPTCHASiteVerifyResponseJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using Owl.reCAPTCHA.v3;

namespace Owl.reCAPTCHA
{
public class reCAPTCHASiteVerifyResponseJsonConverter : JsonConverter<reCAPTCHASiteVerifyResponse>
{
public override bool CanConvert(Type typeToConvert)
{
return typeToConvert == typeof(reCAPTCHASiteVerifyV3Response) ||
typeToConvert == typeof(reCAPTCHASiteVerifyResponse);
}

public override reCAPTCHASiteVerifyResponse Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var rootElement = JsonDocument.ParseValue(ref reader).RootElement;

var success = rootElement.EnumerateObject().FirstOrDefault(x => x.Name.Equals("success", StringComparison.InvariantCultureIgnoreCase));
var challengeTimestamp = rootElement.EnumerateObject().FirstOrDefault(x => x.Name.Equals("challenge_ts", StringComparison.InvariantCultureIgnoreCase));
var hostname = rootElement.EnumerateObject().FirstOrDefault(x => x.Name.Equals("hostname", StringComparison.InvariantCultureIgnoreCase));
var errorCodes = rootElement.EnumerateObject().FirstOrDefault(x => x.Name.Equals("error-codes", StringComparison.InvariantCultureIgnoreCase));

if (typeToConvert == typeof(reCAPTCHASiteVerifyResponse))
{
return new reCAPTCHASiteVerifyResponse
{
Success = success.Value.ValueKind == JsonValueKind.True,
ChallengeTs = challengeTimestamp.Value.ValueKind == JsonValueKind.String ? challengeTimestamp.Value.GetDateTime() : DateTime.MinValue,
HostName = hostname.Value.ValueKind == JsonValueKind.String ? hostname.Value.GetString() : null,
ErrorCodes = errorCodes.Value.ValueKind == JsonValueKind.Array ? JsonSerializer.Deserialize<string[]>(errorCodes.Value.GetRawText()) : null
};
}

if (typeToConvert == typeof(reCAPTCHASiteVerifyV3Response))
{
var score = rootElement.EnumerateObject().FirstOrDefault(x => x.Name.Equals("score", StringComparison.InvariantCultureIgnoreCase));
var action = rootElement.EnumerateObject().FirstOrDefault(x => x.Name.Equals("action", StringComparison.InvariantCultureIgnoreCase));
return new reCAPTCHASiteVerifyV3Response
{
Success = success.Value.ValueKind == JsonValueKind.True,
ChallengeTs = challengeTimestamp.Value.ValueKind == JsonValueKind.String ? challengeTimestamp.Value.GetDateTime() : DateTime.MinValue,
HostName = hostname.Value.ValueKind == JsonValueKind.String ? hostname.Value.GetString() : null,
ErrorCodes = errorCodes.Value.ValueKind == JsonValueKind.Array ? JsonSerializer.Deserialize<string[]>(errorCodes.Value.GetRawText()) : null,
Score = score.Value.ValueKind == JsonValueKind.Number ? score.Value.GetSingle() : 0,
Action = action.Value.ValueKind == JsonValueKind.String ? action.Value.GetString() : null
};
}

throw new JsonException($"Incorrect type: {typeToConvert.FullName}");
}

public override void Write(Utf8JsonWriter writer, reCAPTCHASiteVerifyResponse value, JsonSerializerOptions options)
{
JsonSerializer.Serialize(writer, value);
}
}
}
9 changes: 5 additions & 4 deletions src/Owl.reCAPTCHA/v2/reCAPTCHASiteVerifyV2.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;

namespace Owl.reCAPTCHA.v2
{
Expand Down Expand Up @@ -31,8 +31,9 @@ public async Task<reCAPTCHASiteVerifyResponse> Verify(reCAPTCHASiteVerifyRequest
var v3Response = await _client.PostAsync("recaptcha/api/siteverify", content);
if (v3Response.IsSuccessStatusCode)
{
return JsonConvert.DeserializeObject<reCAPTCHASiteVerifyResponse>(
await v3Response.Content.ReadAsStringAsync());
var options = new JsonSerializerOptions();
options.Converters.Add(new reCAPTCHASiteVerifyResponseJsonConverter());
return JsonSerializer.Deserialize<reCAPTCHASiteVerifyResponse>(await v3Response.Content.ReadAsStringAsync(), options);
}

return new reCAPTCHASiteVerifyResponse
Expand All @@ -45,4 +46,4 @@ public async Task<reCAPTCHASiteVerifyResponse> Verify(reCAPTCHASiteVerifyRequest
};
}
}
}
}
9 changes: 5 additions & 4 deletions src/Owl.reCAPTCHA/v3/reCAPTCHASiteVerifyV3.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;

namespace Owl.reCAPTCHA.v3
{
Expand Down Expand Up @@ -31,8 +31,9 @@ public async Task<reCAPTCHASiteVerifyV3Response> Verify(reCAPTCHASiteVerifyReque
var v3Response = await _client.PostAsync("recaptcha/api/siteverify", content);
if (v3Response.IsSuccessStatusCode)
{
return JsonConvert.DeserializeObject<reCAPTCHASiteVerifyV3Response>(
await v3Response.Content.ReadAsStringAsync());
var options = new JsonSerializerOptions();
options.Converters.Add(new reCAPTCHASiteVerifyResponseJsonConverter());
return JsonSerializer.Deserialize<reCAPTCHASiteVerifyV3Response>(await v3Response.Content.ReadAsStringAsync(), options);
}

return new reCAPTCHASiteVerifyV3Response
Expand All @@ -45,4 +46,4 @@ public async Task<reCAPTCHASiteVerifyV3Response> Verify(reCAPTCHASiteVerifyReque
};
}
}
}
}

0 comments on commit 45297b2

Please sign in to comment.