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

前端 未结 1 519
终归单人心
终归单人心 2020-12-07 10:31

I want to use methods and resources from the code of a website which is developed in PHPFox.

Basically, I\'ll receive request from iPhone/And

相关标签:
1条回答
  • 2020-12-07 11:21

    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

    enter image description here

    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;

    enter image description here

    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.

    0 讨论(0)
提交回复
热议问题