Creating a Function for Backend Custom Authentication
Scenarios
To protect backend services, you can use multiple external authentication systems by configuring one authentication mechanism. You need to create a FunctionGraph function for backend custom authentication and define the required authentication information in the function. The function then serves as a custom authentication backend to authenticate requests forwarded by APIG.
The following figure shows the process of calling APIs using custom authentication.
FunctionGraph is required for custom authorizers. If FunctionGraph is unavailable in the selected region, custom authorizers are not supported.
Procedure
- Create a function in FunctionGraph.
The function code must meet the following requirements (Python 2.7 is used as an example):
- The custom user data contained in the function code must be in the following format: event["user_data"].
- The custom user data corresponds to the user data defined for the custom authorizer. You can define the user data in any format.
- The response of the function cannot be greater than 1 MB and must be displayed in the following format:
{ "statusCode":200, "body": "{\"status\": \"allow\", \"context\": {\"user\": \"abc\"}}" }
The body field is a character string, which is JSON-decoded as follows:
{ "status": "allow/deny", "context": { "user": "abc" } }
- The status field is mandatory and is used to identify the authentication result. The authentication result can only be allow or deny. allow indicates that the authentication is successful, and deny indicates that the authentication fails.
- The context field is optional. It can be key-value pairs, but the key value cannot be a JSON object or an array.
The context field contains custom user data. After successful authentication, the user data is mapped to the backend parameters. The parameter name in context is case-sensitive and must be the same as the system parameter name. The parameter name in context must start with a letter and contain 1 to 32 characters, including uppercase letters, lowercase letters, digits, underscores (_), and hyphens (-).
After successful backend authentication, the value abc of user in context is mapped to the test parameter in the Header location of backend requests and passed to the backend service.
Example user data:
# -*- coding:utf-8 -*- import json import base64 def handler(event, context): exampleuserdata=base64.b64encode(event["user_data"]) resp = { 'statusCode': 200, 'body': json.dumps({ "status":"allow", "context":{ "user":exampleuserdata } }) } return json.dumps(resp)
- Test the function. In the Configure Test Event dialog box, select blank-template, and set the following test event:
{"user_data": "123"}
Click Save. Then click Test.
If the execution result is Success, the test is successful.
Next, you need to go to the APIG console to create a backend custom authorizer.
Follow-Up Operations
Create a custom authorizer for backend authentication on the APIG console. For details, see section "Create a custom authorizer" in the API Gateway User Guide.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot