Skip to content

Commit

Permalink
Support custom claims for Local testing (#8434)
Browse files Browse the repository at this point in the history
Co-authored-by: Magnus Jerre <[email protected]>
  • Loading branch information
magnusjerre and Magnus Jerre authored May 16, 2022
1 parent 30b82b0 commit f4ac510
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 0 deletions.
7 changes: 7 additions & 0 deletions TestData/authorization/claims/12345.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"type": "some:extra:claim",
"value": "claimValue",
"valueType": "http://www.w3.org/2001/XMLSchema#string"
}
]
7 changes: 7 additions & 0 deletions TestData/authorization/claims/1337.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"type": "some:extra:claim",
"value": "claimValue",
"valueType": "http://www.w3.org/2001/XMLSchema#string"
}
]
2 changes: 2 additions & 0 deletions src/Configuration/LocalPlatformSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,7 @@ public string LocalTestingStaticTestDataPath {
public string PartyListFolder { get; set; } = "partylist/";

public string RolesFolder { get; set; } = "roles/";

public string ClaimsFolder { get; set; } = "claims/";
}
}
5 changes: 5 additions & 0 deletions src/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Microsoft.Extensions.Options;

using AltinnCore.Authentication.Constants;
using Altinn.Platform.Authorization.Services.Interface;
using Altinn.Platform.Profile.Models;
using Altinn.Platform.Storage.Interface.Models;
using Altinn.Platform.Storage.Repository;
Expand All @@ -37,6 +38,7 @@ public class HomeController : Controller
private readonly IUserProfiles _userProfileService;
private readonly IAuthentication _authenticationService;
private readonly IApplicationRepository _applicationRepository;
private readonly IClaims _claimsService;
private readonly ILocalApp _localApp;

public HomeController(
Expand All @@ -45,13 +47,15 @@ public HomeController(
IUserProfiles userProfileService,
IAuthentication authenticationService,
IApplicationRepository applicationRepository,
IClaims claimsService,
ILocalApp localApp)
{
_generalSettings = generalSettings.Value;
_localPlatformSettings = localPlatformSettings.Value;
_userProfileService = userProfileService;
_authenticationService = authenticationService;
_applicationRepository = applicationRepository;
_claimsService = claimsService;
_localApp = localApp;
}

Expand Down Expand Up @@ -118,6 +122,7 @@ public async Task<ActionResult> LogInTestUser(StartAppModel startAppModel)
claims.Add(new Claim(AltinnCoreClaimTypes.UserName, profile.UserName, ClaimValueTypes.String, issuer));
claims.Add(new Claim(AltinnCoreClaimTypes.PartyID, profile.PartyId.ToString(), ClaimValueTypes.Integer32, issuer));
claims.Add(new Claim(AltinnCoreClaimTypes.AuthenticationLevel, startAppModel.AuthenticationLevel, ClaimValueTypes.Integer32, issuer));
claims.AddRange(await _claimsService.GetCustomClaims(profile.UserId, issuer));

ClaimsIdentity identity = new ClaimsIdentity(_generalSettings.GetClaimsIdentity);
identity.AddClaims(claims);
Expand Down
21 changes: 21 additions & 0 deletions src/Models/Authentication/CustomClaim.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Altinn.Platform.Authentication.Model
{
public class CustomClaim
{
/// <summary>
/// Gets or sets the claim type, E.g. custom:claim
/// </summary>
public string Type { get; set; }

/// <summary>
/// Gets or sets the claim value, E.g. customValue
/// </summary>
public string Value { get; set; }

/// <summary>
/// Gets or sets the value type for the claim, E.g. http://www.w3.org/2001/XMLSchema#string
/// See System.Security.Claims.ClaimValueTypes for more value types
/// </summary>
public string ValueType { get; set; }
}
}
44 changes: 44 additions & 0 deletions src/Services/Authorization/Implementation/ClaimsService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Altinn.Platform.Authentication.Model;
using Altinn.Platform.Authorization.Services.Interface;
using LocalTest.Configuration;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;

namespace LocalTest.Services.Authorization.Implementation
{
public class ClaimsService : IClaims
{
private readonly LocalPlatformSettings _localPlatformSettings;

public ClaimsService(IOptions<LocalPlatformSettings> localPlatformSettings)
{
_localPlatformSettings = localPlatformSettings.Value;
}

public Task<List<Claim>> GetCustomClaims(int userId, string issuer)
{
var path = GetCustomClaimsPath(userId);

if (File.Exists(path))
{
var content = File.ReadAllText(path);
var claims = JsonConvert.DeserializeObject<List<CustomClaim>>(content) ?? new List<CustomClaim>();
return Task.FromResult(claims.Select(c => new Claim(c.Type, c.Value, c.ValueType, issuer)).ToList());
}

return Task.FromResult(new List<Claim>());
}

private string GetCustomClaimsPath(int userId)
{
return _localPlatformSettings.LocalTestingStaticTestDataPath +
_localPlatformSettings.AuthorizationDataFolder + _localPlatformSettings.ClaimsFolder + userId +
".json";
}
}
}
11 changes: 11 additions & 0 deletions src/Services/Authorization/Interface/IClaims.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;
using System.Security.Claims;
using System.Threading.Tasks;

namespace Altinn.Platform.Authorization.Services.Interface
{
public interface IClaims
{
public Task<List<Claim>> GetCustomClaims(int userId, string issuer);
}
}
1 change: 1 addition & 0 deletions src/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public void ConfigureServices(IServiceCollection services)
services.AddSingleton<Services.Register.Interface.IParties, PartiesWrapper>();
services.AddSingleton<IPersons, PersonsWrapper>();
services.AddSingleton<Altinn.Platform.Authorization.Services.Interface.IParties, PartiesService>();
services.AddSingleton<IClaims, ClaimsService>();
services.AddSingleton<IInstanceRepository, InstanceRepository>();
services.AddSingleton<IDataRepository, DataRepository>();
services.AddSingleton<IInstanceEventRepository, InstanceEventRepository>();
Expand Down

0 comments on commit f4ac510

Please sign in to comment.