Skip to content

Do we support Cookie Authentication? #70

@tmyl123

Description

@tmyl123

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.

const har = {
method: method.toUpperCase(),
url: baseUrl + getFullPath(openApi, path, method),
headers: getHeadersArray(openApi, path, method),
queryString: getQueryStrings(openApi, path, method, queryParamValues),
httpVersion: 'HTTP/1.1',
cookies: [],
headersSize: 0,
bodySize: 0,
};

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'
    }
  ]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions