@dropsh/plugin-oauth2 0.2.0 → 0.2.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/plugins/oauth2/src/provider.js +39 -20
- package/package.json +10 -9
- package/LICENSE +0 -51
|
@@ -54,7 +54,7 @@ export function oauth2Provider(cfg) {
|
|
|
54
54
|
});
|
|
55
55
|
}
|
|
56
56
|
const params = new URLSearchParams({ client_id: cfg.client_id });
|
|
57
|
-
const secret = await ctx.prompt({ label: "Client secret", secret: true });
|
|
57
|
+
const secret = cfg.client_secret ?? (await ctx.prompt({ label: "Client secret", secret: true }));
|
|
58
58
|
params.set("client_secret", secret);
|
|
59
59
|
if (cfg.type === "oauth2_password") {
|
|
60
60
|
const password = await ctx.prompt({ label: "Password", secret: true });
|
|
@@ -87,11 +87,11 @@ export function oauth2Provider(cfg) {
|
|
|
87
87
|
return info;
|
|
88
88
|
},
|
|
89
89
|
createAdapter(session, rt) {
|
|
90
|
-
// Mutable reference to the live session, updated in place after a
|
|
91
|
-
// so subsequent requests reuse the new token instead of
|
|
90
|
+
// Mutable reference to the live session, updated in place after a renewal
|
|
91
|
+
// so subsequent requests reuse the new token instead of renewing again.
|
|
92
92
|
let current = session;
|
|
93
|
-
let
|
|
94
|
-
async function
|
|
93
|
+
let inflight = null;
|
|
94
|
+
async function refreshAuthcode(refreshToken) {
|
|
95
95
|
const params = new URLSearchParams({
|
|
96
96
|
grant_type: "refresh_token",
|
|
97
97
|
refresh_token: refreshToken,
|
|
@@ -103,28 +103,47 @@ export function oauth2Provider(cfg) {
|
|
|
103
103
|
current = refreshed;
|
|
104
104
|
await rt.save(refreshed);
|
|
105
105
|
}
|
|
106
|
+
// client_credentials is idempotent: re-mint from client_id + secret, no
|
|
107
|
+
// refresh_token needed. The secret is config-provided and kept in memory
|
|
108
|
+
// only — never written to the persisted session (rt.save stores the token).
|
|
109
|
+
async function mintClientCredentials(secret) {
|
|
110
|
+
const params = new URLSearchParams({
|
|
111
|
+
grant_type: "client_credentials",
|
|
112
|
+
client_id: cfg.client_id,
|
|
113
|
+
client_secret: secret,
|
|
114
|
+
});
|
|
115
|
+
if (cfg.scope)
|
|
116
|
+
params.set("scope", cfg.scope);
|
|
117
|
+
const minted = await postToken(rt.http, cfg.token_url, params, rt.now);
|
|
118
|
+
current = minted;
|
|
119
|
+
await rt.save(minted);
|
|
120
|
+
}
|
|
121
|
+
async function renew() {
|
|
122
|
+
if (cfg.type === "oauth2_authcode") {
|
|
123
|
+
const refresh = current.refresh_token;
|
|
124
|
+
if (typeof refresh !== "string")
|
|
125
|
+
throw new AuthError("Session expired. Run 'dropsh auth login'.");
|
|
126
|
+
await refreshAuthcode(refresh);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
if (cfg.type === "oauth2_client_credentials" && cfg.client_secret) {
|
|
130
|
+
await mintClientCredentials(cfg.client_secret);
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
throw new AuthError("Session expired. Run 'dropsh auth login'.");
|
|
134
|
+
}
|
|
106
135
|
return {
|
|
107
136
|
async apply(req) {
|
|
108
137
|
const token = current.access_token;
|
|
109
138
|
const expiresAt = typeof current.expires_at === "number" ? current.expires_at : 0;
|
|
110
139
|
if (typeof token === "string" && expiresAt - 30_000 > rt.now())
|
|
111
140
|
return { ...req, headers: bearer(req, token) };
|
|
112
|
-
//
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
// re-login is required instead.
|
|
117
|
-
if (cfg.type !== "oauth2_authcode")
|
|
118
|
-
throw new AuthError("Session expired. Run 'dropsh auth login'.");
|
|
119
|
-
const refresh = current.refresh_token;
|
|
120
|
-
if (typeof refresh !== "string")
|
|
121
|
-
throw new AuthError("Session expired. Run 'dropsh auth login'.");
|
|
122
|
-
// Coalesce concurrent refreshes so we exchange the token only once.
|
|
123
|
-
if (!refreshing)
|
|
124
|
-
refreshing = doRefresh(refresh).finally(() => {
|
|
125
|
-
refreshing = null;
|
|
141
|
+
// Coalesce concurrent renewals so we hit the token endpoint once.
|
|
142
|
+
if (!inflight)
|
|
143
|
+
inflight = renew().finally(() => {
|
|
144
|
+
inflight = null;
|
|
126
145
|
});
|
|
127
|
-
await
|
|
146
|
+
await inflight;
|
|
128
147
|
return { ...req, headers: bearer(req, current.access_token) };
|
|
129
148
|
},
|
|
130
149
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dropsh/plugin-oauth2",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/plugins/oauth2/src/index.js",
|
|
6
6
|
"exports": {
|
|
@@ -13,17 +13,18 @@
|
|
|
13
13
|
"publishConfig": {
|
|
14
14
|
"access": "public"
|
|
15
15
|
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "tsc",
|
|
18
|
+
"test": "vitest run --passWithNoTests",
|
|
19
|
+
"typecheck": "tsc --noEmit",
|
|
20
|
+
"prepublishOnly": "pnpm run build"
|
|
21
|
+
},
|
|
16
22
|
"peerDependencies": {
|
|
17
23
|
"dropsh": "*"
|
|
18
24
|
},
|
|
19
25
|
"devDependencies": {
|
|
26
|
+
"dropsh": "workspace:*",
|
|
20
27
|
"typescript": "^5.6.0",
|
|
21
|
-
"vitest": "^4.1.8"
|
|
22
|
-
"dropsh": "0.2.0"
|
|
23
|
-
},
|
|
24
|
-
"scripts": {
|
|
25
|
-
"build": "tsc",
|
|
26
|
-
"test": "vitest run --passWithNoTests",
|
|
27
|
-
"typecheck": "tsc --noEmit"
|
|
28
|
+
"vitest": "^4.1.8"
|
|
28
29
|
}
|
|
29
|
-
}
|
|
30
|
+
}
|
package/LICENSE
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
drupal-cli is dual-licensed. You may use this software under the terms of
|
|
2
|
-
either the MIT License or the GNU General Public License version 2 or any
|
|
3
|
-
later version, at your option.
|
|
4
|
-
|
|
5
|
-
SPDX-License-Identifier: (MIT OR GPL-2.0-or-later)
|
|
6
|
-
|
|
7
|
-
================================================================================
|
|
8
|
-
MIT License
|
|
9
|
-
================================================================================
|
|
10
|
-
|
|
11
|
-
Copyright (c) 2026 Christian Wiedemann
|
|
12
|
-
|
|
13
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
14
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
15
|
-
in the Software without restriction, including without limitation the rights
|
|
16
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
17
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
18
|
-
furnished to do so, subject to the following conditions:
|
|
19
|
-
|
|
20
|
-
The above copyright notice and this permission notice shall be included in all
|
|
21
|
-
copies or substantial portions of the Software.
|
|
22
|
-
|
|
23
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
24
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
25
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
26
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
27
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
28
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
29
|
-
SOFTWARE.
|
|
30
|
-
|
|
31
|
-
================================================================================
|
|
32
|
-
GNU General Public License, version 2 or later
|
|
33
|
-
================================================================================
|
|
34
|
-
|
|
35
|
-
The full text of the GNU GPL v2 is available at:
|
|
36
|
-
https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
|
|
37
|
-
|
|
38
|
-
The full text of any later GPL version is available at:
|
|
39
|
-
https://www.gnu.org/licenses/
|
|
40
|
-
|
|
41
|
-
Copyright (C) 2026 Christian Wiedemann
|
|
42
|
-
|
|
43
|
-
This program is free software; you can redistribute it and/or modify it under
|
|
44
|
-
the terms of the GNU General Public License as published by the Free Software
|
|
45
|
-
Foundation; either version 2 of the License, or (at your option) any later
|
|
46
|
-
version.
|
|
47
|
-
|
|
48
|
-
This program is distributed in the hope that it will be useful, but WITHOUT
|
|
49
|
-
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
50
|
-
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
51
|
-
details.
|