|
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +# contributor license agreements. See the NOTICE file distributed with |
| 4 | +# this work for additional information regarding copyright ownership. |
| 5 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +# (the "License"); you may not use this file except in compliance with |
| 7 | +# the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | +use t::APISIX 'no_plan'; |
| 18 | + |
| 19 | +no_long_string(); |
| 20 | +no_root_location(); |
| 21 | +no_shuffle(); |
| 22 | +run_tests; |
| 23 | + |
| 24 | +__DATA__ |
| 25 | + |
| 26 | +=== TEST 1: add consumer with basic-auth and key-auth plugins |
| 27 | +--- config |
| 28 | + location /t { |
| 29 | + content_by_lua_block { |
| 30 | + local t = require("lib.test_admin").test |
| 31 | + local code, body = t('/apisix/admin/consumers', |
| 32 | + ngx.HTTP_PUT, |
| 33 | + [[{ |
| 34 | + "username": "foo", |
| 35 | + "plugins": { |
| 36 | + "basic-auth": { |
| 37 | + "username": "foo", |
| 38 | + "password": "bar" |
| 39 | + }, |
| 40 | + "jwt-auth": { |
| 41 | + "key": "user-key", |
| 42 | + "secret": "my-secret-key" |
| 43 | + } |
| 44 | + } |
| 45 | + }]] |
| 46 | + ) |
| 47 | + if code >= 300 then |
| 48 | + ngx.status = code |
| 49 | + end |
| 50 | + ngx.say(body) |
| 51 | + } |
| 52 | + } |
| 53 | +--- request |
| 54 | +GET /t |
| 55 | +--- response_body |
| 56 | +passed |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +=== TEST 2: enable multi auth plugin using admin api |
| 61 | +--- config |
| 62 | + location /t { |
| 63 | + content_by_lua_block { |
| 64 | + local t = require("lib.test_admin").test |
| 65 | + local code, body = t('/apisix/admin/routes/1', |
| 66 | + ngx.HTTP_PUT, |
| 67 | + [[{ |
| 68 | + "plugins": { |
| 69 | + "multi-auth": { |
| 70 | + "auth_plugins": [ |
| 71 | + { |
| 72 | + "basic-auth": {} |
| 73 | + }, |
| 74 | + { |
| 75 | + "jwt-auth": {} |
| 76 | + }, |
| 77 | + { |
| 78 | + "hmac-auth": {} |
| 79 | + } |
| 80 | + ] |
| 81 | + } |
| 82 | + }, |
| 83 | + "upstream": { |
| 84 | + "nodes": { |
| 85 | + "127.0.0.1:1980": 1 |
| 86 | + }, |
| 87 | + "type": "roundrobin" |
| 88 | + }, |
| 89 | + "uri": "/hello" |
| 90 | + }]] |
| 91 | + ) |
| 92 | + |
| 93 | + if code >= 300 then |
| 94 | + ngx.status = code |
| 95 | + end |
| 96 | + ngx.say(body) |
| 97 | + } |
| 98 | + } |
| 99 | +--- request |
| 100 | +GET /t |
| 101 | +--- response_body |
| 102 | +passed |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | +=== TEST 3: invalid basic-auth credentials |
| 107 | +--- request |
| 108 | +GET /hello |
| 109 | +--- more_headers |
| 110 | +Authorization: Basic YmFyOmJhcgo= |
| 111 | +--- error_code: 401 |
| 112 | +--- response_body |
| 113 | +{"message":"Authorization Failed"} |
| 114 | +--- error_log |
| 115 | +basic-auth failed to authenticate the request, code: 401. error: Invalid user authorization |
| 116 | +jwt-auth failed to authenticate the request, code: 401. error: JWT token invalid: invalid jwt string |
| 117 | +hmac-auth failed to authenticate the request, code: 401. error: client request can't be validated: Authorization header does not start with 'Signature' |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | +=== TEST 4: valid basic-auth creds |
| 122 | +--- request |
| 123 | +GET /hello |
| 124 | +--- more_headers |
| 125 | +Authorization: Basic Zm9vOmJhcg== |
| 126 | +--- response_body |
| 127 | +hello world |
| 128 | +--- no_error_log |
| 129 | +failed to authenticate the request |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | +=== TEST 5: missing hmac auth authorization header |
| 134 | +--- request |
| 135 | +GET /hello |
| 136 | +--- error_code: 401 |
| 137 | +--- response_body |
| 138 | +{"message":"Authorization Failed"} |
| 139 | +--- error_log |
| 140 | +hmac-auth failed to authenticate the request, code: 401. error: client request can't be validated: missing Authorization header |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | +=== TEST 6: hmac auth missing algorithm |
| 145 | +--- request |
| 146 | +GET /hello |
| 147 | +--- more_headers |
| 148 | +Authorization: Signature keyId="my-access-key",headers="@request-target date" ,signature="asdf" |
| 149 | +Date: Thu, 24 Sep 2020 06:39:52 GMT |
| 150 | +--- error_code: 401 |
| 151 | +--- response_body |
| 152 | +{"message":"Authorization Failed"} |
| 153 | +--- error_log |
| 154 | +hmac-auth failed to authenticate the request, code: 401. error: client request can't be validated: algorithm missing |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | +=== TEST 7: add consumer with username and jwt-auth plugins |
| 159 | +--- config |
| 160 | + location /t { |
| 161 | + content_by_lua_block { |
| 162 | + local t = require("lib.test_admin").test |
| 163 | + local code, body = t('/apisix/admin/consumers', |
| 164 | + ngx.HTTP_PUT, |
| 165 | + [[{ |
| 166 | + "username": "jack", |
| 167 | + "plugins": { |
| 168 | + "jwt-auth": { |
| 169 | + "key": "user-key", |
| 170 | + "secret": "my-secret-key" |
| 171 | + } |
| 172 | + } |
| 173 | + }]] |
| 174 | + ) |
| 175 | + |
| 176 | + if code >= 300 then |
| 177 | + ngx.status = code |
| 178 | + end |
| 179 | + ngx.say(body) |
| 180 | + } |
| 181 | + } |
| 182 | +--- request |
| 183 | +GET /t |
| 184 | +--- response_body |
| 185 | +passed |
| 186 | + |
| 187 | + |
| 188 | + |
| 189 | +=== TEST 8: test with expired jwt token |
| 190 | +--- request |
| 191 | +GET /hello |
| 192 | +--- error_code: 401 |
| 193 | +--- response_body |
| 194 | +{"message":"Authorization Failed"} |
| 195 | +--- error_log |
| 196 | +jwt-auth failed to authenticate the request, code: 401. error: failed to verify jwt: 'exp' claim expired at Tue, 23 Jul 2019 08:28:21 GMT |
| 197 | +--- more_headers |
| 198 | +Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJrZXkiOiJ1c2VyLWtleSIsImV4cCI6MTU2Mzg3MDUwMX0.pPNVvh-TQsdDzorRwa-uuiLYiEBODscp9wv0cwD6c68 |
| 199 | + |
| 200 | + |
| 201 | + |
| 202 | +=== TEST 9: test with jwt token containing wrong signature |
| 203 | +--- request |
| 204 | +GET /hello |
| 205 | +--- error_code: 401 |
| 206 | +--- response_body |
| 207 | +{"message":"Authorization Failed"} |
| 208 | +--- error_log |
| 209 | +jwt-auth failed to authenticate the request, code: 401. error: failed to verify jwt: signature mismatch: fNtFJnNnJgzbiYmGB0Yjvm-l6A6M4jRV1l4mnVFSYjs |
| 210 | +--- more_headers |
| 211 | +Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJrZXkiOiJ1c2VyLWtleSIsImV4cCI6MTg3OTMxODU0MX0.fNtFJnNnJgzbiYmGB0Yjvm-l6A6M4jRV1l4mnVFSYjs |
| 212 | + |
| 213 | + |
| 214 | + |
| 215 | +=== TEST 10: verify jwt-auth |
| 216 | +--- request |
| 217 | +GET /hello |
| 218 | +--- more_headers |
| 219 | +Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJrZXkiOiJ1c2VyLWtleSIsImV4cCI6MTg3OTMxODU0MX0.fNtFJnNmJgzbiYmGB0Yjvm-l6A6M4jRV1l4mnVFSYjs |
| 220 | +--- response_body |
| 221 | +hello world |
| 222 | +--- no_error_log |
| 223 | +failed to authenticate the request |
0 commit comments