-
Notifications
You must be signed in to change notification settings - Fork 38
/
authenticator_server.go
223 lines (205 loc) · 9.65 KB
/
authenticator_server.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package gfspapp
import (
"context"
"net/http"
"time"
"github.com/bnb-chain/greenfield-storage-provider/base/types/gfsperrors"
"github.com/bnb-chain/greenfield-storage-provider/base/types/gfspserver"
coremodule "github.com/bnb-chain/greenfield-storage-provider/core/module"
"github.com/bnb-chain/greenfield-storage-provider/pkg/log"
"github.com/bnb-chain/greenfield-storage-provider/pkg/metrics"
)
var (
ErrAuthenticatorTaskDangling = gfsperrors.Register(BaseCodeSpace, http.StatusBadRequest, 990101, "OoooH... request lost")
)
var _ gfspserver.GfSpAuthenticationServiceServer = &GfSpBaseApp{}
func (g *GfSpBaseApp) GfSpVerifyAuthentication(ctx context.Context, req *gfspserver.GfSpAuthenticationRequest) (
*gfspserver.GfSpAuthenticationResponse, error) {
if req == nil {
log.Error("failed to verify authentication due to pointer dangling")
return &gfspserver.GfSpAuthenticationResponse{
Err: ErrAuthenticatorTaskDangling,
}, nil
}
ctx = log.WithValue(ctx, log.CtxKeyBucketName, req.GetBucketName())
ctx = log.WithValue(ctx, log.CtxKeyObjectName, req.GetObjectName())
startTime := time.Now()
allow, err := g.authenticator.VerifyAuthentication(ctx, coremodule.AuthOpType(req.GetAuthType()),
req.GetUserAccount(), req.GetBucketName(), req.GetObjectName())
if err != nil || !allow {
metrics.ReqCounter.WithLabelValues(AuthFailure).Inc()
metrics.ReqTime.WithLabelValues(AuthFailure).Observe(time.Since(startTime).Seconds())
} else {
metrics.ReqCounter.WithLabelValues(AuthSuccess).Inc()
metrics.ReqTime.WithLabelValues(AuthSuccess).Observe(time.Since(startTime).Seconds())
}
log.CtxDebugw(ctx, "succeed to authenticate", "user", req.GetUserAccount(), "auth_type", req.GetAuthType(),
"allow", allow, "error", err)
return &gfspserver.GfSpAuthenticationResponse{
Err: gfsperrors.MakeGfSpError(err),
Allowed: allow,
}, nil
}
// GetAuthNonce get the auth nonce for which the Dapp or client can generate EDDSA key pairs.
func (g *GfSpBaseApp) GetAuthNonce(ctx context.Context, req *gfspserver.GetAuthNonceRequest) (*gfspserver.GetAuthNonceResponse, error) {
if req == nil {
log.Error("failed to get auth nonce due to pointer dangling")
return &gfspserver.GetAuthNonceResponse{
Err: ErrAuthenticatorTaskDangling,
}, nil
}
log.CtxDebugw(ctx, "begin to get auth nonce", "user", req.GetAccountId(), "domain", req.GetDomain())
resp, err := g.authenticator.GetAuthNonce(ctx, req.AccountId, req.Domain)
log.CtxDebugw(ctx, "finish to get auth nonce", "user", req.GetAccountId(), "domain", req.GetDomain(), "error", err)
if err != nil {
return &gfspserver.GetAuthNonceResponse{
Err: gfsperrors.MakeGfSpError(err),
}, nil
}
return &gfspserver.GetAuthNonceResponse{
Err: gfsperrors.MakeGfSpError(err),
CurrentNonce: resp.CurrentNonce,
NextNonce: resp.NextNonce,
CurrentPublicKey: resp.CurrentPublicKey,
ExpiryDate: resp.ExpiryDate.UnixMilli(),
}, nil
}
// UpdateUserPublicKey updates the user public key once the Dapp or client generates the EDDSA key pairs.
func (g *GfSpBaseApp) UpdateUserPublicKey(ctx context.Context, req *gfspserver.UpdateUserPublicKeyRequest) (*gfspserver.UpdateUserPublicKeyResponse, error) {
if req == nil {
log.Error("failed to update user publicKey due to pointer dangling")
return &gfspserver.UpdateUserPublicKeyResponse{
Err: ErrAuthenticatorTaskDangling,
}, nil
}
log.CtxDebugw(ctx, "begin to update user public key", "user", req.GetAccountId(), "domain", req.GetDomain(),
"public_key", req.UserPublicKey)
resp, err := g.authenticator.UpdateUserPublicKey(ctx, req.AccountId, req.Domain, req.CurrentNonce, req.Nonce, req.UserPublicKey, req.ExpiryDate)
log.CtxDebugw(ctx, "finish to update user public key", "user", req.GetAccountId(), "domain", req.GetDomain(),
"error", err)
return &gfspserver.UpdateUserPublicKeyResponse{
Err: gfsperrors.MakeGfSpError(err),
Result: resp,
}, nil
}
// VerifyGNFD1EddsaSignature verifies the signature signed by user's EDDSA private key.
func (g *GfSpBaseApp) VerifyGNFD1EddsaSignature(ctx context.Context, req *gfspserver.VerifyGNFD1EddsaSignatureRequest) (*gfspserver.VerifyGNFD1EddsaSignatureResponse, error) {
if req == nil {
log.Error("failed to verify gnfd1EddsaSignature due to pointer dangling")
return &gfspserver.VerifyGNFD1EddsaSignatureResponse{
Err: ErrAuthenticatorTaskDangling,
}, nil
}
log.CtxDebugw(ctx, "begin to verify off-chain signature", "user", req.GetAccountId(), "domain", req.GetDomain(),
"off_chain_sig", req.OffChainSig, "real_msg_to_sign", req.RealMsgToSign)
resp, err := g.authenticator.VerifyGNFD1EddsaSignature(ctx, req.AccountId, req.Domain, req.OffChainSig, req.RealMsgToSign)
log.CtxDebugw(ctx, "finish to verify off-chain signature", "user", req.GetAccountId(), "domain", req.GetDomain(),
"error", err)
return &gfspserver.VerifyGNFD1EddsaSignatureResponse{
Err: gfsperrors.MakeGfSpError(err),
Result: resp,
}, nil
}
func (g *GfSpBaseApp) GetAuthKeyV2(ctx context.Context, req *gfspserver.GetAuthKeyV2Request) (*gfspserver.GetAuthKeyV2Response, error) {
if req == nil {
log.Error("failed to GetAuthKeyV2 to pointer dangling")
return &gfspserver.GetAuthKeyV2Response{
Err: ErrAuthenticatorTaskDangling,
}, nil
}
log.CtxDebugw(ctx, "begin to GetAuthKeyV2", "user", req.GetAccountId(), "domain", req.GetDomain(), "public_key", req.GetUserPublicKey())
resp, err := g.authenticator.GetAuthKeyV2(ctx, req.AccountId, req.Domain, req.UserPublicKey)
log.CtxDebugw(ctx, "finish to GetAuthKeyV2", "user", req.GetAccountId(), "domain", req.GetDomain(), "public_key", req.GetUserPublicKey(), "error", err)
if err != nil {
return &gfspserver.GetAuthKeyV2Response{
Err: gfsperrors.MakeGfSpError(err),
}, nil
}
if resp == nil {
return &gfspserver.GetAuthKeyV2Response{
Err: gfsperrors.MakeGfSpError(err),
}, nil
}
return &gfspserver.GetAuthKeyV2Response{
Err: gfsperrors.MakeGfSpError(err),
PublicKey: resp.PublicKey,
ExpiryDate: resp.ExpiryDate.UnixMilli(),
}, nil
}
func (g *GfSpBaseApp) UpdateUserPublicKeyV2(ctx context.Context, req *gfspserver.UpdateUserPublicKeyV2Request) (*gfspserver.UpdateUserPublicKeyV2Response, error) {
if req == nil {
log.Error("failed to UpdateUserPublicKeyV2 due to pointer dangling")
return &gfspserver.UpdateUserPublicKeyV2Response{
Err: ErrAuthenticatorTaskDangling,
}, nil
}
log.CtxDebugw(ctx, "begin to UpdateUserPublicKeyV2", "user", req.GetAccountId(), "domain", req.GetDomain(), "public_key", req.UserPublicKey)
resp, err := g.authenticator.UpdateUserPublicKeyV2(ctx, req.AccountId, req.Domain, req.UserPublicKey, req.ExpiryDate)
log.CtxDebugw(ctx, "finish to UpdateUserPublicKeyV2", "user", req.GetAccountId(), "domain", req.GetDomain(), "public_key", req.GetUserPublicKey(), "error", err)
return &gfspserver.UpdateUserPublicKeyV2Response{
Err: gfsperrors.MakeGfSpError(err),
Result: resp,
}, nil
}
func (g *GfSpBaseApp) VerifyGNFD2EddsaSignature(ctx context.Context, req *gfspserver.VerifyGNFD2EddsaSignatureRequest) (*gfspserver.VerifyGNFD2EddsaSignatureResponse, error) {
if req == nil {
log.Error("failed to VerifyGNFD2EddsaSignature due to pointer dangling")
return &gfspserver.VerifyGNFD2EddsaSignatureResponse{
Err: ErrAuthenticatorTaskDangling,
}, nil
}
log.CtxDebugw(ctx, "begin to verify off-chain signature", "user", req.GetAccountId(), "domain", req.GetDomain(), "public_key", req.UserPublicKey,
"off_chain_sig", req.OffChainSig, "real_msg_to_sign", req.RealMsgToSign)
resp, err := g.authenticator.VerifyGNFD2EddsaSignature(ctx, req.AccountId, req.Domain, req.UserPublicKey, req.OffChainSig, req.RealMsgToSign)
log.CtxDebugw(ctx, "finish to verify off-chain signature", "user", req.GetAccountId(), "domain", req.GetDomain(), "public_key", req.UserPublicKey,
"error", err)
return &gfspserver.VerifyGNFD2EddsaSignatureResponse{
Err: gfsperrors.MakeGfSpError(err),
Result: resp,
}, nil
}
func (g *GfSpBaseApp) ListAuthKeysV2(ctx context.Context, req *gfspserver.ListAuthKeysV2Request) (*gfspserver.ListAuthKeysV2Response, error) {
if req == nil {
log.Error("failed to ListAuthKeysV2 to pointer dangling")
return &gfspserver.ListAuthKeysV2Response{
Err: ErrAuthenticatorTaskDangling,
}, nil
}
log.CtxDebugw(ctx, "begin to ListAuthKeysV2", "user", req.GetAccountId(), "domain", req.GetDomain())
resp, err := g.authenticator.ListAuthKeysV2(ctx, req.AccountId, req.Domain)
log.CtxDebugw(ctx, "finish to ListAuthKeysV2", "user", req.GetAccountId(), "domain", req.GetDomain(), "error", err)
if err != nil {
return &gfspserver.ListAuthKeysV2Response{
Err: gfsperrors.MakeGfSpError(err),
}, nil
}
if resp == nil {
return &gfspserver.ListAuthKeysV2Response{
Err: gfsperrors.MakeGfSpError(err),
}, nil
}
return &gfspserver.ListAuthKeysV2Response{
Err: gfsperrors.MakeGfSpError(err),
PublicKeys: resp,
}, nil
}
func (g *GfSpBaseApp) DeleteAuthKeysV2(ctx context.Context, req *gfspserver.DeleteAuthKeysV2Request) (*gfspserver.DeleteAuthKeysV2Response, error) {
if req == nil {
log.Error("failed to GetAuthKeyV2 to pointer dangling")
return &gfspserver.DeleteAuthKeysV2Response{
Err: ErrAuthenticatorTaskDangling,
}, nil
}
log.CtxDebugw(ctx, "begin to DeleteAuthKeysV2", "user", req.GetAccountId(), "domain", req.GetDomain(), "public_keys", req.GetPublicKeys())
result, err := g.authenticator.DeleteAuthKeysV2(ctx, req.AccountId, req.Domain, req.PublicKeys)
log.CtxDebugw(ctx, "finish to DeleteAuthKeysV2", "user", req.GetAccountId(), "domain", req.GetDomain(), "public_key", req.GetPublicKeys(), "error", err)
if err != nil {
return &gfspserver.DeleteAuthKeysV2Response{
Err: gfsperrors.MakeGfSpError(err),
}, nil
}
return &gfspserver.DeleteAuthKeysV2Response{
Err: gfsperrors.MakeGfSpError(err),
Result: result,
}, nil
}