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, AllowedGrantTypes = GrantTypes.Code,
RequirePkce = true, RequirePkce = true,
AllowAccessTokensViaBrowser = true, AllowAccessTokensViaBrowser = true,
AlwaysIncludeUserClaimsInIdToken = true,
AllowOfflineAccess = true, AllowOfflineAccess = true,
RequireClientSecret = false, RequireClientSecret = false,
RequireConsent = false, RequireConsent = false,

View File

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

View File

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

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