Azure Python Flask Get Email id of the user logged in - python

I created the sample Python Flask Webapp by following the below link.
https://learn.microsoft.com/en-us/azure/app-service/containers/quickstart-python?tabs=bash
I also added the AD Authentication for the Flask application by enabling Authentication in the Settings of the App Services. Now the app works only when the user is logged in and cannot access otherwise.
I would want to fetch the email id if the user who is logged in. I tried checking online but unable to find a way to get the email address of the user.
Any help is really appreciated.

App Service passes user claims to your application by using special headers. So you can get user information from the request header. Some example headers include:
X-MS-CLIENT-PRINCIPAL-NAME
X-MS-CLIENT-PRINCIPAL-ID
You can use res.headers to see all the values.
Reference:
Access user claims

Related

django rest auth facebook code for login

On project I use django-rest-auth for user registration and authentication. Also i added code for facebook login:
from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter
from rest_auth.registration.views import SocialLoginView
class FacebookOAuth2AdapterFixed(FacebookOAuth2Adapter):
def __init__(self):
pass
class FacebookLogin(SocialLoginView):
adapter_class = FacebookOAuth2Adapter
And in my project urls.py I add
url(r'^rest-auth/facebook/$', FacebookLogin.as_view(), name='fb_login'),
But on url localhost:8000/rest-auth/facebook I see form with 2 parameters: Access token(already have) and code.
My question is next: Can I login via facebook without this code, and if not, how can I get this code without frontend? How can I check work user authentication/registration or not?
PS: SocialApp created, facebook app created, app id and app secret added to social app.
Only one of "Access Token" or "Code" field is required. (I have not tested the Code field but the Access Token field works, with the Code field left blank)
To use Access Token, after the user performs the "Login to Facebook" step on the client side using Facebook javascript SDK, you will receive a response from Facebook which includes "accessToken" for accessing data on Facebook. Simply paste this accessToken into the "Access Token" field and it will automatically login and/or create the user's account from data retrieved from Facebook.
Obviously you can then perform the same process by posting the access token to the form all in javascript.
In the field of Access Token, Pass the User Access Token that you will get from Facebook developer dashboard(generate and debug), code field you can leave as blank. It will create user in your application and will return JWT token with user details.

django register user with django-facebook

Ok I see everyone saying use django-facebook, and it says it can do everything I want.
I have it installed on my app and the example page works, but how do I actually register a user with it?
Apart from the installation all the rest of the docs seem autogenerated and I have yet to find a step by step guide as to how to register a user on my site using django-facebook.
Here is a working example of facebook auth with django.
Facebook uses OAuth2 to federate user logins. An OAuth2 authorization step-by-step consists of
Create an authorization token by calling the request-token url with your consumer keys
Send the user to the authorize url with the token that you created and a callback url
Once authorized the user will be redirected to your callback url with an auth-token
Convert the auth-token to an access token at the request-token url
Save the access token and use it to make authorized requests

Django-social-auth extended facebook permissions like spotify

I was able to get the publish_actions permission for my facebook app by using this setting
FACEBOOK_EXTENDED_PERMISSIONS = ['publish_actions']
The problem is that when authorizing my facebook app is that a second window is used to prompt the user to allow my app to publish actions after the initial prompt for the basic info.
How can I use only one window to get users to authorize my app? I'm trying to get my authorization window to look similar spotify's authorization
Django-social-auth has been superseded by python-social-auth. In python-social-auth, you define extended permission by using SOCIAL_AUTH_FACEBOOK_SCOPE inside settings.py. For example:
SOCIAL_AUTH_FACEBOOK_SCOPE = ['email', 'user_photos']
It will only show a single authorization dialog.

GAE and django same users

I'm running a GAE and Django helper project and i ran into some issues when creating a User from de django shell. After exploring the code under the helper directory i came across with the following code:
class User(BaseModel):
user = db.UserProperty(required=True)
My question is, do i have to bind the django user to a google account if so, how do i create a django or google user from my code?
UPDATE: I meant to say create the account without having the google account itself. For instace not having:
users.get_current_user()
UPDATE: A user must be able to create a new account just like regular django users, with a register page for that matter.
For AppEngine, you can get the users who are logged into your application.
from google.appengine.api import users
And then you do
username = users.get_current_user()
And set your user property
User(user=username)
You could lookup the tutorial on how to do redirect to login to ask the application to login to get the user.

Django request.user is empty

Using django, I am authenticating the user through Google. I get the initial request tokens & redirect the user to google for auth. After which google redirects the user back to my website (using the redirect_url I provide).
At this point the request.user.id is None so is request.user.username why is this happening? I need the user ID to enter the access_tokens (that google sends me) into the DB.
Under what conditions can request.user object in Django be empty?
UPDATE1: When I get redirected back from Google with the url pattern as http://mywebsite.com/lserv?s=goog control comes back to my django views function, but django gives me the request.user object user as Anonymous user with no username or id. why?
UPDATE2:
all this is running on python manage.py runserver for now...
UPDATE3: Anybody faced anythn similar to this? basically, out of no reason the user in request clears out automatically & I get assigned as Anonymous user. All this happens between url requests from the user (from browser). Why?
Django's auth mechanism has nothing to do with Google's or any other auth service. If you want to integrate third party auth service with your Django site, you should do it youself.
If you're using oauth2 library, it's README has a section named "Logging into Django w/ Twitter" may help you.
If you are using oauth api from google. To get the user you have to do something like this
from google.appengine.api import oauth
# ...
try:
# Get the db.User that represents the user on whose behalf the
# consumer is making this request.
user = oauth.get_current_user()
except oauth.OAuthRequestError, e:
# The request was not a valid OAuth request.
# ...

Categories

Resources