-
Notifications
You must be signed in to change notification settings - Fork 1
/
azuretokenaccessor.go
39 lines (32 loc) · 1.01 KB
/
azuretokenaccessor.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
package main
import (
"github.com/Azure/go-autorest/autorest/adal"
)
// AzureTokenAccessor Wrapper to grab Azure AAD token
type AzureTokenAccessor struct {
environment *Environment
oauthConfig *adal.OAuthConfig
}
// NewAzureTokenAccessor Create new AzureTokenAccessor
func NewAzureTokenAccessor(environment *Environment, tenantID string) *AzureTokenAccessor {
oauthConfig, err := adal.NewOAuthConfig(environment.aadLoginURL, tenantID)
if err != nil {
logerrorf("Unable to initialize OAuth config: %v", err)
}
return &AzureTokenAccessor{
environment: environment,
oauthConfig: oauthConfig,
}
}
// GetAccessToken Get access token to read or write custom metrics
func (tokenAccessor *AzureTokenAccessor) GetAccessToken(clientID string, clientSecret string, resourceName string) *adal.ServicePrincipalToken {
token, err := adal.NewServicePrincipalToken(
*tokenAccessor.oauthConfig,
clientID,
clientSecret,
resourceName)
if err != nil {
logerrorf("Unable to get AAD token: %v", err)
}
return token
}