Quantcast
Channel: IdentityModel – leastprivilege.com
Viewing all articles
Browse latest Browse all 204

Retrieving bearer tokens from alternative locations in Katana/OWIN

$
0
0

The Katana bearer token authentication middleware tries to retrieve tokens from the HTTP Authorization header with a scheme of Bearer by default.

You can customize this behavior by providing a so called Provider (this is a common pattern in Katana). The following provider retrieves the access token from a query string:

public class QueryStringOAuthBearerProvider : OAuthBearerAuthenticationProvider

{

    readonly string _name;

 

    public QueryStringOAuthBearerProvider(string name)

    {

        _name = name;

    }

 

    public override Task RequestToken(OAuthRequestTokenContext context)

    {

        var value = context.Request.Query.Get(_name);

 

        if (!string.IsNullOrEmpty(value))

        {

            context.Token = value;

        }

 

        return Task.FromResult<object>(null);

    }

}

 

…or from an alternative header:

public class HeaderOAuthBearerProvider : OAuthBearerAuthenticationProvider

{

    readonly string _name;

 

    public HeaderOAuthBearerProvider(string name)

    {

        _name = name;

    }

 

    public override Task RequestToken(OAuthRequestTokenContext context)

    {

        var value = context.Request.Headers.Get(_name);

 

        if (!string.IsNullOrEmpty(value))

        {

            context.Token = value;

        }

 

        return Task.FromResult<object>(null);

    }

}

You can the pass such a provider to the middleware, e.g.:

var options = new JwtBearerAuthenticationOptions

{

    AllowedAudiences = new[] { audience },

    IssuerSecurityTokenProviders = new[]

        {

            new SymmetricKeyIssuerSecurityTokenProvider(

                issuer,

                signingKey)

        },

    Provider = new QueryStringOAuthBearerProvider(“access_token”)

};

 

..or by using the helpers from Thinktecture.IdentityModel.Owin:

app.UseJsonWebToken(

    issuer:     Constants.AS.IssuerName,

    audience:   Constants.Audience,

    signingKey: Constants.AS.SigningKey,

    location:   TokenLocation.QueryString(“access_token”));

 


Filed under: IdentityModel, Katana, OWIN, WebAPI

Viewing all articles
Browse latest Browse all 204

Trending Articles