Skip to content

Instantly share code, notes, and snippets.

@odony
Created March 29, 2023 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odony/84bf508f97d32f8eb3bc1be90eb38068 to your computer and use it in GitHub Desktop.
Save odony/84bf508f97d32f8eb3bc1be90eb38068 to your computer and use it in GitHub Desktop.
l10n_uk.totp.patch
diff --git l10n_uk_reports/models/hmrc_service.py l10n_uk_reports/models/hmrc_service.py
index c1927802e68..e5fe7db0a97 100644
--- l10n_uk_reports/models/hmrc_service.py
+++ l10n_uk_reports/models/hmrc_service.py
@@ -129,7 +129,7 @@ class HmrcService(models.AbstractModel):
if remote_needed: #no need when on a private network
gov_dict['Gov-Client-Public-IP'] = urls.url_quote(remote_address)
gov_dict['Gov-Client-Public-Port'] = urls.url_quote(str(environ.get('REMOTE_PORT')))
- if self.env.user.totp_enabled:
+ if 'totp_enabled' in self.env.user._fields and self.env.user.totp_enabled:
# We can not percent encode the separator, so we have to split the string as such to percent encode each key and value
gov_dict['Gov-Client-Multi-Factor'] = "{}={type}&{}={time}&{}={unique}".format(
urls.url_quote('type'),
@@ -137,7 +137,7 @@ class HmrcService(models.AbstractModel):
urls.url_quote('unique-reference'),
type=urls.url_quote('TOTP'),
time=urls.url_quote(datetime.utcnow().isoformat(timespec='milliseconds')+'Z', unsafe=':'), # We need to specify to percent encode ':'
- unique=urls.url_quote(self.env.user.l10n_uk_hmrc_unique_reference))
+ unique=urls.url_quote(self.env.user._l10n_uk_hmrc_unique_reference()))
gov_dict['Gov-Client-Timezone'] = utc_offset
gov_dict['Gov-Client-Browser-JS-User-Agent'] = (environ.get('HTTP_USER_AGENT'))
gov_dict['Gov-Vendor-Version'] = '&'.join([urls.url_quote("Odoo") + "=" + urls.url_quote(gov_vendor_version)]*2) # We can not percent encode the separator and we need to do it for the key and the value. Client and Server sides are the same
diff --git l10n_uk_reports/models/res_users.py l10n_uk_reports/models/res_users.py
index 342ee551250..827764c71b5 100644
--- l10n_uk_reports/models/res_users.py
+++ l10n_uk_reports/models/res_users.py
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
-from uuid import uuid4
from odoo import fields, models, api
class User(models.Model):
@@ -14,13 +13,14 @@ class User(models.Model):
l10n_uk_hmrc_vat_token_expiration_time = fields.Datetime("Oauth access token expiration time", copy=False, groups='base.group_system',
help="When the access token expires, then it can be refreshed"
"through the Odoo server with the user token. ")
- l10n_uk_hmrc_unique_reference = fields.Char("Unique reference based on TOTP secret", copy=False, compute="_compute_unique_reference")
-
def hmrc_reset_tokens(self):
self.ensure_one()
self.env['hmrc.service'].sudo()._clean_tokens()
return True
- @api.depends('totp_secret')
- def _compute_unique_reference(self):
- self.l10n_uk_hmrc_unique_reference = str(uuid4())
+ def _l10n_uk_hmrc_unique_reference(self):
+ self.ensure_one()
+ # Generates a unique, stable HMAC signature of this sample string
+ # based on the user's credentials. This is guaranteed to change
+ # if any of the user's credentials change (login, totp, password)
+ return self._compute_session_token("l10n_uk_hmrc_unique_reference")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment