Sending users a password insertion Django Rest Framework - python

I am using Django Rest Framework and want to be able to send a user a password reset view. It actually comes from a user inviting them into the software but it automatically sets a secure password.
I want to be able to send that user an email asking them to change their password.
How can I go about this?

you need to use builtin apps in django for password reset you need to use it on ur urls.py
for example:
in settings.py
INSTALLED_APPS = [
...
'password_reset',
...
]
urls.py in project:
path('',include('django.contrib.auth.urls')),
in app urls.py:
path('password_reset/', include('password_reset.urls')),

Related

LDAP authentication isnt happening over django rest API

I am authenticating users who login against through django REST framework against NIS using pythonPAM . But when I make the changes to settings.py as mentioned below, user credentials isnt being verified against LDAP.
AUTHENTICATION_BACKENDS = (
'django_auth_ldap.backend.LDAPBackend',
)
This along with ldap credentials for binding is used.
Note: I can connect to LDAP server and get all teh details. Its only a problem with django restframework.
I also tried
AUTHENTICATION_BACKENDS = [ 'django.contrib.auth.backends.RemoteUserBackend',]
and it did not work.
Currently in my urls.py, I call url(/login,) endpoint and authenticate against NIS server. How do I change it to LDAP?
I am new to django and need help on this.

django with apache ldap backend auth, get logged in username and ldap group & hide detail based on group (mod_ldap)

im using Django with Apache and LDAP backend auth, my http conf is as below:
LoadModule ldap_module modules/mod_ldap.so
LoadModule authnz_ldap_module modules/mod_authnz_ldap.so
#
<Location />
AuthName "Please enter your domain credentials."
AuthBasicProvider ldap
AuthType basic
AuthLDAPUrl "ldap://example.com:389/DC=example,DC=com?sAMAccountName"
AuthLDAPBindDN "CN=serv,OU=Service Accounts,DC=example,DC=com"
AuthLDAPBindPassword XXXX
AuthLDAPBindAuthoritative off
LDAPReferrals off
Require valid-user
</Location>
Which when i now load my site i get a basic auth prompt which is great, what id like to be able to do now is to receive the logged in username, ive searched and tried a few things such as:
LoggedInUser = request.user.username
which gives me a request is not defined message (i have import requests at the top)
LoggedInUser = os.getenv["REMOTE_USER"]
which gives me TypeError: 'function' object has no attribute 'getitem'
does anyone know what i need to be using?
i also need to hide certain urls from users if they are not in the correct ldap group, so would need to get the users AD groups aswell from the session
Thanks
Per request WSGI environ key/values are found in Django request.META object. Thus try:
request.META['REMOTE_USER']
Whether what Apache passes through to you is in format you expect is a different issue. You may find what you want in other variables passed through. See:
http://httpd.apache.org/docs/2.4/mod/mod_authnz_ldap.html#exposed
According to the documentation topic "Authentication using REMOTE_USER", in order to use Apache authentication, you must include a specific middleware:
Configuration
First, you must add the django.contrib.auth.middleware.RemoteUserMiddleware to the MIDDLEWARE_CLASSES setting after the django.contrib.auth.middleware.AuthenticationMiddleware:
MIDDLEWARE_CLASSES = [
'...',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.RemoteUserMiddleware',
'...',
]
Next, you must replace the ModelBackend with RemoteUserBackend in the AUTHENTICATION_BACKENDS setting:
AUTHENTICATION_BACKENDS = [
'django.contrib.auth.backends.RemoteUserBackend',
]
If you are already doing this without success, and given you need more granular access controls, I would just write a custom authentication backend and ditch mod_ldap altogether. Writing a custom authentication backend is really easy. The key is to get the python ldap module working before writing the backend.
In order to access request.user you must be inside a Django view. For example:
def index(request):
user = request.user
return render(request, 'template.html', {"user": user})
And in the template.html file:
<h1>Hi, {{ user }}</h1>

Django url parameter passing

I am not even sure what category in Django this question falls in and I am very new to django. I have tried looking for Django post requests, parameter passing and even checked under Django APIs but have not found what I am looking for. What I am trying to do is create an API for my client but it must be done in Django. If I was to do this in .Net I could use http post and http get and web services but I am not at all sure how this is done in Django. What my client wants to do is to be able to see:
Enter username and password in url with parameters for date and id and be able to view rooms available based on what is entered
Enter username, password and dates and room code to be able to book the room
No interface needed just simple parameter passing through url. Is this possible with Django and if yes can somebody please point me in the right direction.
What you're looking for are captured parameters
Below is a code snippet from the above link.
# urls.py
from django.conf.urls import patterns, url
urlpatterns = patterns('blog.views',
url(r'^blog/(?P<year>\d{4})/$', 'year_archive', {'foo': 'bar'}),
)
# views.py
def year_archive(request, year, foo=None):
# view method definition
As of Django 1.10, patterns was removed from Django.
Here is a 2019 Django 2.2 solution.
It looks like re_path is the current recommended tool for capturing parameters via regular expressions:
https://docs.djangoproject.com/en/2.2/ref/urls/#re-path
# urls.py
from django.urls import re_path
from myapp import views
urlpatterns = [
re_path(r'^blog/(?P<year>\d{4})/$', 'year_archive', {'foo': 'bar'}),
]
# views.py
def year_archive(request, year, foo=None):
# view method definition

Python/Django - initial/default admin page

After a user has logged into the admin interface is it possible to redirect them to a specific page?
I want to redirect to the list of entries for a particular model.
I'm using Django 1.3
Cheers!
You can set the default redirect url (on login) in your settings.py
LOGIN_REDIRECT_URL = "/admin/mymodel/"
https://docs.djangoproject.com/en/dev/ref/settings/?from=olddocs#login-redirect-url
Look at ticket 14510 .
Seems django admin doesn't support LOGIN_REDIRECT_URL settings and you should subclass AdminSite.

how to set the admin user with GAE dev app server?

I want to create an Admin-only page for my GAE application. But I found there's no way to set a Admin user, so I cannot log on the page I created and test the function. I tried the local Admin console, but no luck. How can I do this?
Google App Engine provides a pretty straightforward way to create a private admin section of your web application.
1.
In your app.yaml, any URL handler can have a login setting to restrict visitors to only those users who have signed in, or just those users who are administrators for the application.
If the setting is login: admin, once the user has signed in, the handler checks whether the user is an administrator for the application. If not, the user is given an error message; if the user is an administrator, the handler proceeds.
Here a snippet of app.yaml where the /admin/.* routes are admin restricted :
- url: /admin/.*
script: admin.py
login: admin
2.
Trying to access the admin url, the dev app server automatically shows the login panel where you should check the Sign in as Administrator checkbox.
When you log in (the blue box where you enter the email) there is a checkbox to apply the administrator flag to your session.
You can define admin only pages in your app.yaml, like so:
- url: /secrets/
script: /secrets/example.py
login: admin
an admin is whoever is so defined in your appengine.google.com control panel, under permissions.

Categories

Resources