2016-04-21 19:50:03 -04:00
|
|
|
package arm
|
|
|
|
|
2017-05-28 03:38:45 -04:00
|
|
|
import (
|
|
|
|
"github.com/Azure/go-autorest/autorest/adal"
|
2017-05-29 00:06:09 -04:00
|
|
|
"github.com/Azure/go-autorest/autorest/azure"
|
2017-05-28 03:38:45 -04:00
|
|
|
)
|
2016-04-21 19:50:03 -04:00
|
|
|
|
|
|
|
type Authenticate struct {
|
|
|
|
env azure.Environment
|
|
|
|
clientID string
|
|
|
|
clientSecret string
|
|
|
|
tenantID string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAuthenticate(env azure.Environment, clientID, clientSecret, tenantID string) *Authenticate {
|
|
|
|
return &Authenticate{
|
|
|
|
env: env,
|
|
|
|
clientID: clientID,
|
|
|
|
clientSecret: clientSecret,
|
|
|
|
tenantID: tenantID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-28 03:38:45 -04:00
|
|
|
func (a *Authenticate) getServicePrincipalToken() (*adal.ServicePrincipalToken, error) {
|
2016-04-21 19:50:03 -04:00
|
|
|
return a.getServicePrincipalTokenWithResource(a.env.ResourceManagerEndpoint)
|
|
|
|
}
|
|
|
|
|
2017-05-28 03:38:45 -04:00
|
|
|
func (a *Authenticate) getServicePrincipalTokenWithResource(resource string) (*adal.ServicePrincipalToken, error) {
|
|
|
|
oauthConfig, err := adal.NewOAuthConfig(a.env.ActiveDirectoryEndpoint, a.tenantID)
|
2016-04-21 19:50:03 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-11-09 11:39:03 -05:00
|
|
|
if a.clientID == "" && a.clientSecret == "" {
|
|
|
|
return adal.NewServicePrincipalTokenFromMSI("http://169.254.169.254/metadata/identity/oauth2/token", resource)
|
|
|
|
}
|
|
|
|
|
2017-05-28 03:38:45 -04:00
|
|
|
spt, err := adal.NewServicePrincipalToken(
|
2016-04-21 19:50:03 -04:00
|
|
|
*oauthConfig,
|
|
|
|
a.clientID,
|
|
|
|
a.clientSecret,
|
|
|
|
resource)
|
|
|
|
|
|
|
|
return spt, err
|
|
|
|
}
|