Skip to content

Commit 6be6afb

Browse files
improve auth flexibility allowing to pass any library on type field
1 parent 29a870e commit 6be6afb

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

src/registry/domain/authentication.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,7 @@ import basicAuth from 'basic-auth-connect';
22
import { RequestHandler } from 'express';
33

44
import strings from '../../resources/';
5-
6-
type Validate<T = unknown> = (config: T) => {
7-
isValid: boolean;
8-
message: string;
9-
};
10-
type Middleware<T> = (config: T) => any;
11-
12-
type Authentication<T = any> = {
13-
validate: Validate<T>;
14-
middleware: Middleware<T>;
15-
};
5+
import { Authentication, PublishAuthConfig } from '../../types';
166

177
const basicAuthentication: Authentication<{
188
username: string;
@@ -40,8 +30,10 @@ const builtin: Record<string, Authentication> = {
4030

4131
let scheme: Authentication;
4232

43-
export function validate(authConfig: { type: string }) {
44-
if (builtin[authConfig.type]) {
33+
export function validate(authConfig: PublishAuthConfig) {
34+
if (typeof authConfig.type !== 'string') {
35+
scheme = authConfig.type;
36+
} else if (builtin[authConfig.type]) {
4537
scheme = builtin[authConfig.type];
4638
} else {
4739
const moduleName = `oc-auth-${authConfig.type}`;

src/types.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,22 @@ export interface VM {
113113
type: 'oc-registry' | 'oc-registry-local';
114114
}
115115

116+
export type Authentication<T = any> = {
117+
validate: (config: T) => {
118+
isValid: boolean;
119+
message: string;
120+
};
121+
middleware: (config: T) => any;
122+
};
123+
124+
export type PublishAuthConfig =
125+
| {
126+
type: 'basic';
127+
username: string;
128+
password: string;
129+
}
130+
| ({ type: string | Authentication } & Record<string, any>);
131+
116132
export interface Config {
117133
baseUrl: string;
118134
baseUrlFunc?: (opts: { host?: string; secure: boolean }) => string;
@@ -135,11 +151,7 @@ export interface Config {
135151
port: number;
136152
postRequestPayloadSize?: number;
137153
prefix: string;
138-
publishAuth?: {
139-
type: string;
140-
username: string;
141-
password: string;
142-
};
154+
publishAuth?: PublishAuthConfig;
143155
publishValidation: (data: unknown) =>
144156
| {
145157
isValid: boolean;

0 commit comments

Comments
 (0)