Thursday, November 30, 2023
HomeSoftware DevelopmentUse OAuth 2.0 tokens in your web site, app, and servers

Use OAuth 2.0 tokens in your web site, app, and servers

Posted by Peter Jacobsen, Technical Author

OAuth 2.0 is an open customary authorization framework for token-based authorization on the web. An OAuth 2.0 entry token is a string that the OAuth 2.0 shopper makes use of to make requests to the useful resource server, and hides the person’s identification or different data from the OAuth 2.0 shopper. Solely use entry tokens to make requests to the useful resource server.

Offline refresh tokens

Entry tokens periodically expire and change into invalid credentials for a associated API request. Should you requested offline entry to the scopes related to the token, you’ll be able to refresh an entry token with out prompting the person for permission, even when the person is not current.

As a greatest apply, set the expiration time for refresh tokens for somewhat longer than the entry tokens. For instance, should you set the expiration to half-hour for an entry token, set the refresh token’s expiration to 24 hours or longer.

For extra data, see Refreshing an entry token (offline entry).

On-line entry

Some apps might request that the person reauthenticate after a shorter time period, which depends on the entry token alone relatively than a refresh token. These apps have on-line entry as opposed to those who have a refresh token and are thought-about to have offline entry.

For extra data, see Refreshing an entry token (offline entry) and Refresh tokens.

JSON Net Token (JWT) and token expiration

To authenticate to Cloud IoT, every system should put together a JWT. JWTs are used for short-lived authentication between units and the MQTT or HTTP bridges.

JWTs are composed of three sections: a header, a payload that incorporates a declare set, and a signature. The header and payload are JSON objects which might be serialized to UTF-8 bytes after which encoded with Base64 URL encoding.

The JWT’s header, payload, and signature are concatenated with intervals. Consequently, a JWT usually takes this way:

{Base64url encoded header}.{Base64url encoded payload}.{Base64url encoded signature}

For extra data, see Utilizing JSON Net Tokens (JWTs) and Managing JWT token expiration.

Widespread token expiration paradigms

There are completely different insurance policies and methods that you should use to handle token expiration. You possibly can:

  • Monitor your HTTP responses, search for 401 HTTP responses, and reply accordingly.
  • Examine the token’s expiration date proactively to find out the validity of the token earlier than you make an HTTP request to the useful resource server.
  • Mix the earlier two methods to deal with expirations the place a legitimate token can expire through the request that causes a 401 HTTP Response.


Please enter your comment!
Please enter your name here

Most Popular

Recent Comments