0

I have some logic ported from an API which I need to be able use within an Azure function

I need to do validation on JWT tokens

My function will have specific roles that a user must have to be able to get a response from the function

I have got this in my function startup

        var tokenOptions = configuration.GetSection("JwtIssuerOptions")
            .Get<TokenConfiguration>();
        
        var tokenValidationParameters = new TokenValidationParameters
        {
            // The signing key must match!
            ValidateIssuerSigningKey = true,
            IssuerSigningKey = tokenOptions.SecurityKey,
            // Validate the JWT Issuer (iss) claim
            ValidateIssuer = true,
            ValidIssuer = tokenOptions.Issuer,
            // Validate the JWT Audience (aud) claim
            ValidateAudience = true,
            ValidAudience = tokenOptions.Audience,
            // Validate the token expiry
            ValidateLifetime = true,
            // If you want to allow a certain amount of clock drift, set that here:
            ClockSkew = TimeSpan.Zero
        };
        
        services.Configure<IdentityConfiguration>(configuration.GetSection("IdentityConfiguration"));
        services.AddScoped<CustomJwtBearerEvents>();

        services
            .AddAuthentication(o =>
            {
                o.DefaultForbidScheme = JwtBearerDefaults.AuthenticationScheme;
                o.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(options =>
            {
                options.TokenValidationParameters = tokenValidationParameters;
                options.EventsType = typeof(CustomJwtBearerEvents);
            });

In an API context I would be able to have this taken care of by middleware packages and [Authorize(Roles="role1")]

However, that doesnt appear to be supported within Azure functions

How can I achieve the same thing within Azure?

I have a request that already has a token on it ready for checking

Paul

1 Answer 1

2

Below links might help. Azure filters are used as attribute in azure.

  1. https://github.com/Azure/azure-webjobs-sdk/wiki/Function-Filters
  2. https://markheath.net/post/secure-azure-functions-app-easy-auth-adb2c
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot I got it to work with function attributes
@Paul can you explain a little how you solved this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.