Skip to content

X-Signature是怎么计算的? #1894

Answered by axiangcoding
axiangcoding asked this question in Q&A
Discussion options

You must be logged in to vote

查看了源码,我给出一个gin middleware的验证方式

// CqhttpAuth 判断X-Self-ID是否和配置项相同,同时当X-Signature存在时,校验签名
// selfQQ 配置的自身qq号
// secert 配置的密钥
func CqhttpAuth(selfQQ string, secret string) gin.HandlerFunc {
	return func(c *gin.Context) {
		hSelfQQ := c.GetHeader("X-Self-ID")
		hSignature := c.GetHeader("X-Signature")
		
		if selfQQ != hSelfQQ {
			// qq参数验证错误
			c.AbortWithStatus(http.StatusUnauthorized)
			return
		}
		if hSignature != "" {
			if secret == "" {
				c.AbortWithStatus(http.StatusUnauthorized)
				return
			}
			// 读取 request body
			body, err := io.ReadAll(c.Request.Body)
			if err != nil {
				c.AbortWithStatus(http.StatusInternalServerError)
				return
			}
			// gin框架中request body只能读取一次,需要…

Replies: 3 comments 4 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
4 replies
@axiangcoding
Comment options

@SlimeNull
Comment options

@axiangcoding
Comment options

@axiangcoding
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by axiangcoding
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants