-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathauth.ts
177 lines (159 loc) · 5.26 KB
/
auth.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import {
getCookie,
setCookie,
} from "https://tfl.dev/@truffle/utils@~0.0.2/cookie/cookie.ts";
import isSsr from "https://tfl.dev/@truffle/utils@~0.0.22/ssr/is-ssr.ts";
import jumper from "https://tfl.dev/@truffle/utils@~0.0.3/jumper/jumper.ts";
import { signal } from "https://tfl.dev/@truffle/state@~0.0.12/signals/signal.ts";
import { _clearCache } from "./client.ts";
import { TRUFFLE_ACCESS_TOKEN_KEY } from "./auth-exchange.ts";
import { default as globalContext } from "https://tfl.dev/@truffle/global-context@^1.0.0/index.ts";
import {
getPackageContext,
setPackageContext,
} from "https://tfl.dev/@truffle/global-context@^1.0.0/package-context.ts";
const ACCESS_TOKEN_COOKIE = "accessToken";
const JUMPER_ACCESS_TOKEN_TIMEOUT_MS = 1000;
if (!isSsr) {
jumper.call(
"user.onAccessTokenChange",
{ orgId: getOrgId() },
({ accessToken }) => {
setAccessTokenCookie(accessToken);
_clearCache();
Object.values(getOnAccessTokenChangeListeners()).forEach((listener) =>
listener?.(accessToken)
);
},
);
// TODO: legacy, rm 4/2023
jumper.call("comms.onMessage", (message: string) => {
getAccessToken().then(getAccessToken$().set);
if (message === "user.accessTokenUpdated") {
_clearCache();
}
});
// end legacy
}
export function onAccessTokenChange(
callback: (accessToken: string) => unknown,
): { unsubscribe: () => void } {
const id = `${Date.now()}${Math.random()}`;
getOnAccessTokenChangeListeners()[id] = callback;
return {
unsubscribe: () => {
delete getOnAccessTokenChangeListeners()[id];
},
};
}
export async function getAccessToken(): Promise<string> {
// we need to rely on jumper first bc we can't trust that third party cookies will be set.
// for ssr this is a little trickier... ssr obj can't use jumper. so for ssr we
let accessTokenFromJumper = isSsr ? "" : // we'll eventually have different accessTokens per orgId
await Promise.race([
jumper.call("user.getAccessToken", { orgId: getOrgId() }),
new Promise((resolve) =>
setTimeout(resolve, JUMPER_ACCESS_TOKEN_TIMEOUT_MS, "")
),
]);
// TODO: legacy, rm 4/2023
if (!accessTokenFromJumper && !isSsr) {
accessTokenFromJumper = await Promise.race([
jumper.call("storage.get", { key: TRUFFLE_ACCESS_TOKEN_KEY }),
new Promise((resolve) =>
setTimeout(resolve, JUMPER_ACCESS_TOKEN_TIMEOUT_MS, "")
),
]);
}
// somewhere and some point in time, some of these were set to str "undefined"
if (accessTokenFromJumper === "undefined") {
setAccessToken("");
}
// end legacy
return accessTokenFromJumper || getCookie(ACCESS_TOKEN_COOKIE);
}
// TODO: simplify process of get/set from context
export function getOnAccessTokenChangeListeners() {
const context = getPackageContext("@truffle/api@0");
if (!context.onAccessTokenChangeListeners) {
setPackageContext("@truffle/api@0", {
...context,
onAccessTokenChangeListeners: {},
});
}
return context.onAccessTokenChangeListeners;
}
export function getAccessToken$() {
const context = getPackageContext("@truffle/api@0");
if (!context.accessToken$) {
const accessToken$ = signal("");
// accessToken$.set may be called elsewhere before getAccessToken resolves here.
// so we need to make sure we're not replacing a valid accessToken with undef
getAccessToken().then((accessToken) => {
if (accessToken) accessToken$.set(accessToken);
});
setPackageContext("@truffle/api@0", {
...context,
accessToken$,
});
}
return context.accessToken$;
}
export async function setAccessToken(
accessToken: string,
{ orgId }: { orgId?: string } = {},
): Promise<void> {
if (accessToken == null || accessToken === "undefined") {
console.warn("Attempting to set nullish accessToken");
return;
}
await setAccessTokenJumper(accessToken, { orgId });
setAccessTokenCookie(accessToken);
_clearCache();
}
export async function setAccessTokenJumper(
accessToken: string,
{ orgId }: { orgId?: string } = {},
) {
// set accessToken in highest possible storage we have, and notify anyone
// listening for accessToken changes
try {
await Promise.all([
jumper.call("user.setAccessToken", {
// we'll eventually have different accessTokens per orgId
orgId: orgId || getOrgId(),
accessToken,
}),
// FIXME: legacy, rm 4/2023 (extension doesn't have user.setAccessToken setup properly in <=3.3.12)
jumper.call("storage.set", {
key: TRUFFLE_ACCESS_TOKEN_KEY,
value: accessToken,
}),
jumper.call("comms.postMessage", "user.accessTokenUpdated"),
// end legacy
]);
} catch {
// ignore
}
}
export function setAccessTokenCookie(accessToken: string) {
getAccessToken$().set(accessToken);
setCookie(ACCESS_TOKEN_COOKIE, accessToken, {});
}
export function setOrgId(orgId: string) {
const context = globalContext.getStore() || {};
globalContext.setGlobalValue({
...context,
orgId,
});
}
function getOrgId() {
const context = globalContext.getStore() || {};
return context.orgId;
}
// DEPRECATED, remove 3/2023
export function _setAccessTokenAndClear(accessToken: string) {
console.warn("_setAccessTokenAndClear is deprecated");
setAccessToken(accessToken);
_clearCache();
}