|
1 | 1 | import { Resolver } from 'varlock/plugin-lib'; |
2 | 2 |
|
3 | 3 | import { createDeferredPromise, DeferredPromise } from '@env-spec/utils/defer'; |
4 | | -import { Client, createClient } from '@1password/sdk'; |
| 4 | +import { Client, createClient, DesktopAuth } from '@1password/sdk'; |
5 | 5 | import { opCliRead } from './cli-helper'; |
6 | 6 |
|
7 | 7 | const { ValidationError, SchemaError, ResolutionError } = plugin.ERRORS; |
@@ -39,43 +39,47 @@ class OpPluginInstance { |
39 | 39 |
|
40 | 40 | opClientPromise: Promise<Client> | undefined; |
41 | 41 | async initSdkClient() { |
42 | | - if (!this.token) return; |
43 | | - if (this.opClientPromise) return; |
| 42 | + if (this.opClientPromise) return this.opClientPromise; |
44 | 43 |
|
45 | | - // TODO: pass through account once SDK allows it |
46 | | - this.opClientPromise = createClient({ |
47 | | - auth: this.token, |
48 | | - integrationName: 'varlock plugin', |
49 | | - integrationVersion: PLUGIN_VERSION, |
50 | | - }); |
| 44 | + if (this.token) { |
| 45 | + // TODO: pass through account once SDK allows it |
| 46 | + this.opClientPromise = createClient({ |
| 47 | + auth: this.token, |
| 48 | + integrationName: 'varlock plugin', |
| 49 | + integrationVersion: PLUGIN_VERSION, |
| 50 | + }); |
| 51 | + } else { |
| 52 | + if (!this.account) throw new Error('account must be set to use desktop auth'); |
| 53 | + this.opClientPromise = createClient({ |
| 54 | + auth: new DesktopAuth(this.account), |
| 55 | + integrationName: 'varlock plugin', |
| 56 | + integrationVersion: PLUGIN_VERSION, |
| 57 | + }); |
| 58 | + } |
| 59 | + return this.opClientPromise; |
51 | 60 | } |
52 | 61 |
|
53 | 62 | readBatch?: Record<string, { defers: Array<DeferredPromise<string>> }> | undefined; |
54 | 63 |
|
55 | 64 | async readItem(opReference: string) { |
56 | | - if (this.token) { |
| 65 | + if (this.token || this.allowAppAuth) { |
57 | 66 | // using JS SDK client using service account token |
58 | 67 | await this.initSdkClient(); |
59 | | - if (this.opClientPromise) { |
60 | | - // simple batching setup, so we can use bulk read sdk method |
61 | | - let triggerBatch = false; |
62 | | - if (!this.readBatch) { |
63 | | - this.readBatch = {}; |
64 | | - triggerBatch = true; |
65 | | - } |
66 | | - // add item to batch, with deferred promise |
67 | | - this.readBatch[opReference] = { defers: [] }; |
68 | | - const deferred = createDeferredPromise(); |
69 | | - this.readBatch[opReference].defers.push(deferred); |
70 | | - if (triggerBatch) { |
71 | | - setImmediate(() => this.executeReadBatch()); |
72 | | - } |
73 | | - return deferred.promise; |
| 68 | + |
| 69 | + // simple batching setup, so we can use bulk read sdk method |
| 70 | + let triggerBatch = false; |
| 71 | + if (!this.readBatch) { |
| 72 | + this.readBatch = {}; |
| 73 | + triggerBatch = true; |
| 74 | + } |
| 75 | + // add item to batch, with deferred promise |
| 76 | + this.readBatch[opReference] = { defers: [] }; |
| 77 | + const deferred = createDeferredPromise<string>(); |
| 78 | + this.readBatch[opReference].defers.push(deferred); |
| 79 | + if (triggerBatch) { |
| 80 | + setImmediate(() => this.executeReadBatch()); |
74 | 81 | } |
75 | | - } else if (this.allowAppAuth) { |
76 | | - // using op CLI to talk to 1Password desktop app |
77 | | - // NOTE - cli helper does its own batching, untethered to a specific op instance |
78 | | - return await opCliRead(opReference, this.account); |
| 82 | + return deferred.promise; |
79 | 83 | } else { |
80 | 84 | throw new SchemaError('Unable to authenticate with 1Password', { |
81 | 85 | tip: `Plugin instance (${this.id}) must be provided either a service account token or have app auth enabled (allowAppAuth=true)`, |
|
0 commit comments