JSON Web Token-securely pass information between Web Apps

Source: Internet
Author: User
Tags base64

The JSON Web Token (JWT) is a very lightweight specification. This specification allows us to use JWT to deliver secure and reliable information between the user and the server.

Let's imagine a scenario. When a user is concerned about the B user, the system sends a message to the B user, and a link "point this attention to a user" is attached. The address of the link can be like this

1
HTTPS://your.awesome-app.com/make-friend/?from_user=b&target_user=a

The above URL mainly through the URL to describe this of course, there is a disadvantage, that is, require User B user must first login. Can you simplify this process so that B users do not have to log in to complete this operation. JWT allows us to do this.

The composition of the JWT

A JWT is actually a string that consists of three parts, the head , the payload , and the signature .

Load (Payload)

Let's first describe the above add a friend's action as a JSON object. Some additional information was added to help understand the JWT for future servers receiving this JWT.

1
2
3
4
5
6
7
8
9
{
"John Wu JWT",
1441593502,
1441594722,
"Www.example.com",
"[Email protected]",
"B",
A
}

The first five fields are defined by the JWT standard.

    • iss: The issuer of the JWT
    • sub: The user the JWT is targeting
    • aud: The party that receives the JWT
    • exp(expires): When expires, here is a UNIX timestamp
    • iat(Issued at): when issued

These definitions can be found in the standard.

The JSON object above is [base64 encoded] to get the following string. This string we call it the Payload(load) of the JWT.

 1 
 eyjpc3mioijkb2huifd 1iepxvcisimlhdci6mtq0mtu5mzuwmiwizxhwijoxndqxntk0nziylcjhdwqioij3d3cuzxhhbxbszs 5jb20ilcjzdwiioijqcm9ja2v< Span class= "number" >0qgv4yw1wbguuy29tiiwiznjvbv91c2vyijoiqiisinrhcmdldf91c 2vyijoiqsj9 

If you use node. js, you can use node. JS's package Base64url to get the string.

1
2
3
4
5
6
7
 var base64url = require ( ' Base64url ') 
var header = {
"From_user": "Target_user": "A"
}
console.log (Base64url (json.stringify (header)))
// Output: eyjpc3mioijkb2huifd1iepxvcisimlhdci6mtq0mtu5mzuwmiwizxhwijoxndqxntk0nziylcjhdwqioij3d3cuzxhhbxbszs5jb20ilcjzdwiioijqcm 9ja2v0qgv4yw1wbguuy29tiiwiznjvbv91c2vyijoiqiisinrhcmdldf91c2vyijoiqsj9

Tip: Base64 is a code that means that it can be translated back to its original appearance. It is not a cryptographic process.

Head (header)

JWT also requires a head, which is used to describe the most basic information about the JWT, such as its type and the algorithm used to sign it. This can also be represented as a JSON object.

1
2
3
4
{
"JWT",
"HS256"
}

Here we show that this is a JWT, and the signature algorithm we use (which is mentioned later) is the HS256 algorithm.

It also has to be Base64 encoded, and then the string becomes the headerof the JWT (head).

1
Eyj0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9
Signature (signature)

The above two encoded strings are concatenated with a period . (head in front), forming a

1
Eyj0eXAiOiJKV1QiLCJhbGciOiJIUzI1nij9.eyjmcm9tx3VzZXIiOiJCIiwidGFyZ2V0X  3VzZXIiOiJBIN0

This part of the process is reflected in the source code of NODE-JWS

Finally, we encrypt the string above the concatenation with the HS256 algorithm. In the encryption, we also need to provide a key (secret). If we use it mystar as a key, then we can get our encrypted content.

1
RSWAMYAYWUHCo7ifagd1oRpSP7nzl7bf5t7ItqpKViM

This part is also called signature .

Finally, the part of the signature is also stitched behind the signed string, and we get the full JWT

1
Eyj0eXAiOiJKV1QiLCJhbGciOiJIUzI1nij9.eyjmcm9tx3VzZXIiOiJCIiwidGFyZ2V0X  3VzZXIiOiJBIn0.rswamyaywuhco7ifagd1oRpSP7nzl7bf5t7ItqpKViM

As a result, we can change the URL in the message to

1
HTTPS://your.awesome-app.com/make-friend/?jwt= EyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmcm9tX3VzZXIiOiJCIiwidGFyZ2V0X3VzZXIiOiJBIn0.rSWamyAYwuHCo7IFAgd1oRpSP7nzL7BF5t7I Tqpkvim

This will allow you to safely complete the action of adding friends!

Wait, we must have some questions:

    1. What is the purpose of the signature?
    2. Base64 is a kind of code, is reversible, then my information is not exposed?

Let me explain it to you.

The purpose of the signature

The process of signing the final step is actually to sign the header and the payload content. In general, cryptographic algorithms are always different for the output produced by the various inputs. For two different inputs, the probability of producing the same output is extremely small (it may be smaller than the probability that I am the richest man in the world). So, let's look at the "different inputs that produce different outputs" as inevitable events.

Therefore, if someone modifies the contents of the head and the payload and then encodes it, then the signature of the new head and payload will be different from the previous signature. Also, if you do not know the server encryption when using the key, the signature will certainly be different.

Server applications once the JWT is accepted, the contents of the head and payload are first re-signed with the same algorithm. So how does the server application know which algorithm we're using? Let's not forget that we have already alg indicated our encryption algorithm with a field in the head of JWT.

If the server application finds that the header and the load are again signed in the same way, and the signature is not the same as the signature received, then it means that the token has been moved by someone else, and we should reject the token and return an HTTP 401 unauthorized response.

Information will be exposed?

Yes.

Therefore, in a JWT, no sensitive data should be added to the load. In the example above, we are transmitting the user ID of the Subscriber. This value is actually not a sensitive content and is generally known to be safe.

But something like a password can't be put in a JWT. If you put a user's password in a JWT, a malicious third party will be able to quickly know your password by Base64 decoding.

The appropriate scenario for JWT

As we can see, JWT is suitable for passing some non-sensitive information to a web app. For example, in the above mentioned the completion of the friend operation, as well as the operation of the order, and so on.

JSON Web Token-securely pass information between Web Apps

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.