From 5fef4d651b960d864a7aeb0b7abd3a404e0dd9a3 Mon Sep 17 00:00:00 2001 From: Ricardo Devis Agullo Date: Tue, 16 Sep 2025 09:36:08 +0200 Subject: [PATCH] allow token pass --- src/cli/commands.ts | 3 +++ src/cli/domain/registry.ts | 7 ++++++- src/cli/facade/publish.ts | 14 +++++++++++--- src/types.ts | 4 +++- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/cli/commands.ts b/src/cli/commands.ts index 76b7259a..0049ffad 100644 --- a/src/cli/commands.ts +++ b/src/cli/commands.ts @@ -127,6 +127,9 @@ export default { description: 'username used to authenticate when publishing to registry' }, + token: { + description: 'token used to authenticate when publishing to registry' + }, skipPackage: { boolean: true, description: 'Skip packaging step', diff --git a/src/cli/domain/registry.ts b/src/cli/domain/registry.ts index 2e2f788d..ce80a67b 100644 --- a/src/cli/domain/registry.ts +++ b/src/cli/domain/registry.ts @@ -91,10 +91,15 @@ export default function registry(opts: RegistryOptions = {}) { async putComponent(options: { username?: string; password?: string; + token?: string; route: string; path: string; }): Promise { - if (!!options.username && !!options.password) { + if (options.token) { + requestsHeaders = Object.assign(requestsHeaders, { + Authorization: `Bearer ${options.token}` + }); + } else if (!!options.username && !!options.password) { requestsHeaders = Object.assign(requestsHeaders, { Authorization: 'Basic ' + diff --git a/src/cli/facade/publish.ts b/src/cli/facade/publish.ts index 780d112e..c6f3b9c6 100644 --- a/src/cli/facade/publish.ts +++ b/src/cli/facade/publish.ts @@ -29,6 +29,7 @@ const publish = ({ dryRun?: boolean; username?: string; password?: string; + token?: string; registries?: string[]; }): Promise => { const componentPath = opts.componentPath; @@ -46,9 +47,15 @@ const publish = ({ fs.readJson(path.join(packageDir, 'package.json')); const getCredentials = async (): Promise<{ - username: string; - password: string; + username?: string; + password?: string; + token?: string; }> => { + if (opts.token) { + logger.ok(strings.messages.cli.USING_CREDS); + return { token: opts.token }; + } + if (opts.username && opts.password) { logger.ok(strings.messages.cli.USING_CREDS); const { username, password } = opts; @@ -85,6 +92,7 @@ const publish = ({ path: string; username?: string; password?: string; + token?: string; }): Promise => { logger.warn(strings.messages.cli.PUBLISHING(options.route, dryRun)); @@ -93,7 +101,7 @@ const publish = ({ logger.ok(strings.messages.cli.PUBLISHED(options.route, dryRun)); } catch (err: any) { if (err === 'Unauthorized' || err.message === 'Unauthorized') { - if (!!options.username || !!options.password) { + if (!!options.username || !!options.password || !!options.token) { logger.err( strings.errors.cli.PUBLISHING_FAIL( strings.errors.cli.INVALID_CREDENTIALS diff --git a/src/types.ts b/src/types.ts index 5c831ca2..234f4d2e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -2,6 +2,8 @@ import type { NextFunction, Request, Response } from 'express'; import type { StorageAdapter } from 'oc-storage-adapters-utils'; import type { PackageJson } from 'type-fest'; +type Middleware = (req: Request, res: Response, next: NextFunction) => void; + export interface Author { email?: string; name?: string; @@ -134,7 +136,7 @@ export type Authentication = { isValid: boolean; message: string; }; - middleware: (config: T) => any; + middleware: (config: T) => Middleware; }; export type PublishAuthConfig =