-
Notifications
You must be signed in to change notification settings - Fork 46
/
token_mock.go
166 lines (133 loc) · 5.11 KB
/
token_mock.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
157
158
159
160
161
162
163
164
165
166
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package test
import (
"bytes"
"fmt"
"github.com/hashicorp/go-azure-sdk/sdk/environments"
"io"
"net/http"
"net/url"
"regexp"
)
const (
uuidRegex = "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"
)
type AzureADAccessTokenMockClient struct {
Authorization environments.Authorization
}
func (c *AzureADAccessTokenMockClient) Do(r *http.Request) (resp *http.Response, err error) {
if r == nil {
return nil, fmt.Errorf("request was nil")
}
resp = &http.Response{
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
StatusCode: http.StatusOK,
Header: http.Header{
"Content-Type": []string{"application/json; charset=utf-8"},
},
Request: r,
}
if err = resp.Request.ParseForm(); err != nil {
return nil, err
}
u, err := url.Parse(c.Authorization.LoginEndpoint)
if err != nil {
return nil, err
}
loginHost := u.Host
if p := u.Port(); p != "" {
loginHost = fmt.Sprintf("%s:%s", loginHost, p)
}
metadataHost := "169.254.169.254"
switch r.Host {
case loginHost:
return c.tokenFromLoginEndpoint(resp)
case metadataHost:
return c.tokenFromInstanceMetadataService(resp)
}
return nil, fmt.Errorf("unexpected Host header, expected %q, received %q", u.Host, loginHost)
}
func (c *AzureADAccessTokenMockClient) tokenFromLoginEndpoint(resp *http.Response) (*http.Response, error) {
if resp.Request.Method != http.MethodPost {
return nil, fmt.Errorf("unexpected request method, should be POST, received %s", resp.Request.Method)
}
pathRegex := fmt.Sprintf(`^\/(%s)(\/oauth2\/v2.0\/token)$`, uuidRegex)
if m := regexp.MustCompile(pathRegex).FindStringSubmatch(resp.Request.URL.Path); len(m) != 3 {
return nil, fmt.Errorf("URL path did not match pattern %q: %q", pathRegex, resp.Request.URL.Path)
}
clientId := resp.Request.PostForm.Get("client_id")
if clientId == "" {
resp.StatusCode = http.StatusBadRequest
resp.Body = io.NopCloser(bytes.NewBufferString(`{"error":"missing 'client_id' field"}`))
return resp, nil
}
scope := resp.Request.PostForm.Get("scope")
if scope == "" {
resp.StatusCode = http.StatusBadRequest
resp.Body = io.NopCloser(bytes.NewBufferString(`{"error":"missing 'scope' field"}`))
return resp, nil
}
grantType := resp.Request.PostForm.Get("grant_type")
switch grantType {
case "client_credentials":
if clientSecret := resp.Request.PostForm.Get("client_secret"); clientSecret != "" {
return c.token(resp)
}
if clientAssertion := resp.Request.PostForm.Get("client_assertion"); clientAssertion != "" {
switch clientAssertionType := resp.Request.PostForm.Get("client_assertion_type"); clientAssertionType {
case "urn:ietf:params:oauth:client-assertion-type:jwt-bearer":
return c.token(resp)
}
resp.StatusCode = http.StatusBadRequest
resp.Body = io.NopCloser(bytes.NewBufferString(`{"error":"unrecognised value for 'client_assertion_type' field"}`))
return resp, nil
}
default:
return nil, fmt.Errorf("grant type %q is not supported", grantType)
}
resp.StatusCode = http.StatusBadRequest
resp.Body = io.NopCloser(bytes.NewBufferString(`{"error":"unsupported or unrecognised authentication parameters"}`))
return resp, nil
}
func (c *AzureADAccessTokenMockClient) tokenFromInstanceMetadataService(resp *http.Response) (*http.Response, error) {
if resp.Request.Method != http.MethodGet {
return nil, fmt.Errorf("unexpected request method, should be GET, received %s", resp.Request.Method)
}
if expected := "/metadata/identity/oauth2/token"; resp.Request.URL.Path != expected {
return nil, fmt.Errorf("unexpected URL path, expected %q, received %q", expected, resp.Request.URL.Path)
}
if resp.Request.Header.Get("Metadata") != "true" {
resp.StatusCode = http.StatusBadRequest
resp.Body = io.NopCloser(bytes.NewBufferString(`{"error":"missing or invalid 'Metadata' header"}`))
return resp, nil
}
apiVersion := resp.Request.Form.Get("api-version")
if apiVersion == "" {
resp.StatusCode = http.StatusBadRequest
resp.Body = io.NopCloser(bytes.NewBufferString(`{"error":"missing 'api-version' field"}`))
return resp, nil
}
clientId := resp.Request.Form.Get("client_id")
if clientId == "" {
resp.StatusCode = http.StatusBadRequest
resp.Body = io.NopCloser(bytes.NewBufferString(`{"error":"missing 'client_id' field"}`))
return resp, nil
}
resource := resp.Request.Form.Get("resource")
if resource == "" {
resp.StatusCode = http.StatusBadRequest
resp.Body = io.NopCloser(bytes.NewBufferString(`{"error":"missing 'resource' field"}`))
return resp, nil
}
body := fmt.Sprintf(`{"access_token":"%s","client_id":"%s","expires_in":"86391","expires_on":"1611701390","ext_expires_in":"86399","not_before":"1611614690","resource":"%s","token_type":"Bearer"}`,
DummyAccessToken, clientId, resource)
resp.Body = io.NopCloser(bytes.NewBufferString(body))
return resp, nil
}
func (c *AzureADAccessTokenMockClient) token(resp *http.Response) (*http.Response, error) {
resp.Body = io.NopCloser(bytes.NewBufferString(fmt.Sprintf(`{"token_type":"Bearer","expires_in":3599,"ext_expires_in":3599,"access_token":"%s"}`, DummyAccessToken)))
return resp, nil
}