I'm just starting out with Identity Server 4.
I'm trying to protect an API using the Client Credentials grant type.
I have an API setup within IS4:
public static IEnumerable<ApiResource> Apis =>
new List<ApiResource>
{
new ApiResource("myapi", "Test API")
{
ApiSecrets = { new Secret("secret".Sha256() )}
}
};
I also have the following client setup:
public static IEnumerable<Client> Clients =>
new List<Client>
{
new Client
{
ClientId = "testc",
ClientName= "Test Client",
AllowedGrantTypes = GrantTypes.ClientCredentials,
ClientSecrets =
{
new Secret("m2msecret".Sha256())
},
AllowedScopes = new List<string>
{
"myapi"
}
},
};
I have a controller within API that I'd like to protect:
[Authorize]
public class TestController : ControllerBase {}
If I then create a token request as follows:
var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
{
Address = disco.TokenEndpoint,
ClientId = "testc",
ClientSecret = "m2msecret",
Scope = "myapi",
});
This allows me to call the API and access the resource. Perfect!
But, I'd like to protect the controller with a role, e.g.
[Authorize(Roles = "admin")]
public class TestController : ControllerBase {}
So my 2 questions are:
1: How do you set up role-based authorization using client credentials?
2: As client credentials doesn't link to a user, how can I keep an audit trail of record changes e.g. supplier X was updated by userId 5, etc.
Thanks