Starting to handle claims

This commit is contained in:
Zoe Roux 2020-03-13 00:19:56 +01:00
parent b6b1a54dae
commit 977a0fa1f6
4 changed files with 26 additions and 19 deletions

View File

@ -25,6 +25,7 @@ namespace Kyoo
AllowedGrantTypes = GrantTypes.Code,
RequirePkce = true,
AllowAccessTokensViaBrowser = true,
AlwaysIncludeUserClaimsInIdToken = true,
AllowOfflineAccess = true,
RequireClientSecret = false,
RequireConsent = false,

View File

@ -1,4 +1,5 @@
using System.Reflection;
using Kyoo.Api;
using Kyoo.Controllers;
using Kyoo.Models;
using Microsoft.AspNetCore.Builder;
@ -71,6 +72,7 @@ namespace Kyoo
.AddInMemoryIdentityResources(IdentityContext.GetIdentityResources())
.AddInMemoryApiResources(IdentityContext.GetApis())
.AddAspNetIdentity<User>()
.AddProfileService<AccountController>()
.AddDeveloperSigningCredential();
services.AddScoped<ILibraryManager, LibraryManager>();

View File

@ -1,5 +1,9 @@
using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Threading.Tasks;
using IdentityServer4.Extensions;
using IdentityServer4.Models;
using IdentityServer4.Services;
using Kyoo.Models;
using Microsoft.AspNetCore.Authorization;
@ -26,7 +30,7 @@ namespace Kyoo.Api
[Route("api/[controller]")]
[ApiController]
public class AccountController : Controller
public class AccountController : Controller, IProfileService
{
private readonly UserManager<User> _userManager;
private readonly SignInManager<User> _signInManager;
@ -69,26 +73,26 @@ namespace Kyoo.Api
await _signInManager.SignOutAsync();
return Ok();
}
[HttpGet]
[Authorize]
public async Task<ActionResult<Account>> Index()
public async Task GetProfileDataAsync(ProfileDataRequestContext context)
{
User account = await _userManager.GetUserAsync(HttpContext.User);
return new Account{
Username = account.UserName,
Email = account.Email,
Picture = "api/account/picture/" + account.UserName
};
User user = await _userManager.GetUserAsync(context.Subject);
if (user != null)
{
List<Claim> claims = new List<Claim>
{
new Claim("email", user.Email),
new Claim("username", user.UserName),
};
context.IssuedClaims.AddRange(claims);
}
}
[HttpGet("picture/{username}")]
public IActionResult Picture(string username)
public async Task IsActiveAsync(IsActiveContext context)
{
string path = $"account/{username}.png";
if (System.IO.File.Exists(path))
return new PhysicalFileResult(path, "image");
return NotFound();
User user = await _userManager.GetUserAsync(context.Subject);
context.IsActive = user != null;
}
}
}

@ -1 +1 @@
Subproject commit ee0c1d6a4843ebbb5836c14be30ab06d895b8209
Subproject commit 6d892fd46e7aa56a7cb0bebd94d4404899382f2b