requireAdmin 要求请求携带有效的管理员 Bearer token。
(next http.HandlerFunc)
| 108 | |
| 109 | // requireAdmin 要求请求携带有效的管理员 Bearer token。 |
| 110 | func (s *ShopAPI) requireAdmin(next http.HandlerFunc) http.HandlerFunc { |
| 111 | return func(w http.ResponseWriter, r *http.Request) { |
| 112 | if s.AdminAuth != nil { |
| 113 | const prefix = "Bearer " |
| 114 | auth := r.Header.Get("Authorization") |
| 115 | token := "" |
| 116 | if strings.HasPrefix(auth, prefix) { |
| 117 | token = strings.TrimSpace(strings.TrimPrefix(auth, prefix)) |
| 118 | } |
| 119 | if token == "" || !s.AdminAuth.ValidateToken(token) { |
| 120 | writeJSONError(w, http.StatusUnauthorized, "admin token required") |
| 121 | return |
| 122 | } |
| 123 | } |
| 124 | next(w, r) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | func (s *ShopAPI) listPlansHandler(mode string) http.HandlerFunc { |
| 129 | return func(w http.ResponseWriter, r *http.Request) { |
no test coverage detected