Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Java] Add support for Bearer Auth #1930

Merged
merged 10 commits into from
Feb 9, 2019
Merged

Conversation

davidwcarlson
Copy link
Contributor

@davidwcarlson davidwcarlson commented Jan 17, 2019

Fix proposed for Issue #457
Similar to Issue #1446 for typescript, Issue #1577 for python

Specs defined as follows currently generate HttpBasicAuth and send an
Authorization: Basic [base64Encode(username + ":" + password)] header

    components:
      securitySchemes:
        bearer:
          type: http
          scheme: bearer

This change will generate code which uses a new HttpBearerAuth class, which will send an Authorization: [scheme] [accessToken] header.

This change is slightly larger and more impactful than reusing the OAuth when CodegenSecurity.bearerBearer is true, but it allows for scheme values other than bearer.

This fix was enabled by the recent commit of 80ca67c

This PR is an alternative to PR #1972

PR checklist

  • Read the contribution guidelines.
  • Ran the shell script under ./bin/ to update Petstore sample so that CIs can verify the change. (For instance, only need to run ./bin/{LANG}-petstore.sh and ./bin/security/{LANG}-petstore.sh if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in .\bin\windows\.
  • Filed the PR against the correct branch: master, 3.4.x, 4.0.x. Default: master.
  • Copied the technical committee to review the pull request if your PR is targeting a particular programming language.

Description of the PR

(details of the change, additional tests that have been done, reference to the issue for tracking, etc)
Add a new Auth type for Bearer so that api specs using Bearer Authentication https://swagger.io/docs/specification/authentication/bearer-authentication/ use the correct auth (was BasicAuth).

Copying the Java technical committee as this is a change targeted to Java.
@bbdouglas @JFCote @sreeshas @jfiala @lukoyanov @cbornet @jeff9finger

@lorenzleutgeb
Copy link

What I am missing here is that both Bearer and Basic authentication are actually closely related and just two variants of HTTP authorization schemes. This connection is not really clear here. However it is a step in the right direction and probably a solution that generalises nicely can be put in place.

@davidwcarlson
Copy link
Contributor Author

davidwcarlson commented Jan 17, 2019 via email

}

@Override
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor suggestions: adding docstrings to these public methods.

davidwcarlson added a commit to davidwcarlson/openapi-generator that referenced this pull request Jan 24, 2019
See OpenAPITools#457
Also OpenAPITools#1446 for typescript, OpenAPITools#1577 for python

Specs defined as follows currently generate BasicAuth and send "Authorization: Basic [base64Encode(username + ":" + password)]"
components:
  securitySchemes:
    bearer:
      type: http
      scheme: bearer

This change will generate an OAuth header, which will send a "Authorization: Bearer [accessToken]"
This is a smaller, less-impactful change than introducing a BearerAuth object, but this change doesn't support scheme values other than bearer
See also OpenAPITools#1930
See OpenAPITools#457
Also OpenAPITools#1446 for typescript, OpenAPITools#1577 for python

Specs defined as follows currently generate BasicAuth and send an "Authorization: Basic [base64Encode(username + ":" + password)]" header
    components:
      securitySchemes:
        bearer:
          type: http
          scheme: bearer

This change will generate code which uses a new HttpBearerAuth class, which will send a "Authorization: [scheme] [accessToken]" header.
This change is slightly larger and more impactful than simply using OAuth for bearerBearer, but it allows for scheme values other than bearer.

This fix was enabled by the recent commit of OpenAPITools@80ca67c

This PR is an alternative to OpenAPITools#1972
@wing328
Copy link
Member

wing328 commented Jan 27, 2019

I've updated the samples to included new files, e.g. samples/client/petstore/java/feign/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java

Let's see if the CI tests pass.

Copy link
Member

@wing328 wing328 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@wing328 wing328 merged commit ef7b28d into OpenAPITools:master Feb 9, 2019
@wing328 wing328 changed the title fix 457 Support for Bearer Auth [Java] Add support for Bearer Auth Feb 9, 2019
@mstead
Copy link

mstead commented Feb 25, 2019

@wing328 Any idea when this change will be released?

@davidwcarlson
Copy link
Contributor Author

davidwcarlson commented Feb 25, 2019 via email

A-Joshi pushed a commit to ihsmarkitoss/openapi-generator that referenced this pull request Feb 27, 2019
* fix OpenAPITools#457 by introducing an HttpBearerAuth object

See OpenAPITools#457
Also OpenAPITools#1446 for typescript, OpenAPITools#1577 for python

Specs defined as follows currently generate BasicAuth and send an "Authorization: Basic [base64Encode(username + ":" + password)]" header
    components:
      securitySchemes:
        bearer:
          type: http
          scheme: bearer

This change will generate code which uses a new HttpBearerAuth class, which will send a "Authorization: [scheme] [accessToken]" header.
This change is slightly larger and more impactful than simply using OAuth for bearerBearer, but it allows for scheme values other than bearer.

This fix was enabled by the recent commit of OpenAPITools@80ca67c

This PR is an alternative to OpenAPITools#1972

* update petstore samples

* Update HttpBearerAuth mustache templates and samples

* correct the expected number of generated java client files

* update the retrofit2 HttpBearerAuth template and samples

* Add resttemplate-specific HttpBearerAuth mustache and samples

* add vertx-specific HttpBearerAuth template and samples

* add java webclient-specific HttpBearerAuth template and samples
@wing328
Copy link
Member

wing328 commented Mar 1, 2019

I just posted an update in #1462

@intelliot
Copy link
Contributor

Is this supposed to generate Javadocs? I'm seeing this:

[WARNING] Javadoc Warnings
[WARNING] /Users/user/company/project/client/java/src/main/java/com/company/auth/HttpBearerAuth.java:33: warning: no @return
[WARNING] public String getBearerToken() {
[WARNING] ^
[WARNING] /Users/user/company/project/client/java/src/main/java/com/company/auth/HttpBearerAuth.java:40: warning: no @param for bearerToken
[WARNING] public void setBearerToken(String bearerToken) {
[WARNING] ^

@intelliot
Copy link
Contributor

Hmm. This doesn't seem to actually work: generating a Java SDK using master creates docs that say Type: HTTP basic authentication, and the code itself still returns an HttpBasicAuth:

HttpBasicAuth bearerAuth = (HttpBasicAuth) defaultClient.getAuthentication("bearerAuth");

@wing328 wing328 added this to the 4.0.0 milestone Mar 11, 2019
@matthewbluezyoncom
Copy link

I agree with @intelliot
I have tried to test this fix using the openAPI generator maven plugin version=4.0.0-beta-2

And I see the same result, spec says:

    "securitySchemes": {
      "Bearer": {
        "type": "http",
        "scheme": "bearer"
      }
    }

and the code generated is

authentications.put("Bearer", new HttpBasicAuth());

I was expecting something like

authentications.put("Bearer", new HttpBearerAuth());

It would be great to have it confirmed that this feature is actually testable !

@davidwcarlson
Copy link
Contributor Author

What library are you specifying in your pom and generating code for?

From what I can tell, there is going to be some separate work required for each library (to modify the mustache template for ApiClient for your library similar to this code).

I admit was focused on trying to fix this for jersey2 for my own selfish purposes. Maybe this will point you in the right direction to submit a pull request or maybe I can take a crack if I get some time.

I hope this helps!

@matthewbluezyoncom
Copy link

@intelliot
I'm also creating a client for jersey2, but, I don't have an OAuth security scheme defined, only ApiKey and Http.
I will try and add an OAuth scheme to see if the methods are generated. This will enable me to test, but, alas I will not be able to proceed further until PR #2479 has been merged.
Many thanks for the prompt reply and the original work to add HttpBearerAuth 👏

@matthewbluezyoncom
Copy link

I added an OAuth scheme expecting to see the setBearerToken method HttpBearerAuth class.
Neither were generated.
I am going to stick with ApiKey and set the token via the key and Bearer scheme via the prefix

@wing328
Copy link
Member

wing328 commented Mar 27, 2019

#2485 has been merged into master to provide better Bearer authentication support on all Java API clients. Please pull the latest master to give it a try.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants