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

Replace IActionResult with HttpResponseData #45

Merged
merged 2 commits into from
Dec 26, 2023
Merged
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
18 changes: 12 additions & 6 deletions SSW.Rules.AzFuncs/Functions/AuthCMS/AuthenticateCMS.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System.Configuration;
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
using SSW.Rules.AzFuncs.helpers;

namespace SSW.Rules.AzFuncs.Functions.AuthCMS;

Expand All @@ -12,7 +14,7 @@ public class AuthenticateCms(ILoggerFactory loggerFactory)

// TODO: Find where this is called and update the name to be generic
[Function("AuthenticateNetlify")]
public IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "auth")] HttpRequestData req,
public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "auth")] HttpRequestData req,
FunctionContext executionContext)
{
_logger.LogInformation($"C# HTTP trigger function {nameof(AuthenticateCms)} processed a request.");
Expand All @@ -22,18 +24,22 @@ public IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route
if (string.IsNullOrEmpty(scope))
{
_logger.LogError("Missing scope param");
return new BadRequestObjectResult(new
return req.CreateJsonResponse(new
{
error = true,
message = "Missing scope param",
});
}, HttpStatusCode.BadRequest);
}

var clientId = Environment.GetEnvironmentVariable("CMS_OAUTH_CLIENT_ID", EnvironmentVariableTarget.Process);

if (!string.IsNullOrEmpty(clientId))
return new RedirectResult($"https://github.com/login/oauth/authorize?client_id={clientId}&scope={scope}",
true);

{
var response = req.CreateResponse(HttpStatusCode.MovedPermanently);
response.Headers.Add("Location", $"https://github.com/login/oauth/authorize?client_id={clientId}&scope={scope}");
return response;
}

_logger.LogError("Missing CMS_OAUTH_CLIENT_ID");
throw new ConfigurationErrorsException("Missing CMS_OAUTH_CLIENT_ID");

Expand Down
15 changes: 11 additions & 4 deletions SSW.Rules.AzFuncs/Functions/AuthCMS/CMSCallback.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System.Configuration;
using System.Net;
using System.Text;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using SSW.Rules.AzFuncs.helpers;
using static System.Formats.Asn1.AsnWriter;

namespace SSW.Rules.AzFuncs.Functions.AuthCMS;

Expand All @@ -14,7 +17,7 @@ public class CmsCallback(ILoggerFactory loggerFactory)

// TODO: Find where this is called and update the name to be generic
[Function("NetlifyCallback")]
public async Task<IActionResult> Run(
public async Task<HttpResponseData> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "callback")] HttpRequestData req,
FunctionContext executionContext)
{
Expand All @@ -26,10 +29,11 @@ public async Task<IActionResult> Run(
if (string.IsNullOrEmpty(code))
{
_logger.LogError("Missing code param");
return new BadRequestObjectResult(new
return req.CreateJsonResponse(new
{
error = true,
message = "Missing code param",
});
}, HttpStatusCode.BadRequest);
}

const string tokenUrl = "https://github.com/login/oauth/access_token";
Expand Down Expand Up @@ -93,7 +97,10 @@ function recieveMessage(e) {
})()
</script>";

return new ContentResult { Content = script, ContentType = "text/html" };
var res = req.CreateResponse(HttpStatusCode.OK);
res.Headers.Add("Content-Type", "text/html");
res.WriteString(script);
return res;
}
catch (HttpRequestException ex)
{
Expand Down
Loading