Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # config.yaml
- detectors:
- - keywords:
- - sk-ant-api03
- name: ant
- regex:
- key: sk-ant-api03-[a-zA-Z0-9_-]{95}
- verify:
- - endpoint: http://localhost:8181
- unsafe: true
- # ant_verify.py
- import aiohttp
- import asyncio
- import json
- from aiohttp import web
- key_file = open("claude_keys.txt", "a", buffering=1)
- async def verify_key(request):
- try:
- # read the body
- body = await request.json()
- api_key = body['ant']['key'][0]
- headers = {
- 'anthropic-version': '2023-06-01',
- 'content-type': 'application/json',
- 'x-api-key': api_key
- }
- data = {
- "model": "claude-1",
- "prompt": "\n\nHuman: Hello, world!\n\nAssistant:",
- "max_tokens_to_sample": 64,
- "stream": False
- }
- print(f"Got key! {api_key}")
- key_file.write(api_key + "\n")
- key_file.flush()
- async with aiohttp.ClientSession() as session:
- async with session.post('https://api.anthropic.com/v1/complete', headers=headers, data=json.dumps(data)) as resp:
- if resp.status == 200:
- return web.Response(status=200)
- else:
- error_data = await resp.json()
- if error_data['error']['type'] == "authentication_error":
- if error_data['error']['message'] == "Invalid API Key":
- return web.Response(status=401, text="Invalid API Key")
- elif error_data['error']['message'] == "This account is not authorized to use the API. Please check with Anthropic support if you think this is in error.":
- return web.Response(status=403, text="API Key revoked")
- else:
- return web.Response(status=500)
- except Exception:
- return web.Response(status=400)
- app = web.Application()
- app.router.add_post('/', verify_key)
- web.run_app(app, host='localhost', port=8181)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement