Skip to content

ApplicationUser on the modules

Calabonga edited this page Jan 30, 2025 · 2 revisions

Authentication & Authorization for Plugins (Commands)

In version 2.1.0 we added some helpful abstractions:

Abstractions

ICommandexIdentity

ICommandexIdentity abstraction added for user managing. This abstraction help to create an application user and store it on the Shell. Please see Wiki samples.

ISecureData

ISecureData abstraction added for availability to store on the Shell side data about tokens (access_token, refresh_token).

IIdentityManager

IIdentityManager abstraction for dependency injection container. This can help to make availdable a logged user on the Shell for commands (plugins/modules).

Samples

CommandexIdentity implementated ICommandexIdentity:

/// <summary>
/// This is a CommandexIdentity
/// </summary>
public class CommandexIdentity : ICommandexIdentity
{
    #region constructors

    protected CommandexIdentity()
    {
        Name = "Anonymous";
        SecureData = new SecureData();
    }

    protected CommandexIdentity(string name, ISecureData secureData)
    {
        Name = name;
        SecureData = secureData;
        AuthenticationType = "OAuth2.0";
    }

    #endregion

    /// <summary>
    /// Gets the name of the current user.
    /// </summary>
    /// <returns>
    /// The name of the user on whose behalf the code is running.
    /// </returns>
    public string Name { get; }

    /// <summary>
    /// Gets the type of authentication used.
    /// </summary>
    /// <returns>
    /// The type of authentication used to identify the user.
    /// </returns>
    public string? AuthenticationType { get; }

    /// <summary>
    /// Gets a value that indicates whether the user has been authenticated.
    /// </summary>
    /// <returns>
    /// true if the user was authenticated; otherwise, false.
    /// </returns>
    public bool IsAuthenticated => !string.IsNullOrEmpty(SecureData.AccessToken);

    /// <summary>
    /// AuthenticationToken for access to server
    /// </summary>
    public ISecureData SecureData { get; }
}

IIdentityManager can be implemented something like this:

/// <summary>
/// Identity manager for commands (modules)
/// </summary>
public class IdentityManager : IIdentityManager
{
    public ICommandexIdentity? User => App.Current.User;
}

Then you should register your IdentityManager in dependency injection:

// Identity manager for commands (modules)
services.AddTransient<IIdentityManager, IdentityManager>();

After that, you can inject IIdentityManager in the modules (commands) to get authenticated user on your plugin.

Clone this wiki locally