def _get_id_and_check_tokens(uname,token_set): """get _id if token_set in user tokens else None """ user = models.User.objects.get(username=uname) if user is not None: tokens = getUserTokens(user) logger.debug( "getid and check tokens %s" % (str(tokens),) ) if (len(tokens.intersection(token_set)) > 0): logger.debug("user with correct token") return user.id else: logger.debug("feiled to check token in set") return None else: logger.debug("failed to get id") return None
def userTicket(self, ticket64,cip=None): """ usertTicket verify given ticket , if it valid and user exist in database return user instance. Arguments: ticket64 (string) : base 64 ticket.\n Return: User (User instance): Django User instance.\n """ ticket = binascii.a2b_base64(ticket64) ticketObj = settings.TICKET #user_data = validateTicket(ticket, settings.SECRET_KEY, mod_auth_pubtkt=settings.MOD_AUTH_PUBTKT, signType=settings.MOD_AUTH_PUBTKT_SIGNTYPE) #if isinstance(ticketObj,SignedTicket): user_data = ticketObj.validateTkt(ticket) #else: #user_data = ticketObj.validateTkt(ticket,cip) if user_data: user_key = ['nickname', 'fullname', 'email', 'language', 'country', 'postcode'] user_value = user_data[2] self.user_dict = {} for i in range(0, len(user_key)): try: self.user_dict[user_key[i]] = user_value[i] except IndexError: continue user = None if self.create_unknown_user: #TODO here we can create a pipeline #Old version with long ticket #user, created = User.objects.get_or_create(username = user_data[1] ,email = self.user_dict['email']) #To reduce the length of ticket it's needed to waiver to the loging with ticket user = User.objects.get(username = user_data[0]) #We can create a user from a ticket #if user: # user = self.configure_user(user) else: try: user = User.objects.get(username=user_data[0]) except User.DoesNotExist: pass tokens = getUserTokens(user) validuntil = settings.TICKET_TIMEOUT + int(time.time()) ticketObj = settings.TICKET new_tkt = ticketObj.createTkt( user.username, tokens=tokens, user_data=user_value, #user_data=[], #cip = cip, validuntil=validuntil, ) #new_tkt = createTicket( # self.user_dict['nickname'], # settings.SECRET_KEY, # ip = cip, # tokens=tokens, # user_data=user_value, # validuntil= validuntil, # mod_auth_pubtkt=settings.MOD_AUTH_PUBTKT, # signType=settings.MOD_AUTH_PUBTKT_SIGNTYPE #) tkt64 = binascii.b2a_base64(new_tkt).rstrip() return user, tkt64