github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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 "context" 8 9 "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2016-06-01/subscriptions" 10 "github.com/Azure/go-autorest/autorest/adal" 11 "github.com/juju/errors" 12 ) 13 14 // OAuthConfig returns an azure.OAuthConfig based on the given resource 15 // manager endpoint and subscription ID. This will make a request to the 16 // resource manager API to discover the Active Directory tenant ID. 17 func OAuthConfig( 18 sdkCtx context.Context, 19 client subscriptions.Client, 20 resourceManagerEndpoint string, 21 subscriptionId string, 22 ) (*adal.OAuthConfig, string, error) { 23 authURI, err := DiscoverAuthorizationURI(sdkCtx, client, subscriptionId) 24 if err != nil { 25 return nil, "", errors.Annotate(err, "detecting auth URI") 26 } 27 logger.Debugf("discovered auth URI: %s", authURI) 28 29 // The authorization URI scheme and host identifies the AD endpoint. 30 // The authorization URI path identifies the AD tenant. 31 tenantId, err := AuthorizationURITenantID(authURI) 32 if err != nil { 33 return nil, "", errors.Annotate(err, "getting tenant ID") 34 } 35 authURI.Path = "" 36 adEndpoint := authURI.String() 37 38 oauthConfig, err := adal.NewOAuthConfig(adEndpoint, tenantId) 39 if err != nil { 40 return nil, "", errors.Annotate(err, "getting OAuth configuration") 41 } 42 return oauthConfig, tenantId, nil 43 }