-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support custom claims for Local testing (#8434)
Co-authored-by: Magnus Jerre <[email protected]>
- Loading branch information
1 parent
30b82b0
commit f4ac510
Showing
8 changed files
with
98 additions
and
0 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
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" | ||
} | ||
] |
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,7 @@ | ||
[ | ||
{ | ||
"type": "some:extra:claim", | ||
"value": "claimValue", | ||
"valueType": "http://www.w3.org/2001/XMLSchema#string" | ||
} | ||
] |
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 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
44
src/Services/Authorization/Implementation/ClaimsService.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 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"; | ||
} | ||
} | ||
} |
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,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); | ||
} | ||
} |
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