Script Development / API Authentication
For HTTP APIs generated by "Func API", additional interface authentication can be added.
Currently supported interface authentications are as follows:
Authentication Type | Description |
---|---|
Fixed Field | Verify that the request's Header, Query, or Body must contain a field with a specific value |
HTTP Basic | Standard HTTP Basic authentication (a login box will pop up when accessed in a browser) |
HTTP Digest | Standard HTTP Digest authentication (a login box will pop up when accessed in a browser) |
Authentication Function | Specify a self-written function as the authentication function |
Users can add authentication configurations in "Manage / API Authentication", and then specify the added authentication configuration in "Func API Configuration".
If high security is required, be sure to access the interface using HTTPS
1. Fixed Field Authentication
Fixed Field Authentication is the simplest authentication method, where the client and DataFlux Func agree to include a specific field and field value somewhere in the request (Header, Query, or Body), and attach this content with each call to complete authentication.
Assuming it is agreed that each request must include x-auth-token="my-auth-token"
in the header, then the following call will complete the authentication:
Text Only | |
---|---|
1 2 |
|
When configuring multiple fixed field authentications, a single match is considered as passing the authentication
For fields used for authentication in Query and Body, the system will automatically delete them after authentication is passed, and they will not be passed to the function
2. HTTP Basic / HTTP Digest
Authentication methods directly supported by browsers.
Interfaces using this authentication method will prompt the browser to pop up a username/password box when accessed directly in the browser's address bar.
If you need to access it programmatically, please refer to the following code:
Python | |
---|---|
1 2 3 4 5 6 7 8 |
|
3. Authentication Function
If the interface authentication method is complex or special (such as needing to connect to a business system, etc.), you can choose to write your own function for authentication.
The function used for authentication does not require parameters, and returning True
indicates successful authentication, returning other content or throwing an error indicates failure.
In the authentication function, you can use the built-in variable _DFF_HTTP_REQUEST
to get request-related information: Script Development / Built-in Features / Built-in Variables / _DFF_HTTP_REQUEST
Example | |
---|---|
1 2 3 |
|
It should be noted that when authentication fails, the response format of the interface will vary depending on the returned content:
Example | |
---|---|
1 2 3 |
|
Authentication failed, when directly returning False
, the response body will not contain any specific error information:
Interface Response Body | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Example | |
---|---|
1 2 3 |
|
Authentication failed, when returning a string, this string will be returned as the detail
field:
Interface Response Body | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Example | |
---|---|
1 2 3 |
|
Authentication failed, when returning JSON, this JSON will be returned as the detail
field:
Interface Response Body | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Example | |
---|---|
1 2 3 |
|
When the authentication function throws an error, since the Func framework cannot perceive whether this Exception belongs to business logic or the code itself, it will return generic error information as the detail
field:
Interface Response Body | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|