Skip to content

Commit

Permalink
added authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
VeniaminBalan committed Oct 29, 2023
1 parent a8b2b3e commit facf793
Show file tree
Hide file tree
Showing 15 changed files with 40 additions and 35 deletions.
2 changes: 2 additions & 0 deletions GdscBackend.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=Gdsc/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
2 changes: 1 addition & 1 deletion GdscBackend/Features/Articles/ArticleController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace GdscBackend.Features.Articles;

[ApiController]
[ApiVersion("1")]
[Authorize]
[Authorize("CoreTeam")]
[Route("v1/Articles")]
public class ArticleController : ControllerBase
{
Expand Down
11 changes: 3 additions & 8 deletions GdscBackend/Features/Contacts/ContactController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
namespace GdscBackend.Features.Contacts;

[ApiController]
[Authorize(Roles = "admin")]
[ApiVersion("1")]
[Authorize(AuthorizeConstants.CoreTeam)]
[ApiVersion("v1")]
[Route("v1/contact")]
public class ContactController : ControllerBase
{
Expand All @@ -35,15 +35,10 @@ public ContactController(IRepository<ContactModel> repository, IMapper mapper, I
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<ActionResult<IEnumerable<ContactModel>>> Post(ContactRequest entity)
{
if (entity is null)
{
return BadRequest(new ErrorViewModel { Message = "Request has no body" });
}
if (entity is null) return BadRequest(new ErrorViewModel { Message = "Request has no body" });

if (!new EmailAddressAttribute().IsValid(entity.Email))
{
return BadRequest(new ErrorViewModel { Message = "Invalid email provided" });
}

var newEntity = await _repository.AddAsync(Map(entity));

Expand Down
5 changes: 3 additions & 2 deletions GdscBackend/Features/Events/EventsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
using AutoMapper;
using GdscBackend.Database;
using GdscBackend.Features.FIles;
using GdscBackend.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace GdscBackend.Features.Events;

[ApiController]
[ApiVersion("1")]
[Authorize(Roles = "admin")]
[Authorize(AuthorizeConstants.CoreTeam)]
[Route("v1/events")]
[Consumes(MediaTypeNames.Application.Json)]
[Produces(MediaTypeNames.Application.Json)]
Expand All @@ -30,7 +31,7 @@ public EventsController(IRepository<EventModel> repository, IMapper mapper,
/*<<<<<<< HEAD
=======
>>>>>>> dev*/
[HttpPost]
[ProducesResponseType(StatusCodes.Status201Created)]
Expand Down
3 changes: 2 additions & 1 deletion GdscBackend/Features/FIles/FilesController.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using GdscBackend.Database;
using GdscBackend.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace GdscBackend.Features.FIles;

[ApiController]
[ApiVersion("1")]
[Authorize(Roles = "admin")]
[Authorize(AuthorizeConstants.CoreTeam)]
[Route("v1/files")]
public class FilesController : ControllerBase
{
Expand Down
3 changes: 2 additions & 1 deletion GdscBackend/Features/Faqs/FaqsController.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System.Net.Mime;
using AutoMapper;
using GdscBackend.Database;
using GdscBackend.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace GdscBackend.Features.Faqs;

[ApiController]
[ApiVersion("1")]
[Authorize(Roles = "admin")]
[Authorize(AuthorizeConstants.CoreTeam)]
[Route("v1/faqs")]
[Consumes(MediaTypeNames.Application.Json)]
[Produces(MediaTypeNames.Application.Json)]
Expand Down
3 changes: 2 additions & 1 deletion GdscBackend/Features/Members/MembersController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Net.Mime;
using AutoMapper;
using GdscBackend.Database;
using GdscBackend.Utils;
using GdscBackend.ViewModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -9,7 +10,7 @@ namespace GdscBackend.Features.Members;

[ApiController]
[ApiVersion("1")]
[Authorize(Roles = "admin")]
[Authorize(AuthorizeConstants.CoreTeam)]
[Route("v1/members")]
[Consumes(MediaTypeNames.Application.Json)]
[Produces(MediaTypeNames.Application.Json)]
Expand Down
3 changes: 2 additions & 1 deletion GdscBackend/Features/MenuItems/MenuItemsController.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System.Net.Mime;
using AutoMapper;
using GdscBackend.Database;
using GdscBackend.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace GdscBackend.Features.MenuItems;

[ApiController]
[ApiVersion("1")]
[Authorize(Roles = "admin")]
[Authorize(AuthorizeConstants.CoreTeam)]
[Route("v1/menu-items")]
[Consumes(MediaTypeNames.Application.Json)]
[Produces(MediaTypeNames.Application.Json)]
Expand Down
3 changes: 2 additions & 1 deletion GdscBackend/Features/Pages/PagesController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using AutoMapper;
using GdscBackend.Database;
using GdscBackend.Utils;
using GdscBackend.ViewModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -8,7 +9,7 @@ namespace GdscBackend.Features.Pages;

[ApiController]
[ApiVersion("1")]
[Authorize(Roles = "admin")]
[Authorize(AuthorizeConstants.CoreTeam)]
[Route("v1/pages")]
public class PagesController : ControllerBase
{
Expand Down
23 changes: 6 additions & 17 deletions GdscBackend/Features/Redirects/RedirectsController.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System.Net.Mime;
using AutoMapper;
using GdscBackend.Database;
using GdscBackend.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace GdscBackend.Features.Redirects;

[ApiController]
[ApiVersion("1")]
[Authorize(Roles = "admin")]
[Authorize(AuthorizeConstants.CoreTeam)]
[Route("v1/redirects")]
[Consumes(MediaTypeNames.Application.Json)]
[Produces(MediaTypeNames.Application.Json)]
Expand All @@ -30,10 +31,7 @@ public async Task<ActionResult> Get()
{
var dict = new Dictionary<string, string>();
var redirects = (await _repository.GetAsync()).ToList();
foreach (var redirectModel in redirects)
{
dict.Add(redirectModel.Path, redirectModel.RedirectTo);
}
foreach (var redirectModel in redirects) dict.Add(redirectModel.Path, redirectModel.RedirectTo);

return Ok(dict);
}
Expand Down Expand Up @@ -67,16 +65,10 @@ public async Task<ActionResult<RedirectResponse>> Delete([FromRoute] string path
{
var all = await _repository.GetAsync();
var newEntity = all.FirstOrDefault(entity => entity.Path == path);
if (newEntity is null)
{
return NotFound();
}
if (newEntity is null) return NotFound();

var result = await _repository.DeleteAsync(newEntity.Id);
if (result is null)
{
return NotFound();
}
if (result is null) return NotFound();

return Ok(result);
}
Expand All @@ -90,10 +82,7 @@ public async Task<ActionResult<RedirectResponse>> Update([FromRoute] string path
{
var all = await _repository.GetAsync();
var newEntity = all.FirstOrDefault(entity => entity.Path == path);
if (newEntity is null)
{
return NotFound();
}
if (newEntity is null) return NotFound();

newEntity.Path = request.Path;
newEntity.RedirectTo = request.RedirectTo;
Expand Down
3 changes: 2 additions & 1 deletion GdscBackend/Features/Settings/SettingsController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Net.Mime;
using AutoMapper;
using GdscBackend.Database;
using GdscBackend.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

Expand All @@ -9,7 +10,7 @@ namespace GdscBackend.Features.Settings;
// This marks this controller as a public one that can be called from the internet
[ApiController]
[ApiVersion("1")]
[Authorize(Roles = "admin")]
[Authorize(AuthorizeConstants.CoreTeam)]
// This sets the URL that we can enter to call the controller's methods
// ex: https://localhost:5000/v1/examples
[Route("v1/settings")]
Expand Down
3 changes: 2 additions & 1 deletion GdscBackend/Features/Teams/TeamsController.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System.Net.Mime;
using AutoMapper;
using GdscBackend.Database;
using GdscBackend.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace GdscBackend.Features.Teams;

[ApiController]
[ApiVersion("1")]
[Authorize(Roles = "admin")]
[Authorize(AuthorizeConstants.CoreTeam)]
[Route("v1/teams")]
[Consumes(MediaTypeNames.Application.Json)]
[Produces(MediaTypeNames.Application.Json)]
Expand Down
1 change: 1 addition & 0 deletions GdscBackend/GdscBackend.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<ItemGroup>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.0"/>
<PackageReference Include="Keycloak.AuthServices.Authentication" Version="1.6.0"/>
<PackageReference Include="Keycloak.AuthServices.Authorization" Version="1.6.0"/>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.0"/>
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="7.0.0"/>
Expand Down
4 changes: 4 additions & 0 deletions GdscBackend/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using GdscBackend.Utils.Mappers;
using GdscBackend.Utils.Services;
using Keycloak.AuthServices.Authentication;
using Keycloak.AuthServices.Authorization;
using Keycloak.AuthServices.Common;
using Microsoft.EntityFrameworkCore;

Expand Down Expand Up @@ -31,6 +32,9 @@
services.AddSwaggerConfiguration(keycloakOptions);

services.AddKeycloakAuthentication(configuration);
services.AddAuthorization(
o => o.AddPolicy(AuthorizeConstants.CoreTeam, b => { b.RequireRealmRoles("GDSC_CORE_TEAM"); }));
services.AddKeycloakAuthorization(configuration);

services.AddTransient<IEmailSender, EmailSender>();
services.AddTransient<IWebhookService, WebhookService>();
Expand Down
6 changes: 6 additions & 0 deletions GdscBackend/Utils/AuthorizeConstants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace GdscBackend.Utils;

public static class AuthorizeConstants
{
public const string CoreTeam = "CoreTeam";
}

0 comments on commit facf793

Please sign in to comment.