Skip to content

Commit

Permalink
Merge pull request #45 from SSWConsulting/fix-upgrade-bug
Browse files Browse the repository at this point in the history
Replace IActionResult with HttpResponseData
  • Loading branch information
Aibono1225 authored Dec 26, 2023
2 parents fd6fa1f + be3625f commit e572751
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
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

0 comments on commit e572751

Please sign in to comment.