Skip to content

Commit

Permalink
work in progress, adduser and authentication working with registry br…
Browse files Browse the repository at this point in the history
…idge
  • Loading branch information
roncodes committed Mar 19, 2024
1 parent 257f6f6 commit b4d73a2
Show file tree
Hide file tree
Showing 13 changed files with 162 additions and 23 deletions.
5 changes: 5 additions & 0 deletions lib/FleetbaseClient.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { AxiosInstance } from 'axios';
import { FleetbaseRegistryAuthConfig } from './config';
export interface IFleetbaseClient extends AxiosInstance {
}
export declare const createFleetbaseClient: (config: FleetbaseRegistryAuthConfig) => IFleetbaseClient;
18 changes: 18 additions & 0 deletions lib/FleetbaseClient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createFleetbaseClient = void 0;
const axios_1 = __importDefault(require("axios"));
const createFleetbaseClient = (config) => {
const instance = axios_1.default.create({
baseURL: `${config.fleetbaseHost}/~registry/v1/`,
headers: {
Authorization: `Bearer ${config.fleetbaseApiKey}`,
'Content-Type': 'application/json',
},
});
return instance;
};
exports.createFleetbaseClient = createFleetbaseClient;
5 changes: 5 additions & 0 deletions lib/config.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Config } from '@verdaccio/legacy-types';
export interface FleetbaseRegistryAuthConfig extends Config {
fleetbaseHost: string;
fleetbaseApiKey: string;
}
2 changes: 2 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
13 changes: 13 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { IPluginAuth, Callback, Config, RemoteUser, AllowAccess, AuthAccessCallback, PackageAccess } from '@verdaccio/types';
import { FleetbaseRegistryAuthConfig } from './config';
export { FleetbaseRegistryAuthConfig };
export default class FleetbaseAuthPlugin implements IPluginAuth<Config> {
private config;
private fleetbaseClient;
private logger;
constructor(config: Config, options: any);
authenticate(identity: string, password: string, callback: Callback): Promise<void>;
adduser(identity: string, password: string, callback: Callback): Promise<void>;
allow_access(user: RemoteUser, pkg: (Config & PackageAccess) | (AllowAccess & PackageAccess), cb: AuthAccessCallback): void;
allow_publish(user: RemoteUser, pkg: PackageAccess, cb: Callback): void;
}
49 changes: 49 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const commons_api_1 = require("@verdaccio/commons-api");
const fleetbaseClient_1 = require("./fleetbaseClient");
class FleetbaseAuthPlugin {
config;
fleetbaseClient;
logger;
constructor(config, options) {
this.config = Object.assign(config, config.auth['@fleetbase/verdaccio-fleetbase-auth']);
this.logger = options.logger;
this.fleetbaseClient = (0, fleetbaseClient_1.createFleetbaseClient)(this.config);
}
async authenticate(identity, password, callback) {
this.logger.debug({ identity }, 'Auth::authenticate() - Authenticating user with identity: @{identity}');
try {
const response = await this.fleetbaseClient.post('auth/authenticate', { identity, password });
const { groups } = response.data;
callback(null, groups);
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Authentication failed for creating developer account';
const conflict = (0, commons_api_1.getConflict)(errorMessage);
this.logger.debug({ error: errorMessage }, 'Auth::authenticate() - Authentication failed with error: @{error}');
callback(conflict);
}
}
async adduser(identity, password, callback) {
this.logger.debug({ identity, password }, 'Auth::addUser() - Creating registry user with identity: @{identity} and password: @{password}');
try {
const response = await this.fleetbaseClient.post('auth/add-user', { identity, password });
this.logger.debug({ response }, 'Auth::addUser() - Respone from Fleetbase: @{response}');
const { token } = response.data;
this.logger.debug({ token }, 'Auth::addUser() - Token Generated: @{token}');
callback(null, true);
}
catch (error) {
// Handle errors and call the callback with the error
callback(error);
}
}
allow_access(user, pkg, cb) {
// Example implementation, modify based on your access logic
}
allow_publish(user, pkg, cb) {
// Example implementation, adjust according to your publish logic
}
}
exports.default = FleetbaseAuthPlugin;
2 changes: 2 additions & 0 deletions lib/setConfigValue.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declare const _default: (configValue: any) => string;
export default _default;
6 changes: 6 additions & 0 deletions lib/setConfigValue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = (configValue) => {
const envValue = process.env[configValue];
return envValue || configValue;
};
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
},
"dependencies": {
"@verdaccio/commons-api": "10.2.0",
"@verdaccio/legacy-types": "^1.0.1",
"axios": "^1.6.8",
"express": "4.18.1"
},
Expand Down
10 changes: 4 additions & 6 deletions src/FleetbaseClient.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import axios, { AxiosInstance } from 'axios';
import { FleetbaseRegistryAuthConfig } from './config';

export interface IFleetbaseClient extends AxiosInstance {}

export const createFleetbaseClient = (): IFleetbaseClient => {
const fleetbaseHost = process.env.FLEETBASE_HOST;
const fleetbaseApiKey = process.env.FLEETBASE_API_KEY;

export const createFleetbaseClient = (config: FleetbaseRegistryAuthConfig): IFleetbaseClient => {
const instance = axios.create({
baseURL: fleetbaseHost,
baseURL: `${config.fleetbaseHost}/~registry/v1/`,
headers: {
Authorization: `Bearer ${fleetbaseApiKey}`,
Authorization: `Bearer ${config.fleetbaseApiKey}`,
'Content-Type': 'application/json',
},
});
Expand Down
6 changes: 6 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Config } from '@verdaccio/legacy-types';

export interface FleetbaseRegistryAuthConfig extends Config {
fleetbaseHost: string;
fleetbaseApiKey: string;
}
57 changes: 40 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,57 @@
import { IPluginAuth, AuthPluginPackage, Callback, Config, IAuth } from '@verdaccio/types';
import { createFleetbaseClient, IFleetbaseClient } from './FleetbaseClient';
import { IPluginAuth, AuthPluginPackage, Callback, Config, RemoteUser, AllowAccess, AuthAccessCallback, PackageAccess, Logger } from '@verdaccio/types';
import { getConflict } from '@verdaccio/commons-api';
import { createFleetbaseClient, IFleetbaseClient } from './fleetbaseClient';
import { FleetbaseRegistryAuthConfig } from './config';
import { Application } from 'express';

export { FleetbaseRegistryAuthConfig };
export default class FleetbaseAuthPlugin implements IPluginAuth<Config> {
private config: Config;
private config: FleetbaseRegistryAuthConfig;
private fleetbaseClient: IFleetbaseClient;
private logger: Logger;

public constructor(config: Config, options: any) {
this.config = config;
this.fleetbaseClient = createFleetbaseClient();
this.config = Object.assign(config, config.auth['@fleetbase/verdaccio-fleetbase-auth']);
this.logger = options.logger;
this.fleetbaseClient = createFleetbaseClient(this.config);
}

public authenticate(user: string, password: string, cb: Callback): void {
// Custom authentication logic here
}
public async authenticate(identity: string, password: string, callback: Callback): Promise<void> {
this.logger.debug({ identity }, 'Auth::authenticate() - Authenticating user with identity: @{identity}');

try {
const response = await this.fleetbaseClient.post('auth/authenticate', { identity, password });
const { groups } = response.data;

public adduser(user: string, password: string, cb: Callback): void {
// Here you can add your custom logic for adduser command
// This should include your Fleetbase authentication and token handling
callback(null, groups);
} catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Authentication failed for creating developer account';
const conflict = getConflict(errorMessage);
this.logger.debug({ error: errorMessage }, 'Auth::authenticate() - Authentication failed with error: @{error}');
callback(conflict);
}
}

public allow_access(user: AuthPluginPackage, pkg: any, cb: Callback): void {
// Access control logic
public async adduser(identity: string, password: string, callback: Callback): Promise<void> {
this.logger.debug({ identity, password }, 'Auth::addUser() - Creating registry user with identity: @{identity} and password: @{password}');
try {
const response = await this.fleetbaseClient.post('auth/add-user', { identity, password });
this.logger.debug({ response }, 'Auth::addUser() - Respone from Fleetbase: @{response}');
const { token } = response.data;
this.logger.debug({ token }, 'Auth::addUser() - Token Generated: @{token}');

callback(null, true);
} catch (error) {
// Handle errors and call the callback with the error
callback(error);
}
}

public allow_publish(user: AuthPluginPackage, pkg: any, cb: Callback): void {
// Publish control logic
public allow_access(user: RemoteUser, pkg: (Config & PackageAccess) | (AllowAccess & PackageAccess), cb: AuthAccessCallback): void {
// Example implementation, modify based on your access logic
}

public register_middlewares(app: Application): void {
// Middleware registration if needed
public allow_publish(user: RemoteUser, pkg: PackageAccess, cb: Callback): void {
// Example implementation, adjust according to your publish logic
}
}

0 comments on commit b4d73a2

Please sign in to comment.