def refreshAuthToken(self, forceRereshToken = False): """ Response: { "auth_token": "a8f5f167f44f4964e6c998dee827110c", "refresh_token": "964e6c998dee827110ca8f5f167f44f4", "expires_at": "1401698962", } @return: False if failed, json structure if success """ try: TOKEN_REFRESH_LOCK.acquire() if HttpBasicClient.UserRefreshToken is None: self.logger.warning('[HTTP] refresh token stored in cache is invalidate') return HttpResponseResult.RefreshTokenIgnore if type(HttpBasicClient.ExpireDate) == int: self.logger.warning('[HTTP] error of expire date: %d', HttpBasicClient.ExpireDate) currentTime = self.getServerCurrentTime() if HttpBasicClient.ExpireDate is not None and currentTime < HttpBasicClient.ExpireDate - datetime.timedelta(seconds=60) and not forceRereshToken: self.logger.debug('[HTTP] do not need to refresh token because other thread have finished it, the expire date is %s', HttpBasicClient.ExpireDate) return HttpResponseResult.RefreshTokenIgnore postBody = {'refresh_token': HttpBasicClient.UserRefreshToken} response = self.post(FangCloudUrl.URL['RERRESH_AUTH_TOKEN'], jsonData=postBody, logData=True) if response != False: HttpBasicClient.UserAuthToken = response['auth_token'] HttpBasicClient.UserRefreshToken = response['refresh_token'] HttpBasicClient.ExpireDate = datetime.datetime.fromtimestamp(int(response['expires_at'])) Utils.storeAuthToken(HttpBasicClient.UserAuthToken) UserDatabaseManager().refreshAuthToken(response['auth_token'], response['refresh_token'], response['expires_at']) else: self.logger.info('[HTTP] Refresh user token failed') self.callbackHandler.handleHttpResponse(response) return response except UnAuthorizedException as exception: self.logger.error(exception, exc_info=1) return HttpResponseResult.Failed except ConnectionError: return HttpResponseResult.Failed finally: TOKEN_REFRESH_LOCK.release()
def login(self, username, password): """ @return: return false if exception or failed. return the following user structure if success. { "auth_token": "a8f5f167f44f4964e6c998dee827110c", "refresh_token": "964e6c998dee827110ca8f5f167f44f4", "auth_success": true, "expires_at": "1401698962", "user": { "type": "user", "id": "12312311", "name": "Yuan Cheng", "login": "******", "phone": "123123", "space_total": "123123", "space_used": "1123123", "name_first_letter": "a", "profile_pic_key" : "ksui667f44f4964e6c998dee8245s61", }, } """ try: urlQuery = {'api_key': API_KEY} postBody = {'login': username, 'password': password} response = self.post(FangCloudUrl.URL['USER_LOGIN'], query=urlQuery, jsonData=postBody, logData=False) if response != False: HttpBasicClient.UserAuthToken = response['auth_token'] HttpBasicClient.UserRefreshToken = response['refresh_token'] HttpBasicClient.ExpireDate = datetime.datetime.fromtimestamp(int(response['expires_at'])) Utils.storeAuthToken(HttpBasicClient.UserAuthToken) else: self.logger.info('[HTTP] User login failed!') self.callbackHandler.handleHttpResponse(response) return response except UnAuthorizedException as exception: self.logger.error(exception, exc_info=1) return HttpResponseResult.Failed except ConnectionError: return HttpResponseResult.Failed