目录

Authentication Plugin Configuration

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.

Preparations

  1. Download and decompress the ohpm-repo tool package.
  2. Go to the root directory where ohpm-repo is decompressed and move tsconfig.json file in the root directory where ohpm-repo is decompressed.
  3. You are advised to save the CustomAuth.ts file in the template file to the plugins folder in the root directory where ohpm-repo is decompressed.

Editing the CustomAuth.ts File to Implement the Authentication Interfaces AuthPlugin

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>;
}
  1. auth

    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.

  2. authWithReadOnly (added in version 5.0.5)

    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.

  3. getUserInfo

    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.

  1. AccessToken has two permission levels: Read-only AccessToken allows the ohpm CLI to perform the info, install, and update operations, and only the ohpm-repo read permission is required; the read-write AccessToken can be used to perform publish, unpublish, and dist-tags operations in addition to operations requiring the read-only permission, and the ohpm-repo read-write permissions are required.
  2. If ohpm-repo supports anonymous access and ohpm executes read operation commands such as info, install, and update, you do not need to configure the AccessToken value. If ohpm-repo does not support anonymous access, you must configure read-only or read-write AccessToken to obtain the permission to run the info, install, and update commands.
  3. To implement the preceding three functions, the AuthPlugin interface class need to be referenced for the CustomAuth.ts plugin file . Therefore, you need to correctly compile the reference address of the AuthPlugin interface class based on the location of the CustomAuth.ts plugin file. The address of the file where the referenced interface class is located is as follows:
    • Location of the file containing the AuthPlugin interface class: ohpm-repo decompression root directory/libs/plugins/auth/AuthPlugin
  4. If the CustomAuth.ts plugin file is stored in the default location (in the plugins folder in the root directory where ohpm-repo is decompressed), the default address of the AuthPlugin interface class referenced in the CustomAuth.ts plugin file is as follows:
    • import {AuthPlugin} from '../libs/plugins/auth/AuthPlugin';

Using the Plugin File and Starting ohpm-repo

  1. Install the necessary npm package. Install the typescript and @types/node packages and compile .ts files into .js files.

    $ npm i typescript
    $ npm i @types/node

  2. Compile the plugin file.

    • If CustomAuth.ts is stored in the plugins folder in the root directory where ohpm-repo is decompressed, run the compilation command in the root directory.
      $ tsc 
    • After the command is executed successfully, the compiled CustomAuth.js file is generated in the plugins/outDir folder in the root directory where ohpm-repo is decompressed.

      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

  3. Specify the location for storing 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.)

  4. Edit the configuration file. To ensure that ohpm-repo can correctly load the custom authentication plugin, you need to modify the auth_plugin configuration item in the config.yaml file. (By default, the auth_plugin parameter is not configured in the configuration file.)

    // Reference format of the auth_plugin configuration option in the configuration file
    auth_plugin: 
      name: CustomAuth              
      path: plugins/CustomAuth.js   

    Parameter description:

    • name: plugin name, which is the implementation class name defined in the CustomAuth.js plugin file. If the implementation class is CustomAuth, set this parameter to CustomAuth.
    • path: storage path of the compiled CustomAuth.js plugin file. The value can be an absolute path or a relative path. The base of the relative path is the root directory where the ohpm-repo tool package is decompressed.

  5. Deploy the ohpm-repo service. After the preceding operations, deploy the service based on the ohpm-repo Deployment Guide.
  6. Use the custom authentication plugin to publish and unpublish packages.

    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.)

    • authWithReadOnly authentication: Configure a read-only AccessToken so that ohpm can run the info, install, and update commands. The three commands require the permission to read the ohpm-repo data.
       //<ip>:<port>/repos/ohpm/:_read_auth=<readOnlyToken>
    • auth authentication: Configure the read-write AccessToken. In addition to the info, install, and update commands that require the read permission of ohpm-repo, ohpm can also run the publish, unpublish, and dist-tags commands that require the read and write permissions.
      //<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.