using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace KTUSAPS.Controllers { [Route("api/[controller]")] [ApiController] [Produces("application/json")] public class AuthMetadataController : ControllerBase { public class AuthMetadata { public string ClientId { get; set; } public string Authority { get; set; } public string Tenant { get; set; } } private readonly IConfiguration _configuration; public AuthMetadataController(IConfiguration configuration) { _configuration = configuration; } /// /// Get authethication metadata needed to obtain token. /// /// [HttpGet] public AuthMetadata Index() => new AuthMetadata { ClientId = _configuration["ClientId"], Authority = _configuration["Authority"], Tenant = _configuration["Tenant"] }; /// /// Returns true is provided token is valid, else throws exception /// /// /// Provided token is correct. /// No valid token provided. [Authorize] [ProducesResponseType(StatusCodes.Status200OK)] [HttpGet("Authed")] public bool IsAuthed() => true; [Authorize] [ProducesResponseType(StatusCodes.Status200OK)] [HttpGet("Claims")] public IEnumerable Claims() => User.Claims.Select((c) => new { c.Type, c.Value }); [Authorize("admin")] [ProducesResponseType(StatusCodes.Status200OK)] [HttpGet("Admin")] public bool IsAdmin() => true; } }