mirror of
https://github.com/OwO-Network/DeepLX.git
synced 2024-11-16 13:22:20 +08:00
feat: support access token-protected in /v2/translate
This commit is contained in:
parent
15cca97b81
commit
e15e63b797
20
README.md
20
README.md
|
@ -2,7 +2,7 @@
|
||||||
* @Author: Vincent Young
|
* @Author: Vincent Young
|
||||||
* @Date: 2022-10-18 07:32:29
|
* @Date: 2022-10-18 07:32:29
|
||||||
* @LastEditors: Vincent Young
|
* @LastEditors: Vincent Young
|
||||||
* @LastEditTime: 2023-11-28 00:24:20
|
* @LastEditTime: 2024-04-16 14:52:46
|
||||||
* @FilePath: /DeepLX/README.md
|
* @FilePath: /DeepLX/README.md
|
||||||
* @Telegram: https://t.me/missuo
|
* @Telegram: https://t.me/missuo
|
||||||
*
|
*
|
||||||
|
@ -69,9 +69,23 @@
|
||||||
- `-port` or `-p` : Listening port. Default is `1188`.
|
- `-port` or `-p` : Listening port. Default is `1188`.
|
||||||
- `-token` : Access token. If you have set it up, each request needs to include `Authorization` in the **Headers** or `token` parameter in the **URL Params**.
|
- `-token` : Access token. If you have set it up, each request needs to include `Authorization` in the **Headers** or `token` parameter in the **URL Params**.
|
||||||
- `-authkey` : DeepL Official `AuthKey`. If you have set it up, after the 429 response, the official AuthKey will be used for the request. If multiple authKeys are used simultaneously, they need to be separated by commas.
|
- `-authkey` : DeepL Official `AuthKey`. If you have set it up, after the 429 response, the official AuthKey will be used for the request. If multiple authKeys are used simultaneously, they need to be separated by commas.
|
||||||
|
- `/v2/translate` : This endpoint is fully compatible with the DeepL official API. When using this endpoint, please strictly adhere to the request styles outlined in the official DeepL documentation. Note that in this endpoint, please use `DeepL-Auth-Key $token` in the `Authorization`, which is actually the Access Token, not the official `Auth Key` of DeepL.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Example of requesting a token-protected /v2/translate endpoint
|
||||||
|
curl -X POST 'http://127.0.0.1:1188/v2/translate' \
|
||||||
|
--header 'Authorization: DeepL-Auth-Key [yourAccessToken]' \
|
||||||
|
--header 'Content-Type: application/json' \
|
||||||
|
--data '{
|
||||||
|
"text": [
|
||||||
|
"Hello, world!"
|
||||||
|
],
|
||||||
|
"target_lang": "DE"
|
||||||
|
}'
|
||||||
|
```
|
||||||
|
|
||||||
#### Requesting a token-protected **DeepLX API** instance using the `curl`
|
#### Requesting a token-protected **DeepLX API** instance using the `curl`
|
||||||
```
|
```bash
|
||||||
curl -X POST http://localhost:1188/translate \
|
curl -X POST http://localhost:1188/translate \
|
||||||
-H "Content-Type: application/json" \
|
-H "Content-Type: application/json" \
|
||||||
-H "Authorization: Bearer your_access_token" \
|
-H "Authorization: Bearer your_access_token" \
|
||||||
|
@ -82,7 +96,7 @@ curl -X POST http://localhost:1188/translate \
|
||||||
}'
|
}'
|
||||||
```
|
```
|
||||||
or
|
or
|
||||||
```
|
```bash
|
||||||
curl -X POST http://localhost:1188/translate?token=your_access_token \
|
curl -X POST http://localhost:1188/translate?token=your_access_token \
|
||||||
-H "Content-Type: application/json" \
|
-H "Content-Type: application/json" \
|
||||||
-d '{
|
-d '{
|
||||||
|
|
22
main.go
22
main.go
|
@ -1,8 +1,8 @@
|
||||||
/*
|
/*
|
||||||
* @Author: Vincent Yang
|
* @Author: Vincent Yang
|
||||||
* @Date: 2023-07-01 21:45:34
|
* @Date: 2023-07-01 21:45:34
|
||||||
* @LastEditors: Vincent Yang
|
* @LastEditors: Vincent Young
|
||||||
* @LastEditTime: 2024-04-13 18:50:25
|
* @LastEditTime: 2024-04-16 14:47:43
|
||||||
* @FilePath: /DeepLX/main.go
|
* @FilePath: /DeepLX/main.go
|
||||||
* @Telegram: https://t.me/missuo
|
* @Telegram: https://t.me/missuo
|
||||||
* @GitHub: https://github.com/missuo
|
* @GitHub: https://github.com/missuo
|
||||||
|
@ -343,7 +343,22 @@ func authMiddleware(cfg *Config) gin.HandlerFunc {
|
||||||
if cfg.Token != "" {
|
if cfg.Token != "" {
|
||||||
providedTokenInQuery := c.Query("token")
|
providedTokenInQuery := c.Query("token")
|
||||||
providedTokenInHeader := c.GetHeader("Authorization")
|
providedTokenInHeader := c.GetHeader("Authorization")
|
||||||
if providedTokenInHeader != "Bearer "+cfg.Token && providedTokenInQuery != cfg.Token {
|
|
||||||
|
// Compatability with the Bearer token format
|
||||||
|
if providedTokenInHeader != "" {
|
||||||
|
parts := strings.Split(providedTokenInHeader, " ")
|
||||||
|
if len(parts) == 2 {
|
||||||
|
if parts[0] == "Bearer" || parts[0] == "DeepL-Auth-Key" {
|
||||||
|
providedTokenInHeader = parts[1]
|
||||||
|
} else {
|
||||||
|
providedTokenInHeader = ""
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
providedTokenInHeader = ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if providedTokenInHeader != cfg.Token && providedTokenInQuery != cfg.Token {
|
||||||
c.JSON(http.StatusUnauthorized, gin.H{
|
c.JSON(http.StatusUnauthorized, gin.H{
|
||||||
"code": http.StatusUnauthorized,
|
"code": http.StatusUnauthorized,
|
||||||
"message": "Invalid access token",
|
"message": "Invalid access token",
|
||||||
|
@ -352,6 +367,7 @@ func authMiddleware(cfg *Config) gin.HandlerFunc {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Next()
|
c.Next()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user