"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Guide to SafeLine Open API: Secure Your Access with API Tokens

Guide to SafeLine Open API: Secure Your Access with API Tokens

Published on 2024-11-01
Browse:412

SafeLine Open API Guide: API Token Authentication

In the latest version of SafeLine (v6.6.0 and above), the Open API now supports API token-based authentication. Although official API documentation is not currently available, you can scrape it yourself if needed.

Requirements:

  • You must be logged in using the default admin account to access this feature.
  • SafeLine version must be at least 6.6.0.

GitHub:https://github.com/chaitin/SafeLine
Demo:https://demo.waf.chaitin.com:9443/dashboard

How to Use the Open API

Step 1: Create an API Token

First, navigate to System Management in SafeLine and generate an API token.

Guide to SafeLine Open API: Secure Your Access with API Tokens

Step 2: Include the API Token in Your Requests

When making API calls, you need to include the token in the request headers like this:

"X-SLCE-API-TOKEN": "Your API Token from SafeLine"

Example: Using API Token in Python to Add a Site

Below is an example of how to use the SafeLine Open API with an API token to add a new site in Python.

import requests
import json

# Define the header with your API token
header = {
    "X-SLCE-API-TOKEN": "Your API Token from SafeLine"
}

# API endpoint to add a site
url = 'https://:9443/api/open/site'

# Payload containing site details
payload = {
    "ports": ["80"],
    "server_names": ["*"],
    "upstreams": ["http://127.0.0.1:9443"],
    "comment": "",
    "load_balance": {
        "balance_type": 1
    }
}

# Convert payload to JSON
payload = json.dumps(payload)

# Send the POST request
response = requests.post(url=url, headers=header, data=payload, verify=False)

# Check the response
print(response.status_code)
print(response.json())

Explanation:

  • The header contains the required API token for authentication.
  • The URL is the endpoint for adding a site (/api/open/site).
  • The payload specifies details about the site such as ports, server names, upstreams, and load-balancing configuration.

By following these steps, you can securely interact with SafeLine's Open API using token-based authentication, enabling you to automate and manage your web security setup with ease.

Release Statement This article is reproduced at: https://dev.to/lulu_liu_c90f973e2f954d7f/guide-to-safeline-open-api-secure-your-access-with-api-tokens-413k?1 If there is any infringement, please contact [email protected] delete
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3