-
Notifications
You must be signed in to change notification settings - Fork 1
/
azure_client.go
156 lines (122 loc) · 4.05 KB
/
azure_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package azure
import (
"encoding/base64"
"encoding/json"
"strings"
"github.com/Azure/azure-sdk-for-go/services/graphrbac/1.6/graphrbac"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/adal"
"github.com/rancher/norman/httperror"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
)
type azureClient struct {
servicePrincipal *adal.ServicePrincipalToken
userClient graphrbac.UsersClient
groupClient graphrbac.GroupsClient
}
// newClientCode sets up the SPT, user and group client using a code
func newClientCode(code string, config *v3.AzureADConfig) (*azureClient, error) {
ac := &azureClient{}
oauthConfig, err := adal.NewOAuthConfig(config.Endpoint, config.TenantID)
if err != nil {
return nil, err
}
// The tenantID should not be in the endpoint, drop /tenantID
tenant := config.TenantID
if strings.Contains(config.GraphEndpoint, tenant) {
i := strings.Index(config.GraphEndpoint, tenant)
config.GraphEndpoint = config.GraphEndpoint[:i-1]
}
spt, err := adal.NewServicePrincipalTokenFromAuthorizationCode(
*oauthConfig,
config.ApplicationID,
config.ApplicationSecret,
code,
config.RancherURL,
config.GraphEndpoint,
nil,
)
if err != nil {
return nil, err
}
// The refresh is required, call above just creates the struct
spt.SetRefreshCallbacks(nil)
err = spt.Refresh()
if err != nil {
return nil, err
}
ac.servicePrincipal = spt
// Create the required bearer token
bearer := autorest.NewBearerAuthorizer(spt)
// Setup the user client
userClient := graphrbac.NewUsersClientWithBaseURI(config.GraphEndpoint, config.TenantID)
userClient.Authorizer = bearer
ac.userClient = userClient
// Setup the group client
groupClient := graphrbac.NewGroupsClientWithBaseURI(config.GraphEndpoint, config.TenantID)
groupClient.Authorizer = bearer
ac.groupClient = groupClient
return ac, nil
}
// newClientToken sets up the SPT, user and group client using a current Token
func newClientToken(config *v3.AzureADConfig, azureToken adal.Token) (*azureClient, error) {
ac := &azureClient{}
oauthConfig, err := adal.NewOAuthConfig(config.Endpoint, config.TenantID)
if err != nil {
return nil, err
}
secret := &adal.ServicePrincipalAuthorizationCodeSecret{
ClientSecret: config.ApplicationSecret,
}
spt, err := adal.NewServicePrincipalTokenFromManualTokenSecret(
*oauthConfig,
config.ApplicationID,
config.GraphEndpoint,
azureToken,
secret,
nil)
if err != nil {
return nil, err
}
spt.SetRefreshCallbacks(nil)
ac.servicePrincipal = spt
// Create the required bearer token
bearer := autorest.NewBearerAuthorizer(spt)
// Setup the user client
userClient := graphrbac.NewUsersClientWithBaseURI(config.GraphEndpoint, config.TenantID)
userClient.Authorizer = bearer
ac.userClient = userClient
// Setup the group client
groupClient := graphrbac.NewGroupsClientWithBaseURI(config.GraphEndpoint, config.TenantID)
groupClient.Authorizer = bearer
ac.groupClient = groupClient
return ac, nil
}
// accessToken returns the OAuthToken from the underlying SPT
func (ac *azureClient) accessToken() string {
return ac.servicePrincipal.OAuthToken()
}
// marshalTokenJSON returns a JSON of the underlying Token
func (ac *azureClient) marshalTokenJSON() ([]byte, error) {
return ac.servicePrincipal.MarshalTokenJSON()
}
// parseJWTforField will parse the claims in a token for the field requested
func parseJWTforField(tokenString string, fieldID string) (string, error) {
pieces := strings.Split(tokenString, ".")
if len(pieces) != 3 {
return "", httperror.NewAPIError(httperror.InvalidFormat, "invalid token")
}
decoded, err := base64.RawStdEncoding.DecodeString(pieces[1])
if err != nil {
return "", httperror.NewAPIError(httperror.InvalidFormat, "invalid token")
}
var dat map[string]interface{}
err = json.Unmarshal([]byte(decoded), &dat)
if err != nil {
return "", httperror.NewAPIError(httperror.InvalidFormat, "invalid token")
}
if _, ok := dat[fieldID]; !ok {
return "", httperror.NewAPIError(httperror.InvalidFormat, "invalid token")
}
return dat[fieldID].(string), nil
}