Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get expire time of OAuth session

To grant or revoke access to my webapis, I use OAuth password- and tokenrefreshworkflow.

If I understand everything correctly the workflow should be something like this:

  1. Authenticate with username / password / client id
  2. Retrieve accestoken, refreshtoken and expire date
  3. Start timeout in client to refresh your token after expired token time
  4. Go on with bullet 2 -> and so on..

The progress above works fine so far. My problem is, that I don't get the expire time out of the users principle after the authentication request. So if I work with stateles webclients, I need to renew my token every request to retrieve a new expire date, even if the users token is valid :/

What I want is something like a /api/session/information service, that provides general information about the current session of an authenticated user.

How do I retrieve my expire date =)

[HttpGet]
[ActionName("information")]
public HttpResponseMessage Information(BaseRequest request)
{

    var p = Request.GetRequestContext().Principal;

    /* here i need help =) */
}
like image 746
Nando Avatar asked Apr 03 '14 06:04

Nando


People also ask

How can I check my OAuth token expiry date?

You can confirm your expiration date anytime by going to Settings > WorkSpace Settings > Social Accounts. Then simply look under the Token Status to learn the expiration date of your accounts token. (This means ContentStudio has access to your social networks for 39 days.

Which is the expired time of access token?

By default, access tokens are valid for 60 days and programmatic refresh tokens are valid for a year.

How long does OAuth code last?

The authorization code must expire shortly after it is issued. The OAuth 2.0 spec recommends a maximum lifetime of 10 minutes, but in practice, most services set the expiration much shorter, around 30-60 seconds.

How long should session tokens last?

The validity period of the session token is typically an hour. However, this can vary per portal and environment based on a backend setting.


1 Answers

Just to expand on Henrik N.'s answer a little. If you're in C# then you can use JWTSecurityTokenHandler within System.IdentityModel.Tokens.Jwt (Nuget: Install-Package System.IdentityModel.Tokens.Jwt) to read the token and the resulting JwtSecurityToken object gives you some handy properties, one of which is ValidTo which converts the exp claim into a DateTime object for you E.g.:

var tokenString = GetTokenString(); // Arbitrary method to get the token
var handler = new JwtSecurityTokenHandler();
var token = handler.ReadToken(tokenString) as JwtSecurityToken;
var tokenExpiryDate = token.ValidTo;

// If there is no valid `exp` claim then `ValidTo` returns DateTime.MinValue
if(tokenExpiryDate == DateTime.MinValue) throw new Exception("Could not get exp claim from token");

// If the token is in the past then you can't use it
if(tokenExpiryDate < DateTime.UtcNow) throw new Exception($"Token expired on: {tokenExpiryDate}");

// Token is valid
like image 88
lee_mcmullen Avatar answered Sep 23 '22 15:09

lee_mcmullen