# HTTP Token 使用方式: Basic Token v.s Bearer Token

### Token 怎麼使用？

一般來說是在HEADER中使用**Authorization**的並帶入存取的Token，如Basic、Bearer等type 的Token。

{% code title="Authorization Header Example" %}

```bash
# Unauthorized Response
GET / HTTP/1.1 

# Authorized Query
GET / HTTP/1.1 
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
```

{% endcode %}

### Basic Token

作為HTTP最基本的認證方式，使用明碼的方式進行傳輸 password、user\_id。設計面並未考慮到TLS認證，以明碼的方式傳送 password、user\_id。

一般來說，在存取資Server的時候，需要預先帶入Token，然而若未能成功驗證通過時，Response 則會回應相關的資訊，帶有WWW-Authenticate HEADER，並帶有Realm (Protection Space)資訊。

![](https://i.imgur.com/9BnSolG.png)

**Key words:**\
**1. Realm(Protection Space): Token有效的網域。**\
**2. charset: 用於設定schema 的編碼，只能允許utf-8一種**

### Bearer Token

是以HTTP 1.1 TLS下去定義的 Token，因此使用Bearer必須具備TLS環境。延續使用Basic Token 的WWW-Authenticate、Authorization HEADER，而 proxy authentication 不延續使用。

**使用Token的方式:**\
1\. 使用**Authorization HEADER:** \
&#x20;   Ex: ***Authorization: Bearer QWxhZGRpbjpvcGVuIHNlc2FtZQ==***\
2\. 使用**Form Body:** \
&#x20;  *不建議將Token加入Body*，使用時 **，**&#x4E26;使用**GET 之外的 HTTP verbs (POST、DELETE、PUT)**\
&#x20;   HEADER: ***Content-Type: "application/x-www-form-urlencoded"***\
3\. 直接使用在**URI :**\
&#x20;   一般來說不會使用這種方法，除非是Authorization header不支援，或者protect resource 支援該方法。\
&#x20;   Ex: ***GET /resource?access\_token=mF\_9.B5f-4.1JqM HTTP/1.1***\
&#x20;  &#x20;

### **Token Response Error**

使用Token時，當HTTP Response Status code 為200時代表成功，然而當失敗的時候則會出現4XX的Status code。

**Basic Token: 當授權為「不」通過時，回傳401 code並告知哪個 Protect Scope 錯誤。**

{% code title="Basic Token Error Example" %}

```bash
HTTP/1.1 401 Unauthorized
Date: Mon, 04 Feb 2014 16:50:53 GMT
WWW-Authenticate: Basic realm="WallyWorld"
```

{% endcode %}

**Bearer Token: 當授權為「不」通過時，回傳400、401、403 code，並根據狀況回傳對應的錯誤。**

| **Error Type**   | 說明                                                   |
| ---------------- | ---------------------------------------------------- |
| invalid\_request | Response 400 error code，表示Request 缺少參數或者多了一些參數等錯誤    |
| invalid\_token   | Response 401 error code，表示Token 錯誤，通過不了認證            |
| invalid\_scope   | Response 403 error code，表示Token無權存取該Protect Resource |

{% code title="Bearer Token Error Example" %}

```bash
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer realm="example",
                  error="invalid_token",
                  error_description="The access token expired"
```

{% endcode %}

### **總結**

Basic Token 主要還是使用在網域上的防護(Realm)，而Bearer則是針對protect resource的存取，主要還是使用在Auth 2.0 上頭，且一定要在TLS環境(HTTPS)使用

### **參考資料**

\[1] [The 'Basic' HTTP Authentication Scheme](https://tools.ietf.org/html/rfc7617) \
\[2] [The OAuth 2.0 Authorization Framework: Bearer Token Usage](https://tools.ietf.org/html/rfc6750) \
\[3] [Basic Authentication](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xiang753017.gitbook.io/zixiang-blog/security/http-token-shi-yong-fang-shi-basic-token-v.s-bearer-token.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
