UIWindowRequestPermission.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * Copyright (C) 2024 Puter Technologies Inc.
  3. *
  4. * This file is part of Puter.
  5. *
  6. * Puter is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published
  8. * by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. import UIWindow from './UIWindow.js'
  20. async function UIWindowRequestPermission(options){
  21. options = options ?? {};
  22. options.reload_on_success = options.reload_on_success ?? false;
  23. return new Promise(async (resolve) => {
  24. let drivers = [
  25. {
  26. name: 'puter-chat-completion',
  27. human_name: 'AI Chat Completion',
  28. description: 'This app wants to generate text using AI. This may incur costs on your behalf.',
  29. },
  30. {
  31. name: 'puter-image-generation',
  32. human_name: 'AI Image Generation',
  33. description: 'This app wants to generate images using AI. This may incur costs on your behalf.',
  34. },
  35. {
  36. name: 'puter-kvstore',
  37. human_name: 'Puter Storage',
  38. description: 'This app wants to securely store data in your Puter account. This app will not be able to access your personal data or data stored by other apps.',
  39. }
  40. ]
  41. let parts = options.permission.split(":");
  42. let driver_name = parts[1];
  43. let action_name = parts[2];
  44. function findDriverByName(driverName) {
  45. return drivers.find(driver => driver.name === driverName);
  46. }
  47. let driver = findDriverByName(driver_name);
  48. if(driver === undefined){
  49. resolve(false);
  50. return;
  51. }
  52. let h = ``;
  53. h += `<div>`;
  54. h += `<div style="padding: 20px; width: 100%; box-sizing: border-box;">`;
  55. // title
  56. h += `<h1 class="perm-title">"<span style="word-break: break-word;">${html_encode(options.app_uid ?? options.origin)}</span>" would Like to use ${html_encode(driver.human_name)}</h1>`;
  57. // todo show the real description of action
  58. h += `<p class="perm-description">${html_encode(driver.description)}</p>`;
  59. // Allow/Don't Allow
  60. h += `<button type="button" class="app-auth-allow button button-primary button-block" style="margin-top: 10px;">Allow</button>`;
  61. h += `<button type="button" class="app-auth-dont-allow button button-default button-block" style="margin-top: 10px;">Don't Allow</button>`;
  62. h += `</div>`;
  63. h += `</div>`;
  64. const el_window = await UIWindow({
  65. title: null,
  66. app: 'request-authorization',
  67. single_instance: true,
  68. icon: null,
  69. uid: null,
  70. is_dir: false,
  71. body_content: h,
  72. has_head: true,
  73. selectable_body: false,
  74. draggable_body: true,
  75. allow_context_menu: false,
  76. is_draggable: true,
  77. is_droppable: false,
  78. is_resizable: false,
  79. stay_on_top: false,
  80. allow_native_ctxmenu: true,
  81. allow_user_select: true,
  82. ...options.window_options,
  83. width: 350,
  84. dominant: true,
  85. on_close: ()=>{
  86. resolve(false)
  87. },
  88. onAppend: function(this_window){
  89. },
  90. window_class: 'window-login',
  91. window_css:{
  92. height: 'initial',
  93. },
  94. body_css: {
  95. width: 'initial',
  96. padding: '0',
  97. 'background-color': 'rgba(231, 238, 245, .95)',
  98. 'backdrop-filter': 'blur(3px)',
  99. }
  100. })
  101. $(el_window).find('.app-auth-allow').on('click', async function(e){
  102. $(this).addClass('disabled');
  103. try{
  104. const res = await fetch( window.api_origin + "/auth/grant-user-app", {
  105. "headers": {
  106. "Content-Type": "application/json",
  107. "Authorization": "Bearer " + window.auth_token,
  108. },
  109. "body": JSON.stringify({
  110. app_uid: options.app_uid,
  111. origin: options.origin,
  112. permission: options.permission
  113. }),
  114. "method": "POST",
  115. });
  116. }catch(err){
  117. console.error(err);
  118. resolve(err);
  119. }
  120. resolve(true);
  121. })
  122. $(el_window).find('.app-auth-dont-allow').on('click', function(e){
  123. $(this).addClass('disabled');
  124. $(el_window).close();
  125. resolve(false);
  126. })
  127. })
  128. }
  129. export default UIWindowRequestPermission