Skip to content

Instantly share code, notes, and snippets.

@matiasinsaurralde
Created July 19, 2019 07:44
Show Gist options
  • Save matiasinsaurralde/dc1f1dc6872335b7486c8012c221f4f5 to your computer and use it in GitHub Desktop.
Save matiasinsaurralde/dc1f1dc6872335b7486c8012c221f4f5 to your computer and use it in GitHub Desktop.
app_sample.json
{
"name": "Tyk Test API",
"api_id": "1",
"org_id": "default",
"definition": {
"location": "header",
"key": "version"
},
"use_keyless": false,
"auth": {
"auth_header_name": "Authorization"
},
"version_data": {
"not_versioned": true,
"versions": {
"Default": {
"name": "Default",
"expires": "3000-01-02 15:04",
"use_extended_paths": true,
"extended_paths": {
"ignored": [],
"white_list": [],
"black_list": []
}
}
}
},
"proxy": {
"listen_path": "/tyk-api-test/",
"target_url": "http://httpbin.org",
"strip_listen_path": true
},
"custom_middleware_bundle": "bundle.zip",
"enable_coprocess_auth": true,
"enable_batch_request_support": true
}
{
"file_list": [
"middleware.py"
],
"custom_middleware": {
"pre": null,
"post": null,
"post_key_auth": null,
"auth_check": {
"name": "MyAuthMiddleware"
},
"driver": "python",
"id_extractor": {
"extract_from": "header",
"extract_with": "value",
"extractor_config": {
"header_name": "Authorization"
}
}
},
"checksum": "a4d36446706297ca18b793ab7d84f6bf",
"signature": ""
}
import time
from tyk.decorators import *
from gateway import TykGateway as tyk
@Hook
def MyAuthMiddleware(request, session, metadata, spec):
print("Auth hook")
auth_header = request.get_header('Authorization')
if auth_header == 'b60d121b438a380c343d5ec3c2037564b82ffef3':
tyk.log("Successful authentication!", "info")
session.rate = 2.0
session.per = 1.0
expiry_time = int(time.time()) + (24 * 60 * 60)
session.id_extractor_deadline = expiry_time
metadata["token"] = auth_header
return request, session, metadata
tyk.log("Bad authentication!", "info")
return request, session, metadata
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment