github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/provider/azure/internal/azureauth/oauth.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package azureauth
     5  
     6  import (
     7  	"github.com/Azure/azure-sdk-for-go/arm/resources/subscriptions"
     8  	"github.com/Azure/go-autorest/autorest/azure"
     9  	"github.com/juju/errors"
    10  )
    11  
    12  // OAuthConfig returns an azure.OAuthConfig based on the given resource
    13  // manager endpoint and subscription ID. This will make a request to the
    14  // resource manager API to discover the Active Directory tenant ID.
    15  func OAuthConfig(
    16  	client subscriptions.Client,
    17  	resourceManagerEndpoint string,
    18  	subscriptionId string,
    19  ) (*azure.OAuthConfig, string, error) {
    20  	authURI, err := DiscoverAuthorizationURI(client, subscriptionId)
    21  	if err != nil {
    22  		return nil, "", errors.Annotate(err, "detecting auth URI")
    23  	}
    24  	logger.Debugf("discovered auth URI: %s", authURI)
    25  
    26  	// The authorization URI scheme and host identifies the AD endpoint.
    27  	// The authorization URI path identifies the AD tenant.
    28  	tenantId, err := AuthorizationURITenantID(authURI)
    29  	if err != nil {
    30  		return nil, "", errors.Annotate(err, "getting tenant ID")
    31  	}
    32  	authURI.Path = ""
    33  	adEndpoint := authURI.String()
    34  
    35  	cloudEnv := azure.Environment{ActiveDirectoryEndpoint: adEndpoint}
    36  	oauthConfig, err := cloudEnv.OAuthConfigForTenant(tenantId)
    37  	if err != nil {
    38  		return nil, "", errors.Annotate(err, "getting OAuth configuration")
    39  	}
    40  	return oauthConfig, tenantId, nil
    41  }