github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/integration/helpers/fake_server.go (about) 1 package helpers 2 3 import ( 4 "fmt" 5 "net/http" 6 "strings" 7 "time" 8 9 . "github.com/onsi/gomega" 10 . "github.com/onsi/gomega/gexec" 11 . "github.com/onsi/gomega/ghttp" 12 ) 13 14 const ( 15 DefaultV2Version string = "2.131.0" 16 DefaultV3Version string = "3.66.0" 17 DefaultAuthorizationPath string = "" 18 ) 19 20 // StartAndTargetMockServerWithAPIVersions starts and targets a server with the given V2 and V3 21 // API versions. 22 func StartAndTargetMockServerWithAPIVersions(v2Version string, v3Version string) *Server { 23 server := StartMockServerWithAPIVersions(v2Version, v3Version) 24 Eventually(CF("api", server.URL(), "--skip-ssl-validation")).Should(Exit(0)) 25 26 return server 27 } 28 29 // StartMockServerWithMinimumCLIVersion starts a server with the default V2 and V3 30 // API versions and the given minimum CLI version. 31 func StartMockServerWithMinimumCLIVersion(minCLIVersion string) *Server { 32 return startServerWithVersions(DefaultV2Version, DefaultV3Version, &minCLIVersion, DefaultAuthorizationPath) 33 } 34 35 // StartMockServerWithAPIVersions starts a server with the given V2 and V3 36 // API versions 37 func StartMockServerWithAPIVersions(v2Version string, v3Version string) *Server { 38 return startServerWithVersions(v2Version, v3Version, nil, DefaultAuthorizationPath) 39 } 40 41 // StartMockServerWithAPIVersions starts a server with the given V2 and V3 42 // API versions 43 func StartMockServerWithCustomAuthorizationEndpoint(authorizationPath string) *Server { 44 return startServerWithVersions(DefaultV2Version, DefaultV3Version, nil, authorizationPath) 45 } 46 47 func startServerWithVersions(v2Version string, v3Version string, minimumCLIVersion *string, authorizationPath string) *Server { 48 server := NewTLSServer() 49 50 rootResponse := fmt.Sprintf(`{ 51 "links": { 52 "self": { 53 "href": "%[1]s" 54 }, 55 "cloud_controller_v2": { 56 "href": "%[1]s/v2", 57 "meta": { 58 "version": "%[2]s" 59 } 60 }, 61 "cloud_controller_v3": { 62 "href": "%[1]s/v3", 63 "meta": { 64 "version": "%[3]s" 65 } 66 }, 67 "network_policy_v0": { 68 "href": "%[1]s/networking/v0/external" 69 }, 70 "network_policy_v1": { 71 "href": "%[1]s/networking/v1/external" 72 }, 73 "uaa": { 74 "href": "%[1]s" 75 }, 76 "logging": { 77 "href": "wss://unused:443" 78 }, 79 "app_ssh": { 80 "href": "unused:2222", 81 "meta": { 82 "host_key_fingerprint": "unused", 83 "oauth_client": "ssh-proxy" 84 } 85 } 86 } 87 }`, server.URL(), v2Version, v3Version) 88 89 v2InfoResponse := struct { 90 APIVersion string `json:"api_version"` 91 AuthorizationEndpoint string `json:"authorization_endpoint"` 92 MinCLIVersion *string `json:"min_cli_version"` 93 }{ 94 APIVersion: v2Version, 95 AuthorizationEndpoint: server.URL() + authorizationPath, 96 MinCLIVersion: minimumCLIVersion} 97 98 server.RouteToHandler(http.MethodGet, "/v2/info", RespondWithJSONEncoded(http.StatusOK, v2InfoResponse)) 99 100 v3Response := strings.Replace(`{"links": { 101 "organizations": { 102 "href": "SERVER_URL/v3/organizations" 103 }, 104 "spaces": { 105 "href": "SERVER_URL/v3/spaces" 106 } 107 }}`, "SERVER_URL", server.URL(), -1) 108 109 server.RouteToHandler(http.MethodGet, "/v3", func(res http.ResponseWriter, req *http.Request) { 110 res.WriteHeader(http.StatusOK) 111 res.Write([]byte(v3Response)) 112 }) 113 114 server.RouteToHandler(http.MethodGet, authorizationPath+"/login", func(res http.ResponseWriter, req *http.Request) { 115 res.WriteHeader(http.StatusOK) 116 res.Write([]byte(`{"links":{}}`)) 117 }) 118 119 server.RouteToHandler(http.MethodGet, "/", func(res http.ResponseWriter, req *http.Request) { 120 res.WriteHeader(http.StatusOK) 121 res.Write([]byte(rootResponse)) 122 }) 123 124 return server 125 } 126 127 // AddMfa adds a mock handler to the given server which returns a login response and a 200 status code 128 // on GET requests to the /login endpoint. It adds another mock handler to validate the given password and MFA token 129 // upon POST requests to /oauth/token. 130 func AddMfa(server *Server, password string, mfaToken string) { 131 getLoginResponse := fmt.Sprintf(`{ 132 "app": { 133 "version": "4.28.0" 134 }, 135 "showLoginLinks": true, 136 "links": { 137 "uaa": "%[1]s", 138 "passwd": "/forgot_password", 139 "login": "%[1]s", 140 "register": "/create_account" 141 }, 142 "zone_name": "uaa", 143 "entityID": "some-host-name.example.com", 144 "commit_id": "8917980", 145 "idpDefinitions": {}, 146 "prompts": { 147 "username": [ 148 "text", 149 "Email" 150 ], 151 "password": [ 152 "password", 153 "Password" 154 ], 155 "passcode": [ 156 "password", 157 "Temporary Authentication Code ( Get one at %[1]s/passcode )" 158 ], 159 "mfaCode": [ 160 "password", 161 "MFA Code ( Register at %[1]s )" 162 ] 163 }, 164 "timestamp": "2019-02-19T18:08:02+0000" 165 }`, server.URL()) 166 167 server.RouteToHandler(http.MethodGet, "/login", 168 RespondWith(http.StatusOK, getLoginResponse), 169 ) 170 171 server.RouteToHandler(http.MethodPost, "/oauth/token", makeMFAValidator(password, mfaToken)) 172 173 } 174 175 func makeMFAValidator(password string, mfaToken string) http.HandlerFunc { 176 return func(res http.ResponseWriter, req *http.Request) { 177 Expect(req.ParseForm()).To(Succeed()) 178 rightPassword := len(req.Form["password"]) == 1 && req.Form["password"][0] == password 179 rightCode := len(req.Form["mfaCode"]) == 1 && req.Form["mfaCode"][0] == mfaToken 180 181 if rightPassword && rightCode { 182 res.WriteHeader(http.StatusOK) 183 res.Write([]byte(`{ 184 "access_token": "some-access-token", 185 "token_type": "bearer", 186 "id_token": "some-id-token", 187 "refresh_token": "some-refresh-token", 188 "expires_in": 599, 189 "scope": "openid routing.router_groups.write scim.read cloud_controller.admin uaa.user routing.router_groups.read cloud_controller.read password.write cloud_controller.write network.admin doppler.firehose scim.write", 190 "jti": "66e46003f28e44c8a6582f6d6e44753f" 191 }`)) 192 return 193 } 194 res.WriteHeader(http.StatusUnauthorized) 195 } 196 } 197 198 // AddLoginRoutes adds a mock handler to the given server which returns an access token and a 200 status code 199 // on POST requests to /oauth/token. 200 func AddLoginRoutes(s *Server) { 201 s.RouteToHandler("POST", "/oauth/token", RespondWith(http.StatusOK, 202 fmt.Sprintf(`{ 203 "access_token": "%s", 204 "expires_in": 599, 205 "id_token": "some-other-token", 206 "jti": "some-other-string", 207 "refresh_token": "some-refresh-token", 208 "scope": "openid routing.router_groups.write scim.read cloud_controller.admin uaa.user routing.router_groups.read cloud_controller.read password.write cloud_controller.write network.admin doppler.firehose scim.write", 209 "token_type": "bearer" 210 }`, BuildTokenString(time.Now()))), 211 ) 212 }