question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TransportHandler.ProcessMessageAsync overwrites Authorization provided by a custom RequestHandler

See original GitHub issue

Describe the bug When using ResourceTokens that may expire, I have a custom RequestHandler that will pull an updated token and write it to the RequestMessage.Headers. The TransportHandler.ProcessMessageAsync does not check for an existing header before overwriting it with the expired value.

        internal Task<DocumentServiceResponse> ProcessMessageAsync(
            RequestMessage request,
            CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            DocumentServiceRequest serviceRequest = request.ToDocumentServiceRequest();

-            //TODO: extrace auth into a separate handler
-            string authorization = ((IAuthorizationTokenProvider)this.client.DocumentClient).GetUserAuthorizationToken(
-                serviceRequest.ResourceAddress,
-                PathsHelper.GetResourcePath(request.ResourceType),
-                request.Method.ToString(),
-                serviceRequest.Headers,
-                AuthorizationTokenType.PrimaryMasterKey,
-                payload: out _);
-
-            serviceRequest.Headers[HttpConstants.HttpHeaders.Authorization] = authorization;
+            if (string.IsNullOrEmpty(serviceRequest.Headers[HttpConstants.HttpHeaders.Authorization]))
+            {
+                //TODO: extrace auth into a separate handler
+                string authorization = ((IAuthorizationTokenProvider)this.client.DocumentClient).GetUserAuthorizationToken(
+                    serviceRequest.ResourceAddress,
+                    PathsHelper.GetResourcePath(request.ResourceType),
+                    request.Method.ToString(),
+                    serviceRequest.Headers,
+                    AuthorizationTokenType.PrimaryMasterKey,
+                    payload: out _);
+
+                serviceRequest.Headers[HttpConstants.HttpHeaders.Authorization] = authorization;
+            }

            IStoreModel storeProxy = this.client.DocumentClient.GetStoreProxy(serviceRequest);
            if (request.OperationType == OperationType.Upsert)
            {
                return this.ProcessUpsertAsync(storeProxy, serviceRequest, cancellationToken);
            }
            else
            {
                return storeProxy.ProcessMessageAsync(serviceRequest, cancellationToken);
            }
        }

Environment summary SDK Version: Source OS Version (e.g. Windows, Linux, MacOSX)

Additional context Add any other context about the problem here (for example, complete stack traces or logs).

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
j82wcommented, Nov 21, 2019

@ausfeldt will implementing a swap credentials be easier once the work is done for the auth fix you are working on?

I don’t see any reason to not support it in v3. It’s not a breaking change.

0reactions
ausfeldtcommented, Nov 21, 2019

I think it should be both v3 and v4.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Feature] CosmosClient multi-tenant enablement · Issue #651
This issue is trying to address the CosmosClient gaps only. Hence token generation gaps are out-of-scope. Possibilities. Authorization provider ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found