Skip to content

TYK gRPC auth plugin that verifies access token validity in mongo

License

Notifications You must be signed in to change notification settings

armujahid/tyk-grpc-go-mongo-accesstoken

Repository files navigation

How to start gRPC server:

cp .env.example .env

modify .env

docker-compose up --build

Tyk configurations:

  1. tyk.conf:
"coprocess_options": {
  "enable_coprocess": true,
  "coprocess_grpc_server": "tcp://host.docker.internal:9111"
},

ensure that tyk can reach the gRPC server

  1. Use apidef.json in tyk

Example DB config

Create a collection in mongo with this object (I have tested this with Loopback 3 AccessToken model which already has compatible schema)

{
  "_id":"asdfasdfasdf32dsafdasdfas",
  "ttl":1209600,
  "created":"2022-08-30T09:20:20.910+00:00"
}

Testing:

curl -s 'http://localhost:8082/mongo-auth/get' -H "Authorization: asdfasdfasdf32dsafdasdfas"

Output if token is valid:

{
  "args": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip", 
    "Authorization": "asdfasdfasdf32dsafdasdfas", 
    "Host": "httpbin.org", 
    "User-Agent": "curl/7.81.0", 
    "X-Amzn-Trace-Id": "Root=1-6315b555-4a26f3f923cde5aa3f596b6f"
  }, 
  "origin": "172.28.0.1, <external ip>", 
  "url": "http://httpbin.org/get"
}

Output if token is invalid:

{
    "error": "Access forbidden"
}