-
Notifications
You must be signed in to change notification settings - Fork 36
/
authorization.go
48 lines (38 loc) · 1.04 KB
/
authorization.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package resourcemanager
import (
"context"
"fmt"
"net/http"
"strings"
"github.com/hashicorp/go-azure-sdk/sdk/auth"
)
func AuthorizeResourceManagerRequest(ctx context.Context, req *http.Request, authorizer auth.Authorizer) error {
if req == nil {
return fmt.Errorf("request was nil")
}
if authorizer == nil {
return fmt.Errorf("authorizer was nil")
}
token, err := authorizer.Token(ctx, req)
if err != nil {
return err
}
if req.Header == nil {
req.Header = make(http.Header)
}
req.Header.Set("Authorization", fmt.Sprintf("%s %s", token.Type(), token.AccessToken))
auxTokens, err := authorizer.AuxiliaryTokens(ctx, req)
if err != nil {
return err
}
auxTokenValues := make([]string, 0)
for _, auxToken := range auxTokens {
auxTokenValues = append(auxTokenValues, fmt.Sprintf("%s %s", auxToken.Type(), auxToken.AccessToken))
}
if len(auxTokenValues) > 0 {
req.Header.Set("X-Ms-Authorization-Auxiliary", strings.Join(auxTokenValues, ", "))
}
return nil
}