UI for Zipcoin Blue

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const tslib_1 = require("tslib");
  4. const chalk_1 = require("chalk");
  5. const guards_1 = require("../guards");
  6. const errors_1 = require("./errors");
  7. const http_1 = require("./http");
  8. class BaseSession {
  9. constructor(config, project, client) {
  10. this.config = config;
  11. this.project = project;
  12. this.client = client;
  13. }
  14. isLoggedIn() {
  15. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  16. const c = yield this.config.load();
  17. return typeof c.tokens.user === 'string';
  18. });
  19. }
  20. logout() {
  21. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  22. const c = yield this.config.load();
  23. c.user = {};
  24. c.tokens.appUser = {};
  25. delete c.tokens.user;
  26. c.git.setup = false;
  27. });
  28. }
  29. getUser() {
  30. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  31. throw new errors_1.FatalException('Invalid operation for Cloud session.');
  32. });
  33. }
  34. getUserToken() {
  35. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  36. const c = yield this.config.load();
  37. if (!c.tokens.user) {
  38. throw new errors_1.SessionException(`Oops, sorry! You'll need to log in:\n ${chalk_1.default.green('ionic login')}\n\n` +
  39. `You can create a new account by signing up:\n\n ${chalk_1.default.green('ionic signup')}\n`);
  40. }
  41. return c.tokens.user;
  42. });
  43. }
  44. }
  45. exports.BaseSession = BaseSession;
  46. class CloudSession extends BaseSession {
  47. login(email, password) {
  48. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  49. const { req } = yield this.client.make('POST', '/login');
  50. req.send({ email, password });
  51. try {
  52. const res = yield this.client.do(req);
  53. if (!guards_1.isLegacyLoginResponse(res)) {
  54. throw http_1.createFatalAPIFormat(req, res);
  55. }
  56. const { token, user_id } = res.data;
  57. const c = yield this.config.load();
  58. if (c.user.id !== Number(user_id)) {
  59. yield this.logout();
  60. }
  61. c.user.id = Number(user_id);
  62. c.user.email = email;
  63. c.tokens.user = token;
  64. }
  65. catch (e) {
  66. if (guards_1.isSuperAgentError(e) && e.response.status === 401) {
  67. throw new errors_1.SessionException('Incorrect email or password.');
  68. }
  69. throw e;
  70. }
  71. });
  72. }
  73. getAppUserToken(app_id) {
  74. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  75. if (!app_id) {
  76. app_id = yield this.project.loadAppId();
  77. }
  78. const c = yield this.config.load();
  79. if (!c.tokens.appUser[app_id]) {
  80. const token = yield this.getUserToken();
  81. const paginator = yield this.client.paginate({
  82. reqgen: () => tslib_1.__awaiter(this, void 0, void 0, function* () {
  83. const { req } = yield this.client.make('GET', '/auth/tokens');
  84. req.set('Authorization', `Bearer ${token}`).query({ 'page_size': 100, type: 'app-user' });
  85. return { req };
  86. }),
  87. guard: guards_1.isAuthTokensResponse,
  88. });
  89. for (let r of paginator) {
  90. const res = yield r;
  91. for (let token of res.data) {
  92. c.tokens.appUser[token.details.app_id] = token.token;
  93. }
  94. }
  95. }
  96. // TODO: if this is a new app, an app-user token may not exist for the user
  97. // TODO: if tokens are invalidated, what do (hint: app tokens)
  98. if (!c.tokens.appUser[app_id]) {
  99. throw new errors_1.SessionException(`A token does not exist for your account on app ${chalk_1.default.bold(app_id)}.`);
  100. }
  101. return c.tokens.appUser[app_id];
  102. });
  103. }
  104. }
  105. exports.CloudSession = CloudSession;
  106. class ProSession extends BaseSession {
  107. login(email, password) {
  108. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  109. const { req } = yield this.client.make('POST', '/login');
  110. req.send({ email, password, source: 'cli' });
  111. try {
  112. const res = yield this.client.do(req);
  113. if (!guards_1.isProLoginResponse(res)) {
  114. throw http_1.createFatalAPIFormat(req, res);
  115. }
  116. const { token, user } = res.data;
  117. const c = yield this.config.load();
  118. const user_id = user.id;
  119. if (c.user.id !== user_id) {
  120. yield this.logout();
  121. }
  122. c.user.id = user_id;
  123. c.user.email = email;
  124. c.tokens.user = token;
  125. }
  126. catch (e) {
  127. if (guards_1.isSuperAgentError(e) && e.response.status === 401) {
  128. throw new errors_1.SessionException('Incorrect email or password.');
  129. }
  130. throw e;
  131. }
  132. });
  133. }
  134. getUser() {
  135. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  136. const c = yield this.config.load();
  137. if (!c.user.id) {
  138. throw new errors_1.SessionException(`Oops, sorry! You'll need to log in:\n ${chalk_1.default.green('ionic login')}\n\n` +
  139. `You can create a new account by signing up:\n\n ${chalk_1.default.green('ionic signup')}\n`);
  140. }
  141. return { id: Number(c.user.id) };
  142. });
  143. }
  144. getAppUserToken(app_id) {
  145. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  146. return this.getUserToken();
  147. });
  148. }
  149. }
  150. exports.ProSession = ProSession;
  151. function promptToLogin(env) {
  152. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  153. const { validators } = yield Promise.resolve().then(() => require('@ionic/cli-framework/lib'));
  154. env.log.msg(`Log into your Ionic account\nIf you don't have one yet, create yours by running: ${chalk_1.default.green(`ionic signup`)}\n`);
  155. const email = yield env.prompt({
  156. type: 'input',
  157. name: 'email',
  158. message: 'Email:',
  159. validate: v => validators.email(v),
  160. });
  161. const password = yield env.prompt({
  162. type: 'password',
  163. name: 'password',
  164. message: 'Password:'
  165. });
  166. yield env.session.login(email, password);
  167. });
  168. }
  169. exports.promptToLogin = promptToLogin;