~juju-qa/ubuntu/xenial/juju/2.0-rc2

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/provider/azure/internal/azureauth/oauth.go

  • Committer: Nicholas Skaggs
  • Date: 2016-09-30 14:39:30 UTC
  • mfrom: (1.8.1)
  • Revision ID: nicholas.skaggs@canonical.com-20160930143930-vwwhrefh6ftckccy
import upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
}