def auth_request(): """Authenticate User. Returns: JSON: JSON data to return to client, whether it be requested data or alerting of lack of authorization. """ if request.method == "GET": return elif request.method == "OPTIONS": # Returns 200 so Chrome's console doesn't spam. return jsonify( {"message": "OPTIONS requests are ignored by this server!"}), 200 data = request.get_json() try: if data["auth"] == "password": return auth.get_perma_token(data["user"].lower(), data["password"]) elif data["auth"] == "perma_token": return auth.get_temp_token(data["user"].lower(), data["token"]) elif data["auth"] == "temp_token": r_data = auth.auth_token(data["token"]) if r_data != {"message": "Authorized"}: return jsonify(r_data), 401 else: return else: return {"message": "Unauthorized!"}, 401 except KeyError: return {"message": "Unauthorized!"}, 401
def get_location(self): search = input(f"Hi {self.first}, where would you like to go?\n" ).strip() # strip all spaces to attach to URL if search: account_sid = auth.account_sid( ) # verify their account_sid to prevent spam auth_token = auth.auth_token() # verify it is them client = Client(account_sid, auth_token) # set up client to authenticate phone_number = input( "What is your phone number?\n" ) # recipient (can be changed to a fixed number) from_ = auth.trial() # sender message = client.messages.create( # create the msg and send! to=phone_number, # input from_=from_, # from auth module body= f"Here are some locations for {search} https://www.google.com/maps/search/{search.replace(' ', '')}" ) # link with location print(message) print(f"I sent you some location(s) for {search}!") else: print("You did not enter a location...") # if input is falsy
def document(docid): document = app.db.document.find_one({'_id': docid}) document = doc_mongo_to_app(document) if can_read(document, session.get('username')): outline = app.db.outline.find({ 'documentid': docid, 'status': { '$ne': 'DELETE' } }) outline = list(outline) logging.debug("numoutlines %d", len(outline)) outline = [outline_mongo_to_app(e) for e in outline] token = auth.auth_token(session.get('username'), app.secret_key) split = urlparse.urlsplit(request.url) if split.scheme == "http": wsurl = "ws://%s/sub" % split.netloc else: #hack wsurl = "wss://%s:9000/sub" % split.netloc return jsonify(document=document, outline=outline, token=token, wsurl=wsurl) else: return redirect("/login")
def document(docid): document = app.db.document.find_one({'_id' : docid}) document = doc_mongo_to_app(document) if can_read(document, session.get('username')): outline = app.db.outline.find({'documentid': docid, 'status' : {'$ne' : 'DELETE'}}) outline = list(outline) logging.debug("numoutlines %d", len(outline)) outline = [outline_mongo_to_app(e) for e in outline] token = auth.auth_token(session.get('username'), app.secret_key) split = urlparse.urlsplit(request.url) if split.scheme == "http": wsurl = "ws://%s/sub" % split.netloc else: #hack wsurl = "wss://%s:9000/sub" % split.netloc return jsonify(document=document, outline=outline, token=token, wsurl=wsurl ) else: return redirect("/login")
def callback(): code = request.args['code'] token_dict = loads(auth_token(code)) access_token = token_dict['access_token'] refresh_token = token_dict['refresh_token'] print(access_token) profile = profile_data(access_token) profile_url = profile['href'] playlist_desc = "List of pop songs from 2002 to 2004" playlist_title = "2000s Pop" category = "Pop" new_playlist = create_playlist(access_token, profile_url, playlist_title, playlist_desc) playlist_id = new_playlist['id'] print(playlist_id) urilist = create_urilist(access_token, songs, 2) try: for song in songs: add_to_playlist(access_token, song["name"], song["artist"], 2, playlist_id) return "Done" except Exception as error: print(error)
def test_some_token(self): headers = {"Authorization": "Bearer bla"} token = auth.auth_token(headers) self.assertEqual(token, "bla")
def test_too_many_token_parts(self): headers = {"Authorization": "Bearer bla bla"} with self.assertRaises(Unauthorized) as cm: auth.auth_token(headers) self.assertEqual(cm.exception.description, "Authorization header must be Bearer + \s + token")
def test_empty_token(self): headers = {"Authorization": "Bearer"} with self.assertRaises(Unauthorized) as cm: auth.auth_token(headers) self.assertEqual(cm.exception.description, "Token not found")
def test_non_bearer_token(self): headers = {"Authorization": "bla"} with self.assertRaises(Unauthorized) as cm: auth.auth_token(headers) self.assertEqual(cm.exception.description, "Authorization header must start with Bearer")
def test_no_auth_header(self): headers = {} with self.assertRaises(Unauthorized) as cm: auth.auth_token(headers) self.assertEqual(cm.exception.description, "Authorization header is expected")
def test_some_token(self): headers = {'Authorization': 'Bearer bla'} token = auth.auth_token(headers) self.assertEqual(token, 'bla')
def test_too_many_token_parts(self): headers = {'Authorization': 'Bearer bla bla'} with self.assertRaises(Unauthorized) as cm: auth.auth_token(headers) self.assertEqual(cm.exception.description, 'Authorization header must be Bearer + \s + token')
def test_empty_token(self): headers = {'Authorization': 'Bearer'} with self.assertRaises(Unauthorized) as cm: auth.auth_token(headers) self.assertEqual(cm.exception.description, 'Token not found')
def test_non_bearer_token(self): headers = {'Authorization': 'bla'} with self.assertRaises(Unauthorized) as cm: auth.auth_token(headers) self.assertEqual(cm.exception.description, 'Authorization header must start with Bearer')
def test_no_auth_header(self): headers = {} with self.assertRaises(Unauthorized) as cm: auth.auth_token(headers) self.assertEqual(cm.exception.description, 'Authorization header is expected')