Corrected OAuth2 login flow and added better error handling

This commit is contained in:
Kristofer Nilsson 2025-12-19 11:46:35 +01:00
parent ba496919dd
commit b483f2ee76
2 changed files with 348 additions and 270 deletions

View file

@ -1,54 +1,51 @@
(rl => {
const client_id = rl.pluginSettingsGet('login-o365', 'client_id'),
// https://learn.microsoft.com/en-us/entra/identity-platform/reply-url#query-parameter-support-in-redirect-uris
query = rl.pluginSettingsGet('login-o365', 'personal') ? '' : '?',
tenant = rl.pluginSettingsGet('login-o365', 'tenant'),
login = () => {
document.location = 'https://login.microsoftonline.com/'+tenant+'/oauth2/v2.0/authorize?' + (new URLSearchParams({
response_type: 'code',
client_id: client_id,
redirect_uri: document.location.href.replace(/\/$/, '') + '/' + query + 'LoginO365',
scope: [
// Associate personal info
'openid',
'offline_access',
'email',
'profile',
// Access IMAP and SMTP through OAUTH
'https://graph.microsoft.com/IMAP.AccessAsUser.All',
// 'https://graph.microsoft.com/Mail.ReadWrite'
'https://graph.microsoft.com/Mail.Send'
/* // Legacy:
'https://outlook.office.com/SMTP.Send',
'https://outlook.office.com/IMAP.AccessAsUser.All'
*/
].join(' '),
state: 'o365', // + rl.settings.app('token') + localStorage.getItem('smctoken')
// Force authorize screen, so we always get a refresh_token
access_type: 'offline',
prompt: 'consent'
}));
};
((rl) => {
const client_id = rl.pluginSettingsGet("login-o365", "client_id"),
// https://learn.microsoft.com/en-us/entra/identity-platform/reply-url#query-parameter-support-in-redirect-uris
tenant = rl.pluginSettingsGet("login-o365", "tenant"),
login = () => {
document.location = "https://login.microsoftonline.com/" +
tenant +
"/oauth2/v2.0/authorize?" +
new URLSearchParams({
response_type: "code",
client_id: client_id,
redirect_uri:
document.location.href.replace(/\/$/, "") + "/LoginO365",
scope: [
// Associate personal info
"openid",
"offline_access",
"email",
"profile",
// Access IMAP and SMTP through OAUTH
"https://outlook.office.com/IMAP.AccessAsUser.All",
"https://outlook.office.com/SMTP.Send",
].join(" "),
state: "o365",
access_type: "offline_access"
// prompt: "consent",
});
};
if (client_id) {
addEventListener('sm-user-login', e => {
if (event.detail.get('Email').includes('@hotmail.com')) {
e.preventDefault();
login();
}
});
if (client_id) {
addEventListener("sm-user-login", (e) => {
const email = (e.detail.get("Email") || "").toLowerCase();
if (/@(outlook\.com|hotmail\.com|live\.com)$/.test(email)) {
e.preventDefault();
login();
}
});
addEventListener('rl-view-model', e => {
if ('Login' === e.detail.viewModelTemplateID) {
const
container = e.detail.viewModelDom.querySelector('#plugin-Login-BottomControlGroup'),
btn = Element.fromHTML('<button type="button">Outlook</button>'),
div = Element.fromHTML('<div class="controls"></div>');
btn.onclick = login;
div.append(btn);
container && container.append(div);
}
});
}
})(window.rl);
addEventListener("rl-view-model", (e) => {
if ("Login" === e.detail.viewModelTemplateID) {
const
container = e.detail.viewModelDom.querySelector("#plugin-Login-BottomControlGroup"),
btn = Element.fromHTML('<button type="button">Outlook</button>'),
div = Element.fromHTML('<div class="controls"></div>');
btn.onclick = login;
div.append(btn);
container && container.append(div);
}
});
}
})(window.rl);