Skip to content

Instantly share code, notes, and snippets.

@ozten
Created May 7, 2013 15:40
Show Gist options
  • Save ozten/5533614 to your computer and use it in GitHub Desktop.
Save ozten/5533614 to your computer and use it in GitHub Desktop.
Early example of PyHawk Server APIs
import hawk
from hawk.util import HawkException
def credential_lookup(app_id):
# Config, filesystem, db or some other lookup
return credentials
server = hawk.Server(req, credential_lookup)
try:
# Authenticate the request
artifacts = server.authenticate()
payload = 'Hello ' + artifacts['ext']
status = '200 OK'
# Sign our response
auth = server.header(artifacts,
{ 'payload': payload,
'contentType': 'text/plain' })
headers = [('Content-Type', 'text/plain'),
('Server-Authorization', auth)]
# Send reponse
start_response(status, headers)
return payload
except (HawkException):
start_response('401 Unauthorized', [])
return 'Please authenticate'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment