Odoo Help

0
2 Answers
1
Hideyuki Otokawa
3/15/19, 12:39 AM

I've also tried to use odoo with keycloak and met with same thing. After some investigation  I've made a small patch for auth_oauth. In my environemnt, in which the docker conatiner of official image runs, its working..

These are the necessary settings.


Making OAuth provider of keycloak as below;

  •  client id should be specified the one assinged for client in keycloak realm

  •  keycloak checkbox should be marked; it's the new one which I added.

  • auth URL should be specified the endpoint of keycloak ended with 'auth'

  • validation URL should be spefied the endpoint of keycloak ended with 'userinfo

  • scope and data URL are blank.

Next, settings for user accounts by which you'd like to sign-in with keycloak

  • OAtuth provider should be selected the one which you made for keycloak

  • OAuth user id shoud be input email address of the keycloak user accout

Finally, necessary setting of keycloak

  • Creating the mapping from 'email' to 'user_id' in the client mapper.

If everything is okay, then you can sign-in through keycloak as me..

Sorry. I did not put my patch. Please apply this.

diff -c -r -x '*.po' ./models/auth_oauth.py /home/s111403/work/auth_oauth/models/auth_oauth.py
*** ./models/auth_oauth.py2019-03-15 00:52:47.824000000 +0000
--- /home/s111403/work/auth_oauth/models/auth_oauth.py2019-03-14 14:22:21.964000000 +0000
***************
*** 18,23 ****
--- 18,24 ----
      validation_endpoint = fields.Char(string='Validation URL', required=True)  # OAuth provider URL to validate tokens
      data_endpoint = fields.Char(string='Data URL')
      enabled = fields.Boolean(string='Allowed')
+     keycloak = fields.Boolean(string='Keycloak')
      css_class = fields.Char(string='CSS class', default='fa fa-fw fa-sign-in text-primary')
      body = fields.Char(required=True)
      sequence = fields.Integer()
diff -c -r -x '*.po' ./models/res_users.py /home/s111403/work/auth_oauth/models/res_users.py
*** ./models/res_users.py2019-03-15 00:52:47.824000000 +0000
--- /home/s111403/work/auth_oauth/models/res_users.py2019-03-14 15:07:20.572000000 +0000
***************
*** 24,41 ****
      ]
      @api.model
!     def _auth_oauth_rpc(self, endpoint, access_token):
!         return requests.get(endpoint, params={'access_token': access_token}).json()
      @api.model
      def _auth_oauth_validate(self, provider, access_token):
          """ return the validation data corresponding to the access token """
          oauth_provider = self.env['auth.oauth.provider'].browse(provider)
!         validation = self._auth_oauth_rpc(oauth_provider.validation_endpoint, access_token)
          if validation.get("error"):
              raise Exception(validation['error'])
          if oauth_provider.data_endpoint:
!             data = self._auth_oauth_rpc(oauth_provider.data_endpoint, access_token)
              validation.update(data)
          return validation
--- 27,48 ----
      ]
      @api.model
!     def _auth_oauth_rpc(self, endpoint, access_token, provider):
!         oauth_provider = self.env['auth.oauth.provider'].browse(provider)
!         if oauth_provider.keycloak:
!           return requests.get(endpoint, headers={'Authorization': 'bearer ' + access_token}).json()
!         else:
!           return requests.get(endpoint, params={'access_token': access_token}).json()
      @api.model
      def _auth_oauth_validate(self, provider, access_token):
          """ return the validation data corresponding to the access token """
          oauth_provider = self.env['auth.oauth.provider'].browse(provider)
!         validation = self._auth_oauth_rpc(oauth_provider.validation_endpoint, access_token, provider)
          if validation.get("error"):
              raise Exception(validation['error'])
          if oauth_provider.data_endpoint:
!             data = self._auth_oauth_rpc(oauth_provider.data_endpoint, access_token, provider)
              validation.update(data)
          return validation
diff -c -r -x '*.po' ./views/auth_oauth_views.xml /home/s111403/work/auth_oauth/views/auth_oauth_views.xml
*** ./views/auth_oauth_views.xml2019-03-15 00:52:47.824000000 +0000
--- /home/s111403/work/auth_oauth/views/auth_oauth_views.xml2019-03-14 14:28:01.508000000 +0000
***************
*** 10,15 ****
--- 10,16 ----
                              <field name="name" />
                              <field name="client_id" />
                              <field name="enabled" />
+                             <field name="keycloak" />
                              <field name="body" />
                          </group>
                          <group>
***************
*** 30,35 ****
--- 31,37 ----
                      <field name="name" />
                      <field name="client_id" />
                      <field name="enabled" />
+                     <field name="keycloak" />
                  </tree>
              </field>
          </record>


0
Karim Gillani
3/15/19, 2:09 AM

i tried what you suggested but I still get access denied.   To confirm, the keycloak settings:

  1. Edit email mapper

  2. update Token Claim Name: user_id

Also, there is no place to put the endpoint "token"?  What settings do you have on keycloak client settings page?

The error is much less but still occurs..

Traceback (most recent call last):

  File "/usr/lib/python3/dist-packages/odoo/addons/auth_oauth/controllers/main.py", line 142, in signin

    credentials = env['res.users'].sudo().auth_oauth(provider, kw)

  File "/usr/lib/python3/dist-packages/odoo/addons/auth_oauth/models/res_users.py", line 96, in auth_oauth

    validation = self._auth_oauth_validate(provider, access_token)

  File "/usr/lib/python3/dist-packages/odoo/addons/auth_oauth/models/res_users.py", line 36, in _auth_oauth_validate

    raise Exception(validation['error'])

Exception: invalid_request

2019-03-15 07:25:38,722 1 INFO db werkzeug: 17x.xx.xx.xx - - [15/Mar/2019 07:25:38] "GET /auth_oauth/signin?state={%22r%22%3A+%22https%253A%252F%252Fxxx.xxx.xx.xx.xx%252Fweb%22%2C+%22p%22%3A+7%2C+%22d%22%3A+%22db%22}&session_state=9e09a4d7-5e1f-4acf-b120-42ed5fc7e200&access_token=eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICI1NXhiZzcyaml6NWQ3cEtkbzdwNFhLdTB2VFA2UXMtemthZzFaY1&token_type=bearer&expires_in=900&not-before-policy=1548956992 HTTP/1.1" 303 - 2 0.002 5.115

 Thank you for your help in this..


9 Comments
Hideyuki Otokawa
3/15/19, 7:31 AM

You do not have to put the endpoint "token".

This is my keycloak client settings;

- access type is confidential

- standard flow is on

- implicit flow is on

Karim Gillani
3/15/19, 9:47 AM
Where did you put the client secret?

Regards

On Mar 15, 2019, at 4:32 AM, Hideyuki Otokawa <otokawa.hideyuki@sharp.co.jp> wrote:

You do not have to put the endpoint "token".

This is my keycloak client settings;

- access type is confidential

- standard flow is on

- implicit flow is on

Sent by Odoo S.A. using Odoo.

Hideyuki Otokawa
3/15/19, 10:44 AM

Without using secret, you can sign-in by keycloak.

Karim Gillani
3/15/19, 10:55 AM

Is there any other mapping I need to do, like name? I am unsure why mine is still not working albeit closer than before.

Again, thank you for your assistance.

Hideyuki Otokawa
3/15/19, 11:25 AM

No. In my investigation of the source codes, the only necessary mapping is email to 'user_id'.

Karim Gillani
3/17/19, 6:10 PM

I realized I didn't do the patch. Now that I do, I get this error, any suggestions?

Odoo Server Error

Traceback (most recent call last):

File "/usr/lib/python3/dist-packages/odoo/models.py", line 1108, in _validate_fields

check(self)

File "/usr/lib/python3/dist-packages/odoo/addons/base/models/ir_ui_view.py", line 351, in _check_xml

self.postprocess_and_fields(view.model, view_doc, view.id)

File "/usr/lib/python3/dist-packages/odoo/addons/base/models/ir_ui_view.py", line 1095, in postprocess_and_fields

self.raise_view_error(message, view_id)

File "/usr/lib/python3/dist-packages/odoo/addons/base/models/ir_ui_view.py", line 548, in raise_view_error

raise ValueError(message)

ValueError: Field `keycloak` does not exist

Error context:

View `auth.oauth.provider.form`

[view_id: 547, xml_id: n/a, model: auth.oauth.provider, parent_id: n/a]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "/usr/lib/python3/dist-packages/odoo/tools/convert.py", line 757, in parse

self._tags[rec.tag](rec, de, mode=mode)

File "/usr/lib/python3/dist-packages/odoo/tools/convert.py", line 662, in _tag_record

record = model.with_context(rec_context)._load_records([data], self.mode == 'update')

File "/usr/lib/python3/dist-packages/odoo/models.py", line 3821, in _load_records

records = self._load_records_create([data['values'] for data in to_create])

File "/usr/lib/python3/dist-packages/odoo/models.py", line 3735, in _load_records_create

return self.create(values)

File "<decorator-gen-32>", line 2, in create

File "/usr/lib/python3/dist-packages/odoo/api.py", line 452, in _model_create_multi

return create(self, arg)

File "/usr/lib/python3/dist-packages/odoo/addons/base/models/ir_ui_view.py", line 428, in create

return super(View, self).create(vals_list)

File "<decorator-gen-3>", line 2, in create

File "/usr/lib/python3/dist-packages/odoo/api.py", line 452, in _model_create_multi

return create(self, arg)

File "/usr/lib/python3/dist-packages/odoo/models.py", line 3552, in create

fields[0].determine_inverse(batch_recs)

File "/usr/lib/python3/dist-packages/odoo/fields.py", line 1097, in determine_inverse

getattr(records, self.inverse)()

File "/usr/lib/python3/dist-packages/odoo/addons/base/models/ir_ui_view.py", line 275, in _inverse_arch

view.write(data)

File "/usr/lib/python3/dist-packages/odoo/addons/base/models/ir_ui_view.py", line 444, in write

return super(View, self).write(self._compute_defaults(vals))

File "/usr/lib/python3/dist-packages/odoo/models.py", line 3269, in write

self._write(store_vals)

File "/usr/lib/python3/dist-packages/odoo/models.py", line 3413, in _write

self._validate_fields(vals)

File "/usr/lib/python3/dist-packages/odoo/models.py", line 1112, in _validate_fields

raise ValidationError("%s\n\n%s" % (_("Error while validating constraint"), tools.ustr(e)))

odoo.exceptions.ValidationError: ('Error while validating constraint\n\nField `keycloak` does not exist\n\nError context:\nView `auth.oauth.provider.form`\n[view_id: 547, xml_id: n/a, model: auth.oauth.provider, parent_id: n/a]', None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "/usr/lib/python3/dist-packages/odoo/http.py", line 654, in _handle_exception

return super(JsonRequest, self)._handle_exception(exception)

File "/usr/lib/python3/dist-packages/odoo/http.py", line 312, in _handle_exception

raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])

File "/usr/lib/python3/dist-packages/odoo/tools/pycompat.py", line 87, in reraise

raise value

File "/usr/lib/python3/dist-packages/odoo/http.py", line 696, in dispatch

result = self._call_function(**self.params)

File "/usr/lib/python3/dist-packages/odoo/http.py", line 344, in _call_function

return checked_call(self.db, *args, **kwargs)

File "/usr/lib/python3/dist-packages/odoo/service/model.py", line 97, in wrapper

return f(dbname, *args, **kwargs)

File "/usr/lib/python3/dist-packages/odoo/http.py", line 337, in checked_call

result = self.endpoint(*a, **kw)

File "/usr/lib/python3/dist-packages/odoo/http.py", line 939, in __call__

return self.method(*args, **kw)

File "/usr/lib/python3/dist-packages/odoo/http.py", line 517, in response_wrap

response = f(*args, **kw)

File "/usr/lib/python3/dist-packages/odoo/addons/web/controllers/main.py", line 966, in call_button

action = self._call_kw(model, method, args, {})

File "/usr/lib/python3/dist-packages/odoo/addons/web/controllers/main.py", line 954, in _call_kw

return call_kw(request.env[model], method, args, kwargs)

File "/usr/lib/python3/dist-packages/odoo/api.py", line 749, in call_kw

return _call_kw_multi(method, model, args, kwargs)

File "/usr/lib/python3/dist-packages/odoo/api.py", line 736, in _call_kw_multi

result = method(recs, *args, **kwargs)

File "/usr/lib/python3/dist-packages/odoo/addons/base/models/res_config.py", line 637, in execute

self._install_modules(to_install)

File "/usr/lib/python3/dist-packages/odoo/addons/base/models/res_config.py", line 34, in _install_modules

result = to_install_modules.button_immediate_install()

File "<decorator-gen-61>", line 2, in button_immediate_install

File "/usr/lib/python3/dist-packages/odoo/addons/base/models/ir_module.py", line 73, in check_and_log

return method(self, *args, **kwargs)

File "/usr/lib/python3/dist-packages/odoo/addons/base/models/ir_module.py", line 444, in button_immediate_install

return self._button_immediate_function(type(self).button_install)

File "/usr/lib/python3/dist-packages/odoo/addons/base/models/ir_module.py", line 553, in _button_immediate_function

modules.registry.Registry.new(self._cr.dbname, update_module=True)

File "/usr/lib/python3/dist-packages/odoo/modules/registry.py", line 86, in new

odoo.modules.load_modules(registry._db, force_demo, status, update_module)

File "/usr/lib/python3/dist-packages/odoo/modules/loading.py", line 421, in load_modules

loaded_modules, update_module, models_to_check)

File "/usr/lib/python3/dist-packages/odoo/modules/loading.py", line 313, in load_marked_modules

perform_checks=perform_checks, models_to_check=models_to_check

File "/usr/lib/python3/dist-packages/odoo/modules/loading.py", line 222, in load_module_graph

load_data(cr, idref, mode, kind='data', package=package, report=report)

File "/usr/lib/python3/dist-packages/odoo/modules/loading.py", line 68, in load_data

tools.convert_file(cr, package.name, filename, idref, mode, noupdate, kind, report)

File "/usr/lib/python3/dist-packages/odoo/tools/convert.py", line 801, in convert_file

convert_xml_import(cr, module, fp, idref, mode, noupdate, report)

File "/usr/lib/python3/dist-packages/odoo/tools/convert.py", line 864, in convert_xml_import

obj.parse(doc.getroot(), mode=mode)

File "/usr/lib/python3/dist-packages/odoo/tools/convert.py", line 763, in parse

exc_info[2]

File "/usr/lib/python3/dist-packages/odoo/tools/pycompat.py", line 86, in reraise

raise value.with_traceback(tb)

File "/usr/lib/python3/dist-packages/odoo/tools/convert.py", line 757, in parse

self._tags[rec.tag](rec, de, mode=mode)

File "/usr/lib/python3/dist-packages/odoo/tools/convert.py", line 662, in _tag_record

record = model.with_context(rec_context)._load_records([data], self.mode == 'update')

File "/usr/lib/python3/dist-packages/odoo/models.py", line 3821, in _load_records

records = self._load_records_create([data['values'] for data in to_create])

File "/usr/lib/python3/dist-packages/odoo/models.py", line 3735, in _load_records_create

return self.create(values)

File "<decorator-gen-32>", line 2, in create

File "/usr/lib/python3/dist-packages/odoo/api.py", line 452, in _model_create_multi

return create(self, arg)

File "/usr/lib/python3/dist-packages/odoo/addons/base/models/ir_ui_view.py", line 428, in create

return super(View, self).create(vals_list)

File "<decorator-gen-3>", line 2, in create

File "/usr/lib/python3/dist-packages/odoo/api.py", line 452, in _model_create_multi

return create(self, arg)

File "/usr/lib/python3/dist-packages/odoo/models.py", line 3552, in create

fields[0].determine_inverse(batch_recs)

File "/usr/lib/python3/dist-packages/odoo/fields.py", line 1097, in determine_inverse

getattr(records, self.inverse)()

File "/usr/lib/python3/dist-packages/odoo/addons/base/models/ir_ui_view.py", line 275, in _inverse_arch

view.write(data)

File "/usr/lib/python3/dist-packages/odoo/addons/base/models/ir_ui_view.py", line 444, in write

return super(View, self).write(self._compute_defaults(vals))

File "/usr/lib/python3/dist-packages/odoo/models.py", line 3269, in write

self._write(store_vals)

File "/usr/lib/python3/dist-packages/odoo/models.py", line 3413, in _write

self._validate_fields(vals)

File "/usr/lib/python3/dist-packages/odoo/models.py", line 1112, in _validate_fields

raise ValidationError("%s\n\n%s" % (_("Error while validating constraint"), tools.ustr(e)))

odoo.tools.convert.ParseError: "Error while validating constraint

Field `keycloak` does not exist

Error context:

View `auth.oauth.provider.form`

[view_id: 547, xml_id: n/a, model: auth.oauth.provider, parent_id: n/a]

None" while parsing /mnt/extra-addons/auth_oauth/views/auth_oauth_views.xml:3, near

<record id="view_oauth_provider_form" model="ir.ui.view">

<field name="name">auth.oauth.provider.form</field>

<field name="model">auth.oauth.provider</field>

<field name="arch" type="xml">

<form string="arch">

<sheet>

<group>

<field name="name"/>

<field name="client_id"/>

<field name="enabled"/>

<field name="keycloak"/>

<field name="body"/>

</group>

<group>

<field name="auth_endpoint"/>

<field name="scope"/>

<field name="validation_endpoint"/>

<field name="data_endpoint"/>

</group>

</sheet>

</form>

</field>

</record>

Karim Gillani
3/17/19, 6:32 PM

A bit further but now I get this error:

odoo.tools.convert.ParseError: "<class 'ValueError'>: "External ID not found in the system: auth_oauth.provider_openerp" while evaluating

"[ref('auth_oauth.provider_openerp')]"" while parsing /mnt/extra-addons/keycloak_oauth/data/auth_oauth_data.xml:34, near

<function model="auth.oauth.provider" name="write">

<value eval="[ref('auth_oauth.provider_openerp')]"/>

<value model="ir.config_parameter" eval="{ 'client_id': obj().env['ir.config_parameter'].get_param('database.uuid'), }"/>

</function>

Karim Gillani
3/17/19, 7:05 PM

Ok, I figured it out. I needed to delete the old Oauth and refresh the list then restart. And all is working now.

For others, I ended up copying the auth_oauth folder, updating with the patch mentioned above and placing in /mnt/extra-addons.

Hideyuki Otokawa thank you so much!!!

Ask a Question
About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

Register
Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now