Skip to content

Latest commit

 

History

History
199 lines (152 loc) · 8.32 KB

README.md

File metadata and controls

199 lines (152 loc) · 8.32 KB

QAToolKit Authentication library

Build .NET Library CodeQL Sonarcloud Quality gate NuGet package Discord

Description

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:

Supported .NET frameworks and standards: netstandard2.0, netstandard2.1, net8.0.

Get in touch with me on:

Discord

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 :).

1. Keycloak support

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.

1.1. Client credential flow

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();

1.2. Exchange token for user token

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");

1.3. Resource owner password credentials grant

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();

2. Identity Server 4 support

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();

Resource owner password

var auth = new IdentityServer4Authenticator(options =>
{
    options.AddResourceOwnerPasswordCredentialFlowParameters(
            new Uri("https://<myserver>/token"),
            "my_client"
            "<client_secret>",
            "user",
            "pass");
});
            
var token = await auth.GetAccessToken();

3. Azure B2C support

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();

Resource owner password credentials flow

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();

To-do

  • This library is an early alpha version

License

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.