From 8ecb652d926acfe3a016e474be804d792d6d750f Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 21 Mar 2024 15:38:36 -0700 Subject: [PATCH] Add basic auth support Closes gh-4 --- README.adoc | 15 +++++++++++ .../lib/index.js | 6 +++++ .../zip-contents-collector-extension-test.js | 26 +++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/README.adoc b/README.adoc index 29cb124..258d46f 100644 --- a/README.adoc +++ b/README.adoc @@ -226,6 +226,21 @@ antora: - url: https://repo.example.com/com/example/myproject/my-project-docs/${version}/my-project-docs-${version}-${name}.zip ---- +You can also configure basic auth by using `username` and `password` configuration: + +.antora-playbook.yml +[,yaml] +---- +antora: + extensions: + - require: '@springio/antora-zip-contents-collector-extension' + version_file: gradle.properties + username: me + password: ${env.MY_SECRET_PASSWORD}" + locations: + - url: https://repo.example.com/com/example/myproject/my-project-docs/${version}/my-project-docs-${version}-${name}.zip +---- + === Local Files diff --git a/packages/antora-zip-contents-collector-extension/lib/index.js b/packages/antora-zip-contents-collector-extension/lib/index.js index 9c2a535..93d8eb7 100644 --- a/packages/antora-zip-contents-collector-extension/lib/index.js +++ b/packages/antora-zip-contents-collector-extension/lib/index.js @@ -183,7 +183,13 @@ function register ({ config, downloadLog }) { for (const location of locations) { if (considerLocation(location, versionClassification)) { const url = resolvePlaceholders(location.url, locationVariables) + const username = location.username || config.username + const password = location.password || config.password const httpHeaders = { ...config.httpHeaders, ...location.httpHeaders } + if (username || password) { + const credentials = Buffer.from(`${username ?? ''}:${password ?? ''}`).toString('base64') + httpHeaders.Authorization = `Basic ${credentials}` + } if (['http:', 'https:'].some((prefix) => url.toLowerCase().startsWith(prefix))) { try { return await download(name, url, httpHeaders, downloadCacheDir, downloadLog) diff --git a/packages/antora-zip-contents-collector-extension/test/zip-contents-collector-extension-test.js b/packages/antora-zip-contents-collector-extension/test/zip-contents-collector-extension-test.js index e1cf652..e9f9910 100644 --- a/packages/antora-zip-contents-collector-extension/test/zip-contents-collector-extension-test.js +++ b/packages/antora-zip-contents-collector-extension/test/zip-contents-collector-extension-test.js @@ -455,6 +455,32 @@ describe('zip contents collector extension', () => { }) }) + it('should download zip and collect files with global username / password', async () => { + process.env.MY_PASSWORD = 'secret' + const extensionConfig = () => ({ + username: 'admin', + password: 'secret', + locations: [{ url: `http://localhost:${httpServerPort}/\${name}.zip` }], + }) + const componentConfig = { include: ['start-page'] } + await runScenario({ + repoName: 'test-at-root', + extensionConfig, + componentConfig, + zipFiles: ['start-page'], + httpPath: '/', + httpUsers: { admin: 'secret' }, + before: ({ contentAggregate }) => { + expect(contentAggregate).to.have.lengthOf(1) + expect(contentAggregate[0].files).to.be.empty() + }, + after: ({ contentAggregate }) => { + expect(contentAggregate[0].files).to.have.lengthOf(1) + expect(contentAggregate[0].files[0].src.path).to.equal('modules/ROOT/pages/index.adoc') + }, + }) + }) + it('should update component metadata from antora.yml file, if found in root', async () => { const extensionConfig = () => ({ locations: [{ url: `http://localhost:${httpServerPort}/\${name}.zip` }],