context: Add authenticated HTTP credentials (#892)

* gRPC: context overide

* exchanges: continue update

* exchange: Update context handling
*Add setter methods for API credentials
*Shift credentials functionality to its own file in exchanges package
*Add tests
*Refactor function DeployCredentialsToContext for library usage
*Add function to process credential metadata from API boundary to internal use context value.
*Add OTP rpc handling

* exchanges: reverts to old style in GetFeeByType, reverts some code I accidently deleted. Plus things and other. XD

* template: update

* exchanges: fix linter issues

* REMOVE THAT AWESOME NEW LINE!

* gct: fix some tests

* I cant spell :(

* exchanges/gctscript: fix more tests

* coinnut: fix tests

* Update exchanges/credentials.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update exchanges/credentials.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update exchanges/credentials.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update exchanges/credentials.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update exchanges/credentials.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* glorious: nits

* exchanges/gctcli: stop applying empty credentials

* fix linters

* exchanges: add test

* rpceserver: actually check error for errors

* rpcserver: fix up tests

* Update exchanges/credentials.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* exchanges/creds: move tests to corresponding files, add protection and segration for Credentials struct & ptr values

* exchanges/creds: allow subaccount to override default credentials via gRPC

* exchanges/credentials: don't return nil in GetCredentials

* creds: spelling

* exchanges: fix glorious NITS!

* credentials: Add in test and refactor IsEmpty method.

* credentials: change type positioning (glorious)

* exchange_template: Fix template changes

* DOCS: Refresh

* docs: fix spelling

* DOCS: fix alignment and add package

* DOCS: ALIGN!

Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io>
Co-authored-by: Scott <gloriousCode@users.noreply.github.com>
This commit is contained in:
Ryan O'Hara-Reid
2022-03-21 13:58:08 +11:00
committed by GitHub
parent 58b9f8b9ec
commit 09fa2f236a
122 changed files with 3006 additions and 2126 deletions

View File

@@ -107,8 +107,7 @@ func (s *RPCServer) authenticateClient(ctx context.Context) (context.Context, er
password != s.Config.RemoteControl.Password {
return ctx, fmt.Errorf("username/password mismatch")
}
return ctx, nil
return exchange.ParseCredentialsMetadata(ctx, md)
}
// StartRPCServer starts a gRPC server with TLS auth
@@ -1553,7 +1552,7 @@ func (s *RPCServer) GetCryptocurrencyDepositAddresses(ctx context.Context, r *gc
}
if !exch.GetAuthenticatedAPISupport(exchange.RestAuthentication) {
return nil, exchange.ErrAuthenticatedRequestWithoutCredentialsSet
return nil, fmt.Errorf("%s, %w", r.Exchange, exchange.ErrAuthenticationSupportNotEnabled)
}
result, err := s.GetCryptocurrencyDepositAddressesByExchange(r.Exchange)
@@ -1586,7 +1585,7 @@ func (s *RPCServer) GetCryptocurrencyDepositAddress(ctx context.Context, r *gctr
}
if !exch.GetAuthenticatedAPISupport(exchange.RestAuthentication) {
return nil, exchange.ErrAuthenticatedRequestWithoutCredentialsSet
return nil, fmt.Errorf("%s, %w", r.Exchange, exchange.ErrAuthenticationSupportNotEnabled)
}
addr, err := s.GetExchangeCryptocurrencyDepositAddress(ctx,
@@ -4158,10 +4157,13 @@ func (s *RPCServer) GetFuturesPositions(ctx context.Context, r *gctrpc.GetFuture
}
b := exch.GetBase()
subAccount := b.API.Credentials.Subaccount
creds, err := b.GetCredentials(ctx)
if err != nil {
return nil, err
}
var subErr string
if subAccount != "" {
subErr = "for subaccount: " + subAccount
if creds.SubAccount != "" {
subErr = "for subaccount: " + creds.SubAccount
}
orders, err := exch.GetFuturesPositions(ctx, a, cp, start, end)
if err != nil {
@@ -4189,7 +4191,7 @@ func (s *RPCServer) GetFuturesPositions(ctx context.Context, r *gctrpc.GetFuture
return nil, fmt.Errorf("%w %v", err, subErr)
}
response := &gctrpc.GetFuturesPositionsResponse{
SubAccount: subAccount,
SubAccount: creds.SubAccount,
}
var totalRealisedPNL, totalUnrealisedPNL decimal.Decimal
for i := range pos {
@@ -4310,24 +4312,29 @@ func (s *RPCServer) GetCollateral(ctx context.Context, r *gctrpc.GetCollateralRe
var calculators []order.CollateralCalculator
var acc *account.SubAccount
var subAccounts []string
subAccount := r.SubAccount
if subAccount == "" {
b := exch.GetBase()
subAccount = b.API.Credentials.Subaccount
creds, err := exch.GetBase().GetCredentials(ctx)
if err != nil {
return nil, err
}
for i := range ai.Accounts {
subAccounts = append(subAccounts, ai.Accounts[i].ID)
if ai.Accounts[i].ID == "main" && subAccount == "" {
if ai.Accounts[i].ID == "main" && creds.SubAccount == "" {
acc = &ai.Accounts[i]
break
}
if strings.EqualFold(subAccount, ai.Accounts[i].ID) {
if strings.EqualFold(creds.SubAccount, ai.Accounts[i].ID) {
acc = &ai.Accounts[i]
break
}
}
if acc == nil {
return nil, fmt.Errorf("%w for %s %s and stored credentials - available subaccounts: %s", errNoAccountInformation, exch.GetName(), r.SubAccount, strings.Join(subAccounts, ","))
return nil, fmt.Errorf("%w for %s %s and stored credentials - available subaccounts: %s",
errNoAccountInformation,
exch.GetName(),
creds.SubAccount,
strings.Join(subAccounts, ","))
}
var spotPairs currency.Pairs
if r.CalculateOffline {
@@ -4369,7 +4376,6 @@ func (s *RPCServer) GetCollateral(ctx context.Context, r *gctrpc.GetCollateralRe
}
calc := &order.TotalCollateralCalculator{
SubAccount: r.SubAccount,
CollateralAssets: calculators,
CalculateOffline: r.CalculateOffline,
FetchPositions: true,
@@ -4382,7 +4388,7 @@ func (s *RPCServer) GetCollateral(ctx context.Context, r *gctrpc.GetCollateralRe
var collateralDisplayCurrency = " " + collateral.CollateralCurrency.String()
result := &gctrpc.GetCollateralResponse{
SubAccount: subAccount,
SubAccount: creds.SubAccount,
CollateralCurrency: collateral.CollateralCurrency.String(),
AvailableCollateral: collateral.AvailableCollateral.String() + collateralDisplayCurrency,
UsedCollateral: collateral.UsedCollateral.String() + collateralDisplayCurrency,