1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-07 12:43:31 +08:00

clear check authorization.

This commit is contained in:
wdvxdr 2021-07-07 21:19:02 +08:00
parent e9b302ad74
commit d266242887
No known key found for this signature in database
GPG Key ID: 55FF1414A69CEBA6
2 changed files with 13 additions and 18 deletions

View File

@ -551,9 +551,3 @@ func formatMemberName(mem *client.GroupMemberInfo) string {
}
return fmt.Sprintf("%s(%d)", mem.DisplayName(), mem.Uin)
}
// ToJSON 生成JSON字符串
func (m MSG) ToJSON() string {
b, _ := json.Marshal(m)
return string(b)
}

View File

@ -97,12 +97,9 @@ func (s *httpServer) ServeHTTP(writer http.ResponseWriter, request *http.Request
writer.WriteHeader(http.StatusNotFound)
return
}
if s.accessToken != "" {
if status := checkAuth(request, s.accessToken); status != http.StatusOK {
writer.WriteHeader(status)
return
}
if status := checkAuth(request, s.accessToken); status != http.StatusOK {
writer.WriteHeader(status)
return
}
action := strings.TrimPrefix(request.URL.Path, "/")
@ -116,6 +113,10 @@ func (s *httpServer) ServeHTTP(writer http.ResponseWriter, request *http.Request
}
func checkAuth(req *http.Request, token string) int {
if token == "" { // quick path
return http.StatusOK
}
auth := req.Header.Get("Authorization")
if auth == "" {
auth = req.URL.Query().Get("access_token")
@ -126,13 +127,13 @@ func checkAuth(req *http.Request, token string) int {
}
}
switch {
case auth == "":
return http.StatusUnauthorized
case auth != token:
return http.StatusForbidden
default:
switch auth {
case token:
return http.StatusOK
case "":
return http.StatusUnauthorized
default:
return http.StatusForbidden
}
}