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.

54 lines
1.6 KiB

  1. from odoo import models, fields, api
  2. from .. import random_tokens
  3. class AccessTokenBase(models.AbstractModel):
  4. _name = "galicea_openid_connect.access_token_base"
  5. token = fields.Char(
  6. readonly=True,
  7. required=True,
  8. default=lambda _: random_tokens.alpha_numeric(64),
  9. index=True,
  10. )
  11. client_id = fields.Many2one(
  12. "galicea_openid_connect.client",
  13. readonly=True,
  14. index=True,
  15. required=True,
  16. ondelete="cascade",
  17. )
  18. class AccessToken(models.Model):
  19. _inherit = "galicea_openid_connect.access_token_base"
  20. _name = "galicea_openid_connect.access_token"
  21. _description = "Acccess token representing user-client pair"
  22. user_id = fields.Many2one(
  23. "res.users", required=True, readonly=True, index=True, ondelete="cascade"
  24. )
  25. @api.model
  26. def retrieve_or_create(self, user_id, client_id):
  27. existing_tokens = self.search(
  28. [("user_id", "=", user_id), ("client_id", "=", client_id),]
  29. )
  30. if existing_tokens:
  31. return existing_tokens[0]
  32. else:
  33. return self.create({"user_id": user_id, "client_id": client_id})
  34. class ClientAccessToken(models.Model):
  35. _inherit = "galicea_openid_connect.access_token_base"
  36. _name = "galicea_openid_connect.client_access_token"
  37. _description = "Access token representing client credentials"
  38. @api.model
  39. def retrieve_or_create(self, client_id):
  40. existing_tokens = self.search([("client_id", "=", client_id),])
  41. if existing_tokens:
  42. return existing_tokens[0]
  43. else:
  44. return self.create({"client_id": client_id})