snappymail/plugins/login-o365/LoginOAuth2.js

53 lines
1.6 KiB
JavaScript
Raw Normal View History

2024-07-01 17:21:02 +08:00
(rl => {
2024-07-01 18:09:49 +08:00
const client_id = rl.pluginSettingsGet('login-o365', 'client_id'),
2024-07-01 21:41:33 +08:00
tenant = rl.pluginSettingsGet('login-o365', 'tenant'),
2024-07-01 17:21:02 +08:00
login = () => {
2024-07-01 21:41:33 +08:00
document.location = 'https://login.microsoftonline.com/'+tenant+'/oauth2/v2.0/authorize?' + (new URLSearchParams({
2024-07-01 17:21:02 +08:00
response_type: 'code',
client_id: client_id,
redirect_uri: document.location.href.replace(/\/$/, '') + '/LoginO365',
2024-07-01 17:21:02 +08:00
scope: [
// Associate personal info
'openid',
'offline_access',
'email',
'profile',
// Access IMAP and SMTP through OAUTH
'https://graph.microsoft.com/IMAP.AccessAsUser.All',
2024-07-01 18:10:37 +08:00
// 'https://graph.microsoft.com/Mail.ReadWrite'
2024-07-01 17:21:02 +08:00
'https://graph.microsoft.com/Mail.Send'
/* // Legacy:
'https://outlook.office.com/SMTP.Send',
'https://outlook.office.com/IMAP.AccessAsUser.All'
*/
].join(' '),
2024-07-01 18:09:49 +08:00
state: 'o365', // + rl.settings.app('token') + localStorage.getItem('smctoken')
2024-07-01 17:21:02 +08:00
// Force authorize screen, so we always get a refresh_token
access_type: 'offline',
prompt: 'consent'
}));
};
if (client_id) {
addEventListener('sm-user-login', e => {
2024-07-01 18:09:49 +08:00
if (event.detail.get('Email').includes('@hotmail.com')) {
2024-07-01 17:21:02 +08:00
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);