cubefs

Форк
0
/
authtransport.go 
58 строк · 1.4 Кб
1
// Copyright 2022 The CubeFS Authors.
2
//
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
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
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.
14

15
package auth
16

17
import (
18
	"net/http"
19
	"time"
20
)
21

22
type AuthTransport struct {
23
	Secret []byte
24
	Tr     http.RoundTripper
25
}
26

27
func NewAuthTransport(tr http.RoundTripper, cfg *Config) http.RoundTripper {
28
	if cfg.EnableAuth {
29
		if cfg.Secret == "" {
30
			panic("auth secret can not be nil")
31
		}
32
		return &AuthTransport{
33
			Secret: []byte(cfg.Secret),
34
			Tr:     tr,
35
		}
36
	}
37
	return nil
38
}
39

40
// a simple auth token
41
func (self *AuthTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
42
	now := time.Now().Unix()
43

44
	info := &authInfo{timestamp: now, others: genEncodeStr(req)}
45

46
	err = calculate(info, self.Secret)
47
	if err != nil {
48
		return self.Tr.RoundTrip(req)
49
	}
50

51
	token, err := encodeAuthInfo(info)
52
	if err != nil {
53
		return self.Tr.RoundTrip(req)
54
	}
55

56
	req.Header.Set(TokenHeaderKey, token)
57
	return self.Tr.RoundTrip(req)
58
}
59

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.