github.phpd.cn/hashicorp/packer@v1.3.2/builder/azure/arm/authenticate.go (about) 1 package arm 2 3 import ( 4 "github.com/Azure/go-autorest/autorest/adal" 5 "github.com/Azure/go-autorest/autorest/azure" 6 ) 7 8 type Authenticate struct { 9 env azure.Environment 10 clientID string 11 clientSecret string 12 tenantID string 13 } 14 15 func NewAuthenticate(env azure.Environment, clientID, clientSecret, tenantID string) *Authenticate { 16 return &Authenticate{ 17 env: env, 18 clientID: clientID, 19 clientSecret: clientSecret, 20 tenantID: tenantID, 21 } 22 } 23 24 func (a *Authenticate) getServicePrincipalToken() (*adal.ServicePrincipalToken, error) { 25 return a.getServicePrincipalTokenWithResource(a.env.ResourceManagerEndpoint) 26 } 27 28 func (a *Authenticate) getServicePrincipalTokenWithResource(resource string) (*adal.ServicePrincipalToken, error) { 29 oauthConfig, err := adal.NewOAuthConfig(a.env.ActiveDirectoryEndpoint, a.tenantID) 30 if err != nil { 31 return nil, err 32 } 33 34 spt, err := adal.NewServicePrincipalToken( 35 *oauthConfig, 36 a.clientID, 37 a.clientSecret, 38 resource) 39 40 return spt, err 41 }