package client

import (
	"github.com/Azure/go-autorest/autorest"
	"github.com/Azure/go-autorest/autorest/adal"
)

func getAuthorizerForResource(config Config) (autorest.Authorizer, error) {
	oauthConfig, err := adal.NewOAuthConfig(
		config.Environment.ActiveDirectoryEndpoint, config.TenantID)
	if err != nil {
		return nil, err
	}

	token, err := adal.NewServicePrincipalToken(
		*oauthConfig, config.ClientID, config.ClientSecret, config.Environment.TokenAudience)
	if err != nil {
		return nil, err
	}
	return autorest.NewBearerAuthorizer(token), err
}
