New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: generic oauth client #11000
feat: generic oauth client #11000
Conversation
@barredterra Can you please explain what problem does this solve? |
@surajshetty3416 Please see the related issue (#9722). There are at least six different implementations of the same thing right now. The goal is to replace these. |
Also, show Token Cache in Dashboard of Connected App.
f0c5575
to
83dcc8a
Compare
Not sure why tests are failing in travis, locally they are working fine. Any Ideas?
(Webserver with multithreading running) |
@barredterra why does travis fail? Edit: sorry did not see your earlier message. Its usually due to the sequence of the tests. Check if there is a commit in a test that may cause this. |
❯ bench new-site test.localhost --mariadb-root-password=`cat /tmp/password` --force
Installing frappe...
Updating DocTypes for frappe : [========================================] 100%
Updating country info : [========================================] 100%
*** Scheduler is disabled ***
Current Site set to test.localhost
❯ bench --site test.localhost set-config allow_tests true
❯ bench run-tests --module frappe.integrations.doctype.connected_app.test_connected_app
Updating Dashboard for frappe
Traceback (most recent call last):
File "/home/revant/.pyenv/versions/3.7.6/lib/python3.7/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/home/revant/.pyenv/versions/3.7.6/lib/python3.7/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/revant/frappe-bench/apps/frappe/frappe/utils/bench_helper.py", line 99, in <module>
main()
File "/home/revant/frappe-bench/apps/frappe/frappe/utils/bench_helper.py", line 18, in main
click.Group(commands=commands)(prog_name='bench')
File "/home/revant/frappe-bench/env/lib/python3.7/site-packages/click/core.py", line 764, in __call__
return self.main(*args, **kwargs)
File "/home/revant/frappe-bench/env/lib/python3.7/site-packages/click/core.py", line 717, in main
rv = self.invoke(ctx)
File "/home/revant/frappe-bench/env/lib/python3.7/site-packages/click/core.py", line 1137, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/home/revant/frappe-bench/env/lib/python3.7/site-packages/click/core.py", line 1137, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/home/revant/frappe-bench/env/lib/python3.7/site-packages/click/core.py", line 956, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/revant/frappe-bench/env/lib/python3.7/site-packages/click/core.py", line 555, in invoke
return callback(*args, **kwargs)
File "/home/revant/frappe-bench/env/lib/python3.7/site-packages/click/decorators.py", line 17, in new_func
return f(get_current_context(), *args, **kwargs)
File "/home/revant/frappe-bench/apps/frappe/frappe/commands/__init__.py", line 26, in _func
ret = f(frappe._dict(ctx.obj), *args, **kwargs)
File "/home/revant/frappe-bench/apps/frappe/frappe/commands/utils.py", line 539, in run_tests
ui_tests=ui_tests, doctype_list_path=doctype_list_path, failfast=failfast)
File "/home/revant/frappe-bench/apps/frappe/frappe/test_runner.py", line 73, in main
ret = run_tests_for_module(module, verbose, tests, profile, junit_xml_output=junit_xml_output)
File "/home/revant/frappe-bench/apps/frappe/frappe/test_runner.py", line 177, in run_tests_for_module
make_test_records(doctype, verbose=verbose)
File "/home/revant/frappe-bench/apps/frappe/frappe/test_runner.py", line 273, in make_test_records
make_test_records_for_doctype(options, verbose, force)
File "/home/revant/frappe-bench/apps/frappe/frappe/test_runner.py", line 321, in make_test_records_for_doctype
frappe.local.test_objects[doctype] += make_test_objects(doctype, test_module.test_records, verbose, force)
File "/home/revant/frappe-bench/apps/frappe/frappe/test_runner.py", line 372, in make_test_objects
d.insert()
File "/home/revant/frappe-bench/apps/frappe/frappe/model/document.py", line 268, in insert
self.run_post_save_methods()
File "/home/revant/frappe-bench/apps/frappe/frappe/model/document.py", line 981, in run_post_save_methods
self.run_method("on_update")
File "/home/revant/frappe-bench/apps/frappe/frappe/model/document.py", line 848, in run_method
out = Document.hook(fn)(self, *args, **kwargs)
File "/home/revant/frappe-bench/apps/frappe/frappe/model/document.py", line 1133, in composer
return composed(self, method, *args, **kwargs)
File "/home/revant/frappe-bench/apps/frappe/frappe/model/document.py", line 1116, in runner
add_to_return_value(self, fn(self, *args, **kwargs))
File "/home/revant/frappe-bench/apps/frappe/frappe/model/document.py", line 842, in <lambda>
fn = lambda self, *args, **kwargs: getattr(self, method)(*args, **kwargs)
File "/home/revant/frappe-bench/apps/frappe/frappe/core/doctype/user/user.py", line 101, in on_update
self.send_password_notification(self.__new_password)
File "/home/revant/frappe-bench/apps/frappe/frappe/core/doctype/user/user.py", line 214, in send_password_notification
self.send_welcome_mail_to_user()
File "/home/revant/frappe-bench/apps/frappe/frappe/core/doctype/user/user.py", line 271, in send_welcome_mail_to_user
link = self.reset_password()
File "/home/revant/frappe-bench/apps/frappe/frappe/core/doctype/user/user.py", line 236, in reset_password
check_password_reset_limit(self.name, rate_limit)
File "/home/revant/frappe-bench/apps/frappe/frappe/core/doctype/user/user.py", line 1133, in check_password_reset_limit
frappe.throw(_("You have reached the hourly limit for generating password reset links. Please try again later."))
File "/home/revant/frappe-bench/apps/frappe/frappe/__init__.py", line 408, in throw
msgprint(msg, raise_exception=exc, title=title, indicator='red', is_minimizable=is_minimizable, wide=wide, as_list=as_list)
File "/home/revant/frappe-bench/apps/frappe/frappe/__init__.py", line 387, in msgprint
_raise_exception()
File "/home/revant/frappe-bench/apps/frappe/frappe/__init__.py", line 341, in _raise_exception
raise raise_exception(msg)
frappe.exceptions.ValidationError: You have reached the hourly limit for generating password reset links. Please try again later. I tried same with ❯ bench run-tests --module frappe.integrations.doctype.connected_app.test_connected_app
Updating Dashboard for frappe
F
======================================================================
FAIL: test_web_application_flow (frappe.integrations.doctype.connected_app.test_connected_app.TestConnectedApp)
Simulate a logged in user who opens the authorization URL.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/revant/frappe-bench/apps/frappe/frappe/integrations/doctype/connected_app/test_connected_app.py", line 63, in test_web_application_flow
self.assertEqual(auth_response.status_code, 200)
AssertionError: 404 != 200
----------------------------------------------------------------------
Ran 1 test in 0.177s
FAILED (failures=1) |
Thanks @revant, i'll try to debug it. |
Still works for me, @revant 😕
Request log:
Tested commit: 689e39a |
I noticed that |
I had
|
still the same error locally ❯ bench drop-site localhost --no-backup --root-password=`cat /tmp/password`
❯ cd apps/frappe
❯ bench new-site localhost --mariadb-root-password=`cat /tmp/password` --force
Installing frappe...
Updating DocTypes for frappe : [========================================] 100%
Updating country info : [========================================] 100%
*** Scheduler is disabled ***
Current Site set to localhost
❯ bench run-tests --module frappe.integrations.doctype.connected_app.test_connected_app
Testing is disabled for the site!
You can enable tests by entering following command:
bench --site localhost set-config allow_tests true
❯ bench --site localhost set-config allow_tests true
❯ bench run-tests --module frappe.integrations.doctype.connected_app.test_connected_app
Updating Dashboard for frappe
E
======================================================================
ERROR: test_web_application_flow (frappe.integrations.doctype.connected_app.test_connected_app.TestConnectedApp)
Simulate a logged in user who opens the authorization URL.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/revant/frappe-bench/apps/frappe/frappe/integrations/doctype/connected_app/test_connected_app.py", line 69, in test_web_application_flow
token = token_cache.get_password('access_token')
File "/home/revant/frappe-bench/apps/frappe/frappe/model/base_document.py", line 757, in get_password
return get_decrypted_password(self.doctype, self.name, fieldname, raise_exception=raise_exception)
File "/home/revant/frappe-bench/apps/frappe/frappe/utils/password.py", line 51, in get_decrypted_password
frappe.throw(_('Password not found'), frappe.AuthenticationError)
File "/home/revant/frappe-bench/apps/frappe/frappe/__init__.py", line 408, in throw
msgprint(msg, raise_exception=exc, title=title, indicator='red', is_minimizable=is_minimizable, wide=wide, as_list=as_list)
File "/home/revant/frappe-bench/apps/frappe/frappe/__init__.py", line 387, in msgprint
_raise_exception()
File "/home/revant/frappe-bench/apps/frappe/frappe/__init__.py", line 341, in _raise_exception
raise raise_exception(msg)
frappe.exceptions.AuthenticationError: Password not found
----------------------------------------------------------------------
Ran 1 test in 2.354s
FAILED (errors=1)
❯ cd apps/frappe
❯ git status
On branch generic_oauth_client
Your branch is up to date with 'alyf-de/generic_oauth_client'.
nothing to commit, working tree clean branch on latest ❯ git status
On branch generic_oauth_client
Your branch is up to date with 'alyf-de/generic_oauth_client'.
nothing to commit, working tree clean
❯ git log
commit 301ed4c7e322fb1de93a197ee3423992d8719435 (HEAD -> generic_oauth_client, alyf-de/generic_oauth_client)
Author: barredterra <14891507+barredterra@users.noreply.github.com>
Date: Tue Dec 22 18:35:26 2020 +0100
fix: rely on frappe.utils.get_url |
I changed the changed [
{
"app_name": "_Test OAuth Client",
"client_secret": "test_client_secret",
"default_redirect_uri": "http://localhost:8000",
"docstatus": 0,
"doctype": "OAuth Client",
"grant_type": "Authorization Code",
"name": "test_client_id",
"redirect_uris": "http://localhost:8000",
"response_type": "Code",
"scopes": "all openid",
"skip_authorization": 1
}
] test worked: ❯ bench run-tests --module frappe.integrations.doctype.connected_app.test_connected_app
test_web_application_flow (frappe.integrations.doctype.connected_app.test_connected_app.TestConnectedApp)
Simulate a logged in user who opens the authorization URL. (3.74s)
.
----------------------------------------------------------------------
Ran 1 test in 3.735s
OK
❯ bench --site localhost reinstall --mariadb-root-password `cat /tmp/password` --yes --admin-password admin
Installing frappe...
Updating DocTypes for frappe : [========================================] 100%
Updating country info : [========================================] 100%
*** Scheduler is disabled ***
❯ bench run-tests --module frappe.integrations.doctype.connected_app.test_connected_app
Updating Dashboard for frappe
test_web_application_flow (frappe.integrations.doctype.connected_app.test_connected_app.TestConnectedApp)
Simulate a logged in user who opens the authorization URL. (3.77s)
.
----------------------------------------------------------------------
Ran 1 test in 3.767s
OK tests worked |
Here's a (hopefully) reproducible example: bench --version # 5.0.0
bench init oauth-test
cd oauth-test/apps/frappe
git remote add origin https://github.com/alyf-de/frappe
git fetch origin
git checkout --track origin/generic_oauth_client
cd ../..
bench new-site oauth-test
bench --site oauth-test set-config developer_mode 1 # needs developer mode because frappe.flags.in_test is not set in web callback.
bench --site oauth-test set-config allow_tests true
bench --site oauth-test add-to-hosts
bench start
bench --site oauth-test run-tests --module frappe.integrations.doctype.connected_app.test_connected_app
# First test-run fails with
# requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',))
# restart bench
bench --site oauth-test run-tests --module frappe.integrations.doctype.connected_app.test_connected_app
# Second test-run works Errors identified:
|
@rmehta tests are passing now :) |
Tested manually. Works as expected. |
Any other changes required, @surajshetty3416? |
Please check #9722 for an explanation.
Based on https://github.com/DigiThinkIT/oauth2_client
Todo
update_token_cache(token_cache, token_data)
Add(Not supported by requests_oauthlib.)base_url
to Connected App to use it later with rauth_session.get('/oauth/profile').#workspace
instead of#desktop
frappe/frappe/utils/oauth.py
Line 309 in e4928e5
access_token
andrefresh_token
should be Password fields.Related Issues
#11038
#11039
#11269
#11982
Documentation: frappe/frappe_docs#67
Close #9722