• This repository has been archived on 23/Mar/2022
  • Stars
    star
    294
  • Rank 141,303 (Top 3 %)
  • Language
    C#
  • License
    MIT License
  • Created over 9 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Library for impersonating users during development leveraging ASP.NET Identity.

Stuntman logo

"Sometimes you need a Stuntman before you send in real, unsuspecting users!"

Package Version
RimDev.Stuntman RimDev.Stuntman NuGet Version

Stuntman is a library for impersonating users during development leveraging .NET Claims Identity. Used primarily in web environments like ASP.NET MVC, ASP.NET Web Forms, and OWIN applications that serve HTML. This allows you to test different user scenarios that exist in your application with minimal friction. It also allows you to share those scenarios with other team members via source control.

Stuntman demo

Installation

Install the RimDev.Stuntman NuGet package.

PM> Install-Package RimDev.Stuntman

Usage

Startup / Middleware registration

Stuntman uses OWIN and is registered as middleware, and allows for programmatically preset user scenarios, in the form of claims identities. These presets can be utilized by you or other team members working on the same code base.

// OWIN Startup class
public class Startup
{
    public static readonly StuntmanOptions StuntmanOptions = new StuntmanOptions();

    public void Configuration(IAppBuilder app)
    {
        StuntmanOptions
            .AddUser(new StuntmanUser("user-1", "User 1")
                .AddClaim("given_name", "John")
                .AddClaim("family_name", "Doe"));

        // Optionally assign a user an access token.
        StuntmanOptions
            .AddUser(new StuntmanUser("user-2", "User 2")
                .SetAccessToken("123")
                .AddClaim("given_name", "Mary")
                .AddClaim("family_name", "Smith"));

        // You can also add users using HTTP/HTTPS or the file system!
        StuntmanOptions
            .AddUsersFromJson("https://example.com/web-test-users.json")
            .AddUsersFromJson(@"C:\local-test-users.json");

        // Optional alignment of user picker
        // Supported options are:
        // - StuntmanAlignment.Left (default)
        // - StuntmanAlignment.Center
        // - StuntmanAlignment.Right
        StuntmanOptions.SetUserPickerAlignment(StuntmanAlignment.Right);

        // Only show when debug is true in Web.config.
        if (System.Web.HttpContext.Current.IsDebuggingEnabled)
        {
            app.UseStuntman(StuntmanOptions);
        }
    }
}
// ASP.NET Core
public class Startup
{
    public static readonly StuntmanOptions StuntmanOptions = new StuntmanOptions();

    public Startup(IConfiguration configuration)
    {
        StuntmanOptions
            .AddUser(new StuntmanUser("user-1", "User 1")
                .AddClaim("given_name", "John")
                .AddClaim("family_name", "Doe"));

        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddStuntman(StuntmanOptions);
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseStuntman(StuntmanOptions);
    }
}

View

Here's how to use Stuntman in a Razor view to show the user picker (assuming the application Startup class has StuntmanOptions that can be used).

@* Only show when debug is true in Web.config. *@
@if (System.Web.HttpContext.Current.IsDebuggingEnabled)
{
    @Html.Raw(YourApplicationNamespace.Startup.StuntmanOptions.UserPicker(User));
}

Bearer-token

Stuntman supports bearer-tokens based on a user's access-token (StuntmanUser.SetAccessToken). There is nothing special about the value and no additional encoding/decoding is necessary. Upon successful authentication, the value is added as a claim. Leveraging the previous Startup code, you could construct an HTTP-request to utilize User 2's access-token:

> curl -i -H "Authorization: Bearer 123" http://localhost:54917/secure
HTTP/1.1 200 OK

Basic format-checking is done on the value:

> curl -i -H "Authorization: Bearer not-real" http://localhost:54917/secure
HTTP/1.1 403 options provided does not include the requested 'not-real' user.
> curl -i -H "Authorization: Bearer abc 123" http://localhost:54917/secure
HTTP/1.1 400 Authorization header is not in correct format.

Remote users

Users can be populated from remote locations using one or more of the following:

  • From the file system
StuntmanOptions.AddUsersFromJson("C:\\path\\to\\users.json");
  • From a web url to a JSON file
StuntmanOptions.AddUsersFromJson("https://example.com/users.json");
  • From a web url to a Stuntman instance with a running server
//
// On the server
//
StuntmanOptions.EnableServer();

//
// On the client
//
StuntmanOptions.AddConfigurationFromServer("https://some-stuntman-enabled-app.example.com/");
// or, if you prefer to not throw an exception
// and have the users silently not added
// if the server is unavailable:
StuntmanOptions.TryAddConfigurationFromServer("https://some-stuntman-enabled-app.example.com/");

Example users JSON

Here's an example users JSON that can be consumed by StuntmanOptions.AddUsersFromJson(string pathOrUrl):

{
  "Users": [
    {
      "Id": "user-1",
      "Name": "User 1"
    },
    {
      "Id": "user-2",
      "Name": "User 2"
    }
  ]
}

Contributing

Have an idea? Let's talk about it in an issue!

Find a bug? Open an issue or submit a pull request!

License

MIT License

More Repositories

1

RimDev.FeatureFlags

C#
34
star
2

automation-sql

C#
30
star
3

filter

The "classy" way to filter collections.
C#
27
star
4

fluentmigrator-generator

A simple PowerShell script to generate database migrations from the package manager console in Visual Studio.
PowerShell
26
star
5

zendesk-ticket-exporter

Zendesk Ticket Incremental Exporter
C#
24
star
6

RimDev.AspNet.Diagnostics.HealthChecks

ASP.NET full framework implementation of ASP.NET Core health checks.
C#
24
star
7

star-orgs

Azure Active Directory Organization Chart Viewer
JavaScript
22
star
8

releases

Pulls and displays releases from multiple GitHub repositories
C#
16
star
9

sandbox

An abstraction utilizing RimDev Automation libraries
C#
15
star
10

markdown-proofing

A markdown proofing platform for individuals, teams, and organizations.
JavaScript
14
star
11

automation-storage-emulator

C#
11
star
12

Supurlative

WebAPI Hypermedia Url generator - Templates, Current Urls, Paging Urls, and More!
C#
10
star
13

rimdev-blog

The technical blog for the Ritter Insurance Marketing development department.
Astro
10
star
14

interview

Technical Interview
8
star
15

jekyll-azure-deploy

Kudu script required to deploy a Jekyll site to Windows Azure App Services
Batchfile
8
star
16

angular2-bank

A bank application built with Angular 2 and Typescript
TypeScript
7
star
17

atlas

A library that contains geographical data about states, counties, and zip codes.
C#
7
star
18

automation-transform

Apply configuration transforms in memory to any xml document
C#
5
star
19

silverpop-dotnet-api

Silverpop .NET API
C#
4
star
20

automation-webhosting

C#
3
star
21

descriptor

Provides a fluent interface for documenting code
C#
3
star
22

temporal

An ASP.NET library enabling easy time traveling at runtime.
C#
3
star
23

permissions

C#
3
star
24

azure-web-app-configuration-copier

A tool to copy appSettings and connection strings between Azure Web Apps.
JavaScript
2
star
25

build-scripts

C#
1
star
26

AutomationDrivers

.Net tool set for CI driven Test Automation
C#
1
star
27

vue-platform-ui

vueJS port of Platform UI
Vue
1
star
28

AspNetBrowserLocale

C#
1
star
29

hubot-azure-alert-notifier

Notify for configured alert webhooks set on Microsoft Azure.
JavaScript
1
star
30

abbrjs

Leverages the abbr tag and a glossary object to add definitions to acronyms wrapped in abbr
JavaScript
1
star
31

ritter-jekyll

Tooling for our approach to using Jekyll
JavaScript
1
star
32

skellyCSS

A light-weight CSS framework to quickly implement skeletons into your projects.
HTML
1
star
33

hubot-github-issue-label-notifier

JavaScript
1
star
34

application-insights-filters

Filters for Microsoft Application Insights ASP.NET and ASP.NET Core applications.
C#
1
star
35

fluentmigrator-zoo

A sample showing Fluentmigrator working with EF6
C#
1
star
36

generator-rim-es6-component

Yeoman generator for es6 components
JavaScript
1
star
37

express-openid-connect-secured-jekyll

An Express application for securing a Jekyll powered website using OpenID Connect.
JavaScript
1
star
38

preload-hover

JavaScript
1
star
39

rimdev-entityframework

Provides helpful extensions for Entity Framework
C#
1
star
40

age-picker

Enhance `input` tags for collecting age information using `data-` attributes and simple wireup JavaScript.
JavaScript
1
star