OpenAPITools / OpenAPITools/openapi-generator

[BUG] Feign OAuth RequestInterceptor not caching access tokens

Open
#13,339 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When using the Feign Java client with OAuth2, a new access token is requested for each interaction, even though a valid and not yet expired access token is still in memory. I think the bug is located in this file:

https://github.com/OpenAPITools/openapi-generator/blob/fea42b547ed61dcfdfc479cab6f68a601ec5adde/modules/openapi-generator/src/main/resources/Java/libraries/feign/auth/OAuth.mustache#L80

In line 80 a current time in milliseconds is compared to the expiration time in seconds. This condition is true in (almost) all cases, so a new access token is requested for each API request.

openapi-generator version

I encountered this issue in version 6.0.1 and was able to work around it by downgrading to 5.4.0

OpenAPI declaration file content or url
Generation Details

very similar to https://github.com/OpenAPITools/openapi-generator/blob/master/bin/configs/java-feign-no-nullable.yaml

Steps to reproduce
  • Use Feign with OAuth2 (in my case with an OauthClientCredentialsGrant)
  • make request to API using generated client -> token is cached in variable accessToken and expiration time in expirationTimeSeconds
  • send another request before expirationTimeSeconds is reached -> updateAccessToken() is invoked although the token is not expired
Related issues/PRs
Suggest a fix

change this line:
https://github.com/OpenAPITools/openapi-generator/blob/fea42b547ed61dcfdfc479cab6f68a601ec5adde/modules/openapi-generator/src/main/resources/Java/libraries/feign/auth/OAuth.mustache#L80

to
if (expirationTimeSeconds == null || System.currentTimeMillis() >= expirationTimeSeconds * MILLIS_PER_SECOND) {

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 in modules/openapi-generator/src/main/resources/Java/libraries/feign/auth/OAuth.mustache around line 80 and compare the millisecond time source with expirationTimeSeconds. Reproduce the Feign OAuth client-credentials flow by making two requests before expiration; done means the second request reuses the cached token instead of invoking updateAccessToken().

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, authentication
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.