Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practices for managing auth token

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

like image 982
user_mda Avatar asked Feb 16 '17 15:02

user_mda


People also ask

How do you handle auth tokens?

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.

How do I manage OAuth tokens?

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.

Where should I store my auth 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.


2 Answers

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:

  • Store it in memory along with a time-to-live timestamp (or alternatively catch the token expired error, request a new token and retry the original request), refresh it if it doesn't exist/is expired
  • If you're concerned about re-requesting API tokens after an application restart also store it in the database and load it at startup if it exists

If it's one token per user:

  • Store it in your user session, it's exactly what sessions are used for, if you're authing users then they'll have a session and the overhead is already there
  • If you don't want to re-request a token everytime they login store their current token in the DB and and load it into their session when they login
like image 89
Dave L Avatar answered Sep 19 '22 18:09

Dave L


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.

like image 40
Daniel Cerecedo Avatar answered Sep 19 '22 18:09

Daniel Cerecedo