You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.4 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016-2017 LasLabs Inc.
  3. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
  4. import logging
  5. from odoo import _, api, fields, models
  6. _logger = logging.getLogger(__name__)
  7. try:
  8. import pyotp
  9. except ImportError:
  10. _logger.debug(
  11. 'Could not import PyOTP. Please make sure this library is available in'
  12. ' your environment.'
  13. )
  14. class ResUsersAuthenticator(models.Model):
  15. _name = 'res.users.authenticator'
  16. _description = 'MFA App/Device'
  17. _sql_constraints = [(
  18. 'user_id_name_uniq',
  19. 'UNIQUE(user_id, name)',
  20. _(
  21. 'There is already an MFA app/device with this name associated with'
  22. ' your account. Please pick a new name and try again.'
  23. ),
  24. )]
  25. name = fields.Char(
  26. required=True,
  27. readonly=True,
  28. )
  29. secret_key = fields.Char(
  30. required=True,
  31. readonly=True,
  32. )
  33. user_id = fields.Many2one(
  34. comodel_name='res.users',
  35. ondelete='cascade',
  36. )
  37. @api.multi
  38. @api.constrains('user_id')
  39. def _check_has_user(self):
  40. self.filtered(lambda r: not r.user_id).unlink()
  41. @api.multi
  42. def validate_conf_code(self, confirmation_code):
  43. for record in self:
  44. totp = pyotp.TOTP(record.secret_key)
  45. if totp.verify(confirmation_code):
  46. return True
  47. return False