How to implement 'Token Based Authentication' securely for accessing the website's resources(i.e. functions and data) that is developed in PHPFox?

六月ゝ 毕业季﹏ 提交于 2019-11-27 17:07:05
Techie

1st you should understand what's token based authentication. It could be explained as below.

The general concept behind a token-based authentication system is simple. Allow users to enter their username and password in order to obtain a token which allows them to fetch a specific resource - without using their username and password. Once their token has been obtained, the user can offer the token - which offers access to a specific resource for a time period - to the remote site.

Read more

Now let's see what are the steps of implementing it in your REST web service.

It will use the following flow of control:

  • The user provides a username and password in the login form and clicks Log In.
  • After a request is made, validate the user on the backend by querying in the database. If the request is valid, create a token by using the user information fetched from the database, and then return that information in the response header so that we can store the token browser in local storage.
  • Provide token information in every request header for accessing restricted endpoints in the application.
  • If the token fetched from the request header information is valid, let the user access the specified end point, and respond with JSON or XML.

See the image below for the flow of control

You might be wondering what's a JWT

JWT stands for JSON Web Token and is a token format used in authorization headers. This token helps you to design communication between two systems in a secure way. Let's rephrase JWT as the "bearer token" for the purposes of this tutorial. A bearer token consists of three parts: header, payload, and signature.

  • The header is the part of the token that keeps the token type and encryption method, encoded in base64.
  • The payload includes the information. You can put any kind of data like user info, product info and so on, all of which is also stored in base64 encoding.
  • The signature consists of combinations of the header, payload, and secret key. The secret key must be kept securely on the server-side. You can see the JWT schema and an example token below;

You do not need to implement the bearer token generator as you can use php-jwt.

Hope the above explains your confusion. if you come across any issues implementing token based authentication let me know. I can help you.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!