Since ohpm-repo 6.0.1, custom login verification plugins can be configured. With such a plugin, verification code checks can be implemented for user login flows. Verification codes are sent to the email address or mobile phone SMS of the user attempting to log in. Perform the following steps to develop a custom login verification plugin.
Before enabling the custom login verification plugin, ensure that the email address or phone number of the administrator account has been completed. Otherwise, the administrator cannot receive verification codes after the plugin is enabled, which will block login access.
When integrating your own verification code sending system with the custom login verification plugin over network communication, you are advised to use the HTTPS protocol to guarantee secure data transmission.
Write code in the plugin file (CustomVerify.ts) to implement the verification plugin interface class VerifyPlugin, which contains the sendMsg, generateCode, and verifyCode functions.
In the CustomVerify.ts file, reference the VerifyPlugin interface class using either a relative path or an absolute path. The VerifyPlugin interface class is located at ohpm-repo decompression root directory/libs/plugins/verify/VerifyPlugin. If the CustomVerify.ts file is stored in the default location (in the plugins folder under the root directory where ohpm-repo is decompressed), use the following import statement: import {VerifyPlugin} from '../libs/plugins/verify/VerifyPlugin'.
Function | Description | Parameters | Return Value |
|---|---|---|---|
sendMsg | Sends the verification code. | user: information of the user attempting to log in. code: verification code. VerifyPluginConf: configuration object for the verification plugin. | An object containing the successCode field. A successCode value of 0 indicates success; other values indicate failure. |
generateCode | Generates and sends the verification code. | user: information of the user attempting to log in. VerifyPluginConf: configuration object for the verification plugin. | |
verifyCode | Validates the verification code. | user: information of the user attempting to log in. code: verification code. VerifyPluginConf: configuration object for the verification plugin. |
If verify_plugin.type in the config.yaml file is set to onlySend, only the sendMsg function needs to be implemented. If verify_plugin.type is set to custom, the generateCode and verifyCode functions need to be implemented.
The VerifyPlugin interface class is implemented as follows:
// The verification plugin interface class is defined as follows:
export interface VerifyPlugin {
/**
* Sends a verification code and returns the sending result.
* @param user
* @param code
* @param config
* @returns Response information. A successCode of 0 indicates success.
*/
sendMsg(user: VerifyUser, code: string, config: VerifyPluginConf): Promise<{
successCode:string
}>;
/**
* Generates a verification code.
* @param user
* @param config
* @returns Response information. A successCode of 0 indicates success.
*/
generateCode(user: VerifyUser, config: VerifyPluginConf): Promise<{
successCode:string
}>;
/**
* Validates the verification code entered by the user.
* @param user
* @param code
* @param config
* @returns Response information. A successCode of 0 indicates success.
*/
verifyCode(user: VerifyUser, code: string, config: VerifyPluginConf): Promise<{
successCode:string
}>;
} Install the typescript and @types/node npm packages.
$ npm i typescript $ npm i @types/node
Compile the CustomVerify.ts file into the CustomVerify.js file.
$ tsc
After the command is executed successfully, the CustomVerify.js file is generated in the plugins/outDir folder under the directory where the ohpm-repo package is decompressed.
// Default configuration in the tsconfig.json file // By default, 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
Specify the storage location for the compiled CustomVerify.js file.
The compiled CustomVerify.js file must be in the same directory level as CustomVerify.ts. Otherwise, runtime errors will occur. The default output directory is ./plugins/outDir. You need to copy the CustomVerify.js file to the same-level directory of CustomVerify.ts, which is ./plugins.
You can delete the CustomVerify.ts file after ohpm-repo starts successfully.
Edit the config.yaml file.
To ensure that ohpm-repo can correctly load the custom login verification plugin, modify verify_plugin in the config.yaml file.
// Reference format of the store configuration option in the configuration file verify_plugin: type: onlySend # Plugin implementation type, which can be onlySend (the plugin only sends verification codes; code generation and validation are completed automatically by the system) or custom (the plugin handles code generation, sending, and validation). name: CustomVerify # Name of the verification code validation plugin. path: plugins/CustomVerify.js # Absolute or relative path (relative to the ohpm-repo package directory) to the plugin. You are advised to place the plugin in the plugins directory of the package. length: 6 # Length of the verification code. The value falls within [4, 12]. This parameter takes effect only in onlySend mode. liveTime: 300 # Validity period of the verification code, in seconds. The value falls within [1, 3600] and defaults to 300 (5 minutes). This parameter takes effect only in onlySend mode. receiver: email # Terminal that receives the verification code, which can be email (email address) and phone (phone number, corresponding to the email address and phone number in user information).
Deploy ohpm-repo.
After completing the preceding steps, deploy the service by following the instructions provided in Deployment Guide.