Created
May 7, 2013 15:40
-
-
Save ozten/5533614 to your computer and use it in GitHub Desktop.
Early example of PyHawk Server APIs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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