-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.go
314 lines (275 loc) · 8.61 KB
/
auth.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package main
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"regexp"
"strconv"
"time"
"github.com/data-preservation-programs/spade/apitypes"
"github.com/data-preservation-programs/spade/internal/app"
filaddr "github.com/filecoin-project/go-address"
filabi "github.com/filecoin-project/go-state-types/abi"
filprovider "github.com/filecoin-project/go-state-types/builtin/v9/miner"
lru "github.com/hashicorp/golang-lru/v2"
blsgo "github.com/jsign/go-filsigner/bls"
"github.com/labstack/echo/v4"
"github.com/ribasushi/go-toolbox-interplanetary/fil"
"github.com/ribasushi/go-toolbox/cmn"
)
const (
sigGraceEpochs = 3
authScheme = `FIL-SPID-V0`
)
type rawHdr struct {
epoch string
addr string
sigB64 string
arg string
}
type sigChallenge struct {
authHdr string
addr filaddr.Address
epoch int64
arg []byte
hdr rawHdr
}
type verifySigResult struct {
invalidSigErrstr string
}
var (
spAuthRe = regexp.MustCompile(
`^` + authScheme + `\s+` +
// fil epoch
`([0-9]+)` + `\s*;\s*` +
// spID
`([ft]0[0-9]+)` + `\s*;\s*` +
// legacy crap, remove once contract signing in place for everyone
`(?:\s*2\s*;)?` +
// signature
`([^; ]+)` +
// optional signed argument
`(?:\s*\;\s*([^; ]+))?` +
`\s*$`,
)
challengeCache, _ = lru.New[rawHdr, verifySigResult](sigGraceEpochs * 128)
beaconCache, _ = lru.New[int64, *fil.LotusBeaconEntry](sigGraceEpochs * 4)
)
func spidAuth(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
ctx, log, _, _ := app.UnpackCtx(c.Request().Context())
// the SP portion does not accept body payloads
if b := c.Request().Body; b != nil && c.Request().ContentLength != 0 {
if _, err := b.Read(make([]byte, 1)); err != io.EOF {
return retFail(c, apitypes.ErrInvalidRequest, "spid requests witn content in the HTTP body are not supported")
}
}
var challenge sigChallenge
challenge.authHdr = c.Request().Header.Get(echo.HeaderAuthorization)
res := spAuthRe.FindStringSubmatch(challenge.authHdr)
if len(res) == 5 {
challenge.hdr.epoch, challenge.hdr.addr, challenge.hdr.sigB64, challenge.hdr.arg = res[1], res[2], res[3], res[4]
} else {
return retAuthFail(c, "invalid/unexpected %s Authorization header '%s'", authScheme, challenge.authHdr)
}
var err error
challenge.addr, err = filaddr.NewFromString(challenge.hdr.addr)
if err != nil {
return retAuthFail(c, "unexpected %s auth address '%s'", authScheme, challenge.hdr.addr)
}
challenge.epoch, err = strconv.ParseInt(challenge.hdr.epoch, 10, 32)
if err != nil {
return retAuthFail(c, "unexpected %s auth epoch '%s'", authScheme, challenge.hdr.epoch)
}
curFilEpoch := int64(fil.ClockMainnet.TimeToEpoch(time.Now()))
if curFilEpoch < challenge.epoch {
log.Debugw("challenge from the future", "challengeEpoch", challenge.epoch, "wallEpoch", curFilEpoch)
return retAuthFail(c, "%s auth epoch '%d' is in the future", authScheme, challenge.epoch)
}
if curFilEpoch-challenge.epoch > sigGraceEpochs {
return retAuthFail(c, "%s auth epoch '%d' is too far in the past", authScheme, challenge.epoch)
}
challenge.arg, err = base64.StdEncoding.DecodeString(challenge.hdr.arg)
if err != nil {
return retAuthFail(c, "unable to decode optional argument: %s", err.Error())
}
signedArgs, err := url.ParseQuery(string(challenge.arg))
if err != nil {
return retAuthFail(c, "unable to parse optional argument: %s", err.Error())
}
var vsr verifySigResult
if maybeResult, known := challengeCache.Get(challenge.hdr); known {
vsr = maybeResult
} else {
vsr, err = verifySig(ctx, challenge)
if err != nil {
return cmn.WrErr(err)
}
challengeCache.Add(challenge.hdr, vsr)
}
if vsr.invalidSigErrstr != "" {
return retAuthFail(c, vsr.invalidSigErrstr)
}
// set only on request object for logging, not part of response
c.Request().Header.Set("X-SPADE-LOGGED-SP", challenge.addr.String())
// if challenge.addr.String() == "f01" {
// challenge.addr, _ = filaddr.NewFromString("f02")
// }
spID := fil.MustParseActorString(challenge.addr.String())
reqCopy := c.Request().Clone(ctx)
// do not need to store any IPs anywhere in the DB
for _, strip := range []string{
"X-Real-Ip", "X-Forwarded-For", "Cf-Connecting-Ip",
} {
delete(reqCopy.Header, strip)
}
reqJ, err := json.Marshal(
struct {
Method string
Host string
Path string
Params string
ParamsSigned url.Values
Headers http.Header
}{
Method: reqCopy.Method,
Host: reqCopy.Host,
Path: reqCopy.URL.Path,
Params: reqCopy.URL.Query().Encode(),
ParamsSigned: signedArgs,
Headers: reqCopy.Header,
},
)
if err != nil {
return cmn.WrErr(err)
}
spDetails := [4]int16{-1, -1, -1, -1}
var requestUUID string
var stateEpoch int64
var spInfo apitypes.SPInfo
var spInfoLastPoll *time.Time
if err := app.GetGlobalCtx(ctx).Db[app.DbMain].QueryRow(
ctx,
`
INSERT INTO spd.requests ( provider_id, request_dump )
VALUES ( $1, $2 )
RETURNING
request_uuid,
( SELECT ( metadata->'market_state'->'epoch' )::INTEGER FROM spd.global ),
(
SELECT
ARRAY[
COALESCE( org_id, -1 ),
COALESCE( city_id, -1),
COALESCE( country_id, -1),
COALESCE( continent_id, -1)
]
FROM spd.providers
WHERE provider_id = $1
),
(
SELECT info
FROM spd.providers_info
WHERE provider_id = $1
),
(
SELECT provider_last_polled
FROM spd.providers_info
WHERE provider_id = $1
)
`,
spID,
reqJ,
).Scan(&requestUUID, &stateEpoch, &spDetails, &spInfo, &spInfoLastPoll); err != nil {
return cmn.WrErr(err)
}
c.Response().Header().Set("X-SPADE-FIL-SPID", challenge.addr.String())
// set on both request (for logging ) and response object
c.Request().Header.Set("X-SPADE-REQUEST-UUID", requestUUID)
c.Response().Header().Set("X-SPADE-REQUEST-UUID", requestUUID)
c.Set("♠️", metaContext{
GlobalContext: app.GetGlobalCtx(ctx),
stateEpoch: stateEpoch,
authedActorID: spID,
signedArgs: signedArgs,
spOrgID: spDetails[0],
spCityID: spDetails[1],
spCountryID: spDetails[2],
spContinentID: spDetails[3],
spInfo: spInfo,
spInfoLastPolled: spInfoLastPoll,
})
return next(c)
}
}
type metaContext struct {
app.GlobalContext
authedActorID fil.ActorID
stateEpoch int64
spInfo apitypes.SPInfo
spInfoLastPolled *time.Time
spOrgID int16
spCityID int16
spCountryID int16
spContinentID int16
signedArgs url.Values
}
func unpackAuthedEchoContext(c echo.Context) (context.Context, metaContext) {
meta, _ := c.Get("♠️").(metaContext) // ignore potential nil error on purpose
return c.Request().Context(), meta
}
func verifySig(ctx context.Context, challenge sigChallenge) (verifySigResult, error) {
sig, err := base64.StdEncoding.DecodeString(challenge.hdr.sigB64)
if err != nil {
return verifySigResult{
invalidSigErrstr: fmt.Sprintf("unexpected %s auth signature encoding '%s'", authScheme, challenge.hdr.sigB64),
}, nil
}
lAPI := app.GetGlobalCtx(ctx).LotusAPI
be, didFind := beaconCache.Get(challenge.epoch)
if !didFind {
// this should work but doesn't...
//
// curChallengeTs, err := lAPI.ChainGetTipSetByHeight(ctx, filabi.ChainEpoch(challenge.epoch), fil.LotusTSK{})
// if err != nil {
// return verifySigResult{}, cmn.WrErr(err)
// }
// be = &curChallengeTs.Blocks()[0].BeaconEntries[0]
be, err = lAPI.BeaconGetEntry(ctx, filabi.ChainEpoch(challenge.epoch))
if err != nil {
return verifySigResult{}, cmn.WrErr(err)
}
beaconCache.Add(challenge.epoch, be)
}
miFinTs, err := lAPI.ChainGetTipSetByHeight(ctx, filabi.ChainEpoch(challenge.epoch)-filprovider.ChainFinality, fil.LotusTSK{})
if err != nil {
return verifySigResult{}, cmn.WrErr(err)
}
mi, err := lAPI.StateMinerInfo(ctx, challenge.addr, miFinTs.Key())
if err != nil {
return verifySigResult{}, cmn.WrErr(err)
}
workerAddr, err := lAPI.StateAccountKey(ctx, mi.Worker, miFinTs.Key())
if err != nil {
return verifySigResult{}, cmn.WrErr(err)
}
// worker keys are always BLS
sigMatch, err := blsgo.Verify(
workerAddr.Payload(),
append(append([]byte{0x20, 0x20, 0x20}, be.Data...), challenge.arg...),
[]byte(sig),
)
if err != nil {
return verifySigResult{}, cmn.WrErr(err)
}
if !sigMatch {
return verifySigResult{
invalidSigErrstr: fmt.Sprintf("%s signature validation failed for auth header '%s'", authScheme, challenge.authHdr),
}, nil
}
return verifySigResult{}, nil
}