Skip to content

Commit

Permalink
[CHG] redirect to the oauth_provider using js to allow to keep track …
Browse files Browse the repository at this point in the history
…of the URI fragment to redirect to
  • Loading branch information
benwillig committed May 10, 2023
1 parent dfb4e45 commit ea8dd8e
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 63 deletions.
2 changes: 1 addition & 1 deletion auth_oauth_autologin/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
"maintainers": ["sbidoul"],
"website": "https://github.com/OCA/server-auth",
"depends": ["auth_oauth"],
"data": ["views/auth_oauth_provider.xml"],
"data": ["views/assets.xml", "views/auth_oauth_provider.xml"],
"demo": [],
}
34 changes: 17 additions & 17 deletions auth_oauth_autologin/controllers/main.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
# Copyright 2021 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import werkzeug
from urllib.parse import urlparse, parse_qsl

from odoo import http

from odoo.http import request
from odoo.addons.auth_oauth.controllers.main import OAuthLogin


class OAuthAutoLogin(OAuthLogin):
def _autologin_disabled(self):
def _autologin_disabled(self, redirect):
url = urlparse(redirect)
params = dict(parse_qsl(url.query))
return (
"no_autologin" in http.request.params
or "oauth_error" in http.request.params
or "error" in http.request.params
"no_autologin" in params
or "oauth_error" in params
or "error" in params
)

def _autologin_link(self):
providers = [p for p in self.list_providers() if p.get("autologin")]
if len(providers) == 1:
return providers[0].get("auth_link")

@http.route()
def web_login(self, *args, **kw):
response = super().web_login(*args, **kw)
if not response.is_qweb:
# presumably a redirect already
return response
if self._autologin_disabled():
return response
@http.route(
"/auth/auto_login_redirect_link", type="json", auth="none",
)
def auto_login_redirect_link(self, *args, **kwargs):
redirect = kwargs.get("redirect")
if self._autologin_disabled(redirect):
return False
request.params["redirect"] = redirect
auth_link = self._autologin_link()
if not auth_link:
return response
return werkzeug.utils.redirect(auth_link, 303)
return auth_link
34 changes: 34 additions & 0 deletions auth_oauth_autologin/static/src/js/web_login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
odoo.define('auth_oauth_autologin.redirect', function (require) {
'use strict';

var publicWidget = require('web.public.widget');

publicWidget.registry.authOauthAutologinWidget = publicWidget.Widget.extend({
selector: '.oe_login_form',

/**
* @override
*/
start: function () {
let def = this._super.apply(this, arguments);
let url = window.location.href;
if (url.includes('/web/login')) {
url = url.replace('/web/login', '/web');
}
$.blockUI();
this._rpc({
route: '/auth/auto_login_redirect_link',
params: {
redirect: url,
}
}).then(function (result) {
if (!!result) {
window.location = result;
}
$.unblockUI();
});
return def;
},

});
});
1 change: 0 additions & 1 deletion auth_oauth_autologin/tests/__init__.py

This file was deleted.

44 changes: 0 additions & 44 deletions auth_oauth_autologin/tests/test_auth_oauth_autologin.py

This file was deleted.

15 changes: 15 additions & 0 deletions auth_oauth_autologin/views/assets.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<template
id="assets_frontend"
name="auth_oauth_autologin assets"
inherit_id="web.assets_frontend"
>
<xpath expr="." position="inside">
<script
type="text/javascript"
src="/auth_oauth_autologin/static/src/js/web_login.js"
/>
</xpath>
</template>
</odoo>

0 comments on commit ea8dd8e

Please sign in to comment.