credential-plugin-jwt-auth.t 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with 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,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. use t::APISIX 'no_plan';
  18. repeat_each(1);
  19. no_long_string();
  20. no_root_location();
  21. run_tests;
  22. __DATA__
  23. === TEST 1: enable jwt-auth on the route /hello
  24. --- config
  25. location /t {
  26. content_by_lua_block {
  27. local t = require("lib.test_admin").test
  28. local code, body = t('/apisix/admin/routes/1',
  29. ngx.HTTP_PUT,
  30. [[{
  31. "plugins": {
  32. "jwt-auth": {}
  33. },
  34. "upstream": {
  35. "nodes": {
  36. "127.0.0.1:1980": 1
  37. },
  38. "type": "roundrobin"
  39. },
  40. "uri": "/hello"
  41. }]]
  42. )
  43. if code >= 300 then
  44. ngx.status = code
  45. end
  46. ngx.say(body)
  47. }
  48. }
  49. --- request
  50. GET /t
  51. --- response_body
  52. passed
  53. === TEST 2: create a consumer
  54. --- config
  55. location /t {
  56. content_by_lua_block {
  57. local t = require("lib.test_admin").test
  58. local code, body = t('/apisix/admin/consumers',
  59. ngx.HTTP_PUT,
  60. [[{
  61. "username": "jack"
  62. }]]
  63. )
  64. if code >= 300 then
  65. ngx.status = code
  66. end
  67. ngx.say(body)
  68. }
  69. }
  70. --- request
  71. GET /t
  72. --- response_body
  73. passed
  74. === TEST 3: create a credential with jwt-auth plugin enabled for the consumer
  75. --- config
  76. location /t {
  77. content_by_lua_block {
  78. local t = require("lib.test_admin").test
  79. local code, body = t('/apisix/admin/consumers/jack/credentials/34010989-ce4e-4d61-9493-b54cca8edb31',
  80. ngx.HTTP_PUT,
  81. [[{
  82. "plugins": {
  83. "jwt-auth": {"key": "user-key", "secret": "my-secret-key"}
  84. }
  85. }]],
  86. [[{
  87. "value":{
  88. "id":"34010989-ce4e-4d61-9493-b54cca8edb31",
  89. "plugins":{
  90. "jwt-auth": {"key": "user-key", "secret": "kK0lkbzXrE7aiTiyK/Z0Sw=="}
  91. }
  92. },
  93. "key":"/apisix/consumers/jack/credentials/34010989-ce4e-4d61-9493-b54cca8edb31"
  94. }]]
  95. )
  96. ngx.status = code
  97. ngx.say(body)
  98. }
  99. }
  100. --- request
  101. GET /t
  102. --- response_body
  103. passed
  104. === TEST 4: access with invalid JWT token
  105. --- request
  106. GET /hello
  107. --- more_headers
  108. Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJrZXkiOiJqd3QtdmF1bHQta2V5IiwiZXhwIjoxNjk1MTM4NjM1fQ.Au2liSZ8eQXUJR3SJESwNlIfqZdNyRyxIJK03L4dk_g
  109. --- error_code: 401
  110. --- response_body
  111. {"message":"Invalid user key in JWT token"}
  112. === TEST 5: access with valid JWT token in header
  113. --- request
  114. GET /hello
  115. --- more_headers
  116. Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJrZXkiOiJ1c2VyLWtleSIsImV4cCI6MTg3OTMxODU0MX0.fNtFJnNmJgzbiYmGB0Yjvm-l6A6M4jRV1l4mnVFSYjs
  117. --- response_body
  118. hello world