|
| 1 | +# Copyright (C) Extensible Service Proxy Authors |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# Redistribution and use in source and binary forms, with or without |
| 5 | +# modification, are permitted provided that the following conditions |
| 6 | +# are met: |
| 7 | +# 1. Redistributions of source code must retain the above copyright |
| 8 | +# notice, this list of conditions and the following disclaimer. |
| 9 | +# 2. Redistributions in binary form must reproduce the above copyright |
| 10 | +# notice, this list of conditions and the following disclaimer in the |
| 11 | +# documentation and/or other materials provided with the distribution. |
| 12 | +# |
| 13 | +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 14 | +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 16 | +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 17 | +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 18 | +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 19 | +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 20 | +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 21 | +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 22 | +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 23 | +# SUCH DAMAGE. |
| 24 | +# |
| 25 | +################################################################################ |
| 26 | +# |
| 27 | +use strict; |
| 28 | +use warnings; |
| 29 | + |
| 30 | +################################################################################ |
| 31 | + |
| 32 | +use src::nginx::t::ApiManager; # Must be first (sets up import path to the Nginx test module) |
| 33 | +use src::nginx::t::HttpServer; |
| 34 | +use src::nginx::t::ServiceControl; |
| 35 | +use src::nginx::t::Auth; |
| 36 | +use Test::Nginx; # Imports Nginx's test module |
| 37 | +use Test::More; # And the test framework |
| 38 | + |
| 39 | +################################################################################ |
| 40 | + |
| 41 | +# Port allocations |
| 42 | + |
| 43 | +my $NginxPort = ApiManager::pick_port(); |
| 44 | + |
| 45 | +my $t = Test::Nginx->new()->has(qw/http proxy/)->plan(5); |
| 46 | + |
| 47 | +my $config = ApiManager::get_bookstore_service_config .<<"EOF"; |
| 48 | +producer_project_id: "endpoints-test" |
| 49 | +authentication { |
| 50 | + providers { |
| 51 | + id: "test_auth" |
| 52 | + issuer: "628645741881-noabiu23f5a8m8ovd8ucv698lj78vv0l\@developer.gserviceaccount.com" |
| 53 | + jwks_uri: "http://127.0.0.1/pubkey" |
| 54 | + authorization_url: "http://dummy-redirect-url" |
| 55 | + } |
| 56 | + rules { |
| 57 | + selector: "ListShelves" |
| 58 | + requirements { |
| 59 | + provider_id: "test_auth" |
| 60 | + audiences: "ok_audience_1" |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | +control { |
| 65 | + environment: "http://127.0.0.1:3000" |
| 66 | +} |
| 67 | +EOF |
| 68 | +$t->write_file('service.pb.txt', $config); |
| 69 | + |
| 70 | +# enable the redirect_authorization_url flag |
| 71 | +ApiManager::write_file_expand($t, 'server_config.txt', <<"EOF"); |
| 72 | +api_authentication_config { |
| 73 | + redirect_authorization_url: true |
| 74 | +} |
| 75 | +EOF |
| 76 | + |
| 77 | +$t->write_file_expand('nginx.conf', <<"EOF"); |
| 78 | +%%TEST_GLOBALS%% |
| 79 | +daemon off; |
| 80 | +events { |
| 81 | + worker_connections 32; |
| 82 | +} |
| 83 | +http { |
| 84 | + %%TEST_GLOBALS_HTTP%% |
| 85 | + server_tokens off; |
| 86 | + server { |
| 87 | + listen 127.0.0.1:${NginxPort}; |
| 88 | + server_name localhost; |
| 89 | + location / { |
| 90 | + endpoints { |
| 91 | + api service.pb.txt; |
| 92 | + server_config server_config.txt; |
| 93 | + on; |
| 94 | + } |
| 95 | + proxy_pass http://127.0.0.1:3000; |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | +EOF |
| 100 | + |
| 101 | +$t->run(); |
| 102 | + |
| 103 | +################################################################################ |
| 104 | +# no auth token |
| 105 | +my $response1 = ApiManager::http_get($NginxPort, "/shelves"); |
| 106 | + |
| 107 | +# should redirect |
| 108 | +like($response1, qr/HTTP\/1\.1 302 Moved Temporarily/, 'Returned HTTP 302.'); |
| 109 | +like($response1, qr/Location: http:\/\/dummy-redirect-url/, 'Return correct redirect location.'); |
| 110 | + |
| 111 | +# expired auth token |
| 112 | +my $expired_token = Auth::get_expired_jwt_token; |
| 113 | +my $response2 = ApiManager::http($NginxPort, <<"EOF"); |
| 114 | +GET /shelves HTTP/1.0 |
| 115 | +Host: localhost |
| 116 | +Authorization: Bearer $expired_token |
| 117 | +
|
| 118 | +EOF |
| 119 | + |
| 120 | +# should redirect |
| 121 | +like($response2, qr/HTTP\/1\.1 302 Moved Temporarily/, 'Returned HTTP 302.'); |
| 122 | +like($response2, qr/Location: http:\/\/dummy-redirect-url/, 'Return correct redirect location.'); |
| 123 | + |
| 124 | +#invalid auth token |
| 125 | +my $response3 = ApiManager::http($NginxPort, <<"EOF"); |
| 126 | +GET /shelves HTTP/1.0 |
| 127 | +Host: localhost |
| 128 | +Authorization: Bearer invalid_token |
| 129 | +
|
| 130 | +EOF |
| 131 | + |
| 132 | +# should not redirect. |
| 133 | +like($response3, qr/HTTP\/1\.1 401 Unauthorized/, 'Returned HTTP 401.'); |
| 134 | + |
| 135 | +$t->stop_daemons(); |
| 136 | + |
| 137 | +################################################################################ |
| 138 | + |
0 commit comments