Coder Social home page Coder Social logo

xaviersolau / azurefunctions.extensions.openidconnect Goto Github PK

View Code? Open in Web Editor NEW

This project forked from fmichellonet/azurefunctions.extensions.openidconnect

0.0 1.0 0.0 69 KB

OIDC / JWT Bearer Tokens authentication made easy for Azure Functions. Work with the well known ASPNET Authorize attribute. Compatible with popular identity providers including Auth0, Azure AD B2C, Azure AD, Google etc..

License: MIT License

C# 100.00%

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

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

dotnet package install AzureFunctions.Extensions.OpenIDConnect

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);
            });
        }
    }
}

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

Watchers

 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.