Skip to content

Instantly share code, notes, and snippets.

@touhami92
Created February 23, 2017 15:35
Show Gist options
  • Save touhami92/155164da9b6ca1cb14868aef37b01e77 to your computer and use it in GitHub Desktop.
Save touhami92/155164da9b6ca1cb14868aef37b01e77 to your computer and use it in GitHub Desktop.
from spyne.server.django import DjangoHttpMethodContext
def _get_user_from_http_header(ctx):
ctx_meta = ctx.transport.req.META if isinstance(ctx, DjangoHttpMethodContext) else ctx.META
if 'HTTP_AUTHORIZATION' not in ctx_meta:
raise MissingAuthenticationError()
auth_header = ctx_meta['HTTP_AUTHORIZATION']
try:
scheme, basic_creds = auth_header.split()
except ValueError:
raise AuthorizationError()
if scheme != 'Basic':
raise MissingAuthenticationError()
username, passwd = base64.b64decode(basic_creds).decode('utf-8').split(':')
user = auth.authenticate(username=username, password=passwd)
if not user:
raise AuthenticationError(username)
return user
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment