Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ported to .Net standard #22

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions DuoApi.Examples/DuoApi.Examples.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\DuoApi\DuoApi.csproj" />
</ItemGroup>

</Project>
20 changes: 7 additions & 13 deletions examples/Program.cs → DuoApi.Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ static int Main(string[] args)
var r = client.JSONApiCall<Dictionary<string, object>>(
"GET", "/admin/v1/info/authentication_attempts", parameters);
var attempts = r["authentication_attempts"] as Dictionary<string, object>;
foreach (KeyValuePair<string, object> info in attempts)
{
foreach (KeyValuePair<string, object> info in attempts) {
var s = String.Format("{0} authentication(s) ended with {1}.",
info.Value,
info.Key);
Expand All @@ -35,28 +34,23 @@ static int Main(string[] args)
var users = client.JSONApiCall<System.Collections.ArrayList>(
"GET", "/admin/v1/users", parameters);
System.Console.WriteLine(String.Format("{0} users.", users.Count));
foreach (Dictionary<string, object> user in users)
{
foreach (Dictionary<string, object> user in users) {
System.Console.WriteLine(
"\t" + "Username: " + (user["username"] as string));
}

// paging call
int? offset = 0;
while (offset != null)
{
var jsonResponse = client.JSONPagingApiCall("GET", "/admin/v1/users", parameters, (int)offset, 10);
var pagedUsers = jsonResponse["response"] as System.Collections.ArrayList;
while (offset != null) {
var pagedUsers = client.JSONPagingApiCall<System.Collections.ArrayList>("GET", "/admin/v1/users", parameters, (int)offset, 10, out var metadata);
System.Console.WriteLine(String.Format("{0} users at offset {1}", pagedUsers.Count, offset));
foreach (Dictionary<string, object> user in pagedUsers)
{
foreach (Dictionary<string, object> user in pagedUsers) {
System.Console.WriteLine(
"\t" + "Username: " + (user["username"] as string));
}
var metadata = jsonResponse["metadata"] as Dictionary<string, object>;
if (metadata.ContainsKey("next_offset"))
if (metadata.next_offset.HasValue)
{
offset = metadata["next_offset"] as int?;
offset = metadata.next_offset.Value;
}
else
{
Expand Down
18 changes: 9 additions & 9 deletions test/ApiCallTest.cs → DuoApi.Tests/ApiCallTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,10 @@ public void TestValidJsonPagingResponseNoParameters()
return "{\"stat\": \"OK\", \"response\": \"hello, world!\", \"metadata\": {\"next_offset\":10}}";
};
var parameters = new Dictionary<string, string>();
var jsonResponse = api.JSONPagingApiCall("GET", "/json_ok", parameters, 0, 10);
Assert.Equal("hello, world!", jsonResponse["response"]);
var metadata = jsonResponse["metadata"] as Dictionary<string, object>;
Assert.Equal(10, metadata["next_offset"]);
var jsonResponse = api.JSONPagingApiCall<string>("GET", "/json_ok", parameters, 0, 10, out var metadata);
Assert.Equal("hello, world!", jsonResponse);

Assert.Equal(10, metadata.next_offset);
// make sure parameters was not changed as a side-effect
Assert.Empty(parameters);
}
Expand All @@ -411,10 +411,10 @@ public void TestValidJsonPagingResponseExistingParameters()
{"offset", "0"},
{"limit", "10"}
};
var jsonResponse = api.JSONPagingApiCall("GET", "/json_ok", parameters, 10, 20);
Assert.Equal("hello, world!", jsonResponse["response"]);
var metadata = jsonResponse["metadata"] as Dictionary<string, object>;
Assert.False(metadata.ContainsKey("next_offset"));
var jsonResponse = api.JSONPagingApiCall<string>("GET", "/json_ok", parameters, 10, 20, out var metadata);
Assert.Equal("hello, world!", jsonResponse);

Assert.NotNull(metadata);
// make sure parameters was not changed as a side-effect
Assert.Equal(2, parameters.Count);
Assert.Equal("0", parameters["offset"]);
Expand Down Expand Up @@ -461,7 +461,7 @@ public void TestJsonResponseMissingField()
});

Assert.NotNull(ex);
var e = Assert.IsType<BadResponseException>(ex);
var e = Assert.IsType<ApiException>(ex);

Assert.Equal(400, e.HttpStatus);

Expand Down
File renamed without changes.
26 changes: 26 additions & 0 deletions DuoApi.Tests/DuoApi.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DuoApi\DuoApi.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,4 @@ public void NextOffsetTest()
var expected = "foo=1&next_offset=fjaewoifjew&next_offset=473891274832917498";
Assert.Equal(expected, DuoApi.CanonicalizeParams(parameters));
}
}
}
2 changes: 1 addition & 1 deletion test/SigningTest.cs → DuoApi.Tests/SigningTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ public void HmacSha512()
var expected = "Basic dGVzdF9pa2V5OjA1MDgwNjUwMzVhMDNiMmExZGUyZjQ1M2U2MjllNzkxZDE4MDMyOWUxNTdmNjVkZjZiM2UwZjA4Mjk5ZDQzMjFlMWM1YzdhN2M3ZWU2YjllNWZjODBkMWZiNmZiZjNhZDVlYjdjNDRkZDNiMzk4NWEwMmMzN2FjYTUzZWMzNjk4";
Assert.Equal(expected, actual);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ internal static X509CertificateCollection GetDuoCertCollection()
internal static string[] ReadCertsFromFile()
{
var certs = "";
using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("duo_api_csharp.ca_certs.pem"))
using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream($"{Assembly.GetExecutingAssembly().GetName().Name}.ca_certs.pem"))
using (StreamReader reader = new StreamReader(stream))
{
certs = reader.ReadToEnd();
Expand Down
43 changes: 43 additions & 0 deletions DuoApi/DataEnvelope.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.ComponentModel.DataAnnotations;

namespace Duo
{

/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
public class DataEnvelope<T>
{
/// <summary>
///
/// </summary>
[Required]
public DuoApiResponseStatus stat { get; set; }

/// <summary>
///
/// </summary>
public int? code { get; set; }

/// <summary>
///
/// </summary>
public T response { get; set; }

/// <summary>
/// Upon error, basic error information
/// </summary>
public string message { get; set; }

/// <summary>
/// Upon error, detailed error information
/// </summary>
public string message_detail { get; set; }

/// <summary>
///
/// </summary>
public PagingInfo metadata { get; set; }
}
}
Loading