6 Ways Azure Functions Endpoints Auth via OpenAPI
Tags: azure, azurefunctions, openapi, authentication
Published: 2021-10-08 00:00:27

---
title: 6 Ways Azure Functions Endpoints Auth via OpenAPI
published: true
description: In this post, I'm going to discuss many options for securing Azure Functions endpoints through API key, bearer token, OAuth2 and OpenIDConnect.
tags: azure, azurefunctions, openapi, authentication
canonical_url: https://devkimchi.com/2021/10/06/securing-azure-function-endpoints-via-openapi-auth/
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5d9bnm2va5ml0gog7csb.png
series: 

---
[Azure security baseline for Azure Functions][az funcapp security baseline] well describes the security consideration in general while developing an [Azure Functions][az fncapp] application. In addition to that, Azure Functions offers a built-in authentication method through the functions key. If you use the [OpenAPI extension][az fncapp extensions openapi] for Azure Functions, you can define the endpoint authentication and authorisation for each API endpoint in various ways. You can even try them through the Swagger UI page. Throughout this post, I'm going to discuss six different approaches for access control to Azure Functions API endpoints using the OpenAPI extension.

> This GitHub repository contains the sample app used in this post.
> {% github https://github.com/devkimchi/azure-functions-oauth-authentications-via-swagger-ui %}


## OpenAPI Spec for Authentication ##

It could be a good idea to take a look at the [authentication spec defined in OpenAPI][openapi spec security] before going further.

* `type`: defines what type of authentication method will be used. Currently, it accepts `API Key`, `HTTP`, `OAuth2`, and `OpenID Connect`. But, the OpenAPI v2 spec doesn't support the OpenID Connect.
* `name`: declares the auth key name. It's required for API Key.
* `in`: defines the location of the auth key. It's required for API Key and accepts `query`, `header`, or `cookie`.
* `scheme`: declares the auth scheme. It's required for HTTP auth and accepts either `Basic` or `Bearer`.
* `bearerFormat`: uses `JWT` in most cases when using the `Bearer` token through the HTTP auth.
* `flows`: is required for the `OAuth2` auth. Its value can be `implicit`, `password`, `clientCredentials`, or `authorizationCode`.
* `openIdConnectUrl`: is necessary for the `OpenID Connect` auth. However, it is advised to use either `OAuth2` or `Bearer` auth for the OpenAPI v2 spec.

Based on the understandings above, let's apply the different auth approach to Azure Function endpoints through the OpenAPI extension.


## APK Key in Querystring ##

This is the built-in feature of Azure Functions. Let's take a look at the code below. If you installed the OpenAPI extension, you could add the decorators. Spot on the `OpenApiSecurityAttribute(...)` decorator, which sets the value (line #6-9).

* `Type`: `SecuritySchemeType.ApiKey`
* `In`: `OpenApiSecurityLocationType.Query`
* `Name`: `code`

{% gist https://gist.github.com/justinyoo/7a9dab5304ef36c8747a3c0cf8430b71 file=01-apikey-in-query-auth-flow-httptrigger.cs %}

Run the function app, and you will see the Swagger UI page.

![Swagger UI - Query][image-01]

Click the lock button on the right-hand side to enter the API key value. This value will be appended to the querystring parameter.

![Swagger UI - Query - API Key][image-02]

The result screen shows the API key passed through the querystring parameter, `code`.

![Swagger UI - Query - Result][image-03]


## API Key in Request Header ##

It's also the Azure Function's built-in feature. This time, set the value of the `OpenApiSecurityAttribute(...)` decorator like below (line #6-9).

* `Type`: `SecuritySchemeType.ApiKey`
* `In`: `OpenApiSecurityLocationType.Header`
* `Name`: `x-functions-key`

{% gist https://gist.github.com/justinyoo/7a9dab5304ef36c8747a3c0cf8430b71 file=02-apikey-in-header-auth-flow-httptrigger.cs %}

Run the function app and see the Swagger UI page.

![Swagger UI - Header][image-04]

If you want to authenticate the endpoint, enter the API key value to the field, labelled as `x-functions-key`.

![Swagger UI - Header - API Key][image-05]

As a result, the API key was sent through the request header, `x-functions-key`.

![Swagger UI - Header - Result][image-06]


## Basic Auth Token ##

Let's use the Basic auth token this time. Set the property values of `OpenApiSecurityAttribute(...)` (line #6-8).

* `Type`: `SecuritySchemeType.Http`
* `Scheme`: `OpenApiSecuritySchemeType.Basic`

As this is not the built-in feature, you can use this approach for additional auth methods or replace the built-in feature. If you don't want to use the built-in API key, you should set the auth level value of the `HttpTrigger` binding to `AuthorizationLevel.Anonymous` (line #12).

{% gist https://gist.github.com/justinyoo/7a9dab5304ef36c8747a3c0cf8430b71 file=03-http-basic-auth-flow-httptrigger.cs %}

Run the app to see the Swagger UI like below.

![Swagger UI - Basic Auth][image-07]

To authenticate your endpoint, you should enter the Username and Password, added to the `Authorization` header.

![Swagger UI - Basic Auth - Details][image-08]

The result screen shows the request header of `Authorization` with the base64 encoded value.

![Swagger UI - Basis Auth - Result][image-09]

Then, you should validate the auth details with your custom logic.


## Bearer Auth Token ##

Similarly, this time, let's use the Bearer auth token. Set the property values of `OpenApiSecurityAttribute(...)` (line #5).

* `Type`: `SecuritySchemeType.Http`
* `Scheme`: `OpenApiSecuritySchemeType.Bearer`
* `BearerFormat`: `JWT`

You now know how to set the auth level of the `HttpTrigger` binding to `AuthorizationLevel.Anonymous` (line #13).

{% gist https://gist.github.com/justinyoo/7a9dab5304ef36c8747a3c0cf8430b71 file=04-http-bearer-auth-flow-httptrigger.cs %}

Run the function app and see the Swagger UI page.

![Swagger UI - Bearer Auth][image-10]

During the authentication, you are asked to enter the Bearer token value. The `Authorization` header will add the value.

![Swagger UI - Bearer Auth - Details][image-11]

The result screen shows the JWT value in the `Authorization` header.

![Swagger UI - Bearer Auth - Result][image-12]

You should decode the JWT and find the appropriate claims and validate them for further processing.


## OAuth2 Implicit Auth Flow ##

Although there are many ways in the OAuth2 authentication flow, I'm going to use the [Implicit flow][az ad oauth2 implicit flow] for this time. Set the properties of `OpenApiSecurityAttribute(...)` (line #6-8).

* `Type`: `SecuritySchemeType.OAuth2`
* `Flows`: `ImplicitAuthFlow`

Auth level is also set to `Anonymous` (line #12).

{% gist https://gist.github.com/justinyoo/7a9dab5304ef36c8747a3c0cf8430b71 file=05-oauth-implicit-auth-flow-httptrigger.cs %}

You can see `ImplicitAuthFlow` as the flow type. Since it uses [Azure Active Directory][az ad], it sets `AuthorizationUrl`, `RefreshUrl`, and `Scopes` values. It also takes the [single tenant type][az ad tenancy], which requires the tenant ID (line #3-6, 10, 14-15). `Scopes` has the default value (line #17).

{% gist https://gist.github.com/justinyoo/7a9dab5304ef36c8747a3c0cf8430b71 file=06-implicit-auth-flow.cs %}

Run the function app and check the Swagger UI page.

![Swagger UI - OAuth2 Implicit Auth][image-13]

When you click the lock button, it asks you to enter the client ID value, redirecting you to sign in to Azure Active Directory. Then, you will get the access token.

![Swagger UI - OAuth2 Implicit Auth - Details][image-14]

The result shows the `Authorization` header with the access token in the JWT format.

![Swagger UI - OAuth2 Implicit Auth - Result][image-15]

That JWT is now decoded and verified for further processing.


## OpenID Connect Auth Flow ##

Finally, let's use the [OpenID Connect auth flow][az ad oidc flow]. `OpenApiSecurityAttribute(...)` contains the following definitions (line #6-9).

* `Type`: `SecuritySchemeType.OpenIdConnect`
* `OpenIdConnectUrl`: `https://login.microsoftonline.com/{tenant_id}/v2.0/.well-known/openid-configuration`
* `OpenIdConnectScopes`: `openid,profile`

The `{tenant_id}` value, of course, should be replaced with the real tenant ID. With this OpenID Connect URL, it automatically discovers the OAuth2 auth flows. Then, set the auth level to `Anonymous` (line #12).

{% gist https://gist.github.com/justinyoo/7a9dab5304ef36c8747a3c0cf8430b71 file=07-openid-connect-auth-flow-httptrigger.cs %}

Run the function app and find the Swagger UI page.

![Swagger UI - OpenID Connect Auth][image-16]

Unlike other auth flows, this OpenID Connect auth flow shows two methods. The first one is the authentication code flow, and the other one is the implicit flow. Let's use the second one and enter the client ID value. It will redirect you to Azure Active Directory to sign in and give you the access token.

![Swagger UI - OpenID Connect Auth - Details][image-17]

Once execute the endpoint, the access token is passed through the `Authorization` header in the JWT format.

![Swagger UI - OpenID Connect Auth - Result][image-18]

Decode and validate the token for further processing.

---

So far, we've covered six different ways to authenticate the HTTP trigger endpoints with the OpenAPI extension. These six ways are the most commonly used ones. Therefore, if you need, you can pick up one approach and implement it.


[image-01]: https://sa0blogs.blob.core.windows.net/devkimchi/2021/10/securing-azure-function-endpoints-via-openapi-auth-01.png
[image-02]: https://sa0blogs.blob.core.windows.net/devkimchi/2021/10/securing-azure-function-endpoints-via-openapi-auth-02.png
[image-03]: https://sa0blogs.blob.core.windows.net/devkimchi/2021/10/securing-azure-function-endpoints-via-openapi-auth-03.png
[image-04]: https://sa0blogs.blob.core.windows.net/devkimchi/2021/10/securing-azure-function-endpoints-via-openapi-auth-04.png
[image-05]: https://sa0blogs.blob.core.windows.net/devkimchi/2021/10/securing-azure-function-endpoints-via-openapi-auth-05.png
[image-06]: https://sa0blogs.blob.core.windows.net/devkimchi/2021/10/securing-azure-function-endpoints-via-openapi-auth-06.png
[image-07]: https://sa0blogs.blob.core.windows.net/devkimchi/2021/10/securing-azure-function-endpoints-via-openapi-auth-07.png
[image-08]: https://sa0blogs.blob.core.windows.net/devkimchi/2021/10/securing-azure-function-endpoints-via-openapi-auth-08.png
[image-09]: https://sa0blogs.blob.core.windows.net/devkimchi/2021/10/securing-azure-function-endpoints-via-openapi-auth-09.png
[image-10]: https://sa0blogs.blob.core.windows.net/devkimchi/2021/10/securing-azure-function-endpoints-via-openapi-auth-10.png
[image-11]: https://sa0blogs.blob.core.windows.net/devkimchi/2021/10/securing-azure-function-endpoints-via-openapi-auth-11.png
[image-12]: https://sa0blogs.blob.core.windows.net/devkimchi/2021/10/securing-azure-function-endpoints-via-openapi-auth-12.png
[image-13]: https://sa0blogs.blob.core.windows.net/devkimchi/2021/10/securing-azure-function-endpoints-via-openapi-auth-13.png
[image-14]: https://sa0blogs.blob.core.windows.net/devkimchi/2021/10/securing-azure-function-endpoints-via-openapi-auth-14.png
[image-15]: https://sa0blogs.blob.core.windows.net/devkimchi/2021/10/securing-azure-function-endpoints-via-openapi-auth-15.png
[image-16]: https://sa0blogs.blob.core.windows.net/devkimchi/2021/10/securing-azure-function-endpoints-via-openapi-auth-16.png
[image-17]: https://sa0blogs.blob.core.windows.net/devkimchi/2021/10/securing-azure-function-endpoints-via-openapi-auth-17.png
[image-18]: https://sa0blogs.blob.core.windows.net/devkimchi/2021/10/securing-azure-function-endpoints-via-openapi-auth-18.png


[gh sample]: https://github.com/devkimchi/azure-functions-oauth-authentications-via-swagger-ui

[openapi spec security]: https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.1.md#securitySchemeObject

[az fncapp]: https://docs.microsoft.com/azure/azure-functions/functions-overview?WT.mc_id=dotnet-44465-juyoo
[az funcapp security baseline]: https://docs.microsoft.com/security/benchmark/azure/baselines/functions-security-baseline
[az fncapp extensions openapi]: https://github.com/azure/azure-functions-openapi-extension

[az ad]: https://docs.microsoft.com/azure/active-directory/fundamentals/active-directory-whatis?WT.mc_id=dotnet-44465-juyoo
[az ad tenancy]: https://docs.microsoft.com/azure/active-directory/develop/single-and-multi-tenant-apps?WT.mc_id=dotnet-44465-juyoo
[az ad oauth2 implicit flow]: https://docs.microsoft.com/azure/active-directory/develop/v2-oauth2-implicit-grant-flow?WT.mc_id=dotnet-44465-juyoo
[az ad oidc flow]: https://docs.microsoft.com/azure/active-directory/develop/v2-protocols-oidc?WT.mc_id=dotnet-44465-juyoo