# Configure password encryption function

Customize user password encryption method in user pool

If you want to customize the password encryption function, please upload the function fragment here (currently only supports Node.js), and the function template please click here to download (opens new window)( Authing does not store the original password of the user).

This function is suitable for the following scenarios:

  1. You migrated all users into Authing, but you don't want users to change their passwords;
  2. You do not trust Authing's password encryption algorithm and want to use your own password encryption algorithm;

This document describes how to configure the password encryption function.

# Configuration steps

Enter the user pool and click Extended Capability -> Custom Password Encryption, as shown in the figure below:

Custom password encryption method

# Download template

Click "Download Template" on the page to download the Node.js code template, the template code is as follows:

var getRawBody = require("raw-body");

const encryptPassword = (password) => {
  // Write the function to encrypt the password here

  return password;
};

// template code, do not change
module.exports.encrypt = function(request, response, context) {
  // get request body
  getRawBody(request, function(err, body) {
    const queries = request.queries;
    const password = queries.password;

    if (!password) {
      response.setStatusCode(500);
      response.setHeader("content-type", "application/json");
      response.send(
        JSON.stringify(
          {
            message: "Please provide password via url query",
          },
          null,
          4
        )
      );
    }

    const respBody = {
      password: encryptPassword(password), // encrypt the password here
    };

    response.setStatusCode(200);
    response.setHeader("content-type", "application/json");
    response.send(JSON.stringify(respBody, null, 4));
  });
};

Developers only need to write the corresponding password encryption method in the encryptPassword function.

# Write code

Developers can write any method in the encryptPassword function to encrypt the original password.

# Introduce NPM package

If the developer needs to import a third-party NPM package, please use NPM to install it directly.

NPM is a package management tool for the Node.js ecosystem.

The following is a code example of introducing the md5 package:

$ npm install blueimp-md5

After the installation is complete, there will be an additional node_modules folder in the folder, and then write the code:

node_modules is the folder where NPM packages are stored.

var getRawBody = require("raw-body");
var md5 = require("blueimp-md5");

const encryptPassword = (password) => {
  // Use MD5 to encrypt the password
  return md5(password);
};

// template code, do not change
module.exports.encrypt = function(request, response, context) {
  // get request body
  getRawBody(request, function(err, body) {
    const queries = request.queries;
    const password = queries.password;

    if (!password) {
      response.setStatusCode(500);
      response.setHeader("content-type", "application/json");
      response.send(
        JSON.stringify(
          {
            message: "Please provide password via url query",
          },
          null,
          4
        )
      );
    }

    const respBody = {
      password: encryptPassword(password), // encrypt the password here
    };

    response.setStatusCode(200);
    response.setHeader("content-type", "application/json");
    response.send(JSON.stringify(respBody, null, 4));
  });
};

This code returns the password after MD5 encryption.

# Upload function to server

The code package supported by Authing can only be in .js format or .zip format.

If you have not imported any package, you can directly upload the template file in .js format; if you have imported the package, please package it with node_modules in .zip format and upload it in the Authing console.

# Test password encryption function

After the upload is successful, the developer can test the password encryption effect, as shown below, enter the original password in the input box and click "Encryption Test" to see the encrypted password (if no encryption function is uploaded, the Authing default password encryption will be displayed result).

# Precautions

The password encryption function takes effect after uploading and will affect the original user. It is recommended that this function be used in a completely new user pool.

If you need to modify the password encryption function in the old user pool, please contact us: +86 17602502507.