認識 JWT

whck6

流程圖

左邊是傳統的 cookie-session 的流程,右邊是使用 token 的流程,乍看是很類似的。也因為運作的慨念很類似,所以兩者之間的轉換幾乎沒什麼困難。

JWT

選擇用 token 的機制去解決身份驗證的問題,在實作上會快很多,網路上也有很多相關套件可以使用。

What is JSON Web Token?

JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed. JWTs can be signed using a secret (with the HMAC algorithm) or a public/private key pair using RSA.

Let's explain some concepts of this definition further.

Compact: Because of their smaller size, JWTs can be sent through a URL, POST parameter, or inside an HTTP header. Additionally, the smaller size means transmission is fast.

Self-contained: The payload contains all the required information about the user, avoiding the need to query the database more than once.

被定義在RFC7519,可使傳輸過程變得安全並有輕量與自我驗證的優勢。

When should you use JSON Web Tokens?

Here are some scenarios where JSON Web Tokens are useful:

Authentication: This is the most common scenario for using JWT. Once the user is logged in, each subsequent request will include the JWT, allowing the user to access routes, services, and resources that are permitted with that token. Single Sign On is a feature that widely uses JWT nowadays, because of its small overhead and its ability to be easily used across different domains.

Information Exchange: JSON Web Tokens are a good way of securely transmitting information between parties. Because JWTs can be signed—for example, using public/private key pairs—you can be sure the senders are who they say they are. Additionally, as the signature is calculated using the header and the payload, you can also verify that the content hasn't been tampered with.

取自官網上的資訊,主要的目的就是身份驗證與資料交換。

What is the JSON Web Token structure?

三個部分

  • Header
  • Payload
  • Signature
{
  "alg": "HS256",
  "typ": "JWT"
}

Payload

{
  "sub": "1234567890",
  "name": "John Doe",
  "admin": true
}

Signature

HMACSHA256(
  base64UrlEncode(header) + "." +
    base64UrlEncode(payload),
  secret,
);

https://jwt.io/introduction/\ https://auth0.com/blog/refresh-tokens-what-are-they-and-when-to-use-them/\ https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Authorization\ https://auth0.com/blog/cookies-vs-tokens-definitive-guide/\ https://tools.ietf.org/html/rfc7519#section-4.1