Skip to content

Commit

Permalink
Fixed Azure Entra token scope (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
tnewman-at-gm authored Oct 29, 2024
1 parent 7541716 commit 7a7d210
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
3 changes: 2 additions & 1 deletion sasl/azure_event_hubs_entra/azure_event_hubs_entra.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ func (m *Mechanism) getEntraToken(ctx context.Context, saslMeta *sasl.Metadata)

func buildTokenRequestOptions(saslMeta *sasl.Metadata) policy.TokenRequestOptions {
tokenRequestOptions := policy.TokenRequestOptions{
Scopes: []string{"https://" + saslMeta.Host},
Scopes: []string{"https://" + saslMeta.Host + "/.default"},
EnableCAE: false,
}

return tokenRequestOptions
Expand Down
20 changes: 20 additions & 0 deletions sasl/azure_event_hubs_entra/azure_event_hubs_entra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package azure_event_hubs_entra
import (
"context"
"errors"
"fmt"
"strings"
"testing"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
Expand All @@ -15,6 +17,24 @@ type MockTokenCredential struct {
}

func (c *MockTokenCredential) GetToken(ctx context.Context, options policy.TokenRequestOptions) (azcore.AccessToken, error) {
if len(options.Scopes) != 1 {
return azcore.AccessToken{}, fmt.Errorf("Scopes must contain 1 element! Contains %d elements.", len(options.Scopes))
}

scope := options.Scopes[0]

if !strings.HasPrefix(scope, "https://") {
return azcore.AccessToken{}, fmt.Errorf("Scope must start with https, and it did not.")
}

if !strings.HasSuffix(scope, "/.default") {
return azcore.AccessToken{}, fmt.Errorf("Scope must end with /.default, and it did not.")
}

if options.EnableCAE {
return azcore.AccessToken{}, fmt.Errorf("CAE must be false. It was true.")
}

token, err := c.getTokenFunc()

if err != nil {
Expand Down

0 comments on commit 7a7d210

Please sign in to comment.