swagger-api / swagger-api/swagger-codegen

[JAVA] JWT Bearer token auth not implemented

Open
#11,847 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

Defining a security scheme as type: bearer does not generate an appropriate OkHttpClient Interceptor. It has OAuth2, Basic & ApiKey but not any others.

Swagger-codegen version

3.0.34

Swagger declaration file content or url
    ...
    "securitySchemes": {
      "Bearer": {
        "type": "http",
        "description": "Bearer Authentication with JWT Token",
        "scheme": "bearer",
        "bearerFormat": "JWT"
      }
    },
    "security": [
      {
        "Bearer": []
      }
    ]
Command line used for generation
{
    "modelPackage": "redacted",
    "apiPackage": "redacted",
    "artifactId": "redacted",
    "artifactVersion": "1.0.0",
    "library": "retrofit2",
    "useRxJava2": "true",
    "dateLibrary": "java8",
    "java8": "true"
}
Steps to reproduce
Related issues/PRs
Suggest a fix/enhancement

Add a simple Interceptor that adds the Authorization: Bearer <token> header and make ApiClient#setAccessToken look for that interceptor.

BearerTokenAuth.java
import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class BearerTokenAuth implements Interceptor {

    private String token;

    public String getAccessToken() {
        return token;
    }

    public void setAccessToken(String token) {
        this.token = token;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();

        // If the request already have an authorization (eg. Basic auth), do nothing
        if (request.header("Authorization") == null) {
            request = request.newBuilder()
                    .addHeader("Authorization", "Bearer " + token)
                    .build();
        }
        return chain.proceed(request);
    }
}
ApiClient.java
public ApiClient(String[] authNames) {
    this();
    for (String authName : authNames) {
        Interceptor auth = null;
        if ("Bearer".equals(authName)) {
            auth = new BearerTokenAuth();
        } else {
            throw new RuntimeException("auth name \"" + authName + "\" not found in available auth names");
        }

        addAuthorization(authName, auth);
    }
}
...
/**
     * Helper method to pre-set the oauth access token of the first oauth found in the apiAuthorizations (there should be only one)
     *
     * @param accessToken Access token
     * @return ApiClient
     */
    public ApiClient setAccessToken(String accessToken) {
        for (Interceptor apiAuthorization : apiAuthorizations.values()) {
            if (apiAuthorization instanceof OAuth) {
                OAuth oauth = (OAuth) apiAuthorization;
                oauth.setAccessToken(accessToken);
                return this;
            } else if (apiAuthorization instanceof BearerTokenAuth) {
                final BearerTokenAuth bearerTokenAuth = (BearerTokenAuth) apiAuthorization;
                bearerTokenAuth.setAccessToken(accessToken);
                return this;
            }
        }
        return this;
    }

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 locating the Java retrofit2 templates or generated ApiClient.java and compare the existing OAuth, Basic, and ApiKey interceptor handling. Review the proposed BearerTokenAuth.java and ApiClient#setAccessToken behavior, then verify that an OpenAPI bearer security scheme generates the interceptor and applies the Authorization header.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, security, tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.