xmpp.chapril.org-conversejs/src/converse-oauth.js

132 lines
4.6 KiB
JavaScript
Raw Normal View History

2020-01-26 16:21:20 +01:00
/**
* @module converse-oauth
* @copyright 2020, the Converse.js contributors
* @license Mozilla Public License (MPLv2)
*/
import { Collection } from "@converse/skeletor/src/collection";
import { View } from '@converse/skeletor/src/view.js';
import { Model } from '@converse/skeletor/src/model.js';
2020-04-23 13:22:31 +02:00
import { converse } from "@converse/headless/converse-core";
2018-10-23 03:41:38 +02:00
import hello from "hellojs";
2020-01-30 12:57:21 +01:00
import tpl_oauth_providers from "templates/oauth_providers.js";
2018-10-23 03:41:38 +02:00
// The following line registers your plugin.
converse.plugins.add("converse-oauth", {
/* Optional dependencies are other plugins which might be
* overridden or relied upon, and therefore need to be loaded before
* this plugin. They are called "optional" because they might not be
* available, in which case any overrides applicable to them will be
* ignored.
*
* NB: These plugins need to have already been loaded via require.js.
*
* It's possible to make optional dependencies non-optional.
* If the setting "strict_plugin_dependencies" is set to true,
* an error will be raised if the plugin is not found.
*/
'optional_dependencies': ['converse-register'],
2019-09-19 16:54:55 +02:00
/* If you want to override some function or a Model or
* View defined elsewhere in converse.js, then you do that under
2018-10-23 03:41:38 +02:00
* the "overrides" namespace.
*/
'overrides': {
/* For example, the private *_converse* object has a
* method "onConnected". You can override that method as follows:
2018-06-12 17:01:10 +02:00
*/
2019-05-24 13:52:15 +02:00
LoginPanel: {
2018-10-23 03:41:38 +02:00
insertOAuthProviders () {
const { _converse } = this.__super__;
2019-07-29 10:19:05 +02:00
if (this.oauth_providers_view === undefined) {
2019-05-24 13:52:15 +02:00
this.oauth_providers_view =
2018-10-23 03:41:38 +02:00
new _converse.OAuthProvidersView({'model': _converse.oauth_providers});
2018-06-12 17:01:10 +02:00
this.oauth_providers_view.render();
2018-10-23 03:41:38 +02:00
this.el.querySelector('.buttons').insertAdjacentElement(
'afterend',
this.oauth_providers_view.el
);
2018-06-12 17:01:10 +02:00
}
2018-10-23 03:41:38 +02:00
this.oauth_providers_view.render();
},
2019-10-09 16:01:38 +02:00
render () {
2018-10-23 03:41:38 +02:00
const { _converse } = this.__super__;
const { api } = _converse;
2018-10-23 03:41:38 +02:00
const result = this.__super__.render.apply(this, arguments);
if (_converse.oauth_providers && !api.settings.get("auto_login")) {
2018-10-23 03:41:38 +02:00
this.insertOAuthProviders();
2018-06-12 17:01:10 +02:00
}
2018-10-23 03:41:38 +02:00
return result;
}
}
},
initialize () {
/* The initialize function gets called as soon as the plugin is
* loaded by converse.js's plugin machinery.
*/
const { _converse } = this;
const { api } = _converse;
const { __ } = _converse;
2018-10-23 03:41:38 +02:00
api.settings.extend({
'oauth_providers': [],
2018-10-23 03:41:38 +02:00
});
2019-09-19 16:54:55 +02:00
_converse.OAuthProviders = Collection.extend({
2019-08-05 11:14:49 +02:00
'sync': function sync () {},
2018-10-23 03:41:38 +02:00
initialize () {
api.settings.get('oauth_providers').forEach(provider => {
2019-09-19 16:54:55 +02:00
const item = new Model(Object.assign(provider, {
2018-10-23 03:41:38 +02:00
'login_text': __('Log in with %1$s', provider.name)
}));
this.add(item, {'silent': true});
});
}
});
_converse.oauth_providers = new _converse.OAuthProviders();
_converse.OAuthProvidersView = View.extend({
2018-10-23 03:41:38 +02:00
toHTML () {
return tpl_oauth_providers(
2019-04-29 09:07:15 +02:00
Object.assign({
2020-01-30 12:57:21 +01:00
'providers': this.model.toJSON(),
'oauthLogin': ev => this.oauthLogin(ev)
2018-10-23 03:41:38 +02:00
}));
},
async fetchOAuthProfileDataAndLogin () {
const profile = await this.oauth_service.api('me');
const response = this.oauth_service.getAuthResponse();
api.user.login(
`${profile.name}@${this.provider.get('host')}`,
response.access_token
);
2018-10-23 03:41:38 +02:00
},
async oauthLogin (ev) {
2018-10-23 03:41:38 +02:00
ev.preventDefault();
const id = ev.target.getAttribute('data-id');
this.provider = _converse.oauth_providers.get(id);
this.oauth_service = hello(id);
const data = {};
data[id] = this.provider.get('client_id');
hello.init(data, {
'redirect_uri': '/redirect.html'
});
await this.oauth_service.login();
this.fetchOAuthProfileDataAndLogin();
2018-10-23 03:41:38 +02:00
}
});
}
});
2018-06-12 17:01:10 +02:00