Since version 2.3.0, ohpm-repo supports custom authentication plugins (the ohpm command line tool 1.8.0 or later is required). You can use AccessToken authentication to develop custom authentication plugins to connect to your user information system.
When you use a custom authentication plugin to connect to your user system, if there is network communication involved, use the HTTPS protocol to ensure secure data transmission.
Open the CustomAuth.ts template file and compile codes to implement the AuthPlugin interface class and two basic methods (auth and getUserInfo). The name of the CustomAuth implementation class can be customized.
The AuthPlugin interface class contains the following three methods, which need to be implemented in the implementation class:
// The interface class of the plugin file is defined as follows:
export interface AuthPlugin {
/**
* Obtaining user information, which is returned based on the value of the read-write accessToken, including the user ID, user name, organization to which the user belongs, and organization created by the user.
* @param accessToken: Read-write accessToken of the user.
* @returns
* id: user ID, which must be unique.
* name: user name, which must be unique.
* belongGroupList: organization to which the user belongs. The user has the permission to send specified organization packages.
* groupAdminList: Organization managed by the user. The user has the permission to delete packages from the organization.
*/
auth(accessToken: string): Promise<{
id: string;
name: string;
belongGroupList: Array<string>;
groupAdminList: Array<string>;
}>;
/**
* Obtaining user information, which is returned based on the value of the read-only accessToken, including the user ID, user name, organization to which the user belongs, and organization created by the user.
* @param accessToken: Read-only access token of the user
* @returns
* id: user ID, which must be unique.
* name: user name, which must be unique.
* belongGroupList: organization to which the user belongs. The user has the permission to send specified organization packages.
* groupAdminList: Organization managed by the user. The user has the permission to delete packages from the organization.
*/
authWithReadOnly(accessToken: string): Promise<{
id: string;
name: string;
belongGroupList: Array<string>;
groupAdminList: Array<string>;
}>;
/**
* Returning the user name based on the user ID.
* @param id indicates the user ID.
*/
getUserInfo(id: string): Promise<string>;
} Implements the authentication using the read-write access token. The input parameter of the function is the value of the read-write access token, and the return value contains four parameters: id, name, belongGroupList, and groupAdminList.
Implements the authentication using the read-only access token. The input parameter of the function is the value of the read-only access token, and the return value contains four parameters: id, name, belongGroupList, and groupAdminList. If ohpm-repo does not support anonymous access, ohpm can obtain the permission to run the install, info, and update commands by configuring the read-only AccessToken value.
Obtains the user name based on the user ID. The input parameter of the function is id, and the return value is the user name.
$ npm i typescript $ npm i @types/node
$ tsc
If CustomAuth.ts is not stored in the plugins folder in the root directory where ohpm-repo is decompressed, modify the include and outDir parameters in tsconfig.json. include and outDir specify the storage directory of the plugin code to be compiled and the storage location of the compiled file, respectively. Then, run the tsc command in the root directory where ohpm-repo is decompressed.
// Default configuration in the tsconfig.json file // Default value: The plugin is stored in ./plugins, and the compiled file is stored in ./plugins/outDir. "include": "plugins/*" // Location of the plugin file "outDir": "./plugins/outDir" // Location of the compiled file
The CustomAuth.js file obtained after compilation must be in the same-level directory as the CustomAuth.ts file. Otherwise, compilation errors may occur. By default, the output is stored in ./plugins/outDir. You need to copy the CustomAuth.js file to ./plugins at the same level as the CustomAuth.ts file. (After ohpm-repo is started successfully, delete the CustomAuth.ts file.)
// Reference format of the auth_plugin configuration option in the configuration file auth_plugin: name: CustomAuth path: plugins/CustomAuth.js
Parameter description:
After ohpm-repo is successfully started, add a line to the .ohpmrc configuration file of the ohpm client. ( // is a valid character and is not a comment. Do not delete it.)
//<ip>:<port>/repos/ohpm/:_read_auth=<readOnlyToken>
//<ip>:<port>/repos/ohpm/:_auth=<readWriteToken>
In the preceding command, ip and port indicate the IP address and port number of the host where ohpm-repo is started. readOnlyToken or readWriteToken is a valid read-only or read-write accessToken value in the user information system. You can use the accessToken value to call the auth and authWithReadOnly method in the custom authentication plugin CustomAuth to obtain valid user information.