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.

117 lines
3.6 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. import urllib
  6. from odoo import _, api, fields, models
  7. from odoo.exceptions import ValidationError
  8. _logger = logging.getLogger(__name__)
  9. try:
  10. import pyotp
  11. except ImportError:
  12. _logger.debug(
  13. 'Could not import PyOTP. Please make sure this library is available in'
  14. ' your environment.'
  15. )
  16. class ResUsersAuthenticatorCreate(models.TransientModel):
  17. _name = 'res.users.authenticator.create'
  18. _description = 'MFA App/Device Creation Wizard'
  19. name = fields.Char(
  20. string='Authentication App/Device Name',
  21. help='A name that will help you remember this authentication'
  22. ' app/device',
  23. required=True,
  24. index=True,
  25. )
  26. secret_key = fields.Char(
  27. default=lambda s: pyotp.random_base32(),
  28. required=True,
  29. )
  30. qr_code_tag = fields.Html(
  31. compute='_compute_qr_code_tag',
  32. string='QR Code',
  33. help='Scan this image with your authentication app to add your'
  34. ' account',
  35. )
  36. user_id = fields.Many2one(
  37. comodel_name='res.users',
  38. default=lambda s: s._default_user_id(),
  39. required=True,
  40. string='Associated User',
  41. help='This is the user whose account the new authentication app/device'
  42. ' will be tied to',
  43. readonly=True,
  44. index=True,
  45. )
  46. confirmation_code = fields.Char(
  47. string='Confirmation Code',
  48. help='Enter the latest six digit code generated by your authentication'
  49. ' app',
  50. required=True,
  51. )
  52. @api.model
  53. def _default_user_id(self):
  54. user_id = self.env.context.get('uid')
  55. return self.env['res.users'].browse(user_id)
  56. @api.multi
  57. @api.depends(
  58. 'secret_key',
  59. 'user_id.display_name',
  60. 'user_id.company_id.display_name',
  61. )
  62. def _compute_qr_code_tag(self):
  63. for record in self:
  64. if not record.user_id:
  65. continue
  66. totp = pyotp.TOTP(record.secret_key)
  67. provisioning_uri = totp.provisioning_uri(
  68. record.user_id.display_name,
  69. issuer_name=record.user_id.company_id.display_name,
  70. )
  71. provisioning_uri = urllib.quote(provisioning_uri)
  72. qr_width = qr_height = 300
  73. tag_base = '<img src="/report/barcode/?type=QR&amp;'
  74. tag_params = 'value=%s&amp;width=%s&amp;height=%s">' % (
  75. provisioning_uri,
  76. qr_width,
  77. qr_height
  78. )
  79. record.qr_code_tag = tag_base + tag_params
  80. @api.multi
  81. def action_create(self):
  82. self.ensure_one()
  83. self._perform_validations()
  84. self._create_authenticator()
  85. action_data = self.env.ref('base.action_res_users_my').read()[0]
  86. action_data.update({'res_id': self.user_id.id})
  87. return action_data
  88. @api.multi
  89. def _perform_validations(self):
  90. totp = pyotp.TOTP(self.secret_key)
  91. if not totp.verify(self.confirmation_code):
  92. raise ValidationError(_(
  93. 'Your confirmation code is not correct. Please try again,'
  94. ' making sure that your MFA device is set to the correct time'
  95. ' and that you have entered the most recent code generated by'
  96. ' your authentication app.'
  97. ))
  98. @api.multi
  99. def _create_authenticator(self):
  100. self.env['res.users.authenticator'].create({
  101. 'name': self.name,
  102. 'secret_key': self.secret_key,
  103. 'user_id': self.user_id.id,
  104. })