DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Secure Your API With JWT: Kong OpenID Connect
  • Securing REST APIs With Nest.js: A Step-by-Step Guide
  • Navigating the API Seas: A Product Manager's Guide to Authentication
  • What Is API-First?

Trending

  • Monitoring Spring Boot Applications with Prometheus and Grafana
  • Working With Cowork: Don’t Be Confused
  • Modernization Is Not Migration
  • Architectural Evidence in Enterprise Java: Making Domain-Driven Design Visible
  1. DZone
  2. Software Design and Architecture
  3. Security
  4. OAuth Access Tokens or JSON Web Tokens (JWT) for Delivering a Secure API?

OAuth Access Tokens or JSON Web Tokens (JWT) for Delivering a Secure API?

When looking to secure your API, which security method do you use? In this article, we compare two of the more popular options among developers.

By 
James Warner user avatar
James Warner
·
Updated Oct. 18, 17 · Analysis
Likes (2)
Comment
Save
Tweet
Share
63.4K Views

Join the DZone community and get the full member experience.

Join For Free

Image title

Let’s begin with what they mean.

First of all, it's not really an either or scenario with OAuth and JSON Tokens as they are compatible - wherein JWT is a token format used by the authentication framework, OAuth.

JWT is a compact and self-contained mechanism, digitally authenticated and trusted, for transmitting data between various parties. They are extremely easy to use and are mainly used for implementing stateless authentication mechanisms.

OAuth 2 offers authorization flows for Java application development, along with mobile devices. It works with a central authorization server, which is used by the client to obtain access to a protected resource placed on the server. The access is only possible using a valid access token issued in the form of JSON – but it has no definitive structure assigned to it. OAuth 2.0 defines a protocol for passing your access token within the Authorization header. The resource server validates the signature using the following information:

  • Client

  • Lifetime

  • Scopes

  • Roles

Similarities

  • Token Presentation to the server: JWT authentication and OAuth2 appear very similar in presenting the token to the Resource Server. However, the former does not specify the methods used to obtain the token. This is where OAuth fills the void by defining various ways in which the Client can obtain an access token from the Authorization Server.

  • Used across services: Both JWT and OAuth2 give you one authorization server to handle all Logins/Registrations and generates the token – making it easy for corporate systems to have a secure environment. It removes all connections between auth-server and other servers, other than the pre-defined public key.

Differences

JSON is less verbose, which makes it compact in size. It becomes a better choice to be used in HTML and HTTP environments. The use of JWT at Internet scale increases the ease of client-side processing of tokens on various platforms simultaneously.

Why Should We Use OAuth2?

OAuth2 aims to simplify authorization to access protected data. It allows you access your data but protects the owner’s account credentials in the process. It allows the user to access data from one web account to the other by simply giving out tokens, instead of credentials. The token grants access to certain specific resources for a specific duration, which can be shared with a third party. This temporary data transfer authorization is done without providing anyone with complete access to all the data. Users can share verifiable assertions about themselves using OAuth2, without releasing personally identifiable information.

Cons of OAuth2:

OAuth2 protocol requires a secret string obtained from the service to be delegated. Developers find it difficult to manage this string on mobile devices, as it can be easily found in the application and misused.

Cons of JWT:

Compromised Secret Key: The one key method makes it slightly less reliable. If the Key is leaked, it compromises the whole system.

No Push Messages: Due to the lack of session records in the DB, identifying a client per user is not possible – hence, we cannot push messages.

Conclusion:

Using OAuth2 setup for authenticating multiple API platforms and applications, both in both public and private settings is a much more sensible way of doing things. JSON web tokens can work as your bearer tokens here and make life easier.

authentication JSON security API JWT (JSON Web Token)

Opinions expressed by DZone contributors are their own.

Related

  • Secure Your API With JWT: Kong OpenID Connect
  • Securing REST APIs With Nest.js: A Step-by-Step Guide
  • Navigating the API Seas: A Product Manager's Guide to Authentication
  • What Is API-First?

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook