Skip to content

Commit

Permalink
Add endpoint for investor access (#1597)
Browse files Browse the repository at this point in the history
* api: Add investor API endpoint

* test: Add more testing funcs

* auth: Add more tests

* test: Add more unit and integration tests, fix failing tests

* permissions: Change constants to camel-case

* docs: Update swagger docs

* test: Fix http handler test

* test: Fix tests that create pools in the bootstrap

* deps: Update gsrpc to fa070294f3fb

* http: Use the new subkey SS58Encode

* loan: Update LoanInfo

* deps: Update gsrpc to 9417989

* deps: Update gsrpc to fd861c0

* tests: Fix test and var names

* deps: Update chain-custom-types to 81196b7a88cd

* test-client: Only log error when context expires

* test: Increase testworld timeout, enable parallel test for investor

* deps: Update gsrpc to v4.0.13 and chain-custom-types to v1.0.3

* deps: Update chain-custom-types v1.0.4

* loan: Update loan types, update tests

* http: Rename validation wrapper to service
  • Loading branch information
cdamian committed Jun 12, 2023
1 parent 98cf6bc commit 683212a
Show file tree
Hide file tree
Showing 87 changed files with 5,953 additions and 2,013 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ run-integration-tests:

run-testworld-tests:
@rm -rf profile.out
go test ./... -v -race -coverprofile=profile.out -covermode=atomic -tags=testworld -timeout 70m
go test ./... -v -race -coverprofile=profile.out -covermode=atomic -tags=testworld -timeout 80m

install: ## Builds and Install binary
@go install -ldflags "-X github.com/centrifuge/pod/version.gitCommit=`git rev-parse HEAD`" ./cmd/centrifuge/...
Expand Down
2 changes: 2 additions & 0 deletions centchain/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,8 @@ func (a *api) getDispatcherRunnerFunc(

eventsRaw, err := a.checkExtrinsicEventSuccess(meta, bh, extIdx)
if err != nil {
log.Errorf("Couldn't check extrinsic event success in block %d", *blockNumber)

return nil, err
}

Expand Down
8 changes: 7 additions & 1 deletion centchain/test_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,15 @@ func (f *TestClient) SubmitAndWait(ctx context.Context, senderKrp signature.Keyr
ticker := time.NewTicker(submitTransferInterval)
defer ticker.Stop()

var lastErr error

for {
select {
case <-ctx.Done():
if lastErr != nil {
log.Errorf("Couldn't submit extrinsic: %s", lastErr)
}

return nil, fmt.Errorf("context done while submitting transfer: %w", ctx.Err())
case <-ticker.C:
blockHash, err := f.submitExtrinsic(ctx, senderKrp, fn)
Expand All @@ -98,7 +104,7 @@ func (f *TestClient) SubmitAndWait(ctx context.Context, senderKrp signature.Keyr
return blockHash, nil
}

log.Errorf("Couldn't submit extrinsic: %s", err)
lastErr = err
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions config/configstore/repository_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions documents/client_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions documents/document_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 10 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ require (
github.com/ChainSafe/go-schnorrkel v1.0.0
github.com/Masterminds/semver v1.5.0
github.com/centrifuge/centrifuge-protobufs v1.0.0
github.com/centrifuge/chain-custom-types v1.0.2
github.com/centrifuge/go-substrate-rpc-client/v4 v4.0.12
github.com/centrifuge/chain-custom-types v1.0.4
github.com/centrifuge/go-substrate-rpc-client/v4 v4.0.13
github.com/centrifuge/gocelery/v2 v2.0.0-20221101190423-3b07af1b49a6
github.com/centrifuge/precise-proofs v1.0.0
github.com/common-nighthawk/go-figure v0.0.0-20200609044655-c4b36f998cf2
Expand Down Expand Up @@ -39,11 +39,11 @@ require (
github.com/stretchr/testify v1.7.2
github.com/swaggo/swag v1.7.0
github.com/syndtr/goleveldb v1.0.1-0.20220614013038-64ee5596c38a
github.com/vedhavyas/go-subkey v1.0.3
github.com/vedhavyas/go-subkey v1.0.4
github.com/vedhavyas/go-subkey/v2 v2.0.0
github.com/whyrusleeping/go-logging v0.0.1
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d
golang.org/x/net v0.0.0-20220607020251-c690dde0001d
golang.org/x/crypto v0.7.0
golang.org/x/net v0.8.0
google.golang.org/grpc v1.40.0
google.golang.org/protobuf v1.28.0
)
Expand Down Expand Up @@ -214,12 +214,11 @@ require (
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/zap v1.21.0 // indirect
golang.org/x/mod v0.6.0-dev.0.20211013180041-c96bc1413d57 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.1.8-0.20211029000441-d6a9af8af023 // indirect
golang.org/x/xerrors v0.0.0-20220517211312-f3a8303e98df // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/tools v0.6.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20210211221406-4ccc9a5e4183 // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
Expand Down
37 changes: 20 additions & 17 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QH
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/centrifuge/centrifuge-protobufs v1.0.0 h1:ZPg0XpkTrGrjQu8scXjMGs7jjqsWPiXmOXdV/bz30ng=
github.com/centrifuge/centrifuge-protobufs v1.0.0/go.mod h1:VL6mcnK6vTRiFljHP39J0WBI3Uu5BHQjhdFkCxY9/9I=
github.com/centrifuge/chain-custom-types v1.0.2 h1:wlHjlK4phXEMVHXQX8JeUuHhINa25RFUg5FFLs1r5JQ=
github.com/centrifuge/chain-custom-types v1.0.2/go.mod h1:hrD9djuAUOJXxV6iYv0Nexw69V/ywk38pTT4waJaakw=
github.com/centrifuge/chain-custom-types v1.0.4 h1:B/7Ehgbu+FA2KdvmAGYiHmBwNOOcuc/oH8TYj0qc8ZY=
github.com/centrifuge/chain-custom-types v1.0.4/go.mod h1:kSUJ3O83vaLutJIiaEfqwn3lfTaisn/G/baS8WrycTg=
github.com/centrifuge/go-merkle v0.0.0-20190727075423-0ac78bbbc01b h1:TPvvMcGAc3TVBVgQ4XYYEWTXxYls8YuylZ8JzrVxPzc=
github.com/centrifuge/go-merkle v0.0.0-20190727075423-0ac78bbbc01b/go.mod h1:0voJY6Qzxvr2S0LeDSFQiCnJzGq5gORg2SwCmn8602I=
github.com/centrifuge/go-substrate-rpc-client/v4 v4.0.12 h1:DCYWIBOalB0mKKfUg2HhtGgIkBbMA1fnlnkZp7fHB18=
github.com/centrifuge/go-substrate-rpc-client/v4 v4.0.12/go.mod h1:5g1oM4Zu3BOaLpsKQ+O8PAv2kNuq+kPcA1VzFbsSqxE=
github.com/centrifuge/go-substrate-rpc-client/v4 v4.0.13 h1:U4DvCQdYgTXSRtSbW/cPxKr45KiRqq3xWoImd8n/wj8=
github.com/centrifuge/go-substrate-rpc-client/v4 v4.0.13/go.mod h1:szA5wf9suAIcNg/1S3rGeFITHqrnqH5TC6b+O0SEQ94=
github.com/centrifuge/gocelery/v2 v2.0.0-20221101190423-3b07af1b49a6 h1:0/y+LOu+sAnIlXNsvCYmxUY9n6OiCyhT/37KoYKEFSk=
github.com/centrifuge/gocelery/v2 v2.0.0-20221101190423-3b07af1b49a6/go.mod h1:q6PvyLLCu1IQcE2dFv8advWprzY5b5GtZYRU/bH9nWE=
github.com/centrifuge/precise-proofs v1.0.0 h1:hcXSK2d5aPEZUa09p+aoyw9ofKI6Twip+UMIx+sJKYc=
Expand Down Expand Up @@ -1314,8 +1314,8 @@ github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyC
github.com/valyala/fasthttp v1.14.0 h1:67bfuW9azCMwW/Jlq/C+VeihNpAuJMWkYPBig1gdi3A=
github.com/valyala/fasthttp v1.14.0/go.mod h1:ol1PCaL0dX20wC0htZ7sYCsvCYmrouYra0zHzaclZhE=
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
github.com/vedhavyas/go-subkey v1.0.3 h1:iKR33BB/akKmcR2PMlXPBeeODjWLM90EL98OrOGs8CA=
github.com/vedhavyas/go-subkey v1.0.3/go.mod h1:CloUaFQSSTdWnINfBRFjVMkWXZANW+nd8+TI5jYcl6Y=
github.com/vedhavyas/go-subkey v1.0.4 h1:QwjBZx4w7qXC2lmqol2jJfhaNXPI9BsgLZiMiCwqGDU=
github.com/vedhavyas/go-subkey v1.0.4/go.mod h1:aOIil/KS9hJlnr9ZSQKSoXdu/MbnkCxG4x9IOlLsMtI=
github.com/vedhavyas/go-subkey/v2 v2.0.0 h1:LemDIsrVtRSOkp0FA8HxP6ynfKjeOj3BY2U9UNfeDMA=
github.com/vedhavyas/go-subkey/v2 v2.0.0/go.mod h1:95aZ+XDCWAUUynjlmi7BtPExjXgXxByE0WfBwbmIRH4=
github.com/vedhavyas/life v0.0.0-20200804102658-e96a0a4f69e3 h1:g0cLRcjmmyzge/oZaOoZYa45j3Num2OzsNU7mrPDi20=
Expand Down Expand Up @@ -1445,8 +1445,8 @@ golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm
golang.org/x/crypto v0.0.0-20210506145944-38f3c27a63bf/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
golang.org/x/crypto v0.0.0-20210813211128-0a44fdfbc16e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20220518034528-6f7dac969898/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d h1:sK3txAijHtOK88l68nt020reeT1ZdKLIYetKl95FzVY=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
Expand Down Expand Up @@ -1479,8 +1479,8 @@ golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzB
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20211013180041-c96bc1413d57 h1:LQmS1nU0twXLA96Kt7U9qtHJEbBk3z6Q0V4UXjZkpr4=
golang.org/x/mod v0.6.0-dev.0.20211013180041-c96bc1413d57/go.mod h1:3p9vT2HGsQu2K1YbXdKPJLVgG5VJdoTa1poYQBtP1AY=
golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20180719180050-a680a1efc54d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -1542,8 +1542,9 @@ golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d/go.mod h1:9nx3DQGgdP8bBQD5qx
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220517181318-183a9ca12b87/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220607020251-c690dde0001d h1:4SFsTMi4UahlKoloni7L4eYzhFRifURQLw+yv0QDCx8=
golang.org/x/net v0.0.0-20220607020251-c690dde0001d/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20181017192945-9dcd33a902f4/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20181203162652-d668ce993890/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand All @@ -1563,8 +1564,9 @@ golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180810173357-98c5dad5d1a0/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down Expand Up @@ -1652,8 +1654,8 @@ golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220517195934-5e4e11fc645e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e h1:CsOuNlbOuf0mzxJIefr6Q4uAUetRUwZE4qt7VfzP+xo=
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
Expand All @@ -1665,8 +1667,9 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down Expand Up @@ -1729,8 +1732,8 @@ golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4f
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.8-0.20211029000441-d6a9af8af023 h1:0c3L82FDQ5rt1bjTBlchS8t6RQ6299/+5bWMnRLh+uI=
golang.org/x/tools v0.1.8-0.20211029000441-d6a9af8af023/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU=
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
64 changes: 4 additions & 60 deletions http/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,78 +2,22 @@ package http

import (
"net/http"
"regexp"
"strings"

"github.com/centrifuge/pod/config"
"github.com/centrifuge/pod/contextutil"
httpAuth "github.com/centrifuge/pod/http/auth"
"github.com/centrifuge/pod/http/auth/access"
"github.com/centrifuge/pod/utils/httputils"
"github.com/go-chi/render"
)

var (
adminPathRegex = regexp.MustCompile(`^/v2/accounts(|/generate|/0x[a-fA-F0-9]+)$`)
)

func isAdminPath(path string) bool {
return adminPathRegex.MatchString(path)
}

func auth(authService httpAuth.Service, cfgService config.Service) func(handler http.Handler) http.Handler {
skippedURLs := map[string]struct{}{
"/ping": {},
}

func auth(validationServices access.ValidationServices) func(handler http.Handler) http.Handler {
return func(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path

if _, ok := skippedURLs[path]; ok {
handler.ServeHTTP(w, r)
return
}

// Header format -> "Authorization": "Bearer $jw3t"
authHeader := r.Header.Get("Authorization")
bearer := strings.Split(authHeader, " ")
if len(bearer) != 2 {
log.Error("Invalid auth header")
render.Status(r, http.StatusForbidden)
render.JSON(w, r, httputils.HTTPError{Message: "Authentication failed"})
return
}
accHeader, err := authService.Validate(r.Context(), bearer[1])
if err != nil {
log.Errorf("Couldn't validate token: %s", err)
render.Status(r, http.StatusForbidden)
render.JSON(w, r, httputils.HTTPError{Message: "Authentication failed"})
return
}

if isAdminPath(path) {
if !accHeader.IsAdmin {
log.Error("Account not an admin")
render.Status(r, http.StatusForbidden)
render.JSON(w, r, httputils.HTTPError{Message: "Authentication failed"})
return
}

handler.ServeHTTP(w, r)
return
}

acc, err := cfgService.GetAccount(accHeader.Identity.ToBytes())
if err != nil {
log.Errorf("Couldn't retrieve account from storage: %s", err)
if err := validationServices.Validate(r); err != nil {
log.Errorf("Couldn't validate access for request: %s", err)
render.Status(r, http.StatusForbidden)
render.JSON(w, r, httputils.HTTPError{Message: "Authentication failed"})
return
}

ctx := contextutil.WithAccount(r.Context(), acc)

r = r.WithContext(ctx)
handler.ServeHTTP(w, r)
})
}
Expand Down
Loading

0 comments on commit 683212a

Please sign in to comment.