oauth.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * @module converse-oauth
  3. * @copyright 2022, the Converse.js contributors
  4. * @license Mozilla Public License (MPLv2)
  5. */
  6. import { Collection } from "@converse/skeletor/src/collection";
  7. import { View } from '@converse/skeletor/src/view.js';
  8. import { Model } from '@converse/skeletor/src/model.js';
  9. import { converse } from "@converse/headless/core";
  10. import hello from "hellojs";
  11. import tpl_oauth_providers from "../templates/oauth_providers.js";
  12. // The following line registers your plugin.
  13. converse.plugins.add("converse-oauth", {
  14. /* Optional dependencies are other plugins which might be
  15. * overridden or relied upon, and therefore need to be loaded before
  16. * this plugin. They are called "optional" because they might not be
  17. * available, in which case any overrides applicable to them will be
  18. * ignored.
  19. *
  20. * NB: These plugins need to have already been loaded via require.js.
  21. *
  22. * It's possible to make optional dependencies non-optional.
  23. * If the setting "strict_plugin_dependencies" is set to true,
  24. * an error will be raised if the plugin is not found.
  25. */
  26. 'optional_dependencies': ['converse-register'],
  27. /* If you want to override some function or a Model or
  28. * View defined elsewhere in converse.js, then you do that under
  29. * the "overrides" namespace.
  30. */
  31. 'overrides': {
  32. /* For example, the private *_converse* object has a
  33. * method "onConnected". You can override that method as follows:
  34. */
  35. LoginPanel: {
  36. insertOAuthProviders () {
  37. const { _converse } = this.__super__;
  38. if (this.oauth_providers_view === undefined) {
  39. this.oauth_providers_view =
  40. new _converse.OAuthProvidersView({'model': _converse.oauth_providers});
  41. this.oauth_providers_view.render();
  42. this.el.querySelector('.buttons').insertAdjacentElement(
  43. 'afterend',
  44. this.oauth_providers_view.el
  45. );
  46. }
  47. this.oauth_providers_view.render();
  48. },
  49. render () {
  50. const { _converse } = this.__super__;
  51. const { api } = _converse;
  52. const result = this.__super__.render.apply(this, arguments);
  53. if (_converse.oauth_providers && !api.settings.get("auto_login")) {
  54. this.insertOAuthProviders();
  55. }
  56. return result;
  57. }
  58. }
  59. },
  60. initialize () {
  61. /* The initialize function gets called as soon as the plugin is
  62. * loaded by converse.js's plugin machinery.
  63. */
  64. const { _converse } = this;
  65. const { api } = _converse;
  66. const { __ } = _converse;
  67. api.settings.extend({
  68. 'oauth_providers': [],
  69. });
  70. _converse.OAuthProviders = Collection.extend({
  71. 'sync': function sync () {},
  72. initialize () {
  73. api.settings.get('oauth_providers').forEach(provider => {
  74. const item = new Model(Object.assign(provider, {
  75. 'login_text': __('Log in with %1$s', provider.name)
  76. }));
  77. this.add(item, {'silent': true});
  78. });
  79. }
  80. });
  81. _converse.oauth_providers = new _converse.OAuthProviders();
  82. _converse.OAuthProvidersView = View.extend({
  83. toHTML () {
  84. return tpl_oauth_providers(
  85. Object.assign({
  86. 'providers': this.model.toJSON(),
  87. 'oauthLogin': ev => this.oauthLogin(ev)
  88. }));
  89. },
  90. async fetchOAuthProfileDataAndLogin () {
  91. const profile = await this.oauth_service.api('me');
  92. const response = this.oauth_service.getAuthResponse();
  93. api.user.login(
  94. `${profile.name}@${this.provider.get('host')}`,
  95. response.access_token
  96. );
  97. },
  98. async oauthLogin (ev) {
  99. ev.preventDefault();
  100. const id = ev.target.getAttribute('data-id');
  101. this.provider = _converse.oauth_providers.get(id);
  102. this.oauth_service = hello(id);
  103. const data = {};
  104. data[id] = this.provider.get('client_id');
  105. hello.init(data, {
  106. 'redirect_uri': '/redirect.html'
  107. });
  108. await this.oauth_service.login();
  109. this.fetchOAuthProfileDataAndLogin();
  110. }
  111. });
  112. }
  113. });