<s> from openid . store . interface import OpenIDStore <EOL> from openid . association import Association as OIDAssociation <EOL> from openid . extensions import sreg <EOL> import openid . store <EOL> from django . db . models . query import Q <EOL> from django . conf import settings <EOL> from django . http import str_to_unicode <EOL> try : <EOL> from openid . yadis import xri <EOL> except : <EOL> from yadis import xri <EOL> import time , base64 , md5 , operator <EOL> import urllib <EOL> from models import Association , Nonce <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> DEFAULT_NEXT = getattr ( settings , '<STR_LIT>' , '<STR_LIT:/>' ) <EOL> def clean_next ( next ) : <EOL> if next is None : <EOL> return DEFAULT_NEXT <EOL> next = str_to_unicode ( urllib . unquote ( next ) , '<STR_LIT:utf-8>' ) <EOL> next = next . strip ( ) <EOL> if next . startswith ( '<STR_LIT:/>' ) : <EOL> return next <EOL> return DEFAULT_NEXT <EOL> class OpenID : <EOL> def __init__ ( self , openid_ , issued , attrs = None , sreg_ = None ) : <EOL> self . openid = openid_ <EOL> self . issued = issued <EOL> self . attrs = attrs or { } <EOL> self . sreg = sreg_ or { } <EOL> self . is_iname = ( xri . identifierScheme ( openid_ ) == '<STR_LIT>' ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . openid <EOL> def __str__ ( self ) : <EOL> return self . openid <EOL> class DjangoOpenIDStore ( OpenIDStore ) : <EOL> def __init__ ( self ) : <EOL> self . max_nonce_age = <NUM_LIT:6> * <NUM_LIT> * <NUM_LIT> <EOL> def storeAssociation ( self , server_url , association ) : <EOL> assoc = Association ( <EOL> server_url = server_url , <EOL> handle = association . handle , <EOL> secret = base64 . encodestring ( association . secret ) , <EOL> issued = association . issued , <EOL> lifetime = association . issued , <EOL> assoc_type = association . assoc_type <EOL> ) <EOL> assoc . save ( ) <EOL> def getAssociation ( self , server_url , handle = None ) : <EOL> assocs = [ ] <EOL> if handle is not None : <EOL> assocs = Association . objects . filter ( <EOL> server_url = server_url , handle = handle <EOL> ) <EOL> else : <EOL> assocs = Association . objects . filter ( <EOL> server_url = server_url <EOL> ) <EOL> if not assocs : <EOL> return None <EOL> associations = [ ] <EOL> for assoc in assocs : <EOL> association = OIDAssociation ( <EOL> assoc . handle , base64 . decodestring ( assoc . secret ) , assoc . issued , <EOL> assoc . lifetime , assoc . assoc_type <EOL> ) <EOL> if association . getExpiresIn ( ) == <NUM_LIT:0> : <EOL> self . removeAssociation ( server_url , assoc . handle ) <EOL> else : <EOL> associations . append ( ( association . issued , association ) ) <EOL> if not associations : <EOL> return None <EOL> return associations [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] <EOL> def removeAssociation ( self , server_url , handle ) : <EOL> assocs = list ( Association . objects . filter ( <EOL> server_url = server_url , handle = handle <EOL> ) ) <EOL> assocs_exist = len ( assocs ) > <NUM_LIT:0> <EOL> for assoc in assocs : <EOL> assoc . delete ( ) <EOL> return assocs_exist <EOL> def useNonce ( self , server_url , timestamp , salt ) : <EOL> if abs ( timestamp - time . time ( ) ) > openid . store . nonce . SKEW : <EOL> return False <EOL> query = [ <EOL> Q ( server_url__exact = server_url ) , <EOL> Q ( timestamp__exact = timestamp ) , <EOL> Q ( salt__exact = salt ) , <EOL> ] <EOL> try : <EOL> ononce = Nonce . objects . get ( reduce ( operator . and_ , query ) ) <EOL> except Nonce . DoesNotExist : <EOL> ononce = Nonce ( <EOL> server_url = server_url , <EOL> timestamp = timestamp , <EOL> salt = salt <EOL> ) <EOL> ononce . save ( ) <EOL> return True <EOL> ononce . delete ( ) <EOL> return False <EOL> def cleanupNonce ( self ) : <EOL> Nonce . objects . filter ( timestamp < int ( time . time ( ) ) - nonce . SKEW ) . delete ( ) <EOL> def cleanupAssociations ( self ) : <EOL> Association . objects . extra ( where = [ '<STR_LIT>' % time . time ( ) ] ) . delete ( ) <EOL> def getAuthKey ( self ) : <EOL> return md5 . new ( settings . SECRET_KEY ) . hexdigest ( ) [ : self . AUTH_KEY_LEN ] <EOL> def isDumb ( self ) : <EOL> return False <EOL> def from_openid_response ( openid_response ) : <EOL> """<STR_LIT>""" <EOL> issued = int ( time . time ( ) ) <EOL> sreg_resp = sreg . SRegResponse . fromSuccessResponse ( openid_response ) or [ ] <EOL> return OpenID ( <EOL> openid_response . identity_url , issued , openid_response . signed_fields , <EOL> dict ( sreg_resp ) <EOL> ) </s>
