Skip to content

Configure the key auth plugin for a Service with Kong

Pradeeban Kathiravelu edited this page Aug 1, 2018 · 1 revision

Configure the key-auth plugin for the Service with Kong

$ curl -i -X POST --url http://localhost:8001/services/find-service/plugins/ --data 'name=key-auth'

HTTP/1.1 201 Created Date: Tue, 31 Jul 2018 20:02:56 GMT Content-Type: application/json; charset=utf-8 Connection: keep-alive Access-Control-Allow-Origin: * Server: kong/0.14.0 Content-Length: 276

{"created_at":1533067377000,"config":{"key_in_body":false,"run_on_preflight":true,"anonymous":"","hide_credentials":false,"key_names":["apikey"]},"id":"13e71371-013a-4d9f-af96-17bd3a55537b","enabled":true,"service_id":"965c3b89-cba6-4de4-b8af-dc81f8a9c05d","name":"key-auth"}

Verify that the plugin is properly configured

$ curl -i -X GET --url http://localhost:8000/ --header 'Host: find-service.com'

HTTP/1.1 401 Unauthorized Date: Tue, 31 Jul 2018 20:03:46 GMT Content-Type: application/json; charset=utf-8 Connection: keep-alive WWW-Authenticate: Key realm="kong" Server: kong/0.14.0 Content-Length: 41

{"message":"No API key found in request"}

As you can see, now the call to the API fails when no apikey was given.

Create a Consumer through the RESTful API

$ curl -i -X POST --url http://localhost:8001/consumers/ --data "username=Jason"

HTTP/1.1 201 Created Date: Tue, 31 Jul 2018 20:04:51 GMT Content-Type: application/json; charset=utf-8 Connection: keep-alive Access-Control-Allow-Origin: * Server: kong/0.14.0 Content-Length: 106

{"custom_id":null,"created_at":1533067491,"username":"Jason","id":"2e4f255f-8430-4acd-af3c-34657ca6de28"}

Provision key credentials for your Consumer

$ curl -i -X POST --url http://localhost:8001/consumers/Jason/key-auth/ --data 'key=ThisIsMyNEWWWKeyeee'

HTTP/1.1 201 Created Date: Tue, 31 Jul 2018 20:05:59 GMT Content-Type: application/json; charset=utf-8 Connection: keep-alive Access-Control-Allow-Origin: * Server: kong/0.14.0 Content-Length: 154

{"id":"37297873-3ff4-4ea3-89b6-8853845c366a","created_at":1533067560000,"key":"ThisIsMyNEWWWKeyeee","consumer_id":"2e4f255f-8430-4acd-af3c-34657ca6de28"}

Verify that your Consumer credentials are valid

$ curl -i -X GET --url http://localhost:8000 --header "Host: find-service.com" --header "apikey: ThisIsMyNEWWWKeyeee"

HTTP/1.1 200 OK Content-Type: application/json Transfer-Encoding: chunked Connection: keep-alive Access-Control-Allow-Origin: * Bindaas-version: 3.0.6 Date: Tue, 31 Jul 2018 20:11:25 GMT metadata: {} responseTime: 0 tags: [] Vendor: CCI Emory University Server: Jetty(8.1.7.v20120910) X-Kong-Upstream-Latency: 5 X-Kong-Proxy-Latency: 35 Via: kong/0.14.0

[{ "item" : "bulk" , "qty" : 1100.0},{ "item" : "bulk" , "qty" : 1100.0},{ "item" : "bulk" , "qty" : 1100.0 , "nu" : 1.0}]

Clone this wiki locally