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.

64 lines
1.8 KiB

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