MakingSense / aspnet-authentication-simpletoken

Middleware that allows to extract token from authenticated requests, delegate it to an appropriated ISecurityTokenValidator and generate and AuthenticationTicket.
GNU Lesser General Public License v3.0
1 stars 4 forks source link

SimpleToken Authentication

This package allows to extract token from authenticated requests and delegate it to an appropriated ISecurityTokenValidator and generate and AuthenticationTicket.

Behavior

Token extraction details

This tries to support almost RFC 6750 and some licenses based on GitHub behavior. But does not support Form-Encoded Body Parameter (http://tools.ietf.org/html/rfc6750#section-2.2).

There are three methods of sending tokens:

The WWW-Authenticate Response Header Field

When a protected resource is requested but request does not include authentication credentials or does not contain an access token that enables access it includes the HTTP "WWW-Authenticate" response header field.

For example:

Usage v2

It is necessary to register all valid ISecurityTokenValidator classes and configure the Authentication service using the AddSimpleTokenAuthentication extension method.

Example:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddTransient<ISecurityTokenValidator, MyCustomTokenValidator>();
        services.AddAuthentication()
            .AddSimpleTokenAuthentication();
    }

    public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory) {
        app.UseAuthentication();
        app.UseMvc();
    }
}

Usage v1

It is necessary to register all valid ISecurityTokenValidator classes and add the middleware to ApplicationBuilder using UseSimpleTokenAuthentication.

Example:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddTransient<ISecurityTokenValidator, MyCustomTokenValidator>();
    }

    public void Configure(IApplicationBuilder app)
    {
        app.UseSimpleTokenAuthentication(o =>
        {
            o.AutomaticAuthentication = true;
        });
        app.UseMvc();
    }
}

Internally, when the token is not valid or there are not any registered any capable ISecurityTokenValidator, an AuthenticationException is thrown.