1
// Copyright 2022 The CubeFS Authors.
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
7
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12
// implied. See the License for the specific language governing
13
// permissions and limitations under the License.
21
type AuthHandler struct {
25
func NewAuthHandler(cfg *Config) *AuthHandler {
28
panic("auth secret can not be nil")
31
Secret: []byte(cfg.Secret),
37
func (self *AuthHandler) Handler(w http.ResponseWriter, req *http.Request, f func(http.ResponseWriter, *http.Request)) {
38
token := req.Header.Get(TokenHeaderKey)
40
w.WriteHeader(http.StatusForbidden)
43
info, err := decodeAuthInfo(token)
45
w.WriteHeader(http.StatusForbidden)
48
info.others = genEncodeStr(req)
50
err = verify(info, self.Secret)
51
if err != nil && err == errMismatchToken {
52
w.WriteHeader(http.StatusForbidden)