Created
January 21, 2011 19:13
-
-
Save staer/790222 to your computer and use it in GitHub Desktop.
Custom Django-Piston authentication backend that can handle multiple types of auth methods
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
# Class that can help django-piston process multiple types of authentication | |
# Example usage: | |
# | |
# from piston.authentication import HttpBasicAuthentication | |
# from somewhere import OtherCustomAuthModule | |
# | |
# httpAuth = HttpBasicAuthentication(realm="CycleCloud") | |
# otherAuth = OtherCustomAuthModule() | |
# | |
# auth = MultiAuthentication([httpAuth, otherAuth]) | |
# Use auth as you would any other authentication object in django-piston | |
class MultiAuthentication(object): | |
""" Authenticated Django-Piston against multiple types of authentication """ | |
def __init__(self, auth_types): | |
""" Takes a list of authenication objects to try against, the default | |
authentication type to try is the first in the list. """ | |
self.auth_types = auth_types | |
self.selected_auth = auth_types[0] | |
def is_authenticated(self, request): | |
""" Try each authentication type in order and use the first that succeeds """ | |
authenticated = False | |
for auth in self.auth_types: | |
authenticated = auth.is_authenticated(request) | |
if authenticated: | |
selected_auth = auth | |
break | |
return authenticated | |
def challenge(self): | |
""" Return the challenge for whatever the selected auth type is (or the default | |
auth type which is the first in the list)""" | |
return self.selected_auth.challenge() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment