How to get a user with 'django-microsoft-authentication' code?

413 views Asked by At

I'm using the library django-microsoft-authentication.

The application for microsoft was created, all the codes were received by me.

I did everything according to the documentation.

MICROSOFT = {
"app_id": "<my app id>",
"app_secret": "my app secret id",
"redirect": "http://localhost:8000",
"scopes": ["user.read"],
"authority": "https://login.microsoftonline.com/common",
"valid_email_domains": ["<list_of_valid_domains>"],
"logout_uri": "http://localhost:8000/admin/logout"
}

Add 'microsoft_authentication' to INSTALLED_APPS

LOGIN_URL = "/microsoft_authentication/login"
LOGIN_REDIRECT_URL = "/admin"

and urls.py

from django.urls import path, include


urlpatterns = [
    .....
    path('microsoft_authentication/', include('microsoft_authentication.urls'))
]

And everything goes well, and without errors. I authenticate and get returned to the home page. But there is no new user in the admin area. Or I need to do create a new user manually? Or is callback not working?

In my address bar I get some this: http://localhost:8000/?code=0.Awfwjhey79kyt4fe..........feky5hmj (random code). I understand that this is some kind of user token grant.

According to the documentation, I checked the decorator @microsoft_login_required(), and it working when I was logged in, and it did not work when I was NOT logged in. So everything is going well. But I only get the code=..... above. But I don't see the user anywhere.

How do I get a user? How do I create and save a user? Please, any help will help me.

1

There are 1 answers

0
grey_ranger On

The README on GitHub for this project shows two more items for your settings.py file:

# True: creates new Django User after valid microsoft authentication. 
# False: it will only allow those users which are already created in Django User model and 
# will validate the email using Microsoft.
MICROSOFT_CREATE_NEW_DJANGO_USER = True  # Optional, default value is True
MICROSOFT_NEW_DJANGO_USER_IS_STAFF = True  # Optional, default value is True

In my experience, the default values for both are actually False even though the documentation says True. I was unable to create new users automatically until I added MICROSOFT_CREATE_NEW_DJANGO_USER=True.

In general, I would set MICROSOFT_NEW_DJANGO_USER_IS_STAFF to False and update later in the admin section once you know that the right user has logged in.