-
Notifications
You must be signed in to change notification settings - Fork 4
Adds 2 factor backend code #169
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
Changes from all commits
d629ff2
11091ec
f78dd30
9451d0a
f417a11
0b165cc
bcd7902
c944f23
5b4b788
36816f1
4d76f31
ba0082d
9cb158a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| """Adds table for 2 factor secret and checking if active | ||
|
|
||
| Revision ID: e3a72926f808 | ||
| Revises: c3803ac9b7dd | ||
| Create Date: 2016-08-10 19:12:49.647496 | ||
|
|
||
| """ | ||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision = 'e3a72926f808' | ||
| down_revision = 'c3803ac9b7dd' | ||
|
|
||
| from alembic import op | ||
| import sqlalchemy as sa | ||
|
|
||
|
|
||
| def upgrade(): | ||
| op.add_column('postmaster_admins', sa.Column('otp_active', sa.Boolean(), nullable=True)) | ||
| op.add_column('postmaster_admins', sa.Column('otp_secret', sa.String(length=16), nullable=True)) | ||
|
|
||
|
|
||
| def downgrade(): | ||
| op.drop_column('postmaster_admins', 'otp_secret') | ||
| op.drop_column('postmaster_admins', 'otp_active') |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -256,44 +256,41 @@ def validate_wtforms_password(form, field): | |
| """ | ||
| username = form.username.data | ||
| password = form.password.data | ||
| two_factor = form.two_factor.data if form.two_factor.data else None | ||
|
|
||
| try: | ||
| if form.auth_source.data == 'PostMaster User': | ||
| admin = models.Admins.query.filter_by(username=username, source='local').first() | ||
|
|
||
| if admin: | ||
|
|
||
| if admin.is_unlocked(): | ||
|
|
||
| if bcrypt.check_password_hash(admin.password, password): | ||
| if admin.otp_active: | ||
| if not admin.verify_totp(two_factor): | ||
| raise WtfStopValidation('The two factor authentication code is incorrect') | ||
| form.admin = admin | ||
| return | ||
| else: | ||
| increment_failed_login(username) | ||
|
|
||
| else: | ||
| raise WtfStopValidation('The user is currently locked out. Please try logging in again later.') | ||
|
|
||
| json_logger( | ||
| 'auth', username, | ||
| 'The administrator "{0}" entered an incorrect username or password'.format( | ||
| username)) | ||
| raise WtfStopValidation('The username or password was incorrect') | ||
| else: | ||
| ad_object = AD() | ||
|
|
||
| if ad_object.login(username, password): | ||
|
|
||
| if ad_object.check_group_membership(): | ||
| friendly_username = ad_object.get_loggedin_user() | ||
| display_name = ad_object.get_loggedin_user_display_name() | ||
|
|
||
| if not models.Admins.query.filter_by(username=friendly_username, source='ldap').first(): | ||
| add_ldap_user_to_db(friendly_username, display_name) | ||
|
|
||
| admin = models.Admins.query.filter_by(username=friendly_username, source='ldap').first() | ||
| if admin.otp_active: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can consolidate these two lines to:
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's written the way it is so we can return messages based on if they even have otp_active or if their token was correct. |
||
| if not admin.verify_totp(two_factor): | ||
| raise WtfStopValidation('The two factor authentication code is incorrect') | ||
| form.admin = admin | ||
|
|
||
| except ADException as e: | ||
| raise WtfStopValidation(e.message) | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can consolidate these two lines to:
if admin.otp_active and not admin.verify_totp(two_factor):There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's written the way it is so we can return messages based on if they even have otp_active or if their token was correct.