-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
113 changed files
with
4,388 additions
and
2,449 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
ANAConversationPlatform/Attributes/BasicAuthenticationAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using ANAConversationPlatform.Helpers; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace ANAConversationPlatform.Attributes | ||
{ | ||
public class BasicAuthenticationAttribute : ActionFilterAttribute | ||
{ | ||
public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) | ||
{ | ||
if (Utils.BasicAuth == null || string.IsNullOrWhiteSpace(Utils.BasicAuth.APIKey) || string.IsNullOrWhiteSpace(Utils.BasicAuth.APISecret)) | ||
{ | ||
await next(); | ||
return; | ||
} | ||
|
||
var headers = context.HttpContext.Request.Headers; | ||
var authHeader = headers["Authorization"].FirstOrDefault(); | ||
if (string.IsNullOrWhiteSpace(authHeader)) | ||
{ | ||
context.HttpContext.Response.StatusCode = 401; | ||
return; | ||
} | ||
var savedAuthBase64 = Utils.BasicAuth.GetBase64(); | ||
var givenAuthBase64 = authHeader.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries).Skip(1).FirstOrDefault()?.Trim(); | ||
if (givenAuthBase64 != savedAuthBase64) | ||
{ | ||
context.HttpContext.Response.StatusCode = 401; | ||
return; | ||
} | ||
await next(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc; | ||
using ANAConversationPlatform.Helpers; | ||
using ANAConversationPlatform.Models; | ||
using System.Collections.Generic; | ||
using ANAConversationPlatform.Attributes; | ||
|
||
namespace ANAConversationPlatform.Controllers | ||
{ | ||
[Produces("application/json"), BasicAuthentication] | ||
public class ProjectController : Controller | ||
{ | ||
[HttpGet] | ||
public async Task<ActionResult> List() | ||
{ | ||
var projs = await MongoHelper.GetProjectsAsync(); | ||
if (projs != null) | ||
return Json(new { Message = "Projects list", Data = projs }); | ||
return BadRequest(new { Message = "Unable to list the projects!" }); | ||
} | ||
|
||
[HttpPost] | ||
public ActionResult Save([FromBody] List<ANAProject> projects) | ||
{ | ||
var projs = MongoHelper.SaveProjects(projects); | ||
if (projs != null) | ||
return Ok(new { Message = "Saved", Data = projects }); | ||
|
||
return BadRequest(new { Message = "Unable to save!" }); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.