Debian Codegraph @

cache/mock_test.go

raw
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3
4 package cache
5
6 import (
7 "bytes"
8 "encoding/base64"
9 "fmt"
10 "io"
11 "net/http"
12 "strings"
13 )
14
15 // mockSTS returns mock Azure AD responses so tests don't have to account for MSAL metadata requests
16 type mockSTS struct {
17 tokenRequestCallback func(*http.Request)
18 }
19
20 func (m *mockSTS) Do(req *http.Request) (*http.Response, error) {
21 res := http.Response{StatusCode: http.StatusOK}
22 switch s := strings.Split(req.URL.Path, "/"); s[len(s)-1] {
23 case "instance":
24 res.Body = io.NopCloser(bytes.NewReader(instanceMetadata("tenant")))
25 case "openid-configuration":
26 res.Body = io.NopCloser(bytes.NewReader(tenantMetadata("tenant")))
27 case "token":
28 if m.tokenRequestCallback != nil {
29 m.tokenRequestCallback(req)
30 }
31 if err := req.ParseForm(); err != nil {
32 return nil, err
33 }
34 scope := strings.Split(req.FormValue("scope"), " ")[0]
35 userinfo := ""
36 if upn := req.FormValue("username"); upn != "" {
37 clientinfo := base64.RawStdEncoding.EncodeToString([]byte(fmt.Sprintf(`{"uid":"%s","utid":"utid"}`, upn)))
38 userinfo = fmt.Sprintf(`, "client_info":"%s", "id_token":"x.e30", "refresh_token": "rt"`, clientinfo)
39 }
40 res.Body = io.NopCloser(bytes.NewReader([]byte(fmt.Sprintf(`{"access_token": %q, "expires_in": 3600%s}`, scope, userinfo))))
41 default:
42 // User realm metadata request paths look like "/common/UserRealm/user@domain".
43 // Matching on the UserRealm segment avoids having to know the UPN.
44 if s[len(s)-2] == "UserRealm" {
45 res.Body = io.NopCloser(
46 strings.NewReader(`{"account_type":"Managed","cloud_audience_urn":"urn","cloud_instance_name":"...","domain_name":"..."}`),
47 )
48 } else {
49 panic("unexpected request " + req.URL.String())
50 }
51 }
52 return &res, nil
53 }
54
55 func (m *mockSTS) CloseIdleConnections() {}
56
57 func instanceMetadata(tenant string) []byte {
58 return []byte(strings.ReplaceAll(`{
59 "tenant_discovery_endpoint": "https://login.microsoftonline.com/{tenant}/v2.0/.well-known/openid-configuration",
60 "api-version": "1.1",
61 "metadata": [
62 {
63 "preferred_network": "login.microsoftonline.com",
64 "preferred_cache": "login.windows.net",
65 "aliases": [
66 "login.microsoftonline.com",
67 "login.windows.net",
68 "login.microsoft.com",
69 "sts.windows.net"
70 ]
71 }
72 ]
73 }`, "{tenant}", tenant))
74 }
75
76 func tenantMetadata(tenant string) []byte {
77 return []byte(strings.ReplaceAll(`{
78 "token_endpoint": "https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token",
79 "token_endpoint_auth_methods_supported": [
80 "client_secret_post",
81 "private_key_jwt",
82 "client_secret_basic"
83 ],
84 "jwks_uri": "https://login.microsoftonline.com/{tenant}/discovery/v2.0/keys",
85 "response_modes_supported": [
86 "query",
87 "fragment",
88 "form_post"
89 ],
90 "subject_types_supported": [
91 "pairwise"
92 ],
93 "id_token_signing_alg_values_supported": [
94 "RS256"
95 ],
96 "response_types_supported": [
97 "code",
98 "id_token",
99 "code id_token",
100 "id_token token"
101 ],
102 "scopes_supported": [
103 "openid",
104 "profile",
105 "email",
106 "offline_access"
107 ],
108 "issuer": "https://login.microsoftonline.com/{tenant}/v2.0",
109 "request_uri_parameter_supported": false,
110 "userinfo_endpoint": "https://graph.microsoft.com/oidc/userinfo",
111 "authorization_endpoint": "https://login.microsoftonline.com/{tenant}/oauth2/v2.0/authorize",
112 "device_authorization_endpoint": "https://login.microsoftonline.com/{tenant}/oauth2/v2.0/devicecode",
113 "http_logout_supported": true,
114 "frontchannel_logout_supported": true,
115 "end_session_endpoint": "https://login.microsoftonline.com/{tenant}/oauth2/v2.0/logout",
116 "claims_supported": [
117 "sub",
118 "iss",
119 "cloud_instance_name",
120 "cloud_instance_host_name",
121 "cloud_graph_host_name",
122 "msgraph_host",
123 "aud",
124 "exp",
125 "iat",
126 "auth_time",
127 "acr",
128 "nonce",
129 "preferred_username",
130 "name",
131 "tid",
132 "ver",
133 "at_hash",
134 "c_hash",
135 "email"
136 ],
137 "kerberos_endpoint": "https://login.microsoftonline.com/{tenant}/kerberos",
138 "tenant_region_scope": "NA",
139 "cloud_instance_name": "microsoftonline.com",
140 "cloud_graph_host_name": "graph.windows.net",
141 "msgraph_host": "graph.microsoft.com",
142 "rbac_url": "https://pas.windows.net"
143 }`, "{tenant}", tenant))
144 }