Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force users to reset password on first login #83

Merged
merged 1 commit into from Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add login interface [#77](https://github.com/azavea/iow-boundary-tool/pull/77)
- Add reset password functionality [#79](https://github.com/azavea/iow-boundary-tool/pull/79)
- Create Remaining Initial Django Models [#82](https://github.com/azavea/iow-boundary-tool/pull/82)
- Force users to reset password on first login [#83](https://github.com/azavea/iow-boundary-tool/pull/83)

### Changed

Expand Down
7 changes: 6 additions & 1 deletion src/app/src/constants.js
Expand Up @@ -43,5 +43,10 @@ export const API_URLS = {
LOGIN: 'api/auth/login/',
LOGOUT: 'api/auth/logout/',
FORGOT: 'api/auth/password/reset/',
RESET: 'api/auth/password/reset/confirm/',
CONFIRM: 'api/auth/password/reset/confirm/',
RESET: 'confirm_password_reset/reset/',
Comment on lines -46 to +47
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was starting to get confusing, so this is my new proposal (see updates to axios calls, below).

};

export const API_STATUSES = {
REDIRECT: 302,
};
6 changes: 5 additions & 1 deletion src/app/src/pages/Login.js
Expand Up @@ -4,7 +4,7 @@ import { useNavigate } from 'react-router-dom';
import { Box, Button, Input, Text, VStack } from '@chakra-ui/react';

import { API } from '../api';
import { API_URLS } from '../constants';
import { API_URLS, API_STATUSES } from '../constants';
import { login } from '../store/authSlice';
import LoginForm from '../components/LoginForm';

Expand All @@ -25,6 +25,10 @@ export default function Login() {
dispatch(login());
})
.catch(apiError => {
if (apiError.response?.status === API_STATUSES.REDIRECT) {
const dest = `/${API_URLS.RESET}${apiError.response.data.uid}/${apiError.response.data.token}/`;
navigate(dest);
}
setErrorDetail(apiError.response?.data?.detail);
});
};
Expand Down
4 changes: 2 additions & 2 deletions src/app/src/pages/ResetPassword.js
Expand Up @@ -45,7 +45,7 @@ export default function ResetPassword() {
};

const forgotPasswordRequest = () => {
API.post(API_URLS.RESET, {
API.post(API_URLS.CONFIRM, {
uid,
token,
new_password1: newPassword1,
Expand All @@ -65,7 +65,7 @@ export default function ResetPassword() {
// This will require sending a dummy password that will never be valid (too short)
// in order to get a response including the token or uid fields.
useEffect(() => {
API.post(API_URLS.RESET, {
API.post(API_URLS.CONFIRM, {
uid,
token,
new_password1: '!', // too short
Expand Down
3 changes: 3 additions & 0 deletions src/django/api/management/commands/resetdb.py
Expand Up @@ -20,15 +20,18 @@ def handle(self, *args, **options):
User.objects.create_superuser(
email="a1@azavea.com",
password="password",
has_admin_generated_password=False,
)
User.objects.create_user(
email="v1@azavea.com",
password="password",
has_admin_generated_password=False,
role=Role.objects.get(pk=Roles.VALIDATOR),
)
contributor = User.objects.create_user(
email="c1@azavea.com",
password="password",
has_admin_generated_password=False,
role=Role.objects.get(pk=Roles.CONTRIBUTOR),
)
contributor.utilities.add(test_utility)
@@ -0,0 +1,18 @@
# Generated by Django 3.2.13 on 2022-09-29 13:30

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('api', '0012_create_annotation'),
]

operations = [
migrations.AddField(
model_name='user',
name='has_admin_generated_password',
field=models.BooleanField(default=True),
),
]
1 change: 1 addition & 0 deletions src/django/api/models/user.py
Expand Up @@ -59,6 +59,7 @@ class User(AbstractBaseUser, PermissionsMixin):
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
date_joined = models.DateTimeField(default=timezone.now)
has_admin_generated_password = models.BooleanField(default=True)

role = models.ForeignKey(
Role,
Expand Down
Empty file.
11 changes: 11 additions & 0 deletions src/django/api/serializers/auth.py
@@ -0,0 +1,11 @@
from django.db import transaction
from dj_rest_auth.serializers import PasswordResetConfirmSerializer


class UserChosenPasswordResetConfirmSerializer(PasswordResetConfirmSerializer):
@transaction.atomic
def save(self):
self.user.has_admin_generated_password = False
self.user.save()

return self.set_password_form.save()
10 changes: 10 additions & 0 deletions src/django/api/views/auth.py
Expand Up @@ -5,6 +5,9 @@
from dj_rest_auth.views import LoginView, LogoutView

from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.tokens import default_token_generator
from django.utils.encoding import force_bytes
from django.utils.http import urlsafe_base64_encode


class Login(LoginView):
Expand All @@ -21,6 +24,13 @@ def post(self, request, *args, **kwargs):

if user is None:
raise AuthenticationFailed("Unable to login with those credentials")
if user.has_admin_generated_password:
context = {
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'token': default_token_generator.make_token(user),
Comment on lines +28 to +30
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
# Ask client to redirect to a password reset link
return Response(context, status=status.HTTP_302_FOUND)

login(request, user)

Expand Down
4 changes: 4 additions & 0 deletions src/django/iow/settings.py
Expand Up @@ -185,6 +185,10 @@

AUTH_USER_MODEL = 'api.User'

REST_AUTH_SERIALIZERS = {
'PASSWORD_RESET_CONFIRM_SERIALIZER': 'api.serializers.auth.UserChosenPasswordResetConfirmSerializer',
}

# Logging
# https://docs.djangoproject.com/en/3.2/topics/logging/

Expand Down