/
token.go
62 lines (47 loc) · 1.69 KB
/
token.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
package common
import (
"encoding/json"
"github.com/pkg/errors"
)
type Identity struct {
ClientId string `json:"client_id"`
}
type TokenResponse struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
ExpiresIn string `json:"expires_in"`
ExpiresOn string `json:"expires_on"`
NotBefore string `json:"not_before"`
Resource string `json:"resource"`
TokenType string `json:"token_type"`
}
const (
TokenURITemplate = "http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01"
)
// GetToken retrieves an authentication token which will be used for authorizing
// requests sent to Azure services requiring authorization (e.g., Azure Blob, AKV)
func GetToken(ResourceId string, i Identity) (r TokenResponse, err error) {
// HTTP GET request to authentication token service
resource_param := "&resource=" + ResourceId
client_id_param := ""
if i.ClientId != "" {
client_id_param = "&client_id=" + i.ClientId
}
uri := TokenURITemplate + resource_param + client_id_param
httpResponse, err := HTTPGetRequest(uri, true)
if err != nil {
return r, errors.Wrapf(err, "http get authentication token failed for %s", uri)
}
httpResponseBodyBytes, err := HTTPResponseBody(httpResponse)
if err != nil {
return r, errors.Wrapf(err, "pulling http get authentication token response failed")
}
// Unmarshall response body into struct
err = json.Unmarshal(httpResponseBodyBytes, &r)
if err != nil {
return r, errors.Wrapf(err, "unmarshalling authentication token response failed")
}
return r, nil
}