为什么我无法将 JWT 令牌传递给我的 swagger

问题描述 投票:0回答:1

我正在尝试向我的 swagger 发送一个令牌来验证自己的身份,但它没有收到任何东西。我有一个 /connect 路由,它允许我生成一个令牌,然后有一个 /get 路由以在控制器中使用此令牌。但是,当我发送令牌时,什么也没有发生,它要么告诉我没有令牌,要么告诉我我的令牌为空。

这是我的招摇:

{
  "swagger": "2.0",
  "info": {
    "title": "Documentation API",
    "version": "1.0.0",
    "description": "API pour un site de gestion de reservations pour 3 chambres d'hôtes chez un particulier."
  },

  "servers": [
    {
      "url": "http://localhost:5000/",
      "description": "Development server"
    }
  ],
  "paths": {
    "/clients/get": {
      "get": {
        "tags": [
          "Clients"
        ],
        "description": "",
        "security": [
          {
            "Bearer": [] 
          }
        ],

        "responses": {
          "200": {
            "description": "OK"
          },
          "401": {
            "description": "Unauthorized"
          },
          "404": {
            "description": "Not Found"
          }
        }
      }
    },
    "/clients/connect": {
      "post": {
        "tags": [
          "Clients"
        ],
        "description": "Connectez-vous en utilisant un email et un mot de passe.",
        "parameters": [
          {
            "in": "formData",
            "name": "email",
            "type": "string",
            "required": true,
            "description": "Votre email"
          },
          {
            "in": "formData",
            "name": "password",
            "type": "string",
            "required": true,
            "description": "Votre mot de passe"
          }
        ],
        "responses": {
          "200": {
            "description": "OK"
          },
          "404": {
            "description": "Not Found"
          }
        }
      }
    },


  "securityDefinitions": {
    "Bearer": {
      "type": "apiKey",
      "name": "Authorization",
      "in": "header"
    }
  },

"security": {
  "Bearer": []
}


我已经尝试使用安全 bearerAuth 但它不起作用。

这是我的 /get 功能:

const findOneClients = async (req, res) => {
    try {
        const token = req.header('Authorization');
        if (!token) {
            return res.status(401).send({ Error: 'Token JWT manquant dans l\'en-tête Authorization' });
        }

        const decodedToken = jwt.verify( token.split(' ')[1], process.env.TOKEN_SECRET);

        const id = decodedToken.id;
        const client = await Client.findOne({ id: id });
        
        if (!client) {
            return res.status(404).send({ Error: `Aucun client trouvé avec l'ID : ${id}` });
        }
        // Decrypter adresse
        const decryptAdresse = crypto.AES.decrypt(client.adresse, process.env.CRYPTO_SECRET);
        client.adresse = decryptAdresse.toString(crypto.enc.Utf8);

        // Decrypter ville
        const decryptVille = crypto.AES.decrypt(client.ville, process.env.CRYPTO_SECRET);
        client.ville = decryptVille.toString(crypto.enc.Utf8);

        // Decrypter code postal
        const decryptCodePostal = crypto.AES.decrypt(client.codePostal, process.env.CRYPTO_SECRET);
        client.codePostal = decryptCodePostal.toString(crypto.enc.Utf8);

        // Decrypter telephone
        const decryptTelephone = crypto.AES.decrypt(client.telephone, process.env.CRYPTO_SECRET);
        client.telephone = decryptTelephone.toString(crypto.enc.Utf8);
        
        return res.status(200).json(client);
    } catch (e) {
        addLog("error", e, "client.controller.js");
    }
};

有人有主意吗?

javascript node.js json swagger swagger-ui
1个回答
0
投票

如果您使用 JWT 授权,您应该有一些配置字段,而不仅仅是空白的 [] 括号

 "securitySchemes":{
         "bearer":{
            "scheme":"bearer",
            "bearerFormat":"JWT",
            "type":"http"
         }
      },

招摇文档

并为某些端点设置安全性或全局应用于所有端点

© www.soinside.com 2019 - 2024. All rights reserved.