-
Notifications
You must be signed in to change notification settings - Fork 26
/
botauth.go
146 lines (131 loc) · 3.48 KB
/
botauth.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package bot
import (
"bytes"
"context"
"crypto/ed25519"
"encoding/base64"
"encoding/hex"
"fmt"
"io"
"log/slog"
"net/http"
"github.com/pkg/errors"
"golang.org/x/crypto/curve25519"
)
const (
userPlatformPrefix = "up_"
)
type BotAuthClient struct {
Cache BotAuthCache
SafeUser *SafeUser
Logger *slog.Logger
}
type BotAuthCache interface {
Get(key string) ([]byte, error)
Put(key string, value []byte) error
Delete(key string) error
}
type MapCache struct {
m map[string][]byte
}
func NewMapCache() *MapCache {
return &MapCache{
m: map[string][]byte{},
}
}
func (c *MapCache) Get(key string) ([]byte, error) {
return c.m[key], nil
}
func (c *MapCache) Put(key string, value []byte) error {
c.m[key] = value
return nil
}
func (c *MapCache) Delete(key string) error {
delete(c.m, key)
return nil
}
func NewBotAuthClient(cache BotAuthCache, su *SafeUser, logger *slog.Logger) *BotAuthClient {
return &BotAuthClient{
Cache: cache,
SafeUser: su,
Logger: logger,
}
}
func NewDefaultClient(su *SafeUser, logger *slog.Logger) *BotAuthClient {
mapCache := NewMapCache()
return NewBotAuthClient(mapCache, su, logger)
}
func (c *BotAuthClient) SignRequest(ctx context.Context, ts int64, botUserId string, r *http.Request) (string, error) {
sharedKey, err := c.getSharedKey(ctx, botUserId)
if err != nil {
return "", errors.Errorf("failed to decode public key: %v", err)
}
seed, err := hex.DecodeString(c.SafeUser.SessionPrivateKey)
if err != nil {
return "", err
}
priv := ed25519.NewKeyFromSeed(seed)
var p [32]byte
PrivateKeyToCurve25519(&p, priv)
data := []byte(fmt.Sprintf("%d%s%s", ts, r.Method, r.URL.RequestURI()))
if r.Body != nil {
var buf bytes.Buffer
_, err = io.Copy(&buf, r.Body)
if err != nil {
return "", errors.Errorf("failed to read body: %v", err)
}
_ = r.Body.Close()
r.Body = io.NopCloser(bytes.NewBuffer(buf.Bytes()))
data = append(data, buf.Bytes()...)
}
hash, err := hex.DecodeString(HmacSha256(sharedKey, data))
if err != nil {
return "", errors.Errorf("failed to hash: %v", err)
}
return base64.RawURLEncoding.EncodeToString([]byte(fmt.Sprintf("%s%s", c.SafeUser.UserId, hash))), nil
}
func (c *BotAuthClient) getSharedKey(ctx context.Context, userId string) ([]byte, error) {
value, err := c.Cache.Get(userId)
var sharedKey []byte
if err != nil || value == nil || len(value) < 32 {
c.Logger.Debug(fmt.Sprintf("cache miss for %s", userId))
userSessions, err := FetchUserSession(ctx, []string{userId}, c.SafeUser)
if err != nil {
return nil, err
}
var userSession *UserSession
for _, us := range userSessions {
userSession = us
}
if userSession == nil {
return nil, fmt.Errorf("userSession for %s nil", userId)
}
uPk, err := base64.RawURLEncoding.DecodeString(userSession.PublicKey)
if err != nil {
return nil, err
}
platform := userSession.Platform
seed, err := hex.DecodeString(c.SafeUser.SessionPrivateKey)
if err != nil {
return nil, err
}
priv := ed25519.NewKeyFromSeed(seed)
var p [32]byte
PrivateKeyToCurve25519(&p, priv)
sharedKey, err = curve25519.X25519(p[:], uPk[:])
if err != nil {
return nil, err
}
err = c.Cache.Put(userId, sharedKey[:])
if err != nil {
c.Logger.Warn(fmt.Sprintf("save shared key for %s error %v", userId, err))
}
err = c.Cache.Put(fmt.Sprint(userPlatformPrefix, userId), []byte(platform))
if err != nil {
c.Logger.Warn(fmt.Sprintf("save platform for %s error %v", userId, err))
}
} else {
sharedKey = value
}
return sharedKey, nil
}