swagger-api / swagger-api/swagger-codegen

CSRF Token Missing

Open
#7,843 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

This is a question about using a CSRF cookie name/header name with the generated JS library.

Swagger-codegen version

2.2.1

Swagger declaration file content or url
Command line used for generation

java -jar ~/Downloads/swagger-codegen-cli-2.2.1.jar generate -l javascript -i ~/Downloads/schema.json -o ~/Documents/heathcliff/clients/javascript

Steps to reproduce

N/A

Related issues/PRs
Suggest a fix/enhancement

Okay, so to get a broader sense of what I am doing:

  • I am using Docker/NGINX/Gunicorn to serve up a Django DRF application
  • The application uses session authentication.
  • I have an endpoint, using drf_yasg, to generate a JSON schema file.
  • I then generate the javascript client.
  • I upload this generated client to npm
  • I use webpack to stuff into budle.js
  • When I use the sample code in the markdown file, provided by client docs, I get a 403 error indicating that the CSRF token is missing

Question is: how can I tell the JS client to use a CSRF token. In COREAPI, i can do something like:

var coreapi = window.coreapi;

// Initialize a client & load the schema document
var url = 'https://localhost/api/v1/schema.json';

var auth = new coreapi.auth.SessionAuthentication({
    csrfCookieName: 'csrftoken',
    csrfHeaderName: 'X-CSRFToken'
})

var client = new coreapi.Client({ auth: auth })

is there something similar with the generated JS code? Do i have to specify a security parameter in settings.py besided basic?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by inspecting the generated JavaScript client's request and authentication code and the sample client documentation, then compare its requests with Django REST Framework's CSRF requirements. Done means the generated client can be configured to send the configured CSRF cookie and header, and the documented request no longer receives a 403.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, docker, javascript, nginx, python, webpack
Domain
api, authentication, security
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.