QAToolKit.Auth
is a .NET Standard 2.1 library, that retrieves the JWT access tokens from different identity providers. This library should only be used for testing software and not in applications to retrieve the token.
Currently it supports next Identity providers and Oauth2 flows:
Keycloak
: Library supports:- client credentials flow or
Protection API token (PAT)
flow. Additionally you can replace the PAT with user token by exchanging the token. - resource owner password credentials grant
- client credentials flow or
Azure B2C
: Library supports:Identity Server 4
: Library supports:
Supported .NET frameworks and standards: netstandard2.0
, netstandard2.1
, net8.0
.
Get in touch with me on:
Please note: The token expiration time is read from the tokens and is used to minimize the hits on the token endpoint. Another benefit is faster tests :).
Keycloak support is limited to the client credential
or Protection API token (PAT)
flow in combination with token exchange
. Additionally you can also get the token by using resource owner password credentials grant
.
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 library:
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");
var auth = new KeycloakAuthenticator(options =>
{
options.AddResourceOwnerPasswordCredentialFlowParameters(
new Uri("https://my.keycloakserver.com/auth/realms/realmX/protocol/openid-connect/token"),
"my_client",
"client_secret",
"user",
"pass");
});
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 Identity Server 4. Additionally you can also get the token by using resource owner password
.
var auth = new IdentityServer4Authenticator(options =>
{
options.AddClientCredentialFlowParameters(
new Uri("https://<myserver>/token"),
"my_client"
"<client_secret>");
});
var token = await auth.GetAccessToken();
var auth = new IdentityServer4Authenticator(options =>
{
options.AddResourceOwnerPasswordCredentialFlowParameters(
new Uri("https://<myserver>/token"),
"my_client"
"<client_secret>",
"user",
"pass");
});
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. Additionally you can also get the token by using resource owner password credentials flow
.
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();
var auth = new AzureB2CAuthenticator(options =>
{
options.AddResourceOwnerPasswordCredentialFlowParameters(
new Uri("https://login.microsoftonline.com/<tenantID>/oauth2/v2.0/token"),
"<clientId>"
"<clientSecret>"
new string[] { "https://graph.windows.net/.default" },
"user",
"pass");
});
var token = await auth.GetAccessToken();
- This library is an early alpha version
MIT License
Copyright (c) 2020-2024 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.