@magpie-community/opencode-factory-auth 0.1.10 → 0.1.11
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 +12 -3
- package/index.mjs +72 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -29,9 +29,18 @@ The sign-in is kept wherever the host keeps provider sign-ins:
|
|
|
29
29
|
It holds WorkOS's access and refresh tokens, with the organization,
|
|
30
30
|
region and host.
|
|
31
31
|
|
|
32
|
-
**Refreshing.**
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
**Refreshing.**
|
|
33
|
+
- magpie renews the access token three minutes before it lapses, through
|
|
34
|
+
the plugin's `auth.refresh`, once for the account and before its
|
|
35
|
+
requests and usage need it; magpie saves the new tokens. OpenCode
|
|
36
|
+
doesn't call that hook: there the token is renewed two minutes before it
|
|
37
|
+
lapses, before a request, and the plugin saves it.
|
|
38
|
+
- WorkOS rotates the refresh token, so the plugin never runs two refreshes
|
|
39
|
+
at once, and never spends one refresh token twice: a request that comes
|
|
40
|
+
before magpie has saved a renewal goes on with the new tokens.
|
|
41
|
+
- The account counts as signed out only when WorkOS refuses the refresh
|
|
42
|
+
token (a 4xx other than 429). Any other failure keeps the sign-in, and
|
|
43
|
+
the current token is used while it lasts.
|
|
35
44
|
|
|
36
45
|
**Refusals.** If Factory refuses the organization a request names, the
|
|
37
46
|
plugin asks `whoami` again and resends the request once. If the refusal
|
package/index.mjs
CHANGED
|
@@ -16,6 +16,10 @@ const CLIENT_ID = "client_01HNM792M5G5G1A2THWPXKFMXB"
|
|
|
16
16
|
const VERSION = "0.231.0"
|
|
17
17
|
// how long before an access token lapses it is renewed (droid: a minute)
|
|
18
18
|
const REFRESH_LEAD = 2 * 60 * 1000
|
|
19
|
+
// and how long before, magpie renews it ahead of time (auth.refresh): a
|
|
20
|
+
// minute more, so under magpie the check before a request finds it fresh.
|
|
21
|
+
// WorkOS's access tokens are short-lived, so not much more
|
|
22
|
+
const MAGPIE_LEAD = 3 * 60 * 1000
|
|
19
23
|
// how long an account whose whoami failed waits before asking again
|
|
20
24
|
const ASK_AGAIN = 10 * 60 * 1000
|
|
21
25
|
|
|
@@ -757,6 +761,24 @@ export const FactoryAuthPlugin = async ({ client }) => {
|
|
|
757
761
|
lock = run.catch(() => {})
|
|
758
762
|
return run
|
|
759
763
|
}
|
|
764
|
+
// renewed is what each refresh this process ran gave, by the refresh
|
|
765
|
+
// token it spent: a sign-in still holding that one (magpie not yet
|
|
766
|
+
// saving what auth.refresh gave, or a save that failed) goes on with
|
|
767
|
+
// the new tokens rather than spending the old one again
|
|
768
|
+
const renewed = new Map()
|
|
769
|
+
const took = (spent, c) => {
|
|
770
|
+
for (const [k, v] of renewed) if (v.expires > 0 && Date.now() >= v.expires) renewed.delete(k)
|
|
771
|
+
renewed.set(spent, { access: c.access, refresh: c.refresh, expires: c.expires, orgId: c.orgId, activeOrganizationId: c.activeOrganizationId, region: c.region, premBaseHost: c.premBaseHost })
|
|
772
|
+
}
|
|
773
|
+
// latest is c with the newest tokens this process got for it
|
|
774
|
+
const latest = (c) => {
|
|
775
|
+
for (let i = 0; i < 16 && c.refresh; i++) {
|
|
776
|
+
const got = renewed.get(c.refresh)
|
|
777
|
+
if (!got || got.access === c.access || (c.expires > 0 && got.expires > 0 && got.expires <= c.expires)) break
|
|
778
|
+
c = { ...c, ...got }
|
|
779
|
+
}
|
|
780
|
+
return c
|
|
781
|
+
}
|
|
760
782
|
// when each account last asked whoami for its org, so one whoami can't
|
|
761
783
|
// answer doesn't ask before every request, nor hold back the others
|
|
762
784
|
const asked = new Map()
|
|
@@ -819,16 +841,17 @@ export const FactoryAuthPlugin = async ({ client }) => {
|
|
|
819
841
|
// it was, the built-in's clearing of the account's lapse.
|
|
820
842
|
const fresh = (renewed) =>
|
|
821
843
|
locked(async () => {
|
|
822
|
-
const c = await current()
|
|
844
|
+
const c = latest(await current())
|
|
823
845
|
if (c.key) return keyed(c) // an API key: nothing to renew, and no org with it
|
|
824
846
|
if ((c.expires > 0 && Date.now() < c.expires - REFRESH_LEAD) || !c.refresh) return orgOf(c)
|
|
847
|
+
const spent = c.refresh
|
|
825
848
|
let t
|
|
826
849
|
try {
|
|
827
|
-
t = await renew(
|
|
850
|
+
t = await renew(spent, "")
|
|
828
851
|
} catch (e) {
|
|
829
852
|
// a hiccup while the token still runs: go on with it
|
|
830
853
|
if (!refused(e) && c.expires > 0 && Date.now() < c.expires) return orgOf(c)
|
|
831
|
-
if (refused(e)) throw
|
|
854
|
+
if (refused(e)) throw lapsed(c, e)
|
|
832
855
|
throw e
|
|
833
856
|
}
|
|
834
857
|
c.access = t.access_token
|
|
@@ -837,6 +860,7 @@ export const FactoryAuthPlugin = async ({ client }) => {
|
|
|
837
860
|
// droid asks whoami again for each new token, keeping the org it names
|
|
838
861
|
await reconcile(c)
|
|
839
862
|
ask(c)
|
|
863
|
+
took(spent, c)
|
|
840
864
|
await save(c)
|
|
841
865
|
renewed?.()
|
|
842
866
|
return c
|
|
@@ -852,7 +876,7 @@ export const FactoryAuthPlugin = async ({ client }) => {
|
|
|
852
876
|
locked(async () => {
|
|
853
877
|
if (status !== 403) return false
|
|
854
878
|
const isOrg = orgRefused(status, text)
|
|
855
|
-
const c = await current()
|
|
879
|
+
const c = latest(await current())
|
|
856
880
|
if (c.key) return false // an API key carries no org droid would send
|
|
857
881
|
if (c.activeOrganizationId) {
|
|
858
882
|
if (!isOrg) return false // the org was sent: the refusal is about something else
|
|
@@ -877,9 +901,10 @@ export const FactoryAuthPlugin = async ({ client }) => {
|
|
|
877
901
|
org = await firstOrg(c)
|
|
878
902
|
} catch {}
|
|
879
903
|
if (!org) return false
|
|
904
|
+
const spent = c.refresh
|
|
880
905
|
let t
|
|
881
906
|
try {
|
|
882
|
-
t = await renew(
|
|
907
|
+
t = await renew(spent, org)
|
|
883
908
|
} catch {
|
|
884
909
|
return false
|
|
885
910
|
}
|
|
@@ -887,6 +912,7 @@ export const FactoryAuthPlugin = async ({ client }) => {
|
|
|
887
912
|
c.expires = expiry(t.access_token)
|
|
888
913
|
c.orgId = org
|
|
889
914
|
if (t.refresh_token) c.refresh = t.refresh_token
|
|
915
|
+
took(spent, c)
|
|
890
916
|
await save(c)
|
|
891
917
|
renewed?.()
|
|
892
918
|
return true
|
|
@@ -928,6 +954,18 @@ export const FactoryAuthPlugin = async ({ client }) => {
|
|
|
928
954
|
return { fresh, mendOrg, usage }
|
|
929
955
|
}
|
|
930
956
|
|
|
957
|
+
// lapsed is WorkOS refusing c's refresh token for good: the account
|
|
958
|
+
// needs signing in again
|
|
959
|
+
const lapsed = (c, e) =>
|
|
960
|
+
Object.assign(new Error(`${c.accountId || "the account"}'s Factory sign-in has expired — sign in again (${e.message})`), { lapsed: true, signIn: "expired" })
|
|
961
|
+
|
|
962
|
+
// renewal is what of c magpie keeps over the stored sign-in
|
|
963
|
+
const renewal = (c) => {
|
|
964
|
+
const out = { access: c.access, refresh: c.refresh, expires: c.expires }
|
|
965
|
+
for (const k of ["orgId", "activeOrganizationId", "region", "premBaseHost"]) if (typeof c[k] === "string") out[k] = c[k]
|
|
966
|
+
return out
|
|
967
|
+
}
|
|
968
|
+
|
|
931
969
|
return {
|
|
932
970
|
config: async (config) => {
|
|
933
971
|
config.provider ??= {}
|
|
@@ -951,6 +989,35 @@ export const FactoryAuthPlugin = async ({ client }) => {
|
|
|
951
989
|
// droid's FACTORY_API_KEY: the key is the bearer, never renewed
|
|
952
990
|
{ type: "api", label: "Factory API key (fk-…)", placeholder: "fk-…" },
|
|
953
991
|
],
|
|
992
|
+
// magpie renews the sign-in MAGPIE_LEAD before its end, before its
|
|
993
|
+
// requests and usage ask for it, and saves what this gives. Under
|
|
994
|
+
// the same lock as the renewal before a request, so the two never
|
|
995
|
+
// spend one refresh token; that check stays for OpenCode, which
|
|
996
|
+
// doesn't call this
|
|
997
|
+
refreshLead: MAGPIE_LEAD,
|
|
998
|
+
async refresh(auth) {
|
|
999
|
+
if (auth?.type !== "oauth" || !auth.access || !auth.refresh) return undefined
|
|
1000
|
+
return locked(async () => {
|
|
1001
|
+
const c = latest({ ...auth })
|
|
1002
|
+
// renewed here already, the store not yet saying so
|
|
1003
|
+
if (c.access !== auth.access && c.expires > 0 && Date.now() < c.expires - MAGPIE_LEAD) return renewal(c)
|
|
1004
|
+
const spent = c.refresh
|
|
1005
|
+
let t
|
|
1006
|
+
try {
|
|
1007
|
+
t = await renew(spent, "")
|
|
1008
|
+
} catch (e) {
|
|
1009
|
+
if (refused(e)) throw lapsed(c, e)
|
|
1010
|
+
throw e
|
|
1011
|
+
}
|
|
1012
|
+
c.access = t.access_token
|
|
1013
|
+
c.expires = expiry(t.access_token)
|
|
1014
|
+
if (t.refresh_token) c.refresh = t.refresh_token
|
|
1015
|
+
await reconcile(c)
|
|
1016
|
+
ask(c)
|
|
1017
|
+
took(spent, c)
|
|
1018
|
+
return renewal(c)
|
|
1019
|
+
})
|
|
1020
|
+
},
|
|
954
1021
|
// magpie's own hook: the account's limits
|
|
955
1022
|
async usage(getAuth) {
|
|
956
1023
|
return account(getAuth).usage()
|