2017-12-16 13:29:26 +01:00
---
id: dev-plugins
2018-06-19 19:18:49 +02:00
title: "Extensiones en Desarrollo"
2017-12-16 13:29:26 +01:00
---
2018-08-25 16:23:40 +02:00
Existen muchas maneras de extender `verdaccio` , los tipos de extensiones soportados son:
2018-07-25 07:56:23 +02:00
2018-08-25 16:23:40 +02:00
* Extensiones de autenticación
* Extensiones de Middleware (since `v2.7.0` )
* Extensiones de Almacenamiento desde (`v3.x`)
2018-07-25 07:56:23 +02:00
2018-08-25 16:23:40 +02:00
> Nosotros reocmendados desarrollar extensiones usando nuestras [definiciones de tipado de Flow](https://github.com/verdaccio/flow-types).
2017-12-16 13:29:26 +01:00
2018-06-19 19:18:49 +02:00
## Extensión de Autenticación
2017-12-16 13:29:26 +01:00
2018-08-25 16:23:40 +02:00
Basicamente tenemos que retornar un objecto con un simple método llamado `authenticate` que recivirá 3 argumentos (`user, password, callback`).
2017-12-16 13:29:26 +01:00
2018-01-21 00:04:36 +01:00
### API
2018-07-25 07:56:23 +02:00
```flow
interface IPluginAuth extends IPlugin {
login_url?: string;
authenticate(user: string, password: string, cb: Callback): void;
adduser(user: string, password: string, cb: Callback): void;
allow_access(user: RemoteUser, pkg: $Subtype< PackageAccess > , cb: Callback): void;
allow_publish(user: RemoteUser, pkg: $Subtype< PackageAccess > , cb: Callback): void;
2018-01-21 00:04:36 +01:00
}
```
2018-08-25 16:23:40 +02:00
> Solamente `adduser`, `allow_access` and `allow_publish` son opcionales, verdaccio contiene una implementación interna en caso que no sean definidos.
2017-12-16 13:29:26 +01:00
2018-07-25 07:56:23 +02:00
#### Callback
2017-12-16 13:29:26 +01:00
2018-08-25 16:23:40 +02:00
Una vez que la autenticación ha sido ejecutada habrá 2 argumentos que dara una respuesta a `verdaccio` .
2018-07-25 07:56:23 +02:00
###### OnError
2018-08-25 16:23:40 +02:00
O bien algo malo paso o la autenticación no fue satisfactoria.
2018-07-25 07:56:23 +02:00
```flow
callback(null, false)
```
2017-12-16 13:29:26 +01:00
2018-07-25 07:56:23 +02:00
###### OnSuccess
2017-12-16 13:29:26 +01:00
2018-07-25 07:56:23 +02:00
The auth was successful.
2017-12-16 13:29:26 +01:00
2018-07-25 07:56:23 +02:00
`groups` is an array of strings where the user is part of.
2017-12-16 13:29:26 +01:00
callback(null, groups);
2018-06-19 19:18:49 +02:00
### Ejemplo
2017-12-16 13:29:26 +01:00
```javascript
function Auth(config, stuff) {
var self = Object.create(Auth.prototype);
self._users = {};
// config for this module
self._config = config;
// verdaccio logger
self._logger = stuff.logger;
// pass verdaccio logger to ldapauth
self._config.client_options.log = stuff.logger;
return self;
}
Auth.prototype.authenticate = function (user, password, callback) {
var LdapClient = new LdapAuth(self._config.client_options);
....
LdapClient.authenticate(user, password, function (err, ldapUser) {
...
var groups;
...
callback(null, groups);
});
};
module.exports = Auth;
```
2018-07-25 07:56:23 +02:00
And the configuration will looks like:
2018-01-21 00:04:36 +01:00
```yaml
auth:
htpasswd:
file: ./htpasswd
```
2017-12-16 13:29:26 +01:00
2018-07-25 07:56:23 +02:00
Where `htpasswd` is the sufix of the plugin name. eg: `verdaccio-htpasswd` and the rest of the body would be the plugin configuration params.
2017-12-16 13:29:26 +01:00
2018-06-19 19:18:49 +02:00
## Extensión de Middleware
2017-12-16 13:29:26 +01:00
2018-07-25 07:56:23 +02:00
Middleware plugins have the capability to modify the API layer, either adding new endpoints or intercepting requests.
2018-05-29 07:32:59 +02:00
2018-07-25 07:56:23 +02:00
```flow
interface verdaccio$IPluginMiddleware extends verdaccio$IPlugin {
register_middlewares(app: any, auth: IBasicAuth, storage: IStorageManager): void;
}
```
### register_middlewares
The method provide full access to the authentification and storage via `auth` and `storage` . `app` is the express application that allows you to add new endpoints.
> A pretty good example of middleware plugin is the [sinopia-github-oauth](https://github.com/soundtrackyourbrand/sinopia-github-oauth) and [verdaccio-audit](https://github.com/verdaccio/verdaccio-audit).
2018-01-21 00:04:36 +01:00
### API
```js
2018-05-29 07:32:59 +02:00
function register_middlewares(expressApp, authInstance, storageInstance) {
/* more stuff */
2018-01-21 00:04:36 +01:00
}
```
2018-07-25 07:56:23 +02:00
To register a middleware we need an object with a single method called `register_middlewares` that will recieve 3 arguments (`expressApp, auth, storage`). *Auth* is the authentification instance and *storage* is also the main Storage instance that will give you have access to all to the storage actions.
2018-01-21 00:04:36 +01:00
2018-06-19 19:18:49 +02:00
## Extensión de Almacenamiento
2018-01-21 00:04:36 +01:00
2018-07-25 07:56:23 +02:00
Verdaccio by default uses a file system storage plugin [local-storage ](https://github.com/verdaccio/local-storage ), but, since `verdaccio@3.x` you can plug in a custom storage replacing the default behaviour.
2018-01-21 00:04:36 +01:00
### API
2018-07-25 07:56:23 +02:00
The storage API is a bit more complex, you will need to create a class that return a `IPluginStorage` implementation. Please see details bellow.
2018-01-21 00:04:36 +01:00
2018-07-25 07:56:23 +02:00
```flow
class LocalDatabase< IPluginStorage > {
constructor(config: $Subtype< verdaccio $ Config > , logger: verdaccio$Logger): ILocalData;
2018-01-21 00:04:36 +01:00
}
2018-07-25 07:56:23 +02:00
interface IPluginStorage {
logger: verdaccio$Logger;
config: $Subtype< verdaccio $ Config > ;
2018-05-10 10:16:51 +02:00
add(name: string, callback: verdaccio$Callback): void;
remove(name: string, callback: verdaccio$Callback): void;
get(callback: verdaccio$Callback): void;
getSecret(): Promise< string > ;
setSecret(secret: string): Promise< any > ;
getPackageStorage(packageInfo: string): verdaccio$IPackageStorage;
2018-07-25 07:56:23 +02:00
search(onPackage: verdaccio$Callback, onEnd: verdaccio$Callback, validateName: Function): void;
2018-01-21 00:04:36 +01:00
}
2018-07-25 07:56:23 +02:00
interface IPackageStorageManager {
path: string;
logger: verdaccio$Logger;
2018-05-10 10:16:51 +02:00
writeTarball(name: string): verdaccio$IUploadTarball;
readTarball(name: string): verdaccio$IReadTarball;
readPackage(fileName: string, callback: verdaccio$Callback): void;
createPackage(name: string, value: verdaccio$Package, cb: verdaccio$Callback): void;
deletePackage(fileName: string, callback: verdaccio$Callback): void;
removePackage(callback: verdaccio$Callback): void;
2018-01-21 00:04:36 +01:00
updatePackage(pkgFileName: string,
2018-05-10 10:16:51 +02:00
updateHandler: verdaccio$Callback,
onWrite: verdaccio$Callback,
2018-01-21 00:04:36 +01:00
transformPackage: Function,
2018-05-10 10:16:51 +02:00
onEnd: verdaccio$Callback): void;
savePackage(fileName: string, json: verdaccio$Package, callback: verdaccio$Callback): void;
2018-01-21 00:04:36 +01:00
}
2018-07-25 07:56:23 +02:00
class verdaccio$IUploadTarball extends stream$PassThrough {
abort: Function;
done: Function;
_transform: Function;
2018-01-21 00:04:36 +01:00
abort(): void;
done(): void;
}
2018-07-25 07:56:23 +02:00
class verdaccio$IReadTarball extends stream$PassThrough {
abort: Function;
2018-01-21 00:04:36 +01:00
abort(): void;
}
```
2018-07-25 07:56:23 +02:00
> The Storage API is still experimental and might change in the next minor versions. For further information about Storage API please follow the [types definitions in our official repository](https://github.com/verdaccio/flow-types).
2018-05-10 10:16:51 +02:00
2018-07-25 07:56:23 +02:00
### Storage Plugins Examples
2018-05-10 10:16:51 +02:00
2018-07-25 07:56:23 +02:00
The following list of plugins are implementing the Storage API and might be used them as example.
2018-05-10 10:16:51 +02:00
* [verdaccio-memory ](https://github.com/verdaccio/verdaccio-memory )
* [local-storage ](https://github.com/verdaccio/local-storage )
* [verdaccio-google-cloud ](https://github.com/verdaccio/verdaccio-google-cloud )
* [verdaccio-s3-storage ](https://github.com/Remitly/verdaccio-s3-storage/tree/s3 )
2018-07-25 07:56:23 +02:00
> Are you willing to contribute with new Storage Plugins? [Click here.](https://github.com/verdaccio/verdaccio/issues/103#issuecomment-357478295)