def test_init_auth_sa_token(mocker): """Test the function init_auth_sa_token.""" mocker.return_value = mock_response("true") out = Authentication.init_auth_sa_token() assert out == "f98d1366-738e-4c14-a3ff-594f359e131c" mocker.return_value = mock_response("false") out = Authentication.init_auth_sa_token() assert out == "f98d1366-738e-4c14-a3ff-594f359e131c"
def get_auth_header(): """ Get the Authentication object from the AUTHORIZATION header replacing the new line chars. """ auth_data = bottle.request.headers['AUTHORIZATION'].replace( AUTH_NEW_LINE_SEPARATOR, "\n") auth_data = auth_data.split(AUTH_LINE_SEPARATOR) return Authentication(Authentication.read_auth_data(auth_data))
def add_follower(): if 'user_id' in session: if request.form['follower']: profile = Authentication.get_token(session['user_id']) following = profile['following'] following.append(request.form['follower']) Authentication.add_to_following(following) else: return render_template('notloggedin.html')
async def change_aroio_password(form: AroioPasswordForm, aroio: Aroio = Depends(get_auth_aroio)): """`new_password` replaces password for Aroio. `old_password` is required for authentication.""" auth = Authentication.verify_password(hashed=aroio.password, plain=form.old_password) if not auth: raise UnauthorizedException(detail="Wrong password") aroio.password = Authentication.hash_password(password=form.new_password) datasource.save(aroio=aroio) return create_access_token(data={"sub": aroio.dict()})
class Subreddit(object): def __init__(self): self._reddit_client = Authentication().GetRedditClient() def getSubreddit(self, subreddit_name): return self._reddit_client.subreddit(subreddit_name) def getHotTenSubreddit(self, subreddit_name): return self._reddit_client.subreddit(subreddit_name).hot(limit=10) def getTopTenSubreddit(self, subreddit_name): return self._reddit_client.subreddit(subreddit_name).top(limit=10)
def get_auth_header(): """ Get the Authentication object from the AUTHORIZATION header replacing the new line chars. """ auth_header = bottle.request.headers['AUTHORIZATION'] if Config.SINGLE_SITE: if auth_header.startswith("Basic "): auth_data = base64.b64decode(auth_header[6:]) user_pass = auth_data.split(":") im_auth = {"type": "InfrastructureManager", "username": user_pass[0], "password": user_pass[1]} single_site_auth = {"type": Config.SINGLE_SITE_TYPE, "host": Config.SINGLE_SITE_AUTH_HOST, "username": user_pass[0], "password": user_pass[1]} return Authentication([im_auth, single_site_auth]) elif auth_header.startswith("Bearer "): token = auth_header[7:] im_auth = {"type": "InfrastructureManager", "username": "******", "token": token} single_site_auth = {"type": Config.SINGLE_SITE_TYPE, "host": Config.SINGLE_SITE_AUTH_HOST, "token": token} return Authentication([im_auth, single_site_auth]) auth_data = auth_header.replace(AUTH_NEW_LINE_SEPARATOR, "\n") auth_data = auth_data.split(AUTH_LINE_SEPARATOR) return Authentication(Authentication.read_auth_data(auth_data))
def main(): # получим логин, пароль из файла auth.ini auth = configparser.ConfigParser() auth.optionxform = str auth.read('auth.ini') settings = configparser.ConfigParser() settings.optionxform = str settings.read('settings.ini') # создадим объект аторизации a = Authentication(auth['Authorization']['Login'], auth['Authorization']['Password'], dict(settings['Browser Headers'])) print(a.uid) # создадим объект коллекции # c = Collection(a) # print(c.req_id, c.csrf_token, sep='\n') # new_coll = c.create('new-coll1', 'description new-coll1') # создадим карточку-ссылку link = 'https://zip-sm.ru/product/mufta-motora-kuhonnogo-kombajna-kenwood' collection_url = 'https://yandex.ru/collections/user/company%40zip/mekhanika-dlia-blenderov-zapchasti-dlia-miasorubok-i-blenderov/' l = Link(a, link, collection_url) print(l.link, l.collection_url, l.collection_id, sep='\n')
class Aroio(BaseModel): name: str = "aroio" password: str = Authentication.hash_password("abacus") # default password authentication_enabled: bool = True timestamp: float = datetime.datetime.now().timestamp() description: str = "This is a raw Aroio Configuration without any device specifications. ÜÄÖ" configuration: Configuration = Configuration()
def connect(self): try: self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) except socket.error: return (-1, "Could not create socket") try: self.socket.settimeout(10) self.socket.connect((self.ip_addr, self.port)) self.waiting = False self.auth = Authentication(self.shared_key, self, self.app, debug=True, is_server=False) self.bind() # Added because we need the send/recv threads running for authentication if (self.auth.mutualauth()): print "Server Authenticated!" Logger.log("Connected to Server", self.is_server) self.authenticated = True self.sessionkey = self.auth.get_sessionkey() self.clear_queues() return (0, "Connected to (%s, %i)" % (self.ip_addr, self.port)) else: print "Could not authenticate" self.authenticated = False self.broken_conn_callback() return (-1, "Authentication failed") except socket.error: self.authenticated = False self.broken_conn_callback() return (-1, "Could not connect to (%s, %i)" % (self.ip_addr, self.port)) return (-1, "Could not connect to (%s, %i)" % (self.ip_addr, self.port))
def run(self): self.socket.setblocking(0) self.server.authenticated = False while (self.keep_alive and self.server.authenticated == False): try: client_socket, addr = self.socket.accept() self.server.waiting = False self.auth = Authentication(self.shared_key, self.server, self.app, debug=True, is_server=True) self.server.bind(client_socket) self.app.debug_continue.disabled = False if (self.auth.mutualauth()): print "Client Authenticated!" self.server.authenticated = True self.server.auth = self.auth self.connected_callback(addr[0], addr[1]) self.server.clear_queues() else: print "Unable to authenticate" self.server.authenticated=False self.auth=False self.server.broken_conn_callback() except socket.error: pass if not self.keep_alive: self.socket.close()
def get_auth_header(): """ Get the Authentication object from the AUTHORIZATION header replacing the new line chars. """ auth_data = bottle.request.headers[ 'AUTHORIZATION'].replace(AUTH_NEW_LINE_SEPARATOR, "\n") auth_data = auth_data.split(AUTH_LINE_SEPARATOR) return Authentication(Authentication.read_auth_data(auth_data))
class Listener(threading.Thread): server_str = "SERVER" #TODO find better place for this def __init__(self, socket, shared_key, server, connected_callback, app): threading.Thread.__init__(self) self.keep_alive = True self.socket = socket self.shared_key = shared_key self.server = server self.connected_callback = connected_callback self.app = app self.auth = None def run(self): self.socket.setblocking(0) self.server.authenticated = False while (self.keep_alive and self.server.authenticated == False): try: client_socket, addr = self.socket.accept() self.server.waiting = False self.auth = Authentication(self.shared_key, self.server, self.app, debug=True, is_server=True) self.server.bind(client_socket) self.app.debug_continue.disabled = False if (self.auth.mutualauth()): print "Client Authenticated!" self.server.authenticated = True self.server.auth = self.auth self.connected_callback(addr[0], addr[1]) self.server.clear_queues() else: print "Unable to authenticate" self.server.authenticated=False self.auth=False self.server.broken_conn_callback() except socket.error: pass if not self.keep_alive: self.socket.close() def broken_conn(self): self.authenticated = False self.auth=None if self.server is not None: self.server.auth = None def close(self): self.keep_alive = False self.server.authenticated = False self.auth=None if self.server is not None: self.server.auth = None
def handle_login(): access_token = request.args['access_token'] b = StringIO.StringIO() #logging.error(str(type(access_token))) #logging.error(str(access_token)) access_token = access_token.encode( 'utf-8') #access code is unicode convert to utf-8 # verify that the access token belongs to us c = pycurl.Curl() c.setopt( pycurl.URL, "https://api.amazon.com/auth/o2/tokeninfo?access_token=" + urllib.quote_plus(access_token)) c.setopt(pycurl.SSL_VERIFYPEER, 1) c.setopt(pycurl.WRITEFUNCTION, b.write) c.perform() d = json.loads(b.getvalue()) if d['aud'] != 'amzn1.application-oa2-client.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx': # the access token does not belong to us raise BaseException("Invalid Token") # exchange the access token for user profile b = StringIO.StringIO() c = pycurl.Curl() c.setopt(pycurl.URL, "https://api.amazon.com/user/profile") c.setopt(pycurl.HTTPHEADER, ["Authorization: bearer " + access_token]) c.setopt(pycurl.SSL_VERIFYPEER, 1) c.setopt(pycurl.WRITEFUNCTION, b.write) c.perform() d = json.loads(b.getvalue()) #todo implement sessions #Session['user_id'] = d['user_id'] #return "%s %s %s"%(d['name'], d['email'], d['user_id']) Authentication.post_token(d['name'], d['email'], d['user_id']) session['email'] = d['email'] session['user_id'] = d['user_id'] t = Authentication.get_token(d['user_id']) return render_template("profile.html", follower=t['following'])
def generate_notification_payload(): """Generate the final payload.""" print("generate_notification_payload() started") final_payload = [] for data in FINAL_DATA: repo_data = FINAL_DATA[data] if repo_data['notify'] == 'true': tmp_json = {"data": { "attributes": { "custom": { "repo_url": "", "scanned_at": datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S'), "version_updates": [] }, "id": "", "type": "analytics.notify.version" }, "id": str(uuid4()), "type": "notifications" } } tmp_json['data']['attributes']['custom']['repo_url'] = data tmp_json['data']['attributes']['id'] = data tmp_json['data']['attributes']['custom']['version_updates'] \ = repo_data['version_updates'] final_payload.append(tmp_json) print("<---Repo Data--->") print(REPO_DATA) print("<---Package Data--->") print(PACKAGE_DATA) print("<---New Version Data--->") print(NEW_VERSION_DATA) print("<---Version Data--->") print(VERSION_DATA) print("<---Final Data--->") print(FINAL_DATA) print("<-------------Payload for Notification------------->") print(final_payload) try: auth_ = Authentication.init_auth_sa_token() print("<------------AUTH------------->") print(auth_) if auth_ is not None: notify_ = un.send_notification(final_payload, auth_) print("<------------NOTIFY------------>") print(notify_) except Exception as e: logger.info(str(e)) print(str(e)) sys.exit() print("generate_notification_payload() ended")
def login(self, username, password): ''' Login to server and receive session token. Based on user preferences, we will invalidate session token when needed. If for some reason the server does not respond (times out), we can login offline. However, we will be severely limited in what actions we can perform. We also need to make sure our own (stale) session token is dead. ''' auth_handler = Authentication(username, password) user = User(self.db.users, username) try: token = auth_handler.signin() self.user = user.set_token(token) except InvalidCredentials as e: print("Something wrong with username/password") raise except ServerTimeout: self.user = user.signin(password)
def load(info): urls = ['//dev-developer.bsvecosystem.net/sdk/api/BSVE.API.js'] events.trigger('minerva.additional_js_urls', urls) info['apiRoot'].bsve = Authentication() # Add an endpoint for bsve wms dataset info['apiRoot'].bsve_datasets_wms = bsve_wms.BsveWmsDataset() # Add test endpoints info['apiRoot'].test = TestEndpoint() events.bind('minerva.get_layer_info', 'bsve', get_layer_info)
def login(formData: OAuth2PasswordRequestForm = Depends()): """The login route to use in production""" db_aroio: Aroio = datasource.load_aroio() if db_aroio.authentication_enabled: auth_result = Authentication.authenticate( aroio_name=db_aroio.name, aroio_password=db_aroio.password, username=formData.username, password=formData.password) if not auth_result: raise UnauthorizedException( detail="Incorrect username or password", headers={"WWW-Authenticate": "Bearer"}) return create_access_token(data={"sub": db_aroio.dict()})
def update_aroio_setup(setup: AroioSetup, aroio: Aroio = Depends(get_auth_aroio)): """Changing the base Aroio setup with name, password, description and authentication_enabled. Returns new access token, that must be used for further use of this API. For authentication the current password must be given""" authorized = Authentication.verify_password(plain=setup.verify_password, hashed=aroio.password) if not authorized: raise UnauthorizedException( detail="Not authorized changing authorization parameters", headers={"WWW-Authenticate": "Bearer"}) aroio.name = setup.name aroio.description = setup.description aroio.authentication_enabled = setup.authentication_enabled datasource.save(aroio=aroio) return create_access_token(data={"sub": aroio.dict()})
def start(): config = Config("config.json") url1 = config.get_url1() url2 = config.get_url2() aws_auth_value1 = "" aws_auth_value2 = "" if ".amazonaws.com/" in url1: aws_auth = Authentication(es_host=urlparse(url1).netloc, region="eu-west-1", aws_type="es") aws_auth_value1 = aws_auth.get_auth(); if ".amazonaws.com/" in url2: aws_auth = Authentication(es_host=urlparse(url2).netloc, region="eu-west-1", aws_type="es") aws_auth_value2 = aws_auth.get_auth(); print("Comparing results from:\n%s\n%s\n" %(url1, url2)) search_terms = config.get_search_terms() process_data(config=config, terms=search_terms, aws_auth_value1=aws_auth_value1, aws_auth_value2=aws_auth_value2)
def _call_function(self): self._error_mesage = "Error Removing resources." (inf_id, vm_list, auth_data, context) = self.arguments return InfrastructureManager.InfrastructureManager.RemoveResource( inf_id, vm_list, Authentication(auth_data), context)
def _call_function(self): self._error_mesage = "Error Getting VM Property." (inf_id, vm_id, property_name, auth_data) = self.arguments return InfrastructureManager.InfrastructureManager.GetVMProperty( inf_id, vm_id, property_name, Authentication(auth_data))
def main(): # ----- load inputs ----- # with open('inputs.json') as f: inputs = json.load(f) # ----- load single geometry ----- # geometry_geojson = inputs['geometry_geojson'] with open(geometry_geojson) as f: extent = geojson.load(f) # ----- authentication ----- # print('authentication - get JWT') username = inputs['username'] password = inputs['password'] authorize = Authentication(username, password) headers = authorize.get_headers() # ----- search imagery with rangar ----- # print('search imagery (ragnar)') startDatetime = inputs['startDatetime'] endDatetime = inputs['endDatetime'] search_ragnar = SearchImagery(headers, extent, startDatetime, endDatetime) search_ragnar.init_image() resolve_pipeline = Wait(headers, search_ragnar.pipelineId, timeout=100) sceneIds = search_ragnar.ret_image() sceneIds = sceneIds[:min(len(sceneIds), inputs['max_pictures'])] n_cars_all = [] images_all = [] date_times = [] i = 1 for sceneId in sceneIds: # ----- get imagery with rangar ----- # print('-----------------------------------') print('Process scene no. ' + str(i)) print('-----------------------------------') print('get imagery (ragnar)') get_ragnar = GetImagery(headers, extent, sceneId) get_ragnar.init_image() resolve_pipeline = Wait(headers, get_ragnar.pipelineId, inputs['timeout']) if resolve_pipeline.status == 'PROCESSING': continue url, meta = get_ragnar.ret_image() # ----- detect cars with kraken ----- # print('detect cars (kraken)') map_type = 'cars' kraken = Analyses(headers, sceneId, map_type, extent) kraken.init_image() resolve_pipeline = Wait(headers, kraken.pipelineId, inputs['timeout']) if resolve_pipeline.status == 'PROCESSING': continue map_id, max_zoom, tiles = kraken.ret_image() cars = Cars(map_id, tiles) n_cars = cars.count_cars() mask_cars = cars.combine_tiles() # ----- analyze scene with kraken ----- # print('analyze imagery (kraken)') map_type = 'imagery' kraken = Analyses(headers, sceneId, map_type, extent) kraken.init_image() resolve_pipeline = Wait(headers, kraken.pipelineId, inputs['timeout']) if resolve_pipeline.status == 'PROCESSING': continue map_id, zoom, tiles = kraken.ret_image() background = TrueImage(map_id, tiles) true_image = background.combine_tiles() combined_picture = combine_pictures(mask_cars, true_image) n_cars_all.append(n_cars) images_all.append(combined_picture) date_times.append(meta['datetime']) i += 1 for image, date_time, n_cars in zip(images_all, date_times, n_cars_all): file_name = 'scene_' + date_time im = Image.fromarray(image) im.save("{}.png".format(file_name)) plt.imshow(image) plt.show() print('Number of cars on ' + date_time + ': ' + str(n_cars))
def _call_function(self): self._error_mesage = "Error Changing VM Info." (inf_id, vm_id, radl, auth_data) = self.arguments return str( InfrastructureManager.InfrastructureManager.AlterVM( inf_id, vm_id, radl, Authentication(auth_data)))
def _call_function(self): self._error_mesage = "Error Stopping Inf." (inf_id, auth_data) = self.arguments return InfrastructureManager.InfrastructureManager.StopInfrastructure( inf_id, Authentication(auth_data))
def _call_function(self): self._error_mesage = "Error Creating Inf." (radl_data, auth_data) = self.arguments return InfrastructureManager.InfrastructureManager.CreateInfrastructure( radl_data, Authentication(auth_data))
class VpnClient(object): def __init__(self, ip_addr, port, shared_key, broken_conn_callback, app): self.ip_addr = ip_addr self.port = port self.shared_key = shared_key self.broken_conn_callback = broken_conn_callback self.send_queue = Queue() self.receive_queue = Queue() self.waiting = True self.is_server=False self.authenticated=False self.sender = None self.receiver = None self.app = app def connect(self): try: self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) except socket.error: return (-1, "Could not create socket") try: self.socket.settimeout(10) self.socket.connect((self.ip_addr, self.port)) self.waiting = False self.auth = Authentication(self.shared_key, self, self.app, debug=True, is_server=False) self.bind() # Added because we need the send/recv threads running for authentication if (self.auth.mutualauth()): print "Server Authenticated!" Logger.log("Connected to Server", self.is_server) self.authenticated = True self.sessionkey = self.auth.get_sessionkey() self.clear_queues() return (0, "Connected to (%s, %i)" % (self.ip_addr, self.port)) else: print "Could not authenticate" self.authenticated = False self.broken_conn_callback() return (-1, "Authentication failed") except socket.error: self.authenticated = False self.broken_conn_callback() return (-1, "Could not connect to (%s, %i)" % (self.ip_addr, self.port)) return (-1, "Could not connect to (%s, %i)" % (self.ip_addr, self.port)) def clear_queues(self): self.receive_queue.queue.clear() self.send_queue.queue.clear() def send(self, msg): if (self.authenticated): emsg = self.auth.encrypt_message(msg, self.auth.get_sessionkey()) self.send_queue.put(emsg) Logger.log("Put message on send queue: " + msg, self.is_server) else: self.send_queue.put(msg) Logger.log("Put message on send queue: " + msg, self.is_server) def bind(self): self.sender = Sender(self.socket, self.send_queue, self) self.receiver = Receiver(self.socket, self.receive_queue, self) self.sender.start() self.receiver.start() def close(self): Logger.log("Connection closing", self.is_server) self.send_queue.queue.clear() self.receive_queue.queue.clear() if self.sender: self.sender.close() if self.receiver: self.receiver.close() self.waiting = True self.authenticated = False self.auth = None def receive(self): if (not self.receive_queue.empty()): msg = self.receive_queue.get() Logger.log("Received decrypted msg: "+ msg, self.is_server) if (self.authenticated): msg, valid = self.auth.decrypt_message(msg, self.auth.get_sessionkey()) if valid is False: return None # ignore failed CBC authentication message Logger.log("Decrypted msg: "+ msg, self.is_server) return msg else: return None
def fan_tweet(): # Created data frame to store the tweets tweets_fan = pd.DataFrame(columns=[ "CREATED_AT", "TWEET_ID", "TWEET", "USER_ID", "USER_NAME", "RETWEET_COUNT", "FAVORITE_COUNT", "HASHTAGS" ]) # Open the csv contains selected 20 players from NBA fan_df = pd.read_csv("rawData//fan//fan_twitter.csv") # Iterate through each player and scrape their tweets for i in range(0, fan_df.shape[0]): account = fan_df.at[i, "TWITTER_ACC"] current_fan = TweetProcress() current_fan_tweets = current_fan.request_tweet(screen_name=account, count=200, pages=40) tweets_fan = tweets_fan.append(current_fan_tweets) file = "Tweet " + fan_df.at[i, "FULL_NAME"] + ".csv" current_fan_tweets.to_csv("rawData//fan//" + file, index=False) print(file + " is done") # One single csv contains all the tweets tweets_fan.to_csv("rawData//fan//tweets_fan.csv", index=False) if __name__ == "__main__": auth = Authentication() api = Authentication.load_api(auth) #team_tweet() #player_tweet() #fan_tweet()
def _call_function(self): self._error_mesage = "Error Reconfiguring Inf." (inf_id, radl_data, auth_data, vm_list) = self.arguments return InfrastructureManager.InfrastructureManager.Reconfigure( inf_id, radl_data, Authentication(auth_data), vm_list)
def __init__(self): self._reddit_client = Authentication().GetRedditClient()
def _call_function(self): self._error_mesage = "Error Importing Inf." (str_inf, auth_data) = self.arguments return InfrastructureManager.InfrastructureManager.ImportInfrastructure( str_inf, Authentication(auth_data))
def _call_function(self): self._error_mesage = "Error Exporting Inf." (inf_id, delete, auth_data) = self.arguments return InfrastructureManager.InfrastructureManager.ExportInfrastructure( inf_id, delete, Authentication(auth_data))
def _call_function(self): self._error_mesage = "Error Getting VM cont msg." (inf_id, vm_id, auth_data) = self.arguments return InfrastructureManager.InfrastructureManager.GetVMContMsg( inf_id, vm_id, Authentication(auth_data))
def _call_function(self): self._error_mesage = "Error gettinf the Inf. cont msg" (inf_id, auth_data) = self.arguments return InfrastructureManager.InfrastructureManager.GetInfrastructureContMsg( inf_id, Authentication(auth_data))
def _call_function(self): self._error_mesage = "Error Getting Inf. List." (auth_data) = self.arguments return InfrastructureManager.InfrastructureManager.GetInfrastructureList( Authentication(auth_data))
def _call_function(self): self._error_mesage = "Error stopping VM" (inf_id, vm_id, auth_data) = self.arguments InfrastructureManager.InfrastructureManager.StopVM( inf_id, vm_id, Authentication(auth_data)) return ""
def __init__(self): log_debug(3) Authentication.__init__(self) self.functions = [ 'download', ]
def _call_function(self): self._error_mesage = "Error getting the Inf. state" (inf_id, auth_data) = self.arguments return InfrastructureManager.InfrastructureManager.GetInfrastructureState( inf_id, Authentication(auth_data))