From 6db098eb8b57af98f8341d209e565715715278a3 Mon Sep 17 00:00:00 2001 From: chenxiaolei Date: Thu, 7 Mar 2024 15:10:51 +0800 Subject: [PATCH] feat: support for getting token from url parameter (#95) * support for getting token from url parameter * support for getting token from url parameter improve docs --- README.md | 12 +++++++++++- main.go | 5 +++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 43d1f9e..4c9d4d4 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ ### Custom Options **The following settings are optional and not required.** - `-port` or `-p` : Listening port. Default is `1188`. -- `-token` : Access token. If you have set it up, each request will need to include an `Authorization` header. +- `-token` : Access token. If you have set it up, each request will needs to include an `Authorization` header or `token` parameter in the parameters. - `-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. #### Requesting a token-protected **DeepLX API** instance using the `curl` @@ -81,6 +81,16 @@ curl -X POST http://localhost:1188/translate \ "target_lang": "DE" }' ``` +or +``` +curl -X POST http://localhost:1188/translate?token=your_access_token \ +-H "Content-Type: application/json" \ +-d '{ + "text": "Hello, world!", + "source_lang": "EN", + "target_lang": "DE" +}' +``` ### Run with Docker ```bash diff --git a/main.go b/main.go index 57f32ff..5995e99 100644 --- a/main.go +++ b/main.go @@ -274,8 +274,9 @@ func main() { c.BindJSON(&req) if cfg.Token != "" { - providedToken := c.GetHeader("Authorization") - if providedToken != "Bearer "+cfg.Token { + providedTokenInQuery := c.Query("token") + providedTokenInHeader := c.GetHeader("Authorization") + if providedTokenInHeader != "Bearer "+cfg.Token && providedTokenInQuery != cfg.Token { c.JSON(http.StatusUnauthorized, gin.H{ "code": http.StatusUnauthorized, "message": "Invalid access token",