Azure security baseline for Azure Functions well describes the security consideration in general while developing an Azure Functions application. In addition to that, Azure Functions offers a built-in authentication method through the functions key. If you use the OpenAPI extension 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.
devkimchi / azure-functions-oauth-authentications-via-swagger-ui
This provides sample app for authenticating Azure Functions endpoints by OAuth through the integrated Swagger UI.
Azure Functions OAuth Authentications via Swagger UI
This provides sample app for authenticating Azure Functions endpoints by OAuth through the integrated Swagger UI.
Prerequisites
To run this sample app on your local machine, you should be able to access Azure Active Directory in your tenant. Otherwise you can create a free Azure Account.
Related Read
- 한국어: TBD
- English: TBD
OpenAPI Spec for Authentication
It could be a good idea to take a look at the authentication spec defined in OpenAPI before going further.
-
type: defines what type of authentication method will be used. Currently, it acceptsAPI Key,HTTP,OAuth2, andOpenID 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 acceptsquery,header, orcookie. -
scheme: declares the auth scheme. It's required for HTTP auth and accepts eitherBasicorBearer. -
bearerFormat: usesJWTin most cases when using theBearertoken through the HTTP auth. -
flows: is required for theOAuth2auth. Its value can beimplicit,password,clientCredentials, orauthorizationCode. -
openIdConnectUrl: is necessary for theOpenID Connectauth. However, it is advised to use eitherOAuth2orBearerauth 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
Run the function app, and you will see the Swagger UI page.
Click the lock button on the right-hand side to enter the API key value. This value will be appended to the querystring parameter.
The result screen shows the API key passed through the querystring parameter, code.
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
Run the function app and see the Swagger UI page.
If you want to authenticate the endpoint, enter the API key value to the field, labelled as x-functions-key.
As a result, the API key was sent through the request header, x-functions-key.
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).
Run the app to see the Swagger UI like below.
To authenticate your endpoint, you should enter the Username and Password, added to the Authorization header.
The result screen shows the request header of Authorization with the base64 encoded value.
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).
Run the function app and see the Swagger UI page.
During the authentication, you are asked to enter the Bearer token value. The Authorization header will add the value.
The result screen shows the JWT value in the Authorization header.
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 for this time. Set the properties of OpenApiSecurityAttribute(...) (line #6-8).
-
Type:SecuritySchemeType.OAuth2 -
Flows:











