-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
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
How to program the refresh token logic when using Swift generated code? #5462
Comments
Hey,
Then you assign the Hope this helps. |
This is also possible if someone is using URLSession.
Then you assign the |
Thanks a lot @4brunu Last question, what do you recommend me know if a request needs an access token or not? Right now, in my prototype I have this:
Which is working, but it is crappy using those endpoints to know it. In the open api specification file, I see:
But no mention in the generated code. Thanks a lot. |
Hum, sorry but I don't have a better solution for that. |
@4brunu |
I don't see how this can fit in the generator. :/ |
@4brunu |
Well, the refresh token logic I implemented is working properly. |
Problem: custom headers could be overwritten between request retries following, e.g. a token refresh ( for example, while implementing @4brunu refresh code : OpenAPITools#5462 (comment) ). A simple reordering of the modifiedRequest headers construction solves the problem.
…7527) * fix: correct handling of customHeaders Problem: custom headers could be overwritten between request retries following, e.g. a token refresh ( for example, while implementing @4brunu refresh code : #5462 (comment) ). A simple reordering of the modifiedRequest headers construction solves the problem. * fix: correct PetStore samples implementation [PR#7527] Co-authored-by: Franz Marini <[email protected]>
Do you guys have an example project for this? |
@Ricardo1980 Sorry for digging this issue up. I am wondering whether you guys did find a better solution for doing this instead of implementing a hardcoded whitelist of requests in your code base? |
Description
Hello,
I'm using openapi-generator, version 4.2.3, exactly this one:
openapi-generator generate -g swift5 -i my_openapi.yaml -o ./swift5 --library alamofire
The reason I want to use Alamofire is because my api (created by a workmate), uses access token and refresh token, and the access token expires every hour so the refresh token has to be used to get a new one, while the ongoing requests are paused until I get a new valid access token. Basically, if I get 401 in any request, I try to download the access token using the refresh token. If unauthorized, I logout the user, otherwise, I continue with the original request. That logic is very easy to implement using alamofire.
Without this generated code, I do something like:
In the adapter, I add the access token for all requests, except signup request.
In retrier, I add the refresh token logic.
I already did that in other app using Alamofire but a custom networking layer, not code autogenerated.
https://github.com/Alamofire/Alamofire/blob/master/Documentation/AdvancedUsage.md#requestadapter
https://github.com/Alamofire/Alamofire/blob/master/Documentation/AdvancedUsage.md#requestretrier
for the retrier I used code similar to this one, and working perfectly
https://stackoverflow.com/questions/42079180/understanding-alamofire-oauth-example
Researching the generated code, I see:
I guess I have to subclass AlamofireRequestBuilder and overwrite createSessionManager.
Perhaps I also have to subclass AlamofireRequestBuilderFactory and then pass this new instance to OpenAPIClientAPI.requestBuilderFactory
So, the question is, how can I attach the refresh token logic I explained to the generated code without modifying it? Is that possible? Any other suggestion?
How do you usually manage the refresh token logic when using autogenerated code?
Thanks for suggestions.
EDIT:
I can do something like this and inject it using
OpenAPIClientAPI.requestBuilderFactory = CustomAlamofireRequestBuilderFactory()
But AlamofireDecodableRequestBuilder is a subclass of AlamofireRequestBuilder and I cannot change that. I tried to copy/paste the entire AlamofireDecodableRequestBuilder class (creating CustomAlamofireDecodableRequestBuilder), changing the base class from AlamofireRequestBuilder to CustomAlamofireRequestBuilder, but it does not work because "credential" and "managerStore" are innacesible.
However, if I edit manually the autogenerated code and I use something like:
It works. But this is a crappy solution. It means I have to edit the generated code all the time is generated.
Any idea? How should I fix this?
openapi-generator version
4.2.3
OpenAPI declaration file content or url
No issues with this, the code generated works perfectly.
Command line used for generation
openapi-generator generate -g swift5 -i my_openapi.yaml -o ./swift5 --library alamofire
Related issues/PRs
I see several of them but regarding other languages, nothing about swift.
Suggest a fix/enhancement
Explained in the description.
The text was updated successfully, but these errors were encountered: