You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> This project is originally forked from https://github.com/bryanknox/AzureFunctionsOpenIDConnectAuthSample. Thank you to Bryan for the helpful sample.
4
+
> This project is originally forked from https://github.com/AspNetMonsters/AzureFunctions.OidcAuthentication. Thanks goes to [David Paquette](https://github.com/dpaquette) the helpful intial codebase.
5
+
5
6
6
7
## Why?
7
8
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.
@@ -10,23 +11,15 @@ This library makes it easy to authenticate a user by validating a bearer token.
10
11
11
12
## Requirements
12
13
13
-
Azure Functions v3
14
-
Dependency Injection using Azure Functions Extensions
15
-
An identity provider (e.g. Auth0, Azure AD, Okta)
14
+
Azure Functions using v3 runtime and of course an identity provider (e.g. Google, Azure AD, etc..)
16
15
17
16
## How to use it
18
17
19
-
First, configure dependency injection for your Azure Functions project. Start by adding the Microsoft.Azure.Functions.Exentions NuGet package.
Now add a `FunctionStartup` class to configure services that will be used in your Azure Functions app. In the `Configure` method, add a call to `builder.Services.AddOidcApiAuthorization();` This will configure the `IApiAuthentication` service that you will use to authenticate users.
Configuration is loaded from a environment variables which can be set in `local.settings.json` for local development or in the Azure portal for your deployed app. Settings are prefixed with `OidcApiAuthSettings:`.
45
-
46
-
Here is an example `local.settings.json` file for Azure AD B2C:
Identifies the API to be authorized by the Open ID Connect provider (issuer).
66
-
67
-
The "Audience" is the identifer used by the authorization provider to identify the API (HTTP triggered Azure Function) being protected. This is often a URL but it is not used as a URL is is simply used as an identifier.
68
-
69
-
For Auth0 use the API's Identifier in the Auth0 Dashboard.
70
-
71
-
For Azure AD B2C, use your API Application's (client) ID. This is a GUID.
72
-
73
-
**OidcApiAuthSettings:IssuerUrl** - Required
74
-
75
-
The URL of the Open ID Connect provider (issuer) that will perform API authorization.
76
-
77
-
The "Issuer" is the URL for the authorization provider's end-point. This URL will be used as part of the OpenID Connect protocol to obtain the the signing keys that will be used to validate the JWT Bearer tokens in incoming HTTP request headers.
78
-
79
-
For Auth0 the URL format is: `https://{Auth0-tenant-domain}.auth0.com`
80
-
For Auzre AD B2C, the format is: `https://yourb2cdomain.b2clogin.com/Your Directory (tenant) ID/v2.0/`
81
-
82
-
**OidcApiAuthSettings:MetadataAddress** - Optional (depending on identity provider)
83
-
84
-
The URL for the identity provider's well-known openid-configuration url.
Not that everything is configured, you can inject the `IApiAuthentication` service into your Azure Function and authenticate users as follows:
45
+
Not that everything is configured, you can decorate your http-triggered functions with the well known [Authorize](https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.authorization.authorizeattribute?view=aspnetcore-3.1) attribute as follows:
var authResult = await this.apiAuthentication.AuthenticateAsync(req.Headers);
126
-
127
-
// Check the authentication result
128
-
if (authResult.Failed)
129
-
{
130
-
return new ForbidResult(authenticationScheme: "Bearer");
131
-
}
132
-
133
-
// User is authenticated. Proceed with function logic
134
-
string name = authResult.User.Identity.Name; // This gives us the unique user name
135
-
136
-
string responseMessage = $"Hello, {name}. This HTTP triggered function executed successfully.";
58
+
string responseMessage = $"Hello, {name}. This HTTP triggered function is protected.";
137
59
138
60
return new OkObjectResult(responseMessage);
139
61
}
140
62
}
141
63
}
142
64
```
143
-
144
-
`AuthResult.User` is a [ClaimsPrincipal](https://docs.microsoft.com/dotnet/api/system.security.claims.claimsprincipal) that is created using the claims that were included in the JWT token that was validated by the `IApiAuthentication` service. You can use `authResult.User` to inspect the user's claims and add your own authorization rules inside your Function.
0 commit comments