Python request authentication without password - python-3.x

I have a need to log in to an administrative profile on a site using 81 port. Username is "admin", and a password is empty. That is, if I open the login window through the browser, I will only need to specify the username in the input form.
I'm trying to do this:
s = requests.session
But using s.get(url=f'http://{ip}:81', auth=('admin', '')) i get (Responce [401])
And also using s.get(url=f'http://{ip}:81', auth=('admin', None)) i get (Responce [401])
Question: How can I get (Responce [200]) without password with python request?

Problem resolved:
from requests.auth import HTTPDigestAuth

Related

How do I get the current user of a FastAPI app that uses SQLModel?

I started to use SQLModel that is created by the same person as FastAPI, but I cannot seem to find how to combine authentication/authorization to get the logged-in user and then get current user from that with SQLModel.
I can authenticate a user by just checking it against a database as below, but then how can I keep this session alive so that I can do other stuff with it such as get current user?
I am purely testing with localhost:8000/docs, so I thought maybe I need to create some Jinja2 templates to test it out in a browser, but not sure?
def login_test(input_email: str, input_password: str, session: Session = Depends(get_session)):
# Check if user exists or not
if not check_user_exists(input_email=input_email):
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
detail="User does not exist, please create an account")
# Check if password is ok
if not check_hashed_password(input_password, session.query(User).filter(User.email == input_email).first().password):
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
detail="Password is incorrect")
# Login based on email and password if both ok
return {"Message": "Login successful"}```

username and password to url in python selenium

Introduction:
i am doing UI testing with python and selenium and i am blocked when sign in page is asked as pop up window and i only seen best way is to pass the username and password to the url.. but my username contains "#" and "*" i even tried with encoded version of it still its not working
Problem:
normal url - driver.get("https://firstname.lastname#test.com:testpass*11#test.company.com")
encoded - driver.get("https://firstname.lastname%40test.com:testpass%2A11#test.company.com")
i used '%40' for '#' and '%2A' for '*' ,
Error:
Getting error like 404

When I use pycurl to execute a curl command, I get error 3 "illegal characters found in URL" but when pasting said URL in Chome, it can be resolved

Good day. I'm writing a python program that requests some posts from my Facebook page. To do so, Facebook offers a tool that they call "Graph API Explorer". Using something similar to a GET request, I can get anything that I want (granted that I have access and a valid token). I've come up with my own solution for the Graph API Explorer and that is generating my URLs. After generating a URL, I use pycurl to get a JSON object from Facebook that contains all of my data.
When I use pycurl, I get the following error:
pycurl.error: (3, 'Illegal characters found in URL')
but when printing said URL and pasting it to a browser, I got a valid response.
URL: https://graph.facebook.com/v7.0/me?fields=posts%7Bmessage%2Cfrom%7D&access_token=<and my access token which is valid>
my code looks like this:
def get_posts_curl(nodes=['posts'], fields=[['message', 'from']], token_file='Facebook/token.txt'):
curl = pycurl.Curl()
response = BytesIO()
token = get_token_from_file(token_file)
# constructing request.
url = parse_facebook_url_request(nodes, fields, token)
url = convert_to_curl(url)
print("---URL---: " + url)
# curl session and settings.
curl.setopt(curl.CAINFO, certifi.where())
curl.setopt(curl.URL, url)
curl.setopt(curl.WRITEDATA, response)
curl.perform()
curl.close()
return response.getvalue().decode('utf-8')
The error pops up at curl.perform()
Some info that might be relevant:
All was working great a while ago. After transferring my program from my workstation (that is running Windows 10) to my server (Ubuntu 18.04 Server) still, all was working fine and I placed that project to the side. Only now that error pops up and I haven't touched the project in a while.
It seems that the token is causing the issue. I've tried about 100 tokens and some cause the problem and some don't. Also, a fix that solved it all was using urllib3.unquote
from urllib.parse import unquote
...
url = unquote(url)

Python SMTP Authentication error when sending email through Outlook only when password is provided

I am trying to use the generic python SMTP lib modules for sending an email from an outlook web application (not office 365) through Python. I am using the right host server name and port no. But still, I am unsure why I am receiving an authentication error. I understand that this is a frequently asked question, but I still wonder why it is unable to authenticate. Any help on this would be appreciated. Thank you.
Our email is configured with a DUO set-up (Two-factor authentication). I initially assumed this might be the reason. But the other methods I used worked perfectly fine without doing any additional setups
Some methods I tried were.
Using Python's win32com.client module in Python for sending email
through Desktop Outlook App.
Using Powershell to send an email
The code is pretty straight forward and simple.
import smtplib
from getpass import getpass
email = '********'
password =getpass("Enter your password")
with smtplib.SMTP('xxxx.xxx.xxx',25) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login(email,password)
subject = 'Test'
body = 'Testing email'
msg = f'Subject:{subject}\n\n{body}'
smtp.sendmail(email,email,msg)
It always shows this error, irrespective of the ports I am using.
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, b'5.7.8 Error: authentication failed: authentication failure')
EDIT:
Ok!! so the code absolutely runs fine if I remove the login and prompt password statement. But this seems to be dangerous and I am not sure why it's allowing us to send emails even though we are not logged in. Maybe the Windows Server Active Directory is already registered on the domain

Spotipy - Cannot log in to authenticate (Authorization Code Flow)

I am working with the Spotipy Python library to connect to the Spotify web API. I want to get access to my Spotify's user account via Authorization Code Flow. I am using Python 3.5, Spotipy 2.4.4, Google Chrome 55.0.2883.95 (64-bit) and Mac OS Sierra 10.12.2
First, I went to the Spotify Developer website to register the program to get a Client ID, a Client Secret key and enter a redirect URI (https://www.google.com) on my white-list.
Second, I set the environment variables from terminal:
export SPOTIPY_CLIENT_ID='my_spotify_client_id'
export SPOTIPY_CLIENT_SECRET='my_spotify_client_secret'
export SPOTIPY_REDIRECT_URI='https://www.google.com'
Then I try to run the example code typing 'python3.5 script.py my_username' from terminal. Here is the script:
import sys
import spotipy
import spotipy.util as util
scope = 'user-library-read'
if len(sys.argv) > 1:
username = sys.argv[1]
else:
print "Usage: %s username" % (sys.argv[0],)
sys.exit()
token = util.prompt_for_user_token(username, scope)
if token:
sp = spotipy.Spotify(auth=token)
results = sp.current_user_saved_tracks()
for item in results['items']:
track = item['track']
print track['name'] + ' - ' + track['artists'][0]['name']
else:
print "Can't get token for", username
When running this code, it takes me to a log in screen on my browser. I enter my Spotify's credential to grant access to my app. But when I finally click on 'log in' (or 'Iniciar sesiĆ³n' in Spanish) nothing happens. I tried to log in with my Facebook account but it does not work either. It seems that I get a Bad Request every time that I click on 'log in'. Here is a capture of Chrome:
The process is incomplete because when trying to enter the redirect URI back on terminal I get another Bad Request:
raise SpotifyOauthError(response.reason)
spotipy.oauth2.SpotifyOauthError: Bad Request
I tried to restart my computer, clean my browser cookies, use another different browser but did not work. It seems that I am not the only one having this problem. Is it maybe a bug? Please, avoid answers like "read the documentation of the API going here". Thank you.
Solved it. In order to run the Authorization Code Flow example code provided in the Spotipy's documentation correctly, I specified the redirect URI in line 13 of the example script when calling util.prompt_for_user_token, even when I had done this previously when setting environment variables:
token = util.prompt_for_user_token(username, scope, redirect_uri = 'https://example.com/callback/')
Likewise, do not use https://www.google.com or similar web address as your redirect URI. Instead, try 'https://example.com/callback/' or 'http://localhost/' as suggested here. Do not forget that the redirected URL once you are logged in must have the word code included.
Cheers,

Resources