Enable Bearer authentication for an HTTP trigger
Configure an HTTP trigger in Function Compute to require a Bearer token, so that authorized users can access your function in a simple and secure manner.
Background information
Function Compute supports enabling Bearer authentication for an HTTP trigger. With Bearer authentication, you use the Function Compute console to configure the token information that grants access to your function. When a client sends a request, it includes valid token information in the Authorization header. If the token data in the request matches the token data configured on the trigger, the client can access the function.
Prerequisites
A function is created and an HTTP trigger is created for the function. For instructions, see Function creation and Configure an HTTP trigger.
Limits
Each token name must be unique within a single trigger, can be up to 128 characters in length, and can contain only letters, digits, underscores, and hyphens. A token name cannot start with a digit or a hyphen.
The value of each token must be 32 to 128 characters in length and can contain only the standard Base64 characters 'A-Z', 'a-z', '0-9', '+', '/', '=', '-', '~', and '.'.
Each HTTP trigger supports 1 to 20 tokens.
The token values of different triggers and of tokens within the same trigger should be different. Whenever possible, do not use common permutations and combinations as token values, to avoid security issues caused by overly simple token data.
For Bearer authentication, use the HTTPS protocol in the production environment. The HTTP protocol is intended only for development and testing. Function Compute assumes no security responsibility for token leaks caused by the use of the HTTP protocol.
Function Compute is responsible only for storing and verifying the token information that you configure. You are responsible for managing your tokens. Promptly rotate leaked tokens and tokens that have been proven insecure, and proactively rotate tokens that have been in use for a long time.
Procedure
Step 1: Configure Bearer authentication
Log on to the Function Compute console. In the left-side navigation pane, click Functions.
In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.
On the function details page, click the Triggers tab, and then, in the Actions column of the HTTP trigger, click Edit.
In the Edit Trigger panel, set the following configuration items, and then click OK.
Authentication Method: Select Bearer Authentication. For Token Type, select Opaque. In Authentication Token, set the value of
tokenDatato your own token value.To disable a token, set the
enablefield tofalse.
If you need to configure multiple tokens, use the following format:
{ "tokens": [ { "enable": true, "tokenData": "token-8g7f2a2c9fc23hid82593421g995", "tokenName": "tokenName-20i" }, { "enable": true, "tokenData": "token-8g7f2a2c9fc23hid82593421g995", "tokenName": "tokenName-20i" } ] }
Step 2: Verify the operation
Use the curl tool to initiate a verification request that carries the Authorization header.
curl --data <your-data> -X <request-method> -H "Authorization: Bearer <your-token>" https://<your-HTTP-trigger-address>The following command uses example values:
curl -X POST -H "Authorization: Bearer token-c9e25351******" https://******.cn-hangzhou.fcapp.runFAQ
Why is the error message "Authorization header is expected but missing" returned when I access the domain name after Bearer authentication is enabled?
This message indicates that the request for accessing the HTTP trigger does not include the Authorization header. Add the Authorization information to the request.
Why is the error message "access denied due to invalid bearer token" returned when I access the domain name after Bearer authentication is enabled?
This message indicates that the request for accessing the HTTP trigger does not include valid token information. Check whether the token data is transmitted correctly. The token data comes from the value of tokenData.
Are additional fees incurred after Bearer authentication is enabled?
No. The gateway-related features that Function Compute provides by default are billed as part of function invocations. Therefore, no additional fees are incurred regardless of whether you enable Bearer authentication.