ErikWittern / openapi-snippet

Generates code snippets for given Swagger / Open API documents
MIT License
115 stars 66 forks source link

Do we support Cookie Authentication? #70

Open tmyl123 opened 3 years ago

tmyl123 commented 3 years ago

OpenAPI 3.0 has support for describing cookie authentication, it's a type of apiKey with in: cookie.

Since HTTP Snippet also support cookies, can we just leverage the cookies over here. https://github.com/ErikWittern/openapi-snippet/blob/b999cd08b2f52863709e41e09e1bd2d27ff2e5fb/openapi-to-har.js#L41-L50

Here's the fork I'm using for cookie authentication, I'm fairly new to OpenAPI stuff, so just assume where cookies can only been taken from security relative sections.

Test Input

const OpenAPISnippet = require("openapi-snippet");
const openApi = {
    servers: [
        {
            url: "http://api.example.com/api/v1",
        },
    ],
    paths: {
        "/books": {
            post: {
                security: [
                    {
                        cookieAuth: [],
                    },
                ],
            },
        },
    },
    components: {
        securitySchemes: {
            cookieAuth: {
                type: "apiKey",
                in: "cookie",
                name: "my-session",
            },
        },
    },
    security: [
        {
            cookieAuth: [],
        },
    ],
};
const targets = ["shell_curl"];

try {
    // console.log(openApi);
    const results2 = OpenAPISnippet.getEndpointSnippets(
        openApi,
        "/books",
        "post",
        targets
    );
    console.log(results2);
} catch (err) {
    console.log(err);
}

Output

{
  method: 'POST',
  url: 'http://api.example.com/api/v1/books',
  description: undefined,
  resource: 'books',
  snippets: [
    {
      id: 'shell_curl',
      title: 'Shell + Curl',
      content: 'curl --request POST \\\n' +
        '  --url http://api.example.com/api/v1/books \\\n' +
        '  --cookie my-session=REPLACE_KEY_VALUE'
    }
  ]
}