@microsoft/yarn-plugin-ado-auth 0.1.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.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Microsoft Corporation.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE
package/README.md ADDED
@@ -0,0 +1,257 @@
1
+ # yarn-plugin-ado-auth
2
+
3
+ A Yarn Berry (v4.x+) plugin that provides seamless, on-demand authentication for Azure DevOps (ADO) npm package feeds. This plugin automatically authenticates to private ADO registries when Yarn needs to download packages, eliminating the need for manual token management.
4
+
5
+ ## What This Plugin Does
6
+
7
+ The `yarn-plugin-ado-auth` plugin integrates directly into Yarn's package resolution process by implementing the `getNpmAuthenticationHeader` hook. When Yarn attempts to fetch packages from an Azure DevOps npm registry, the plugin:
8
+
9
+ 1. **Detects ADO registry requests** - Identifies when Yarn is connecting to an ADO feed
10
+ 2. **Checks for existing tokens** - First looks for authentication tokens in your `.yarnrc.yml` configuration
11
+ 3. **Automatically authenticates** - If no valid token exists, uses the `azureauth` CLI tool to obtain a fresh token via MSAL authentication
12
+ 4. **Caches tokens** - Stores tokens in memory for the duration of the Yarn process to avoid redundant authentication
13
+ 5. **Injects authentication** - Seamlessly provides the Bearer token to Yarn for registry requests
14
+
15
+ This approach provides a frictionless developer experience - you never need to manually run authentication commands or manage token expiration.
16
+
17
+ ## Installation
18
+
19
+ Add the plugin to your Yarn project:
20
+
21
+ ```bash
22
+ yarn plugin import https://github.com/microsoft/ado-npm-auth/releases/download/latest/yarn-plugin-ado-auth.cjs
23
+ ```
24
+
25
+ Or install from a local build:
26
+
27
+ ```bash
28
+ yarn plugin import /path/to/yarn-plugin-ado-auth/dist/yarn-plugin-ado-auth.cjs
29
+ ```
30
+
31
+ ## Configuration
32
+
33
+ The plugin supports two configuration options in your `.yarnrc.yml` file:
34
+
35
+ ### `adoNpmAuthFeedPrefix`
36
+
37
+ **Type:** `string`
38
+ **Default:** `"https://pkgs.dev.azure.com/"`
39
+
40
+ The URL prefix used to identify Azure DevOps npm feeds. The plugin only attempts authentication for registries that start with this prefix.
41
+
42
+ ```yaml
43
+ adoNpmAuthFeedPrefix: "https://pkgs.dev.azure.com/"
44
+ ```
45
+
46
+ ### `adoNpmAuthToolPath`
47
+
48
+ **Type:** `string` (optional)
49
+ **Default:** `null` (uses `azureauth` from PATH)
50
+
51
+ The absolute path to the `azureauth` CLI tool. If not specified, the plugin will search for `azureauth` in your system PATH. Note that yarn has a pattern where environment variables starting with YARN\_ will override .yarnrc.yml settings. So setting YARN_ADO_NPM_AUTH_TOOL_PATH can be used to override this value on the fly.
52
+
53
+ ```yaml
54
+ adoNpmAuthToolPath: "/usr/local/bin/azureauth"
55
+ ```
56
+
57
+ ## The azureauth Command Line Tool
58
+
59
+ This plugin depends on the [azureauth CLI](https://github.com/AzureAD/microsoft-authentication-cli), a cross-platform MSAL (Microsoft Authentication Library) wrapper that handles the OAuth flow with Azure Active Directory.
60
+
61
+ ### Installation
62
+
63
+ The `azureauth` tool is distributed via the [`node-azureauth`](https://www.npmjs.com/package/azureauth) npm package, which automatically downloads the appropriate binary for your platform:
64
+
65
+ ```bash
66
+ npm install -g azureauth
67
+ # or
68
+ yarn global add azureauth
69
+ ```
70
+
71
+ You can also install it as a dev dependency in your project:
72
+
73
+ ```bash
74
+ yarn add -D azureauth
75
+ ```
76
+
77
+ NOTE: To have this tool baked into your yarn workflow this will need to be installed via another method besides yarn install in your repository, as the tool will not be available for the first download in a fresh repo otherwise.
78
+
79
+ ### How It Works
80
+
81
+ When authentication is needed, the plugin executes the `azureauth` CLI with parameters specific to your ADO organization and feed. The tool:
82
+
83
+ 1. Opens an interactive authentication prompt (or uses cached credentials)
84
+ 2. Obtains a Personal Access Token (PAT) from Azure DevOps
85
+ 3. Returns the token to the plugin
86
+ 4. The plugin uses this token for subsequent npm registry requests
87
+
88
+ The `azureauth` tool supports multiple platforms (Windows, macOS, Linux) and architectures (x64, ARM64).
89
+
90
+ ## Using npmAuthToken in .yarnrc.yml
91
+
92
+ The plugin respects Yarn's standard npm authentication configuration. You can pre-configure tokens in your `.yarnrc.yml` file, and the plugin will use them instead of triggering authentication.
93
+
94
+ ### Basic Token Configuration
95
+
96
+ ```yaml
97
+ npmScopes:
98
+ myorg:
99
+ npmRegistryServer: "https://pkgs.dev.azure.com/myorg/myproject/_packaging/myfeed/npm/registry/"
100
+ npmAuthToken: "your-personal-access-token-here"
101
+ ```
102
+
103
+ ### Environment Variable Pattern
104
+
105
+ Yarn supports environment variable substitution in configuration files. This is the recommended approach for secure token management:
106
+
107
+ ```yaml
108
+ npmScopes:
109
+ myorg:
110
+ npmRegistryServer: "https://pkgs.dev.azure.com/myorg/myproject/_packaging/myfeed/npm/registry/"
111
+ npmAuthToken: "${ADO_NPM_TOKEN}"
112
+ ```
113
+
114
+ Then set the environment variable:
115
+
116
+ ```bash
117
+ export ADO_NPM_TOKEN="your-token-here"
118
+ yarn install
119
+ ```
120
+
121
+ ### Environment Variables with Fallbacks
122
+
123
+ Yarn allows you to specify fallback values when an environment variable is not set. This pattern enables the plugin to handle authentication automatically when running locally, while still respecting tokens in CI/CD environments:
124
+
125
+ ```yaml
126
+ npmScopes:
127
+ myorg:
128
+ npmRegistryServer: "https://pkgs.dev.azure.com/myorg/myproject/_packaging/myfeed/npm/registry/"
129
+ npmAuthToken: "${ADO_NPM_TOKEN-}" # Empty fallback - plugin will authenticate if not set
130
+ ```
131
+
132
+ The `${VAR_NAME-default}` syntax means:
133
+
134
+ - If `ADO_NPM_TOKEN` is set, use its value
135
+ - If `ADO_NPM_TOKEN` is not set or empty, use the default (empty string in this case)
136
+ - When the token is empty/missing, the plugin detects this and triggers `azureauth` authentication
137
+
138
+ **CI/CD Usage:**
139
+
140
+ ```yaml
141
+ # In CI/CD pipeline (GitHub Actions, Azure Pipelines, etc.)
142
+ env:
143
+ ADO_NPM_TOKEN: ${{ secrets.ADO_PAT }}
144
+
145
+ steps:
146
+ - run: yarn install # Uses the provided token
147
+ ```
148
+
149
+ **Local Development:**
150
+
151
+ ```bash
152
+ # No environment variable set
153
+ yarn install # Plugin automatically authenticates via azureauth
154
+ ```
155
+
156
+ ### Per-Registry Configuration
157
+
158
+ You can configure authentication for multiple ADO feeds:
159
+
160
+ ```yaml
161
+ npmScopes:
162
+ org1:
163
+ npmRegistryServer: "https://pkgs.dev.azure.com/org1/project1/_packaging/feed1/npm/registry/"
164
+ npmAuthToken: "${ORG1_TOKEN-}"
165
+
166
+ org2:
167
+ npmRegistryServer: "https://pkgs.dev.azure.com/org2/project2/_packaging/feed2/npm/registry/"
168
+ npmAuthToken: "${ORG2_TOKEN-}"
169
+ ```
170
+
171
+ ## Complete Configuration Example
172
+
173
+ Here's a comprehensive `.yarnrc.yml` example:
174
+
175
+ ```yaml
176
+ # Yarn version
177
+ yarnPath: .yarn/releases/yarn-4.1.0.cjs
178
+
179
+ # Plugin configuration
180
+ plugins:
181
+ - path: .yarn/plugins/@yarnpkg/plugin-ado-auth.cjs
182
+ spec: "https://github.com/microsoft/ado-npm-auth/releases/download/latest/yarn-plugin-ado-auth.cjs"
183
+
184
+ # ADO Auth Plugin Settings
185
+ adoNpmAuthFeedPrefix: "https://pkgs.dev.azure.com/"
186
+ adoNpmAuthToolPath: null # Use azureauth from PATH
187
+
188
+ # Registry Configuration
189
+ npmScopes:
190
+ mycompany:
191
+ npmRegistryServer: "https://pkgs.dev.azure.com/mycompany/myproject/_packaging/internal/npm/registry/"
192
+ npmAuthToken: "${ADO_NPM_TOKEN-}" # Fallback to plugin auth if not set
193
+
194
+ # Global registry (if all packages come from ADO)
195
+ npmRegistryServer: "https://pkgs.dev.azure.com/mycompany/myproject/_packaging/internal/npm/registry/"
196
+ npmAuthToken: "${ADO_NPM_TOKEN-}"
197
+ ```
198
+
199
+ ## Authentication Flow
200
+
201
+ Here's what happens when you run `yarn install`:
202
+
203
+ 1. **Yarn requests package metadata** from the configured registry
204
+ 2. **Plugin intercepts the request** via the `getNpmAuthenticationHeader` hook
205
+ 3. **Plugin checks if the registry URL starts with** `adoNpmAuthFeedPrefix`
206
+ 4. **Plugin looks for a token** in the configuration:
207
+ - If `npmAuthToken` is set and non-empty → use it
208
+ - If `npmAuthToken` is missing/empty → proceed to step 5
209
+ 5. **Plugin invokes azureauth CLI**:
210
+ - Extracts the organization from the feed URL
211
+ - Executes `azureauth` with appropriate parameters
212
+ - Receives a Personal Access Token (PAT)
213
+ 6. **Plugin caches the token** in memory for subsequent requests
214
+ 7. **Plugin returns authentication header** `Bearer <token>` to Yarn
215
+ 8. **Yarn completes the package installation** using the authenticated connection
216
+
217
+ ## Troubleshooting
218
+
219
+ ### Plugin not authenticating
220
+
221
+ - Verify the registry URL starts with your configured `adoNpmAuthFeedPrefix`
222
+ - Check that `azureauth` is installed and accessible in your PATH
223
+ - Set `adoNpmAuthToolPath` explicitly if `azureauth` is in a non-standard location
224
+
225
+ ### Authentication fails in CI/CD
226
+
227
+ - Ensure the `ADO_NPM_TOKEN` (or your chosen environment variable) is set in your pipeline
228
+ - Verify the token has appropriate permissions for the ADO feed
229
+ - Check that the token hasn't expired (ADO PATs have configurable expiration dates)
230
+
231
+ ### Token caching issues
232
+
233
+ - The plugin caches tokens per registry URL for the duration of a single Yarn command
234
+ - Each `yarn install` or `yarn add` will re-check token validity
235
+ - Clear your Yarn cache if you suspect stale authentication: `yarn cache clean`
236
+
237
+ ## Comparison with ado-npm-auth CLI
238
+
239
+ This plugin provides similar functionality to the [`ado-npm-auth`](../ado-npm-auth) CLI tool but with key differences:
240
+
241
+ | Feature | yarn-plugin-ado-auth | ado-npm-auth CLI |
242
+ | -------------------------- | -------------------- | ------------------------------ |
243
+ | **Authentication Trigger** | On-demand, automatic | Manual or preinstall script |
244
+ | **Token Storage** | In-memory cache | Writes to `~/.npmrc` |
245
+ | **Yarn Integration** | Native plugin hook | External process |
246
+ | **Configuration** | `.yarnrc.yml` | `.npmrc` files |
247
+ | **Best For** | Yarn Berry projects | npm/pnpm/Yarn Classic projects |
248
+
249
+ Choose `yarn-plugin-ado-auth` if you're using Yarn Berry (v2+) and want seamless, automatic authentication without modifying global config files.
250
+
251
+ ## License
252
+
253
+ MIT
254
+
255
+ ## Contributing
256
+
257
+ See the [main repository README](../../README.md) for contribution guidelines.