QAToolKit.Auth
is a .NET Standard 2.1 library, that retrieves the JWT access tokens from different identity providers.
Currently it supports next Identity providers and Oauth2 flows:
Keycloak
: Library supports Keycloak client credentials flow orProtection API token (PAT)
flow. Additionally you can replace the PAT with user token by exchanging the token.Azure B2C
: Library supports AzureB2C client credentials flow.Identity Server 4
: Library supports Identity Server 4 client credentials flow
Supported .NET frameworks and standards: netstandard2.0
, netstandard2.1
, netcoreapp3.1
, net5.0
Keycloak support is limited to the client credential
or Protection API token (PAT)
flow in combination with token exchange
.
A mocked request below is sent to the Keycloak endpoint, and the PAT token is retrieved:
curl -X POST \
-H "Content-Type: application/x-www-form-urlencoded" \
-d 'grant_type=client_credentials&client_id=${client_id}&client_secret=${client_secret}' \
"http://localhost:8080/auth/realms/${realm_name}/protocol/openid-connect/token"
Read more here in the Keycloak documentation.
Now let's retrive a PAT token with QAToolKit Auth libraray:
var auth = new KeycloakAuthenticator(options =>
{
options.AddClientCredentialFlowParameters(
new Uri("https://my.keycloakserver.com/auth/realms/realmX/protocol/openid-connect/token"),
"my_client",
"client_secret");
});
var token = await auth.GetAccessToken();
If you want to replace the PAT token with user token, you can additionally specify a username. A mocked request looks like this:
curl -X POST \
-H "Content-Type: application/x-www-form-urlencoded" \
-d 'grant_type=urn:ietf:params:oauth:grant-type:token-exchange&client_id=${client_id}&client_secret=${client_secret}&subject_token=eyJhbGciOiJI...&requested_subject=myuser@users.com' \
"http://localhost:8080/auth/realms/${realm_name}/protocol/openid-connect/token"
As you see it has a different grant_type
and additionally 2 more properties in the URL; PAT token (subject_token
) and userName for which we want to replace the token (requested_subject
).
var auth = new KeycloakAuthenticator(options =>
{
options.AddClientCredentialFlowParameters(
new Uri("https://my.keycloakserver.com/auth/realms/realmX/protocol/openid-connect/token"),
"my_client",
"client_secret");
});
//Get client credentials flow access token
var token = await auth.GetAccessToken();
//Replace client credentials flow token for user access token
var userToken = await auth.ExchangeForUserToken("myuser@email.com");
Under the hood it's the same code that retrieves the client credentials flow
access token, but authenticator is explicit for Identity Server 4.
var auth = new IdentityServer4Authenticator(options =>
{
options.AddClientCredentialFlowParameters(
new Uri("https://<myserver>/token"),
"my_client"
"<client_secret>");
});
var token = await auth.GetAccessToken();
Under the hood it's the same code that retrieves the client credentials flow
access token, but authenticator is explicit for Azure B2C.
Azure B2C client credentials flow needs a defined scope which is usually https://graph.windows.net/.default
.
var auth = new AzureB2CAuthenticator(options =>
{
options.AddClientCredentialFlowParameters(
new Uri("https://login.microsoftonline.com/<tenantID>/oauth2/v2.0/token"),
"<clientId>"
"<clientSecret>"
new string[] { "https://graph.windows.net/.default" });
});
var token = await auth.GetAccessToken();
- This library is an early alpha version
- Add Password flows to AzurteB2C and IdentityServer4.
MIT License
Copyright (c) 2020 Miha Jakovac
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.