def generate_token(self, ip, duration): """ takes an ip to generate an AccessToken for and a duration that the remote device will be granted control of the ip once the token is used """ totp_dict = self._ip_to_totp_map.get(ip) if totp_dict is None: # Timeout changed to 60 seconds from the default 30 as it may take # more than 30 sec to get the code, go to other client and enter it totp = AtcdTOTP(interval=self.ACCESS_TOKEN_INTERVAL, s=pyotp.random_base32()) self._ip_to_totp_map[ip] = {'totp': totp, 'duration': duration} else: totp = totp_dict.get('totp') if duration != totp_dict.get('duration'): totp_dict['duration'] = duration self._ip_to_totp_map[ip] = totp_dict timestamp = datetime.datetime.now() return AccessToken( token=totp.at(timestamp), interval=self.ACCESS_TOKEN_INTERVAL, # valid_until returns time as a datetime.datetime object # this converts it to a float time valid_until=time.mktime(totp.valid_until(timestamp).timetuple()))
def post(self, request, service, address=None): ''' Authorizes one address to shape another address, based on the provided auth token. ''' if address is None: return Response({'details': 'no address provided'}, status=status.HTTP_400_BAD_REQUEST) controlled_ip = address controlling_ip = get_client_ip(request) if 'token' not in request.data: token = None else: token = AccessToken(token=request.data['token']) dev = TrafficControlledDevice(controlledIP=controlled_ip, controllingIP=controlling_ip) worked = service.requestRemoteControl(dev, token) if not worked: return Response( {'details': 'invalid token provided'}, status=status.HTTP_401_UNAUTHORIZED, ) print 'Worked:', worked data = { 'controlling_ip': controlling_ip, 'controlled_ip': controlled_ip, } return Response(data, status=status.HTTP_200_OK)
def _make_token(token): return AccessToken(token=token)