@dropsh/plugin-oauth2 0.2.1 → 0.3.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/LICENSE ADDED
@@ -0,0 +1,51 @@
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.
package/README.md CHANGED
@@ -45,22 +45,41 @@ dropsh auth login --provider oauth2_authcode
45
45
  dropsh auth status
46
46
  ```
47
47
 
48
+ ## Named profiles
49
+
50
+ Every `oauth2Plugin` is one auth **profile**, identified by its `id`. `id` defaults
51
+ to the grant `type`, but set it explicitly to run several profiles of the same
52
+ grant flow side by side (e.g. two `client_credentials` with different scopes).
53
+ Mark one profile `default: true` to make it the fallback when none is selected.
54
+
55
+ ```js
56
+ oauth2Plugin({ id: "session", default: true, type: "oauth2_client_credentials",
57
+ client_id: "my-client", client_secret, token_url, scope: "some:scope" }),
58
+ oauth2Plugin({ id: "pm", type: "oauth2_client_credentials",
59
+ client_id: "my-client", client_secret, token_url, scope: "other:scope" }),
60
+ ```
61
+
62
+ See the [dropsh README](../../README.md#named-profiles--many-identities-per-host)
63
+ for `auth use`, `--auth-profile`, and selection precedence.
64
+
48
65
  ## Grant types
49
66
 
50
- | `type` | Interactive login prompts | Token refresh |
51
- | ----------------------------- | -------------------------------- | -------------------- |
52
- | `oauth2_authcode` | opens browser (PKCE, public client) | yes (refresh token) |
53
- | `oauth2_password` | client secret + user password | re-login on expiry |
54
- | `oauth2_client_credentials` | client secret | re-login on expiry |
67
+ | `type` | Interactive login prompts | Auto-renew (proactive + on 401) |
68
+ | ----------------------------- | -------------------------------- | ------------------------------- |
69
+ | `oauth2_authcode` | opens browser (PKCE, public client) | yes — via `refresh_token` |
70
+ | `oauth2_password` | client secret + user password | yes — if `client_secret` in config |
71
+ | `oauth2_client_credentials` | client secret | yes — if `client_secret` in config |
55
72
 
56
- Config fields per grant:
73
+ Config fields per grant (all accept the shared `id` and `default`):
57
74
 
58
75
  - `oauth2_authcode` — `client_id`, `token_url`, optional `scope`, `redirect_port`
59
- - `oauth2_password` — `client_id`, `token_url`, `username`, optional `scope`
60
- - `oauth2_client_credentials` — `client_id`, `token_url`, optional `scope`
76
+ - `oauth2_password` — `client_id`, `token_url`, `username`, optional `scope`, `client_secret`
77
+ - `oauth2_client_credentials` — `client_id`, `token_url`, optional `scope`, `client_secret`
61
78
 
62
- `client_secret` and the user `password` are **prompted at login**, never stored
63
- in config.
79
+ `client_secret` and the user `password` may be **prompted at login** and are never
80
+ stored on disk. For unattended runs, put `client_secret` in config so dropsh can
81
+ re-mint tokens automatically; a purely prompted secret cannot be renewed and needs
82
+ a fresh `dropsh auth login` when the token expires.
64
83
 
65
84
  ## Required Drupal module
66
85
 
@@ -35,9 +35,13 @@ async function postToken(http, url, params, now) {
35
35
  }
36
36
  }
37
37
  export function oauth2Provider(cfg) {
38
+ // Profile identity is `cfg.id` (falling back to `cfg.type`); `cfg.type` stays
39
+ // purely the grant-flow selector used by login/renew/status below.
40
+ const id = cfg.id ?? cfg.type;
38
41
  return {
39
- id: cfg.type,
40
- displayName: DISPLAY[cfg.type],
42
+ id,
43
+ displayName: cfg.id ? `${DISPLAY[cfg.type]} [${cfg.id}]` : DISPLAY[cfg.type],
44
+ default: cfg.default === true,
41
45
  capabilities: { login: true, logout: true, status: true },
42
46
  async login(ctx) {
43
47
  if (cfg.type === "oauth2_authcode") {
@@ -118,7 +122,7 @@ export function oauth2Provider(cfg) {
118
122
  current = minted;
119
123
  await rt.save(minted);
120
124
  }
121
- async function renew() {
125
+ async function renewSession() {
122
126
  if (cfg.type === "oauth2_authcode") {
123
127
  const refresh = current.refresh_token;
124
128
  if (typeof refresh !== "string")
@@ -132,20 +136,34 @@ export function oauth2Provider(cfg) {
132
136
  }
133
137
  throw new AuthError("Session expired. Run 'dropsh auth login'.");
134
138
  }
139
+ // Coalesce concurrent renewals so we hit the token endpoint once.
140
+ function coalescedRenew() {
141
+ if (!inflight)
142
+ inflight = renewSession().finally(() => {
143
+ inflight = null;
144
+ });
145
+ return inflight;
146
+ }
135
147
  return {
136
148
  async apply(req) {
137
149
  const token = current.access_token;
138
150
  const expiresAt = typeof current.expires_at === "number" ? current.expires_at : 0;
139
151
  if (typeof token === "string" && expiresAt - 30_000 > rt.now())
140
152
  return { ...req, headers: bearer(req, token) };
141
- // Coalesce concurrent renewals so we hit the token endpoint once.
142
- if (!inflight)
143
- inflight = renew().finally(() => {
144
- inflight = null;
145
- });
146
- await inflight;
153
+ await coalescedRenew();
147
154
  return { ...req, headers: bearer(req, current.access_token) };
148
155
  },
156
+ // Reactive path: the server rejected the token (401). Try once to re-mint /
157
+ // refresh from the credentials we have; report whether the caller may retry.
158
+ async renew() {
159
+ try {
160
+ await coalescedRenew();
161
+ return true;
162
+ }
163
+ catch {
164
+ return false;
165
+ }
166
+ },
149
167
  };
150
168
  },
151
169
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dropsh/plugin-oauth2",
3
- "version": "0.2.1",
3
+ "version": "0.3.1",
4
4
  "type": "module",
5
5
  "main": "./dist/plugins/oauth2/src/index.js",
6
6
  "exports": {
@@ -13,18 +13,17 @@
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
- },
22
16
  "peerDependencies": {
23
17
  "dropsh": "*"
24
18
  },
25
19
  "devDependencies": {
26
- "dropsh": "workspace:*",
27
20
  "typescript": "^5.6.0",
28
- "vitest": "^4.1.8"
21
+ "vitest": "^4.1.8",
22
+ "dropsh": "0.3.1"
23
+ },
24
+ "scripts": {
25
+ "build": "tsc",
26
+ "test": "vitest run --passWithNoTests",
27
+ "typecheck": "tsc --noEmit"
29
28
  }
30
- }
29
+ }