@codeo-engineering/licenserail-youtrack 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -56,7 +56,9 @@ and never return the key or the token.
56
56
  - **Activation, trial, refresh, release:** async HTTP (`postAsync`); the answer is stored in app storage and the page
57
57
  shows the result. The license is refreshed daily; network errors keep it working until its offline period ends.
58
58
  - **Users:** the instance's user count is sent with activations and refreshes; the license decides (block, warn or report).
59
- - **Trials:** one per YouTrack instance, reinstalls included; no key ships with the app.
59
+ - **Trials:** one per YouTrack instance, reinstalls included; no key ships with the app. LicenseRail confirms the instance at its
60
+ address first, so it must be reachable from the internet (self-hosted behind a firewall: give a trial key instead). Needs 0.1.2
61
+ or later for trials started from your own code.
60
62
 
61
63
  ## Marketplace listing
62
64
 
@@ -11,6 +11,7 @@
11
11
  "licenserailToken": { "type": "string" },
12
12
  "licenserailLastSeen": { "type": "string" },
13
13
  "licenserailInstallation": { "type": "string" },
14
+ "licenserailBaseUrls": { "type": "string" },
14
15
  "licenserailState": { "type": "string" }
15
16
  },
16
17
  "widget": {
@@ -514,7 +514,15 @@ function trialRequest(o) {
514
514
  method: "POST",
515
515
  url: url(o.apiUrl, "/trial"),
516
516
  headers: headers(o.fingerprint),
517
- body: JSON.stringify(clean({ keyId: o.keyId, product: o.product, fingerprint: o.fingerprint, machineName: o.machineName, appVersion: o.appVersion, hostUsers: o.hostUsers }))
517
+ body: JSON.stringify(clean({
518
+ keyId: o.keyId,
519
+ product: o.product,
520
+ fingerprint: o.fingerprint,
521
+ machineName: o.machineName,
522
+ appVersion: o.appVersion,
523
+ hostUsers: o.hostUsers,
524
+ host: o.host
525
+ }))
518
526
  };
519
527
  }
520
528
  var REVOKING_CODES = [
@@ -618,9 +626,16 @@ function createLicenseClient(o) {
618
626
  floating
619
627
  })));
620
628
  },
621
- /** Starts (or gets back) this device's or installation's keyless trial. */
622
- async startTrial(keyId) {
623
- return await unidentified() ?? accept(await send(trialRequest({ ...target, keyId, machineName: o.machineName, appVersion: o.appVersion, hostUsers: o.hostUsers?.() })));
629
+ /** Starts (or gets back) this device's or installation's keyless trial; apps inside a host pass the installation. */
630
+ async startTrial(keyId, host) {
631
+ return await unidentified() ?? accept(await send(trialRequest({
632
+ ...target,
633
+ keyId,
634
+ machineName: o.machineName,
635
+ appVersion: o.appVersion,
636
+ hostUsers: o.hostUsers?.(),
637
+ host
638
+ })));
624
639
  },
625
640
  /** Refreshes when the token asks for it (or always with force); keeps the token on network errors. */
626
641
  async refresh(force = false) {
@@ -26,8 +26,8 @@ function body(ctx) {
26
26
  }
27
27
 
28
28
  function remember(ctx) {
29
- const id = body(ctx).installationId;
30
- if (id) licenserail.setInstallation(ctx, id);
29
+ const b = body(ctx);
30
+ if (b.installationId) licenserail.setInstallation(ctx, b.installationId, b.baseUrls);
31
31
  }
32
32
 
33
33
  exports.httpHandler = {
@@ -60,7 +60,7 @@ exports.httpHandler = {
60
60
  handle: function (ctx) {
61
61
  guard(ctx, function () {
62
62
  remember(ctx);
63
- licenserail.startTrial(ctx);
63
+ licenserail.startTrial(ctx, body(ctx).baseUrls);
64
64
  ctx.response.statusCode = 202;
65
65
  ctx.response.json(licenserail.status(ctx));
66
66
  });
@@ -18,6 +18,7 @@ const PROPERTY = {
18
18
  token: 'licenserailToken',
19
19
  lastSeen: 'licenserailLastSeen',
20
20
  installation: 'licenserailInstallation',
21
+ baseUrls: 'licenserailBaseUrls',
21
22
  state: 'licenserailState',
22
23
  };
23
24
  const PLATFORM = 'youtrack';
@@ -105,10 +106,21 @@ function hasFeature(ctx, code) {
105
106
  return license.valid && license.summary.entitlements.indexOf(code) >= 0;
106
107
  }
107
108
 
108
- /** Remembers this YouTrack instance's id (read by the admin page from /api/config). */
109
- function setInstallation(ctx, id) {
109
+ /** Addresses like https://acme.youtrack.cloud (scheme and host only), at most three. */
110
+ function cleanBaseUrls(baseUrls) {
111
+ return (Array.isArray(baseUrls) ? baseUrls : [])
112
+ .filter(function (u) { return typeof u === 'string' && /^https?:\/\/[^\s/]+$/.test(u); }).slice(0, 3);
113
+ }
114
+
115
+ /**
116
+ * Remembers this YouTrack instance's id (read by the admin page from /api/config) and the addresses the page was opened from,
117
+ * which a trial started later from the app's own code uses.
118
+ */
119
+ function setInstallation(ctx, id, baseUrls) {
110
120
  if (typeof id !== 'string' || !/^[A-Za-z0-9-]{8,64}$/.test(id)) throw new Error('Invalid installation id.');
111
121
  if (props(ctx)[PROPERTY.installation] !== id) props(ctx)[PROPERTY.installation] = id;
122
+ const urls = cleanBaseUrls(baseUrls);
123
+ if (urls.length && props(ctx)[PROPERTY.baseUrls] !== urls.join(' ')) props(ctx)[PROPERTY.baseUrls] = urls.join(' ');
112
124
  }
113
125
 
114
126
  function common(ctx) {
@@ -122,11 +134,19 @@ function activate(ctx) {
122
134
  send(ctx, core.activateRequest(Object.assign(t, common(ctx))), RESPONSE, true);
123
135
  }
124
136
 
125
- function startTrial(ctx) {
137
+ /**
138
+ * Starts the keyless trial. LicenseRail asks this YouTrack instance for its id at its address before the trial starts, so the
139
+ * instance must be reachable from the internet. Without baseUrls the addresses remembered from the License page are used.
140
+ */
141
+ function startTrial(ctx, baseUrls) {
126
142
  const t = target(ctx);
127
143
  if (!t.fingerprint) throw new Error('Open the license page once so the app knows this YouTrack instance.');
144
+ let urls = cleanBaseUrls(baseUrls);
145
+ if (!urls.length) urls = cleanBaseUrls(String(props(ctx)[PROPERTY.baseUrls] || '').split(' '));
128
146
  writeState(ctx, { pending: 'trial', lastAttemptAt: new Date().toISOString() });
129
- send(ctx, core.trialRequest(Object.assign(t, common(ctx), { keyId: config.keyId })), RESPONSE, false);
147
+ send(ctx, core.trialRequest(Object.assign(t, common(ctx), {
148
+ keyId: config.keyId, host: { instanceId: installationId(ctx), baseUrls: urls },
149
+ })), RESPONSE, false);
130
150
  }
131
151
 
132
152
  function refresh(ctx) {
@@ -19,7 +19,7 @@
19
19
  </section>
20
20
  <section class="card" id="trial-card" hidden>
21
21
  <h2>Trial</h2>
22
- <p>Try the app on this YouTrack instance without a key. One trial per instance.</p>
22
+ <p>Try the app on this YouTrack instance without a key. One trial per instance. The instance must be reachable from the internet; otherwise ask the vendor for a trial key.</p>
23
23
  <div class="actions"><button id="trial">Start trial</button></div>
24
24
  </section>
25
25
  <p id="message" class="message" role="status"></p>
@@ -12,8 +12,23 @@ try {
12
12
  installationId = null
13
13
  }
14
14
 
15
+ /** Where this YouTrack is reached, as the browser sees it (the page runs inside it); LicenseRail checks the instance there. */
16
+ function baseUrls() {
17
+ const seen = []
18
+ const add = (value) => {
19
+ try {
20
+ const origin = new URL(value).origin
21
+ if (/^https?:/.test(origin) && !seen.includes(origin)) seen.push(origin)
22
+ } catch { /* not a URL */ }
23
+ }
24
+ if (window.location.ancestorOrigins?.length) add(window.location.ancestorOrigins[window.location.ancestorOrigins.length - 1])
25
+ if (document.referrer) add(document.referrer)
26
+ add(window.location.href)
27
+ return seen.slice(0, 3)
28
+ }
29
+
15
30
  async function call(path, method = 'GET') {
16
- return host.fetchApp(`${HANDLER}/${path}`, method === 'GET' ? { method } : { method, body: { installationId } })
31
+ return host.fetchApp(`${HANDLER}/${path}`, method === 'GET' ? { method } : { method, body: { installationId, baseUrls: baseUrls() } })
17
32
  }
18
33
 
19
34
  const LABELS = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codeo-engineering/licenserail-youtrack",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "LicenseRail for YouTrack apps: license keys, keyless trials and per-user limits for apps sold outside JetBrains Marketplace billing. Adds readable backend modules and a License admin page to your app.",
5
5
  "license": "MIT",
6
6
  "publishConfig": { "access": "public" },