Code refactoring for bpa operator
[icn.git] / cmd / bpa-operator / vendor / github.com / Azure / go-autorest / autorest / adal / config.go
1 package adal
2
3 // Copyright 2017 Microsoft Corporation
4 //
5 //  Licensed under the Apache License, Version 2.0 (the "License");
6 //  you may not use this file except in compliance with the License.
7 //  You may obtain a copy of the License at
8 //
9 //      http://www.apache.org/licenses/LICENSE-2.0
10 //
11 //  Unless required by applicable law or agreed to in writing, software
12 //  distributed under the License is distributed on an "AS IS" BASIS,
13 //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 //  See the License for the specific language governing permissions and
15 //  limitations under the License.
16
17 import (
18         "fmt"
19         "net/url"
20 )
21
22 // OAuthConfig represents the endpoints needed
23 // in OAuth operations
24 type OAuthConfig struct {
25         AuthorityEndpoint  url.URL `json:"authorityEndpoint"`
26         AuthorizeEndpoint  url.URL `json:"authorizeEndpoint"`
27         TokenEndpoint      url.URL `json:"tokenEndpoint"`
28         DeviceCodeEndpoint url.URL `json:"deviceCodeEndpoint"`
29 }
30
31 // IsZero returns true if the OAuthConfig object is zero-initialized.
32 func (oac OAuthConfig) IsZero() bool {
33         return oac == OAuthConfig{}
34 }
35
36 func validateStringParam(param, name string) error {
37         if len(param) == 0 {
38                 return fmt.Errorf("parameter '" + name + "' cannot be empty")
39         }
40         return nil
41 }
42
43 // NewOAuthConfig returns an OAuthConfig with tenant specific urls
44 func NewOAuthConfig(activeDirectoryEndpoint, tenantID string) (*OAuthConfig, error) {
45         apiVer := "1.0"
46         return NewOAuthConfigWithAPIVersion(activeDirectoryEndpoint, tenantID, &apiVer)
47 }
48
49 // NewOAuthConfigWithAPIVersion returns an OAuthConfig with tenant specific urls.
50 // If apiVersion is not nil the "api-version" query parameter will be appended to the endpoint URLs with the specified value.
51 func NewOAuthConfigWithAPIVersion(activeDirectoryEndpoint, tenantID string, apiVersion *string) (*OAuthConfig, error) {
52         if err := validateStringParam(activeDirectoryEndpoint, "activeDirectoryEndpoint"); err != nil {
53                 return nil, err
54         }
55         api := ""
56         // it's legal for tenantID to be empty so don't validate it
57         if apiVersion != nil {
58                 if err := validateStringParam(*apiVersion, "apiVersion"); err != nil {
59                         return nil, err
60                 }
61                 api = fmt.Sprintf("?api-version=%s", *apiVersion)
62         }
63         const activeDirectoryEndpointTemplate = "%s/oauth2/%s%s"
64         u, err := url.Parse(activeDirectoryEndpoint)
65         if err != nil {
66                 return nil, err
67         }
68         authorityURL, err := u.Parse(tenantID)
69         if err != nil {
70                 return nil, err
71         }
72         authorizeURL, err := u.Parse(fmt.Sprintf(activeDirectoryEndpointTemplate, tenantID, "authorize", api))
73         if err != nil {
74                 return nil, err
75         }
76         tokenURL, err := u.Parse(fmt.Sprintf(activeDirectoryEndpointTemplate, tenantID, "token", api))
77         if err != nil {
78                 return nil, err
79         }
80         deviceCodeURL, err := u.Parse(fmt.Sprintf(activeDirectoryEndpointTemplate, tenantID, "devicecode", api))
81         if err != nil {
82                 return nil, err
83         }
84
85         return &OAuthConfig{
86                 AuthorityEndpoint:  *authorityURL,
87                 AuthorizeEndpoint:  *authorizeURL,
88                 TokenEndpoint:      *tokenURL,
89                 DeviceCodeEndpoint: *deviceCodeURL,
90         }, nil
91 }