Examples of usage#

The examples illustrated in this pages are based on the following configuration which is is instantiated in this JSON configuration file:

  • One rights context is defined: Can TRANSFORM.
  • Two processing contexts are defined: Protect and Deprotect.
  • The User.BirthDate data instance can be protected/deprotected using the "Birth date protection"/"Birth date deprotection" transformer sequences which are based on "Date tokenisation" transformers
  • The User.LastName data instance can be protected/deprotected using the "Last name searchable protection"/"Last name searchable deprotection" transformer sequences which are based on "AES based searchable encryption" transformers
  • The User.FirstName data instance can be protected/deprotected using the "First name simple protection"/"First name simple deprotection" transformer sequences which are based on "AES deterministic encryption" transformers
  • The Payment.Amount data instance can be protected using the "Payment amount randomization" transformer sequences which are based on "Number variance anonymization" transformers
  • The Payment.Date data instance can be protected using the "Payment amount randomization" transformer sequences which are based on "Probabilistic date anonymization" transformers

ProcessingContext.json#

Definition of the processing contexts as a .json file. Beware of the font case in javascript.

{
  "Admin": {
    "Evidences": [
      {
        "Name": "Action",
        "Value": "Protect"
      },
      {
        "Name": "Action",
        "Value": "Deprotect"
      }
    ]
  }
}

RightsContext.json#

Definition of the rights contexts as a .json file. Beware of the font case in javascript.

{
  "Admin": {
    "Evidences": [
      {
        "Name": "Role",
        "Value": "Admin"
      }
    ]
  }
}

Simple usage#

public class SimpleUsageExample : IHostedService
{
    private readonly RPSEngine _rpsEngine;

    public SimpleUsageExample(RPSEngine rpsEngine)
    {
        _rpsEngine = rpsEngine;
    }

    public async Task StartAsync(CancellationToken cancellationToken)
    {
        Console.ForegroundColor = ConsoleColor.DarkGreen;
        Console.WriteLine("--- Example of simple protection and deprotection ---");
        Console.ResetColor();

        // Manually creates admin rights context.
        var adminRightsContext = new Context
        {
            Evidences = new List<Evidence>
            {
                new Evidence
                {
                    Name = "Role",
                    Value = "Admin"
                }
            }
        };

        // Manually creates protecting processing context.
        var protectProcessingContext = new ProcessingContext
        {
            Evidences = new List<Evidence>
            {
                new Evidence
                {
                    Name = "Action",
                    Value = "Protect"
                }
            }
        };

        // Manually creates deprotecting processing context.
        var deprotectProcessingContext = new ProcessingContext
        {
            Evidences = new List<Evidence>
            {
                new Evidence
                {
                    Name = "Action",
                    Value = "Deprotect"
                }
            }
        };

        // Different constructors with ability to specify Instance and Original value.
        var rawFirstName = new RPSValue(className: "User", propertyName: "FirstName", originalValue: "Jonny");
        var rawLastName = new RPSValue("User", "LastName", "Silverhand");
        var rawBirthDate = new RPSValue(new RPSInstance("User", "BirthDate"), "16.11.1988");

        var protectedFirstName = new RPSValue(className: "User", propertyName: "FirstName", originalValue: "n99toNMwdjjGtWs3SxkrxQ==");
        var protectedLastName = new RPSValue("User", "LastName", "FLGqfDklPngzYAD8066q40drM1jZYQzKktF1YO81A==");
        var protectedBirthDate = new RPSValue(new RPSInstance("User", "BirthDate"), "02.09.1961");

        // In this example we create a context with two different requests (protect and deprotect), BUT in single call to RPS Engine API.
        RequestContext requestContext = _rpsEngine
            .CreateContext()
            .WithRequest(
                rpsValues: new[] { rawFirstName, rawLastName, rawBirthDate },
                rightsContext: adminRightsContext,
                processingContext: protectProcessingContext)
            .WithRequest(
                rpsValues: new[] { protectedFirstName, protectedLastName, protectedBirthDate },
                rightsContext: adminRightsContext,
                processingContext: deprotectProcessingContext);

        // This method will do REST API call to RPS Engine API.
        await requestContext.TransformAsync();

        Console.WriteLine($"Raw fist name. Original: {rawFirstName.Original}. Transformed: {rawFirstName.Transformed}");
        Console.WriteLine($"Raw last name. Original: {rawLastName.Original}. Transformed: {rawLastName.Transformed}");
        Console.WriteLine($"Raw birth date. Original: {rawBirthDate.Original}. Transformed: {rawBirthDate.Transformed}");
        Console.WriteLine($"Protected first name. Original: {protectedFirstName.Original}. Transformed: {protectedFirstName.Transformed}");
        Console.WriteLine($"Protected last name. Original: {protectedLastName.Original}. Transformed: {protectedLastName.Transformed}");
        Console.WriteLine($"Protected birth date. Original: {protectedBirthDate.Original}. Transformed: {protectedBirthDate.Transformed}");

        Console.WriteLine();
    }

    public async Task StopAsync(CancellationToken cancellationToken)
        => await Task.CompletedTask;
}

Contexts provided by the resolver/json file#

public class ContextsProvidedByResolverExample : IHostedService
{
    private readonly RPSEngine _rpsEngine;

    public ContextsProvidedByResolverExample(RPSEngine rpsEngine)
    {
        _rpsEngine = rpsEngine;
    }

    public async Task StartAsync(CancellationToken cancellationToken)
    {
        Console.ForegroundColor = ConsoleColor.DarkGreen;
        Console.WriteLine("--- Example with rights and processing contexts provided by abstract resolver ---");
        Console.WriteLine("1. To use separate JSON files (RightsContexts.json and ProcessingContext.json):");
        Console.WriteLine("Uncomment line at Program.cs: 'services.AddRPSEngineContextJsonFileProvider(...);'");
        Console.WriteLine("2. To use Rights and Processing contexts from configuration file (appsettings.json):");
        Console.WriteLine("Uncomment line at Program.cs: 'services.AddRPSEngineContextConfigurationProvider();'");
        Console.ResetColor();

        // Different constructors with ability to specify Instance and Original value.
        var rawFirstName = new RPSValue(className: "User", propertyName: "FirstName", originalValue: "Jonny");
        var rawLastName = new RPSValue("User", "LastName", "Silverhand");
        var rawBirthDate = new RPSValue(new RPSInstance("User", "BirthDate"), "16.11.1988");

        var protectedFirstName = new RPSValue(className: "User", propertyName: "FirstName", originalValue: "n99toNMwdjjGtWs3SxkrxQ==");
        var protectedLastName = new RPSValue("User", "LastName", "FLGqfDklPngzYAD8066q40drM1jZYQzKktF1YO81A==");
        var protectedBirthDate = new RPSValue(new RPSInstance("User", "BirthDate"), "02.09.1961");

        // In this example we create a two different contexts (protect and deprotect)
        // with a single request inside each, it will do a two call to RPS Engine API.
        RequestContext protectRequestContext = _rpsEngine
            .CreateContext()
            .WithRequest(
                rpsValues: new[] {protectedFirstName, protectedLastName, protectedBirthDate},
                rightsContextName: "Admin",
                processingContextName: "Protect");

        RequestContext deprotectRequestContext = _rpsEngine
            .CreateContext()
            .WithRequest(
                rpsValues: new[] {protectedFirstName, protectedLastName, protectedBirthDate},
                rightsContextName: "Admin",
                processingContextName: "Deprotect");

        // This methods will do REST API calls to RPS Engine API.
        await protectRequestContext.TransformAsync();
        await deprotectRequestContext.TransformAsync();

        Console.WriteLine($"Raw fist name. Original: {rawFirstName.Original}. Transformed: {rawFirstName.Transformed}");
        Console.WriteLine($"Raw last name. Original: {rawLastName.Original}. Transformed: {rawLastName.Transformed}");
        Console.WriteLine($"Raw birth date. Original: {rawBirthDate.Original}. Transformed: {rawBirthDate.Transformed}");
        Console.WriteLine($"Protected first name. Original: {protectedFirstName.Original}. Transformed: {protectedFirstName.Transformed}");
        Console.WriteLine($"Protected last name. Original: {protectedLastName.Original}. Transformed: {protectedLastName.Transformed}");
        Console.WriteLine($"Protected birth date. Original: {protectedBirthDate.Original}. Transformed: {protectedBirthDate.Transformed}");

        Console.WriteLine();
    }

    public async Task StopAsync(CancellationToken cancellationToken)
        => await Task.CompletedTask;
}

Usage with dependencies#

class UsageWithDependenciesExample : IHostedService
{
    private readonly RPSEngine _rpsEngine;

    public UsageWithDependenciesExample(RPSEngine rpsEngine)
    {
        _rpsEngine = rpsEngine;
    }

    public async Task StartAsync(CancellationToken cancellationToken)
    {
        Console.ForegroundColor = ConsoleColor.DarkGreen;
        Console.WriteLine("--- Example of protection with dependencies ---");
        Console.ResetColor();

        // Different constructors with ability to specify Instance, Original value and Dependencies.
        var paymentDate = new RPSValue(className: "Payment",
            propertyName: "Date",
            originalValue: "02.11.2021",
            dependencies: new Dictionary<string, string>
            {
                { "min", "01.10.2021" },
                { "max", "02.11.2021" }
            });

        var paymentAmount = new RPSValue(new RPSInstance("Payment", "Amount"), "999");

        // This method will do REST API call to RPS Engine API.
        await _rpsEngine.TransformAsync(
                rpsValues: new[] { paymentDate, paymentAmount },
                rightsContextName: "Admin",
                processingContextName: "Protect");

        Console.WriteLine($"Payment date. Original: {paymentDate.Original}. Transformed: {paymentDate.Transformed}");
        Console.WriteLine($"Payment amount. Original: {paymentAmount.Original}. Transformed: {paymentAmount.Transformed}");

        Console.WriteLine();
    }

    public async Task StopAsync(CancellationToken cancellationToken)
        => await Task.CompletedTask;
}