Skip to content

Commit

Permalink
Support new kind of token provider for SP and MSI auth
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas11 committed Sep 16, 2022
1 parent 0d3d2db commit 111c536
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions provider/pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"strings"
"time"

hamiltonAuth "github.com/manicminer/hamilton-autorest/auth"
"github.com/manicminer/hamilton/environments"
"github.com/segmentio/encoding/json"

Expand Down Expand Up @@ -1831,14 +1832,26 @@ func (k *azureNativeProvider) getOAuthToken(ctx context.Context, auth *authentic
if err != nil {
return "", fmt.Errorf("getting authorization token: %w", err)
}

// go-azure-helpers returns different kinds of Authorizer from different auth methods so we
// need to check to choose the right method to get a token.
var token string
ba, ok := authorizer.(*autorest.BearerAuthorizer)
if !ok {
return "", fmt.Errorf("converting %T to a BearerAuthorizer", authorizer)
if ok {
tokenProvider := ba.TokenProvider()
token = tokenProvider.OAuthToken()
} else {
if outer, ok := authorizer.(*hamiltonAuth.Authorizer); ok {
t, err := outer.Token()
if err != nil {
return "", err
}
token = t.AccessToken
}
}
tokenProvider := ba.TokenProvider()
token := tokenProvider.OAuthToken()

if token == "" {
return "", fmt.Errorf("empty token from %T", tokenProvider)
return "", fmt.Errorf("empty token from %T", authorizer)
}
return token, nil
}
Expand Down

0 comments on commit 111c536

Please sign in to comment.