如何使用 TinyGo 解码 JWT 令牌
How to decode a JWT token with TinyGo
我们有一个需要解码的 JWT 令牌,问题是我们正在使用 TinyGo and some libraries are not supported,
对于已经支持的 TinyGo / 核心 Go 库怎么办?我想打印“名称”值:
我不知道名字,知道吗?
func main() {
token := `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c`
base64String := base64.StdEncoding.EncodeToString([]byte(token))
decodedData, err := base64.StdEncoding.DecodeString(base64String)
if err != nil {
panic(err)
}
name := decodedData["name"]
fmt.Println(name)
}
解码后的令牌是:
PAYLOAD
{
"sub": "1234567890",
"name": "John Doe",
"iat": 1516239022
}
查看来自 https://jwt.io
的示例令牌
解码和获取 name
部分很容易。 但这并不能确保令牌有效,这意味着令牌的所有者名副其实!
JWT tokens just contain the base64 encoded forms of a header, payload and signature parts, connected with a .
. So just split the token by .
, decode the base64 string and you may use json.Unmarshal()
将 header 和播放负载部分转换为映射或结构。
您必须验证签名以确保名称有效。如果不进行签名验证,token很容易被伪造冒充任何人。签名验证正是 JWT 库所做的(除了解析和生成令牌)。如何做到这一点,请检查 JWT 库的来源。我也相信有 open-source 处理 JWT 令牌的库也适用于 tiny-go.
解码部分并打印 name
:
的示例代码
token := `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c`
for i, part := range strings.Split(token, ".") {
fmt.Printf("[%d] part: %s\n", i, part)
decoded, err := base64.RawURLEncoding.DecodeString(part)
if err != nil {
panic(err)
}
fmt.Println("decoded:", string(decoded))
if i != 1 {
continue // i == 1 is the payload
}
var m map[string]interface{}
if err := json.Unmarshal(decoded, &m); err != nil {
fmt.Println("json decoding failed:", err)
continue
}
if name, ok := m["name"]; ok {
fmt.Println("name:", name)
}
}
哪些输出(在 Go Playground 上尝试):
[0] part: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
decoded: {"alg":"HS256","typ":"JWT"}
[1] part: eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ
decoded: {"sub":"1234567890","name":"John Doe","iat":1516239022}
name: John Doe
[2] part: SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
decoded: I�J�IHNJ(]�O���lj~�:N�%_�u,×
我们有一个需要解码的 JWT 令牌,问题是我们正在使用 TinyGo and some libraries are not supported, 对于已经支持的 TinyGo / 核心 Go 库怎么办?我想打印“名称”值:
我不知道名字,知道吗?
func main() {
token := `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c`
base64String := base64.StdEncoding.EncodeToString([]byte(token))
decodedData, err := base64.StdEncoding.DecodeString(base64String)
if err != nil {
panic(err)
}
name := decodedData["name"]
fmt.Println(name)
}
解码后的令牌是:
PAYLOAD
{
"sub": "1234567890",
"name": "John Doe",
"iat": 1516239022
}
查看来自 https://jwt.io
解码和获取 name
部分很容易。 但这并不能确保令牌有效,这意味着令牌的所有者名副其实!
JWT tokens just contain the base64 encoded forms of a header, payload and signature parts, connected with a .
. So just split the token by .
, decode the base64 string and you may use json.Unmarshal()
将 header 和播放负载部分转换为映射或结构。
您必须验证签名以确保名称有效。如果不进行签名验证,token很容易被伪造冒充任何人。签名验证正是 JWT 库所做的(除了解析和生成令牌)。如何做到这一点,请检查 JWT 库的来源。我也相信有 open-source 处理 JWT 令牌的库也适用于 tiny-go.
解码部分并打印 name
:
token := `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c`
for i, part := range strings.Split(token, ".") {
fmt.Printf("[%d] part: %s\n", i, part)
decoded, err := base64.RawURLEncoding.DecodeString(part)
if err != nil {
panic(err)
}
fmt.Println("decoded:", string(decoded))
if i != 1 {
continue // i == 1 is the payload
}
var m map[string]interface{}
if err := json.Unmarshal(decoded, &m); err != nil {
fmt.Println("json decoding failed:", err)
continue
}
if name, ok := m["name"]; ok {
fmt.Println("name:", name)
}
}
哪些输出(在 Go Playground 上尝试):
[0] part: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
decoded: {"alg":"HS256","typ":"JWT"}
[1] part: eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ
decoded: {"sub":"1234567890","name":"John Doe","iat":1516239022}
name: John Doe
[2] part: SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
decoded: I�J�IHNJ(]�O���lj~�:N�%_�u,×