Skip to content
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

[16.0][FIX] auth_saml: update signin method #667

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 55 additions & 51 deletions auth_saml/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@
from werkzeug.exceptions import BadRequest
from werkzeug.urls import url_quote_plus

import odoo
from odoo import SUPERUSER_ID, _, api, http, models, registry as registry_get
from odoo import (
SUPERUSER_ID,
_,
api,
exceptions,
http,
models,
registry as registry_get,
)
from odoo.http import request
from odoo.tools.misc import clean_context

from odoo.addons.web.controllers.home import Home, ensure_db
from odoo.addons.web.controllers.utils import _get_login_redirect_url
from odoo.addons.web.controllers.home import Home
from odoo.addons.web.controllers.utils import _get_login_redirect_url, ensure_db

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -207,54 +215,50 @@
dbname = state["d"]
if not http.db_filter([dbname]):
return BadRequest()
context = state.get("c", {})
registry = registry_get(dbname)

with registry.cursor() as cr:
try:
env = api.Environment(cr, SUPERUSER_ID, context)
credentials = (
env["res.users"]
.sudo()
.auth_saml(
provider,
saml_response,
request.httprequest.url_root.rstrip("/"),
)
ensure_db(db=dbname)

request.update_context(**clean_context(state.get("c", {})))
try:
credentials = (
request.env["res.users"]
.with_user(SUPERUSER_ID)
.auth_saml(
provider,
saml_response,
request.httprequest.url_root.rstrip("/"),
)
action = state.get("a")
menu = state.get("m")
redirect = (
werkzeug.urls.url_unquote_plus(state["r"])
if state.get("r")
else False
)
url = "/"
if redirect:
url = redirect
elif action:
url = "/#action=%s" % action
elif menu:
url = "/#menu_id=%s" % menu
pre_uid = request.session.authenticate(*credentials)
resp = request.redirect(_get_login_redirect_url(pre_uid, url), 303)
resp.autocorrect_location_header = False
return resp

except odoo.exceptions.AccessDenied:
# saml credentials not valid,
# user could be on a temporary session
_logger.info("SAML2: access denied")

url = "/web/login?saml_error=expired"
redirect = werkzeug.utils.redirect(url, 303)
redirect.autocorrect_location_header = False
return redirect

except Exception as e:
# signup error
_logger.exception("SAML2: failure - %s", str(e))
url = "/web/login?saml_error=access-denied"
)
action = state.get("a")
menu = state.get("m")
redirect = (
werkzeug.urls.url_unquote_plus(state["r"]) if state.get("r") else False
)
url = "/web"
if redirect:
url = redirect
elif action:
url = "/#action=%s" % action

Check warning on line 240 in auth_saml/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

auth_saml/controllers/main.py#L240

Added line #L240 was not covered by tests
elif menu:
url = "/#menu_id=%s" % menu

Check warning on line 242 in auth_saml/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

auth_saml/controllers/main.py#L242

Added line #L242 was not covered by tests
pre_uid = request.session.authenticate(*credentials)
resp = request.redirect(_get_login_redirect_url(pre_uid, url), 303)
resp.autocorrect_location_header = False
return resp

except exceptions.AccessDenied:
# saml credentials not valid,
# user could be on a temporary session
_logger.info("SAML2: access denied")

Check warning on line 251 in auth_saml/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

auth_saml/controllers/main.py#L251

Added line #L251 was not covered by tests

url = "/web/login?saml_error=expired"
redirect = werkzeug.utils.redirect(url, 303)
redirect.autocorrect_location_header = False
return redirect

Check warning on line 256 in auth_saml/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

auth_saml/controllers/main.py#L253-L256

Added lines #L253 - L256 were not covered by tests

except Exception as e:

Check warning on line 258 in auth_saml/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

auth_saml/controllers/main.py#L258

Added line #L258 was not covered by tests
# signup error
_logger.exception("SAML2: failure - %s", str(e))
url = "/web/login?saml_error=access-denied"

Check warning on line 261 in auth_saml/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

auth_saml/controllers/main.py#L260-L261

Added lines #L260 - L261 were not covered by tests

redirect = request.redirect(url, 303)
redirect.autocorrect_location_header = False
Expand Down
1 change: 0 additions & 1 deletion auth_saml/tests/test_pysaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,6 @@ def test_redirect_after_login(self):
timeout=300,
)
self.assertTrue(response.ok)
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.url,
self.base_url()
Expand Down
Loading