I am writing a REST client in Java using the HttpCLient , the REST API that I access needs an auth token for every REST action. This token is valid for 24 hours.
The way I am handling this now is calling a "getAuth()
" method everytime I need to make a REST call which seems like an overhead on the auth server.
How can I conveniently store this auth token and manage its life cycle? Are there any documented best practices?
I thought of the following solution
public class MySession { String user; String pass; public MySession(String user, String pass) { this.user = user; this.pass = pass; } public getAuth() { //user user, pass to get auth token } }
and then pass the sessions object to any class that nees the token. If the token is expired, just call this method again
Token-based authentication works through this five-step process: Request: The user logs in to a service using their login credentials, which issues an access request to a server or protected resource. Verification: The server verifies the login information to determine that the user should have access.
To generate a token for your client, you can expose your own OAuth 2.0 endpoints and then sent the OAuth request on to Stormpath, essentially functioning as a proxy. Alternatively, you collect the client's credentials and use Stormpath (either with the SDK or via REST) to exchange these for an OAuth token.
If any of the third-party scripts you include in your page is compromised, it can access all your users' tokens. To keep them secure, you should always store JWTs inside an httpOnly cookie. This is a special kind of cookie that's only sent in HTTP requests to the server.
For brevity I'll assuming you're calling an endpoint that you can't change. How you should implement will heavily depend on whether the token is app or user based (one token for all users on a shared app instance or one token per user).
If it's one auth token for the entire app:
If it's one token per user:
I'm assuming you are using OAuth for authorization. Whether you are using JWT or other tokens is irrelevant to this situation.
When performing authorization you will be issued an access_token
with an expiration and, depending on the grant type you are requesting (Client credentials, Authorization code, Implicit, Resource owner), a refresh_token
.
The client should keep the access_token
and the expiration. The refresh_token, if issued, must be kept secret (beware of using the correct grant for your use case).
In subsequent calls, your client should not request new tokens on each call, it should use the stored access_token
.
Once the API starts returning 401 Unauthorized
, the access_token
has probably expired. Your client should try to refresh the access_token
using the refresh_token
if you got one.
If you have no refresh_token
or the refresh request also failed, because the refresh_token
is no longer valid, you can perform a new authorization flow.
You can use the expiration time as a clue to know when to get a new access_token
either through refresh or through a new full authorization flow. This will avoid the 401 Unauthorized
. In any case, your client should have a fall back policy when this response is received after having used a valid access_token
for some calls.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With