-
Notifications
You must be signed in to change notification settings - Fork 0
/
slimauth_name_resolver.go
86 lines (69 loc) · 2.58 KB
/
slimauth_name_resolver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package slimauth
import (
"fmt"
"github.com/cmstar/go-webapi"
"github.com/cmstar/go-webapi/slimapi"
)
type slimAuthApiNameResolver struct {
authScheme string
secretFinder SecretFinderFunc
timeChecker TimeCheckerFunc
raw webapi.ApiNameResolver
}
// NewSlimAuthApiNameResolver 返回 SlimAuth 协议的 [webapi.ApiNameResolver] 。
// 它增加了签名校验,其余都和 SlimAPI 一样。
func NewSlimAuthApiNameResolver(authScheme string, secretFinder SecretFinderFunc, timeChecker TimeCheckerFunc) webapi.ApiNameResolver {
if secretFinder == nil {
panic("secretFinder must be provided")
}
if timeChecker == nil {
panic("timeChecker must be provided")
}
return &slimAuthApiNameResolver{
authScheme: authScheme,
secretFinder: secretFinder,
timeChecker: timeChecker,
raw: slimapi.NewSlimApiNameResolver(),
}
}
// FillMethod implements [webapi.ApiNameResolver.FillMethod].
func (x slimAuthApiNameResolver) FillMethod(state *webapi.ApiState) {
x.verifySignature(state)
x.raw.FillMethod(state)
}
func (x slimAuthApiNameResolver) verifySignature(state *webapi.ApiState) {
r := state.RawRequest
auth, err := ParseAuthorizationHeader(r, x.authScheme)
if err != nil {
panic(webapi.CreateBadRequestError(state, err, "invalid Authorization"))
}
// 存起来,用于后续方法参数的赋值和日志输出。
SetBufferedAuthorization(state, auth)
// 签名算法目前就一个版本,不允许出现其他值。
if auth.Version != DefaultSignVersion {
panic(webapi.CreateBadRequestError(state, err, "unsupported signature version"))
}
secret := x.secretFinder(auth.Key)
if secret == "" {
panic(webapi.CreateBadRequestError(state, nil, "unknown key"))
}
// 后续走 SlimAPI 的 decode 过程,需要重读 body 。
signResult := Sign(r, true, secret, auth.Timestamp)
// 时间戳校验。
timeCheckErr := x.timeChecker(auth.Timestamp)
if timeCheckErr != nil {
panic(webapi.CreateBadRequestError(state, timeCheckErr, "timestamp error"))
}
switch signResult.Type {
case SignResultType_MissingContentType:
panic(webapi.CreateBadRequestError(state, signResult.Cause, "missing Content-Type"))
case SignResultType_UnsupportedContentType:
panic(webapi.CreateBadRequestError(state, signResult.Cause, "unsupported Content-Type"))
case SignResultType_InvalidRequestBody:
panic(webapi.CreateBadRequestError(state, signResult.Cause, "invalid request body"))
}
if signResult.Sign != auth.Sign {
err = fmt.Errorf("signature mismatch, want %s, got %s", signResult.Sign, auth.Sign)
panic(webapi.CreateBadRequestError(state, err, "signature error"))
}
}