Skip to content

Commit

Permalink
Refactoring HTTP APIs (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
shibayan authored Apr 16, 2020
1 parent 6dc8a9f commit 2ff36c7
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 20 deletions.
25 changes: 14 additions & 11 deletions KeyVault.Acmebot/AddCertificateFunctions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Threading.Tasks;

using Azure.WebJobs.Extensions.HttpApi;

using KeyVault.Acmebot.Models;

using Microsoft.AspNetCore.Http;
Expand All @@ -9,36 +11,37 @@
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;

using Newtonsoft.Json;

namespace KeyVault.Acmebot
{
public class AddCertificateFunctions
public class AddCertificateFunctions : HttpFunctionBase
{
public AddCertificateFunctions(IHttpContextAccessor httpContextAccessor)
: base(httpContextAccessor)
{
}

[FunctionName(nameof(AddCertificate_HttpStart))]
public async Task<IActionResult> AddCertificate_HttpStart(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "add-certificate")] HttpRequest req,
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "add-certificate")] AddCertificateRequest request,
[DurableClient] IDurableClient starter,
ILogger log)
{
if (!req.HttpContext.User.Identity.IsAuthenticated)
if (!User.Identity.IsAuthenticated)
{
return new UnauthorizedResult();
return Unauthorized();
}

var request = JsonConvert.DeserializeObject<AddCertificateRequest>(await req.ReadAsStringAsync());

if (request?.Domains == null || request.Domains.Length == 0)
if (!TryValidateModel(request))
{
return new BadRequestObjectResult($"{nameof(request.Domains)} is empty.");
return BadRequest(ModelState);
}

// Function input comes from the request content.
var instanceId = await starter.StartNewAsync(nameof(SharedFunctions.IssueCertificate), request.Domains);

log.LogInformation($"Started orchestration with ID = '{instanceId}'.");

return starter.CreateCheckStatusResponse(req, instanceId, true);
return starter.CreateCheckStatusResponse(Request, instanceId, true);
}
}
}
13 changes: 10 additions & 3 deletions KeyVault.Acmebot/GetDnsZonesFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Linq;
using System.Threading.Tasks;

using Azure.WebJobs.Extensions.HttpApi;

using DurableTask.TypedProxy;

using KeyVault.Acmebot.Contracts;
Expand All @@ -16,8 +18,13 @@

namespace KeyVault.Acmebot
{
public class GetDnsZonesFunctions
public class GetDnsZonesFunctions : HttpFunctionBase
{
public GetDnsZonesFunctions(IHttpContextAccessor httpContextAccessor)
: base(httpContextAccessor)
{
}

[FunctionName(nameof(GetDnsZones))]
public async Task<IList<string>> GetDnsZones([OrchestrationTrigger] IDurableOrchestrationContext context)
{
Expand All @@ -34,9 +41,9 @@ public async Task<IActionResult> GetDnsZones_HttpStart(
[DurableClient] IDurableClient starter,
ILogger log)
{
if (!req.HttpContext.User.Identity.IsAuthenticated)
if (!User.Identity.IsAuthenticated)
{
return new UnauthorizedResult();
return Unauthorized();
}

// Function input comes from the request content.
Expand Down
4 changes: 2 additions & 2 deletions KeyVault.Acmebot/KeyVault.Acmebot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
<PackageReference Include="Microsoft.Azure.KeyVault" Version="3.0.5" />
<PackageReference Include="Microsoft.Azure.Management.Dns" Version="3.0.1" />
<PackageReference Include="Microsoft.Azure.Services.AppAuthentication" Version="1.4.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.DurableTask" Version="2.2.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.DurableTask" Version="2.2.1" />
<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.3" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.5" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.6" />
<PackageReference Include="WebJobs.Extensions.HttpApi" Version="1.0.2" />
</ItemGroup>
<ItemGroup>
Expand Down
15 changes: 13 additions & 2 deletions KeyVault.Acmebot/Models/AddCertificateRequest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
namespace KeyVault.Acmebot.Models
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace KeyVault.Acmebot.Models
{
public class AddCertificateRequest
public class AddCertificateRequest : IValidatableObject
{
public string[] Domains { get; set; }

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (Domains == null || Domains.Length == 0)
{
yield return new ValidationResult($"The {nameof(Domains)} is required.", new[] { nameof(Domains) });
}
}
}
}
5 changes: 5 additions & 0 deletions KeyVault.Acmebot/StaticPageFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public IActionResult StaticPage(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "static-page/index")] HttpRequest req,
ILogger log)
{
if (!User.Identity.IsAuthenticated)
{
return Forbid();
}

return File("index.html", "text/html");
}
}
Expand Down
11 changes: 9 additions & 2 deletions KeyVault.Acmebot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,22 @@ <h1 class="title">
}

this.sending = true;
var response;

var response = await axios.post("/api/add-certificate", { Domains: this.domains });
try {
response = await axios.post("/api/add-certificate", { Domains: this.domains });
} catch (error) {
alert("An error has occurred. No certificate was issued.");
this.sending = false;
return;
}

while (true) {
await delay(5000);

try {
response = await axios.get(response.headers["location"]);
} catch {
} catch (error) {
alert("An error has occurred. No certificate was issued.");
break;
}
Expand Down

0 comments on commit 2ff36c7

Please sign in to comment.