Skip to content

Commit

Permalink
Merge pull request #107 from tombuildsstuff/bugfix/aad-auth-headers-f…
Browse files Browse the repository at this point in the history
…or-files

bugfix: add `x-ms-file-request-intent` header for the Files API when authenticating using OAuth
  • Loading branch information
manicminer authored Mar 1, 2024
2 parents 22fff23 + 019d82f commit 4b9fa74
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
17 changes: 17 additions & 0 deletions storage/2023-11-03/file/directories/client.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package directories

import (
"context"
"fmt"
"net/http"

"github.com/hashicorp/go-azure-sdk/sdk/auth"
"github.com/hashicorp/go-azure-sdk/sdk/client/dataplane/storage"
)

Expand All @@ -16,6 +19,20 @@ func NewWithBaseUri(baseUri string) (*Client, error) {
if err != nil {
return nil, fmt.Errorf("building base client: %+v", err)
}

baseClient.Client.AuthorizeRequest = func(ctx context.Context, req *http.Request, authorizer auth.Authorizer) error {
if err := auth.SetAuthHeader(ctx, req, authorizer); err != nil {
return fmt.Errorf("authorizing request: %+v", err)
}

// Only set this header if OAuth is being used (i.e. not shared key authentication)
if _, ok := authorizer.(*auth.SharedKeyAuthorizer); !ok {
req.Header.Set("x-ms-file-request-intent", "backup")
}

return nil
}

return &Client{
Client: baseClient,
}, nil
Expand Down
17 changes: 17 additions & 0 deletions storage/2023-11-03/file/files/client.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package files

import (
"context"
"fmt"
"net/http"

"github.com/hashicorp/go-azure-sdk/sdk/auth"
"github.com/hashicorp/go-azure-sdk/sdk/client/dataplane/storage"
)

Expand All @@ -16,6 +19,20 @@ func NewWithBaseUri(baseUri string) (*Client, error) {
if err != nil {
return nil, fmt.Errorf("building base client: %+v", err)
}

baseClient.Client.AuthorizeRequest = func(ctx context.Context, req *http.Request, authorizer auth.Authorizer) error {
if err := auth.SetAuthHeader(ctx, req, authorizer); err != nil {
return fmt.Errorf("authorizing request: %+v", err)
}

// Only set this header if OAuth is being used (i.e. not shared key authentication)
if _, ok := authorizer.(*auth.SharedKeyAuthorizer); !ok {
req.Header.Set("x-ms-file-request-intent", "backup")
}

return nil
}

return &Client{
Client: baseClient,
}, nil
Expand Down

0 comments on commit 4b9fa74

Please sign in to comment.