Giter Club home page Giter Club logo

azurefunctions.extensions.openidconnect's Introduction

AzureFunctions.Extensions.OpenIDConnect

Build status NuGet Badge

This project is originally forked from https://github.com/AspNetMonsters/AzureFunctions.OidcAuthentication. Thanks goes to David Paquette for the helpful intial codebase.

Why?

As of writing this, securing Azure Functions using Bearer token is clumsy. For some auth providers, you can enable App Service Authentication in the Azure Portal but that only works for the deployed version of your app which makes testing locally difficult and clumsy.

This library makes it easy to authenticate a user by validating a bearer token.

Requirements

Azure Functions using v3 or V4 runtime and of course an identity provider (e.g. Google, Azure AD, etc..)

How to use it

AzureFunctions.Extensions.OpenIDConnect support In-Process and Isolated azure functions hosting and brings the same features set. Be sure to use the nuget package according to your hosting choice.

In process

Add AzureFunctions.Extensions.OpenIDConnect.InProcess NuGet package to your Azure Functions project.

dotnet package install AzureFunctions.Extensions.OpenIDConnect.InProcess

Now head over the Configure method of the Startup class, add configure OpenID-Connect the way you like it.

namespace MySecuredApp
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            var audience = Environment.GetEnvironmentVariable("OpenIdConnect_Audience");
            var issuer = Environment.GetEnvironmentVariable("OpenIdConnect_Issuer");
            var issuerUrl = Environment.GetEnvironmentVariable("OpenIdConnect_IssuerUrl");
            builder.Services.AddOpenIDConnect(config =>
            {
                config.SetTokenValidation(TokenValidationParametersHelpers.Default(audience, issuer));
                config.SetIssuerBaseUrlConfiguration(issuerUrl);
            });
        }
    }
}

Isolated

Add AzureFunctions.Extensions.OpenIDConnect.Isolated NuGet package to your Azure Functions project.

dotnet package install AzureFunctions.Extensions.OpenIDConnect.Isolated

Now head over your entrypoint class, add configure the host like this :

static class Program
{
    private static async Task Main(string[] args)
    {
        var host = new HostBuilder()
            .ConfigureFunctionsWorkerDefaults((context, builder) =>
            {
                builder.UseAuthorization();
            })
            .ConfigureServices((context, services) =>
            {
                services.AddOpenIDConnect(config =>
                {
                    var audience = Environment.GetEnvironmentVariable("OpenIdConnect_Audience");
                    var issuer = Environment.GetEnvironmentVariable("OpenIdConnect_Issuer");
                    var issuerUrl = Environment.GetEnvironmentVariable("OpenIdConnect_IssuerUrl");

                    config.SetTokenValidation(TokenValidationParametersHelpers.Default(audience, issuer));
                    config.SetIssuerBaseUrlConfiguration(issuerUrl);
                });
            })
            .Build();
        
        await host.RunAsync();
    }
}

Securing an Azure Function

Now that everything is configured, you can decorate your http-triggered functions with the well known Authorize attribute as follows:

namespace MySecuredApp
{
    public class MyFunction
    {
        [Authorize]
        [FunctionName("MyFunction")]
        public async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            string responseMessage = $"Hello. This HTTP triggered function is protected.";

            return new OkObjectResult(responseMessage);
        }
   }
}

azurefunctions.extensions.openidconnect's People

Contributors

bryanknox avatar fmichellonet avatar xaviersolau avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.