best practices for access and refresh token re-use
See original GitHub issueWhat is considered the best usage of creating a CF client when we have an access and refresh token stored (like the CLI client does)?
Current we use the PasswordGrantTokenProvider
to extract the access and refresh token from the login response and store that locally. When we need to connect to CF again (with a new instance of the client) we implemented a class from the TokenProvider
interface that has the accessToken, and when this is invalid we renew it in the invalidate() method using a RefreshTokenGrantProvider
.
So the question is, what is the best approach to create a TokenProvider
with a known access and refresh token?
Our current implementation looks like this
final Properties props = new Properties();
TokenProvider tp = new TokenProvider() {
Mono<String> accessToken = Mono.just(props.getProperty("accessToken"));
@Override
public Mono<String> getToken(ConnectionContext connectionContext) {
return accessToken;
}
@Override
public void invalidate(ConnectionContext connectionContext) {
DefaultConnectionContext newConCtx = DefaultConnectionContext.builder().from(connectionContext).build();
RefreshTokenGrantTokenProvider newToken = RefreshTokenGrantTokenProvider.builder()
.token(props.getProperty("refreshToken"))
.build();
try {
accessToken = newToken.getToken(newConCtx)
.doOnSuccess(s -> System.out.println("Got a new token: " + s));
} catch (Exception e) {
throw new IllegalStateException("Something is wrong renewing our token", e);
}
System.out.println("We'll get you a new token!");
}
};
Please advice
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Token Best Practices - Auth0
Store and reuse: Reduce unnecessary roundtrips that extend your application's attack surface, and optimize plan token limits (where applicable) by storing ...
Read more >Refresh access tokens - Okta Developer
This guide explains how to refresh access tokens with Okta. Learning outcomes. Understand how to set up refresh token rotation. Refresh access tokens....
Read more >A Critical Analysis of Refresh Token Rotation in Single-page ...
In practice, this means that if an attacker manages to steal a refresh token from a frontend application, they can use that token...
Read more >Antipattern: Set a long expiration time for OAuth tokens
Set the expiration time for refresh tokens in such a way that it is valid for a little longer period than the access...
Read more >OAuth best practices - eBay Developers Program
OAuth token best practices · Access tokens are short-lived in that they expire relatively quickly after they have been minted. · Refresh tokens...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@cwesdorp It’s important to remember that there are only two types that maintain any significant state, the
ConnectionContext
and theTokenProvider
. In your multi-tenant situation I’d recommend havingConnectionContext
used by all requests (if there is a single target host, multiple if there are multiple target hosts)TokenProvider
s keyed by the user*Client
s created on each incoming requestBy far the most expensive (resource-wise) component is the
ConnectionContext
. It contains the thread and connection pools and is designed to be used for any and all requests, even with different users. TheTokenProviders
store valid tokens, resulting in fewer network calls for them and renegotiate when a401
is received. Since there is some unnecessary overhead in negotiating a token when you’ve got a valid one it makes sense to cache these. Beyond that, aCloudFoundryClient
(and its peers) is totally stateless, simply combining aConnectionContext
and aTokenProvider
to make a REST call. You can create and destroy these quickly, easily, and regularly with no real penalty.The client is designed so that you can mux
ConnectionContext
s andTokenProviders
in any combination and they’ll behave properly. This allows you to cache expensive resources in memory and mix and match them in lightweight clients as needed.@nebhale Thanks for the guide lines. I’ll understand the idea and have a sample application from which I came to that conclusion, good that you confirm this.