-
Notifications
You must be signed in to change notification settings - Fork 46
/
auth.go
97 lines (80 loc) · 2.44 KB
/
auth.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
package autorest
import (
"context"
"fmt"
"net/http"
"strings"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/adal"
"github.com/hashicorp/go-azure-sdk/sdk/auth"
)
func AutorestAuthorizer(authorizer auth.Authorizer) *Authorizer {
return &Authorizer{Authorizer: authorizer}
}
type Authorizer struct {
auth.Authorizer
}
// WithAuthorization implements the autorest.Authorizer interface
func (c *Authorizer) WithAuthorization() autorest.PrepareDecorator {
return func(p autorest.Preparer) autorest.Preparer {
return autorest.PreparerFunc(func(req *http.Request) (*http.Request, error) {
ctx := req.Context()
var err error
req, err = p.Prepare(req)
if err == nil {
token, err := c.Token(ctx, req)
if err != nil {
return nil, err
}
req, err = autorest.Prepare(req, autorest.WithHeader("Authorization", fmt.Sprintf("Bearer %s", token.AccessToken)))
if err != nil {
return req, err
}
auxTokens, err := c.AuxiliaryTokens(ctx, req)
if err != nil {
return req, err
}
auxTokenList := make([]string, 0)
for _, a := range auxTokens {
if a != nil && a.AccessToken != "" {
auxTokenList = append(auxTokenList, fmt.Sprintf("%s %s", a.TokenType, a.AccessToken))
}
}
if len(auxTokenList) > 0 {
return autorest.Prepare(req, autorest.WithHeader("x-ms-authorization-auxiliary", strings.Join(auxTokenList, ", ")))
}
return req, nil
}
return req, err
})
}
}
// BearerAuthorizerCallback is a helper that returns an *autorest.BearerAuthorizerCallback for use in data plane API clients in the Azure SDK
func (c *Authorizer) BearerAuthorizerCallback() *autorest.BearerAuthorizerCallback {
return autorest.NewBearerAuthorizerCallback(nil, func(_, resource string) (*autorest.BearerAuthorizer, error) {
token, err := c.Token(context.TODO(), &http.Request{})
if err != nil {
return nil, fmt.Errorf("obtaining token: %v", err)
}
return autorest.NewBearerAuthorizer(&servicePrincipalTokenWrapper{
tokenType: "Bearer",
tokenValue: token.AccessToken,
}), nil
})
}
type servicePrincipalTokenWrapper struct {
tokenType string
tokenValue string
}
func (s *servicePrincipalTokenWrapper) OAuthToken() string {
return s.tokenValue
}
func (s *servicePrincipalTokenWrapper) Token() adal.Token {
return adal.Token{
AccessToken: s.tokenValue,
Type: s.tokenType,
}
}
type ServicePrincipalToken interface {
Token() adal.Token
}