Skip to content

Commit e3274cd

Browse files
committed
Disallow blocking oauth on oauth logins
When a user logs in with oauth, we should not allow them to disable their own ability to log in.
1 parent dae2780 commit e3274cd

5 files changed

Lines changed: 23 additions & 8 deletions

File tree

pgweb/account/admin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
from pgweb.util.widgets import TemplateRenderWidget
1212
from pgweb.util.db import exec_to_dict
13-
from pgweb.account.views import OAUTH_PASSWORD_STORE
1413

1514
from .models import CommunityAuthSite, CommunityAuthOrg, SecondaryEmail
15+
from .models import OAUTH_PASSWORD_STORE
1616

1717

1818
class CommunityAuthSiteAdminForm(forms.ModelForm):

pgweb/account/forms.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from django import forms
2+
from django.core.validators import ValidationError
23
from django.contrib.auth.forms import AuthenticationForm
34

45
import re
@@ -7,6 +8,7 @@
78
from pgweb.core.models import UserProfile
89
from pgweb.contributors.models import Contributor
910
from .models import SecondaryEmail
11+
from .models import OAUTH_PASSWORD_STORE
1012

1113
from .recaptcha import ReCaptchaField
1214

@@ -134,6 +136,17 @@ class Meta:
134136
model = UserProfile
135137
exclude = ('user',)
136138

139+
def __init__(self, user, *args, **kwargs):
140+
self.user = user
141+
super().__init__(*args, **kwargs)
142+
143+
def clean_block_oauth(self):
144+
if self.cleaned_data.get('block_oauth', False):
145+
if self.user.password == OAUTH_PASSWORD_STORE:
146+
raise ValidationError("Your account used OAuth to log in, you cannot block the use of it")
147+
148+
return self.cleaned_data['block_oauth']
149+
137150

138151
class UserForm(forms.ModelForm):
139152
primaryemail = forms.ChoiceField(choices=[], required=True, label='Primary email address')

pgweb/account/models.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
from django.contrib.auth.models import User, Group
33

44

5+
# The value we store in user.password for oauth logins. This is
6+
# a value that must not match any hashers.
7+
OAUTH_PASSWORD_STORE = 'oauth_signin_account_no_password'
8+
9+
510
class CommunityAuthOrg(models.Model):
611
orgname = models.CharField(max_length=100, null=False, blank=False, unique=True,
712
help_text="Name of the organisation")

pgweb/account/views.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
from pgweb.profserv.models import ProfessionalService
4141

4242
from .models import CommunityAuthSite, CommunityAuthConsent, SecondaryEmail
43+
from .models import OAUTH_PASSWORD_STORE
4344
from .forms import PgwebAuthenticationForm, ConfirmSubmitForm
4445
from .forms import CommunityAuthConsentForm
4546
from .forms import SignupForm, SignupOauthForm
@@ -55,10 +56,6 @@
5556

5657
log = logging.getLogger(__name__)
5758

58-
# The value we store in user.password for oauth logins. This is
59-
# a value that must not match any hashers.
60-
OAUTH_PASSWORD_STORE = 'oauth_signin_account_no_password'
61-
6259

6360
def _modobjs(qs):
6461
l = list(qs)
@@ -157,7 +154,7 @@ def profile(request):
157154
if request.method == 'POST':
158155
# Process this form
159156
userform = UserForm(can_change_email, secondaryaddresses, data=request.POST, instance=request.user)
160-
profileform = UserProfileForm(data=request.POST, instance=profile)
157+
profileform = UserProfileForm(request.user, data=request.POST, instance=profile)
161158
secondaryemailform = AddEmailForm(request.user, data=request.POST)
162159
if contrib:
163160
contribform = ContributorForm(data=request.POST, instance=contrib)
@@ -202,7 +199,7 @@ def profile(request):
202199
else:
203200
# Generate form
204201
userform = UserForm(can_change_email, secondaryaddresses, instance=request.user)
205-
profileform = UserProfileForm(instance=profile)
202+
profileform = UserProfileForm(request.user, instance=profile)
206203
secondaryemailform = AddEmailForm(request.user)
207204
if contrib:
208205
contribform = ContributorForm(instance=contrib)

pgweb/core/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from pgweb.util.misc import send_template_mail
3232
from pgweb.util.sitestruct import get_all_pages_struct
3333
from pgweb.mailqueue.util import send_simple_mail
34-
from pgweb.account.views import OAUTH_PASSWORD_STORE
34+
from pgweb.account.models import OAUTH_PASSWORD_STORE
3535
from pgweb.release.util import CurrentRelease
3636

3737
# models needed for the pieces on the frontpage

0 commit comments

Comments
 (0)