This repository has been archived by the owner on Jun 19, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
49 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
appengine/credentials.py | ||
*.pyc | ||
.DS_Store | ||
docs/_build |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import rest | ||
import logging | ||
import base64 | ||
import credentials | ||
|
||
AUTHENTICATE_HEADER = "WWW-Authenticate" | ||
AUTHORIZATION_HEADER = "Authorization" | ||
AUTHENTICATE_TYPE = 'Basic realm="Secure Area"' | ||
CONTENT_TYPE_HEADER = "Content-Type" | ||
HTML_CONTENT_TYPE = "text/html" | ||
|
||
class BasicAuthenticator(rest.Authenticator): | ||
"""Example implementation of HTTP Basic Auth.""" | ||
|
||
def __init__(self): | ||
super(BasicAuthenticator, self).__init__() | ||
|
||
def authenticate(self, dispatcher): | ||
|
||
user_arg = None | ||
pass_arg = None | ||
try: | ||
# Parse the header to extract a user/password combo. | ||
# We're expecting something like "Basic XZxgZRTpbjpvcGVuIHYlc4FkZQ==" | ||
auth_header = dispatcher.request.headers[AUTHORIZATION_HEADER] | ||
|
||
# Isolate the encoded user/passwd and decode it | ||
auth_parts = auth_header.split(' ') | ||
user_pass_parts = base64.b64decode(auth_parts[1]).split(':') | ||
user_arg = user_pass_parts[0] | ||
pass_arg = user_pass_parts[1] | ||
|
||
except Exception: | ||
# set the headers requesting the browser to prompt for a user/password: | ||
dispatcher.response.set_status(401, message="Authentication Required") | ||
dispatcher.response.headers[AUTHENTICATE_HEADER] = AUTHENTICATE_TYPE | ||
dispatcher.response.headers[CONTENT_TYPE_HEADER] = HTML_CONTENT_TYPE | ||
|
||
dispatcher.response.out.write("<html><body>401 Authentication Required</body></html>") | ||
raise rest.DispatcherException() | ||
|
||
if user_arg==credentials.USER and pass_arg==credentials.PASS: | ||
return | ||
|
||
dispatcher.forbidden() |
This file contains 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