Skip to content

Commit

Permalink
Expose OAuth token provider for use outside autorest (#520)
Browse files Browse the repository at this point in the history
* feat: extract token creation to public method for MSI auth

* Add getter for token provider on BearerAuthorizer
  • Loading branch information
alespour authored Jun 5, 2020
1 parent 2e06601 commit 5ac3904
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
5 changes: 5 additions & 0 deletions autorest/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ func (ba *BearerAuthorizer) WithAuthorization() PrepareDecorator {
}
}

// TokenProvider returns OAuthTokenProvider so that it can be used for authorization outside the REST.
func (ba *BearerAuthorizer) TokenProvider() adal.OAuthTokenProvider {
return ba.tokenProvider
}

// BearerAuthorizerCallbackFunc is the authentication callback signature.
type BearerAuthorizerCallbackFunc func(tenantID, resource string) (*BearerAuthorizer, error)

Expand Down
14 changes: 12 additions & 2 deletions autorest/azure/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,8 +713,8 @@ type MSIConfig struct {
ClientID string
}

// Authorizer gets the authorizer from MSI.
func (mc MSIConfig) Authorizer() (autorest.Authorizer, error) {
// ServicePrincipalToken creates a ServicePrincipalToken from MSI.
func (mc MSIConfig) ServicePrincipalToken() (*adal.ServicePrincipalToken, error) {
msiEndpoint, err := adal.GetMSIEndpoint()
if err != nil {
return nil, err
Expand All @@ -733,5 +733,15 @@ func (mc MSIConfig) Authorizer() (autorest.Authorizer, error) {
}
}

return spToken, nil
}

// Authorizer gets the authorizer from MSI.
func (mc MSIConfig) Authorizer() (autorest.Authorizer, error) {
spToken, err := mc.ServicePrincipalToken()
if err != nil {
return nil, err
}

return autorest.NewBearerAuthorizer(spToken), nil
}

0 comments on commit 5ac3904

Please sign in to comment.