-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/azure-ad-auth' into develop
- Loading branch information
Showing
11 changed files
with
190 additions
and
27 deletions.
There are no files selected for viewing
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,14 @@ | ||
namespace CellCms.Api.Constants | ||
{ | ||
/// <summary> | ||
/// Scopes para Cell CMS. | ||
/// </summary> | ||
public static class CellScopes | ||
{ | ||
// A ideia aqui é armazenar todas as nossas strings "fixas" em um único lugar, pra caso precisemos alterar ficar fácil. | ||
/// <summary> | ||
/// Permite acesso ao SwaggerUi para testes da API. | ||
/// </summary> | ||
public const string AcessoSwagger = "api://cell-cms-dev/SwaggerUi.Acesso"; | ||
} | ||
} |
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,21 @@ | ||
namespace CellCms.Api.Settings | ||
{ | ||
/// <summary> | ||
/// Configurações do Azure AD. | ||
/// </summary> | ||
public class AzureAdSettings : IAzureAdSettings | ||
{ | ||
/// <summary> | ||
/// Chave para buscar as configurações. | ||
/// </summary> | ||
public const string SettingsKey = "AzureAd"; | ||
|
||
public string ClientId { get; set; } | ||
|
||
public string TokenEndpoint { get; set; } | ||
|
||
public string AuthorizeEndpoint { get; set; } | ||
|
||
public string MetadataEndpoint { get; set; } | ||
} | ||
} |
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,28 @@ | ||
namespace CellCms.Api.Settings | ||
{ | ||
/// <summary> | ||
/// Descreve as propriedades para configurar autenticação através do Azure AD. | ||
/// </summary> | ||
public interface IAzureAdSettings | ||
{ | ||
/// <summary> | ||
/// ClientId esperado. | ||
/// </summary> | ||
string ClientId { get; } | ||
|
||
/// <summary> | ||
/// Endpoint para obter access tokens. | ||
/// </summary> | ||
string TokenEndpoint { get; } | ||
|
||
/// <summary> | ||
/// Endpoint para obter autorização dos usuários. | ||
/// </summary> | ||
string AuthorizeEndpoint { get; } | ||
|
||
/// <summary> | ||
/// Endpoint para buscar o well-known document. | ||
/// </summary> | ||
string MetadataEndpoint { get; } | ||
} | ||
} |
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
44 changes: 44 additions & 0 deletions
44
src/CellCms.Api/Swagger/SecurityRequirementOperationFilter.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,44 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.OpenApi.Models; | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace CellCms.Api.Swagger | ||
{ | ||
/// <summary> | ||
/// Filtro para operações protegidas. | ||
/// </summary> | ||
/// <see cref="https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/master/test/WebSites/OAuth2Integration/ResourceServer/Swagger/SecurityRequirementsOperationFilter.cs"/> | ||
public class SecurityRequirementsOperationFilter : IOperationFilter | ||
{ | ||
public void Apply(OpenApiOperation operation, OperationFilterContext context) | ||
{ | ||
// Policy names map to scopes | ||
var requiredScopes = context.MethodInfo | ||
.GetCustomAttributes(true) | ||
.OfType<AuthorizeAttribute>() | ||
.Select(attr => attr.Policy) | ||
.Distinct(); | ||
|
||
if (requiredScopes.Any()) | ||
{ | ||
operation.Responses.Add("401", new OpenApiResponse { Description = "Unauthorized" }); | ||
operation.Responses.Add("403", new OpenApiResponse { Description = "Forbidden" }); | ||
|
||
var oAuthScheme = new OpenApiSecurityScheme | ||
{ | ||
Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "oauth2" } | ||
}; | ||
|
||
operation.Security = new List<OpenApiSecurityRequirement> | ||
{ | ||
new OpenApiSecurityRequirement | ||
{ | ||
[ oAuthScheme ] = requiredScopes.ToList() | ||
} | ||
}; | ||
} | ||
} | ||
} | ||
} |
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