mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-04 15:10:54 +00:00
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:
@@ -376,8 +376,9 @@ func (b *Bittrex) SendHTTPRequest(ctx context.Context, ep exchange.URL, path str
|
||||
|
||||
// SendAuthHTTPRequest sends an authenticated request
|
||||
func (b *Bittrex) SendAuthHTTPRequest(ctx context.Context, ep exchange.URL, method, action string, params url.Values, data, result interface{}, resultHeader *http.Header) error {
|
||||
if !b.AllowAuthenticatedRequest() {
|
||||
return fmt.Errorf("%s %w", b.Name, exchange.ErrAuthenticatedRequestWithoutCredentialsSet)
|
||||
creds, err := b.GetCredentials(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
endpoint, err := b.API.Endpoints.GetURL(ep)
|
||||
if err != nil {
|
||||
@@ -409,13 +410,13 @@ func (b *Bittrex) SendAuthHTTPRequest(ctx context.Context, ep exchange.URL, meth
|
||||
sigPayload := ts + endpoint + path + method + contentHash
|
||||
hmac, err = crypto.GetHMAC(crypto.HashSHA512,
|
||||
[]byte(sigPayload),
|
||||
[]byte(b.API.Credentials.Secret))
|
||||
[]byte(creds.Secret))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
headers := make(map[string]string)
|
||||
headers["Api-Key"] = b.API.Credentials.Key
|
||||
headers["Api-Key"] = creds.Key
|
||||
headers["Api-Timestamp"] = ts
|
||||
headers["Api-Content-Hash"] = contentHash
|
||||
headers["Api-Signature"] = crypto.HexEncodeToString(hmac)
|
||||
|
||||
@@ -516,7 +516,7 @@ func TestGetOrderHistory(t *testing.T) {
|
||||
// Any tests below this line have the ability to impact your orders on the exchange. Enable canManipulateRealOrders to run them
|
||||
// ----------------------------------------------------------------------------------------------------------------------------
|
||||
func areTestAPIKeysSet() bool {
|
||||
return b.ValidateAPICredentials()
|
||||
return b.ValidateAPICredentials(b.GetDefaultCredentials()) == nil
|
||||
}
|
||||
|
||||
func TestSubmitOrder(t *testing.T) {
|
||||
|
||||
@@ -118,7 +118,7 @@ func (b *Bittrex) WsConnect() error {
|
||||
}
|
||||
|
||||
if b.GetAuthenticatedAPISupport(exchange.WebsocketAuthentication) {
|
||||
err = b.WsAuth()
|
||||
err = b.WsAuth(context.TODO())
|
||||
if err != nil {
|
||||
b.Websocket.DataHandler <- err
|
||||
b.Websocket.SetCanUseAuthenticatedEndpoints(false)
|
||||
@@ -149,9 +149,13 @@ func (b *Bittrex) WsSignalRHandshake(ctx context.Context, result interface{}) er
|
||||
|
||||
// WsAuth sends an authentication message to receive auth data
|
||||
// Authentications expire after 10 minutes
|
||||
func (b *Bittrex) WsAuth() error {
|
||||
func (b *Bittrex) WsAuth(ctx context.Context) error {
|
||||
creds, err := b.GetCredentials(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// [apiKey, timestamp in ms, random uuid, signed payload]
|
||||
apiKey := b.API.Credentials.Key
|
||||
apiKey := creds.Key
|
||||
randomContent, err := uuid.NewV4()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -160,7 +164,7 @@ func (b *Bittrex) WsAuth() error {
|
||||
hmac, err := crypto.GetHMAC(
|
||||
crypto.HashSHA512,
|
||||
[]byte(timestamp+randomContent.String()),
|
||||
[]byte(b.API.Credentials.Secret),
|
||||
[]byte(creds.Secret),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -485,7 +489,7 @@ func (b *Bittrex) wsHandleData(respRaw []byte) error {
|
||||
if b.Verbose {
|
||||
log.Debugf(log.WebsocketMgr, "%s - Re-authenticating.\n", b.Name)
|
||||
}
|
||||
err = b.WsAuth()
|
||||
err = b.WsAuth(context.TODO())
|
||||
if err != nil {
|
||||
b.Websocket.DataHandler <- err
|
||||
b.Websocket.SetCanUseAuthenticatedEndpoints(false)
|
||||
|
||||
@@ -899,7 +899,7 @@ func (b *Bittrex) GetFeeByType(ctx context.Context, feeBuilder *exchange.FeeBuil
|
||||
if feeBuilder == nil {
|
||||
return 0, fmt.Errorf("%T %w", feeBuilder, common.ErrNilPointer)
|
||||
}
|
||||
if !b.AllowAuthenticatedRequest() && // Todo check connection status
|
||||
if !b.AreCredentialsValid(ctx) && // Todo check connection status
|
||||
feeBuilder.FeeType == exchange.CryptocurrencyTradeFee {
|
||||
feeBuilder.FeeType = exchange.OfflineTradeFee
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user