@faable/faable 4.30.0 → 4.30.1
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/dist/api/context.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import { FaableApi } from './FaableApi.js';
|
|
2
|
-
import { apikey_strategy } from './strategies/apikey.strategy.js';
|
|
3
1
|
import { getIDToken } from '@actions/core';
|
|
4
|
-
import { oidc_strategy } from './strategies/oidc.strategy.js';
|
|
5
|
-
import { bearer_strategy } from './strategies/bearer.strategy.js';
|
|
6
2
|
import { CredentialsStore } from '../lib/CredentialsStore.js';
|
|
7
|
-
import { loadLiveCredentials } from './session.js';
|
|
8
|
-
import { configuredProject, resolveProjectRef } from './project.js';
|
|
9
3
|
import { CliError, NOT_LOGGED_IN } from '../lib/errors.js';
|
|
4
|
+
import { FaableApi } from './FaableApi.js';
|
|
5
|
+
import { configuredProject, resolveProjectRef } from './project.js';
|
|
6
|
+
import { loadLiveCredentials } from './session.js';
|
|
7
|
+
import { apikey_strategy } from './strategies/apikey.strategy.js';
|
|
8
|
+
import { bearer_strategy } from './strategies/bearer.strategy.js';
|
|
9
|
+
import { oidc_strategy } from './strategies/oidc.strategy.js';
|
|
10
|
+
import { session_strategy } from './strategies/session.strategy.js';
|
|
10
11
|
|
|
11
12
|
const context = async (targetAppId) => {
|
|
12
13
|
let api;
|
|
@@ -23,22 +24,22 @@ const context = async (targetAppId) => {
|
|
|
23
24
|
const apikey = process.env.FAABLE_API_KEY;
|
|
24
25
|
api = FaableApi.create({ authStrategy: apikey_strategy, auth: { apikey } });
|
|
25
26
|
}
|
|
26
|
-
else if (process.env.GITHUB_ACTIONS ===
|
|
27
|
+
else if (process.env.GITHUB_ACTIONS === 'true') {
|
|
27
28
|
// Github actions environment
|
|
28
29
|
try {
|
|
29
|
-
const idToken = process.env.FAABLE_ID_TOKEN || (await getIDToken(
|
|
30
|
+
const idToken = process.env.FAABLE_ID_TOKEN || (await getIDToken('https://faable.com'));
|
|
30
31
|
// targetAppId disambiguates a monorepo (several apps, one repo) in the
|
|
31
32
|
// OIDC exchange — from `faable deploy --app <app_id>`.
|
|
32
33
|
api = FaableApi.create({
|
|
33
34
|
authStrategy: oidc_strategy,
|
|
34
|
-
auth: { idToken, appId: targetAppId }
|
|
35
|
+
auth: { idToken, appId: targetAppId }
|
|
35
36
|
});
|
|
36
37
|
}
|
|
37
38
|
catch (_) {
|
|
38
39
|
console.error("Error fetching token, configure 'permissions: id-token: write'");
|
|
39
40
|
}
|
|
40
41
|
}
|
|
41
|
-
else if (process.env.GITLAB_CI ===
|
|
42
|
+
else if (process.env.GITLAB_CI === 'true' && process.env.FAABLE_ID_TOKEN) {
|
|
42
43
|
// GitLab CI: the job declares
|
|
43
44
|
// id_tokens: { FAABLE_ID_TOKEN: { aud: https://faable.com } }
|
|
44
45
|
// and the app must trust its project (`deploy apps set --gitlab-project`).
|
|
@@ -49,8 +50,8 @@ const context = async (targetAppId) => {
|
|
|
49
50
|
auth: {
|
|
50
51
|
idToken: process.env.FAABLE_ID_TOKEN,
|
|
51
52
|
appId: targetAppId,
|
|
52
|
-
provider:
|
|
53
|
-
}
|
|
53
|
+
provider: 'gitlab'
|
|
54
|
+
}
|
|
54
55
|
});
|
|
55
56
|
}
|
|
56
57
|
else {
|
|
@@ -59,15 +60,16 @@ const context = async (targetAppId) => {
|
|
|
59
60
|
const config = await loadLiveCredentials(store);
|
|
60
61
|
if (config) {
|
|
61
62
|
if (config.token) {
|
|
63
|
+
// Refreshed mid-command too, not only here (session.strategy.ts).
|
|
62
64
|
api = FaableApi.create({
|
|
63
|
-
authStrategy:
|
|
64
|
-
auth: { token: config.token }
|
|
65
|
+
authStrategy: session_strategy,
|
|
66
|
+
auth: { token: config.token }
|
|
65
67
|
});
|
|
66
68
|
}
|
|
67
69
|
else if (config.apikey) {
|
|
68
70
|
api = FaableApi.create({
|
|
69
71
|
authStrategy: apikey_strategy,
|
|
70
|
-
auth: { apikey: config.apikey }
|
|
72
|
+
auth: { apikey: config.apikey }
|
|
71
73
|
});
|
|
72
74
|
}
|
|
73
75
|
}
|
|
@@ -83,7 +85,7 @@ const context = async (targetAppId) => {
|
|
|
83
85
|
const appId = await api?.strategy?.app_id?.();
|
|
84
86
|
return {
|
|
85
87
|
api,
|
|
86
|
-
appId
|
|
88
|
+
appId
|
|
87
89
|
};
|
|
88
90
|
};
|
|
89
91
|
// Resolve the API client for a command that needs an authenticated session.
|
|
@@ -93,7 +95,7 @@ const context = async (targetAppId) => {
|
|
|
93
95
|
const requireApi = async (targetAppId) => {
|
|
94
96
|
const ctx = await context(targetAppId);
|
|
95
97
|
if (!ctx.api)
|
|
96
|
-
throw new CliError(
|
|
98
|
+
throw new CliError('not_logged_in', NOT_LOGGED_IN);
|
|
97
99
|
return ctx;
|
|
98
100
|
};
|
|
99
101
|
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { isTokenLive, loadLiveCredentials } from '../session.js';
|
|
2
|
+
|
|
3
|
+
// The `faable login` session, checked on every request instead of once per
|
|
4
|
+
// command: a command that waits longer than the access token lives — a
|
|
5
|
+
// volume restore or clone, `deploy run --timeout 3600` — used to die halfway
|
|
6
|
+
// with «session expired» (2026-11-03, a clone of a WordPress). While the
|
|
7
|
+
// token is live nothing is read; when it is not, ONE refresh is in flight and
|
|
8
|
+
// every request of that moment waits for it: two refreshes with the same
|
|
9
|
+
// refresh token could have the second one revoke the session.
|
|
10
|
+
const session_strategy = ({ token, load = () => loadLiveCredentials() }) => {
|
|
11
|
+
let current = token;
|
|
12
|
+
let inflight = null;
|
|
13
|
+
return {
|
|
14
|
+
headers: async () => {
|
|
15
|
+
if (!isTokenLive(current)) {
|
|
16
|
+
inflight ??= load()
|
|
17
|
+
.then(c => {
|
|
18
|
+
if (c?.token)
|
|
19
|
+
current = c.token;
|
|
20
|
+
})
|
|
21
|
+
.finally(() => {
|
|
22
|
+
inflight = null;
|
|
23
|
+
});
|
|
24
|
+
await inflight;
|
|
25
|
+
}
|
|
26
|
+
return { Authorization: `Bearer ${current}` };
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export { session_strategy };
|