Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Passport vs JWT vs Oauth2 vs Auth0

Confusion about API auth types in Laravel?

I'm currently learning how to create an API in Laravel and I found myself into this confusing concepts. After a few days of research and practice, I could finally understand enough this concepts to make a mini-guide. I had to look into a lot of separate webpages so I will make my best attempt to explain the relationship between them.

like image 530
Luis L Avatar asked Dec 11 '17 03:12

Luis L


People also ask

Is Auth0 better than JWT?

Auth0 has a broader approval, being mentioned in 121 company stacks & 55 developers stacks; compared to JSON Web Token, which is listed in 29 company stacks and 15 developer stacks.

Is OAuth2 better than JWT?

JWT token vs oauth token: JWT defines a token format while OAuth deals in defining authorization protocols. JWT is simple and easy to learn from the initial stage while OAuth is complex. OAuth uses both client-side and server-side storage while JWT must use only client-side storage. JWT has limited scope and use cases.

Is Passport better than JWT?

JSON Web Token and Passport can be primarily classified as "User Management and Authentication" tools. JSON Web Token and Passport are both open source tools. It seems that Passport with 15.9K GitHub stars and 936 forks on GitHub has more adoption than JSON Web Token with 2.59K GitHub stars and 259 GitHub forks.

What is difference between JWT and Passport laravel?

The "tymondesigns/jwt-auth" is a PHP Laravel implementation of the JWT protocol. On the other hand, Passport also uses JWT by default plus a huge extra, a complete Oauth2 implementation. Regarding the functionality, as I said they both use JWT thus you can use whichever you like to authentication via tokens.


1 Answers

What are these concepts?

  • Passport is an official Laravel package that implements Oauth2 and JWT.
  • Auth0 is an authentication and authorization service. It is kinda "all in one" solution for API auth. It implements JWT by default and can implement Oauth2 as well as many other protocols.
  • OAuth2 is an authorization framework or protocol that enables applications(the ones you're going to build) to give limited access to user accounts such as Facebook, GitHub, etc... Imagine that your app grew big enough to offer a "Sign in with APP NAME HERE" or provide limited access to your users account to other third-party applications. With Oauth2 you can achieve this among many other things.
  • JWT is a token format. In other words, it is how data will be arranged to create a token. Another token format would be SWT.

In other words:

  • Passport implements -> Oauth2 and JWT.
  • Auth0 can implement -> Oauth2 or many other protocols like LDAP, OPEN ID, etc...
  • Oauth2 can implement -> JWT or other token formats like SWT...

What should you use?

Not only it would be crazy for a beginner to create its own Oauth2 implementation compatible with Laravel, but also chances are that if you're reading this is because you don't know exactly how to all the Oauth2 complexity. Oauth2 out. All we have remeaning is three package options. Passport, tymondesigns/jwt-auth and Auth0.

Before continuing, let me note that when I refer to 'JWT' or 'plain JWT' I really mean to the tymondesigns/jwt-auth package. The real match comes between Auth0, Passport and plain JWT...

Auth0 is a Swiss army knife compared to Passport and JWT. This knife is an all in one solution. It can do a lot of stuff on its own plus some extras that you might never need without depending on Laravel or other frameworks. To not extend this too long, let me just say that it can do whatever you could possibly imagine as an API beginner. Also, you would have a really good dashboard straight out of the box which will allow you to manage all aspects of your API. From third-party authentication to add more apps (mobile, web, desktop) that can consume that API.

Passport could be compared with a more delicate knife. Its creators knew what it will cut and designed it specifically for those tasks. In other words, it was built especially for Laravel to work smoothly and flawlessly without having many bells and whistles bothering around. Though, don't forget that in the long run you can implement your own code on top of it. Like a nice and custom dashboard to manage your API clients.

Finally, back to the knife analogy, I like to compare JWT with a sharpened blade without any fancy extras. It's lightweight, functional, secure and does its job. You get the benefit that you will not have to worry about all extras that the other packages add. Furthermore, you can upgrade this knife however you want due to its flexibility or even switch to another package if you need a heavier upgrade.

Conclusion

It depends on you, if you want to cut everything that might be thrown at you and stay more in the API world than in Laravel (Auth0), narrow down to the focus of Laravel and Oauth2 for third party authentications (Passport) or have a basic secure data transfer out of the box via API (JWT). And yes... You can also link your app to your mobile app with the three of them.

Personally I prefer Passport because:

  • I like to work with the official Laravel packages.
  • Besides the many extra routes and tables added to my project, its performance will not be affected.
  • If I started with the most basic API auth and wanted to scale, it would be much easier.
  • The coziness of working with a Laravel Package.

Still talking about the last point, some may say that Auth0 community is small. It basically is, but also it has awesome client support personnel.


Tutorials on Passport and Oauth2

Laravel and Oauth2 Docs explanation of tokens might be a little difficult. Here is a Good Explanation of Passport's(therefore Oauth2) Different Types of Tokens and Their Use Cases. Since I couldn't figure out the "routes" part of the tutorial I wouldn't recommend the tutorial part.

This is a Good Passport Video Tutorial which also uses the PostMan Chrome app for API calls. For those of you who are new to this API stuff, apps like PostMan will make your work a lot easier than using a "curl" Linux/Mac command. You could watch the complete series or just the Passport part. At the moment I'm stuck on video 4. Here's my Stack Overflow question.

Resources

Many of the resources are all spread above in the article but I also have some here.

  • Stack Overflow JWT vs Oauth
  • Auth0 explanation of Oauth2
  • An Introduction to OAuth2
  • OAuth2 Simplified
like image 162
Luis L Avatar answered Sep 20 '22 14:09

Luis L