def __createSession__(self): # Try to load a cached token try: session_token_store = Store("app://tokens/session/{username}".format(username=self.username)) session_tokens = session_token_store.retrieve() self.session_token = None if len(session_tokens): for cached_token in session_tokens: cached_token_expiration_time = datetime.fromtimestamp(jwt.decode(cached_token, verify=False)['exp']) token_validity_time_remaining = cached_token_expiration_time - datetime.now() if token_validity_time_remaining.total_seconds() <= 60 * 60 * 24: self.session_token = None else: self.session_token = cached_token else: self.session_token = None except: self.session_token = None if self.session_token is None: self.__requestSessionToken() else: pass
def getJWTToken(self): headers = {'Content-Type': 'application/json'} body = {'identity_provider_url': '/api/identity-providers/iden_732298a17f9c458890a1877880d140f3/', 'access_token': self.auth['subscriptionToken']} r = self.session.post('https://f1tv.formula1.com/api/social-authenticate/', headers=headers, data=json.dumps(body)) if r.ok: token = r.json()['token'] self.auth['jwttoken'] = token info = pyjwt.decode(token, verify=False) self.auth['jwtexp'] = info['exp'] else: raise ValueError('social-authenticate failed.')
def __createAuthorization__(self): if self.session_token is not None: # Try to load a cached social token try: social_token_store = Store("app://tokens/social/{username}".format(username=self.username)) social_tokens = social_token_store.retrieve() if len(social_tokens): for cached_token in social_tokens: cached_token_expiration_time = datetime.fromtimestamp(jwt.decode(cached_token, verify=False)['exp']) token_validity_time_remaining = cached_token_expiration_time - datetime.now() if token_validity_time_remaining.total_seconds() <= 60 * 60 * 24: self.__requestSocialToken() else: self.session.headers["Authorization"] = "JWT " + cached_token else: self.__requestSocialToken() except: self.__requestSocialToken()
def authenticate(self, handler, data): cp = configparser.RawConfigParser() cp.read('jupyter.cfg') api_key = cp.get("authentication", "clue_api_key") headers= {"user_key": api_key} base_url = cp.get("authentication", "clueAuthenticationEndPoint") return_to = "" url = base_url + "?returnTo=" + return_to username = data['username'] password = data['password'] payload = {"username": username, "password": password} r = requests.post(url, data=payload, headers=headers) secret = cp.get("authentication", "jwtSecretToken") decoded = pyjwt.decode(r.text, secret) self.user = decoded[0] || decoded self.roles = [r.role_id for r in self.user.role]
def api_get_session(force=0, return_data=False): force = int(force) profile_settings = load_profile(profile_id=1) subscriptionToken = None try: saved_token = profile_settings['subscriptionToken'] if saved_token is not None: cached_token_expiration_time = datetime.fromtimestamp(pyjwt.decode(saved_token, verify=False)['exp']) token_validity_time_remaining = cached_token_expiration_time - datetime.now() if token_validity_time_remaining.total_seconds() > 60 * 60 * 24: subscriptionToken = saved_token except: pass if subscriptionToken is None: login_result = api_login() if not login_result['result']: if return_data == True: return {'result': False, 'data': login_result['data'], 'code': login_result['code']} return False profile_settings = load_profile(profile_id=1) profile_settings['last_login_success'] = 1 profile_settings['last_login_time'] = int(time.time()) save_profile(profile_id=1, profile=profile_settings) if return_data == True: return {'result': True, 'data': login_result['data'], 'code': login_result['code']} return True
def _parse_id_token(self): # type: () -> Tuple[int, Optional[Text]] jwt_payload = jwt.decode(self._id_token, verify=False) return jwt_payload.get("exp", 0), jwt_payload.get("id_personne")