Authing DocsDocuments
Concept
workflow
Guides
Development Integration
Application integration
Concept
workflow
Guides
Development Integration
Application integration
Old Version
workflow
  • An overview of identity automation
  • Quick start

  • Write Workflow

  • Execute Workflow

  • Operate internal resources of Authoring
  • Using application connectors

  • Process control nodes

  • Data processing nodes

    • Custom Code
    • Data conversion
    • Data filtering
    • Incoming JSON data
    • XML
    • Split array
    • Data synthesis
    • Data reception
    • Data encryption
    • Read RSS
    • Array merging
    • Dataset comparison
    • Tree structure transformation
    • Analyzing Recurrent Data
    • HTML extraction
    • Markdown
    • Boolean

    • Text

    • Url

    • Number

    • Date

    • Object

    • List

  • Workflow customization capability

  • Using templates

  1. workflow
  2. /
  3. Data processing nodes

  4. /
  5. Custom Code

¶ Custom Code

Custom code nodes allow you to write very flexible custom logic, including but not limited to:

-Perform complex data transformations; -Send network requests; -Throw custom exceptions, manually terminate the process, and so on.

Authoring custom code runs in the isolated sandbox environment of Node.js 14, supporting async/await and the latest [ES6]( https://es6.ruanyifeng.com/ )Grammar. We provide developers with many out of the box libraries and tool classes:

  1. Network request:axios (opens new window)
  2. Data operation:lodash (opens new window)、ramda (opens new window)
  3. Database operations:pg (opens new window)、mysql2 (opens new window)、mysql2/promise (opens new window)、mongodb (opens new window)
  4. Data encryption:bcrypt (opens new window)、crypto (opens new window)
  5. Date tool:moment (opens new window)、dayjs (opens new window)
  6. other:faker (opens new window)、validator (opens new window)、qs (opens new window)、jsonwebtoken (opens new window)、uuid (opens new window)、js-yaml (opens new window)
  7. Authing built-in tool class: utils (details can be found in the following text)

¶ Create custom code nodes

You can add custom code nodes to the Authing identity automation application list:

The configuration of custom code nodes is divided into two parts: input data and function body. For example, if you define a variable data in the input data, you can reference this data in the code through data. If this custom code has a return value, a return statement is required to return the result.

¶ Authoring built-in tool class

¶ async utils.generateSerialNumber

Function description: Generate a self increasing sequence of user pool isolation levels.

Function definition: async utils. generateSerialNumber (length, start)

Parameter description:

  • Length: The length of the self increasing sequence, for example, if set to 6 bits, if not called once, data such as 00000 1 and 00000 2 will be generated.
  • Start: The starting value of the self increasing sequence, which defaults to 0, meaning that the first generated sequence value is 00000 1.

Usage scenario:

  • Generate a self increasing job number for the user.

Code example: As shown in the following example code, a self increasing sequence with the formats E00001 and E000002 will be generated.

return "E" + await utils.generateSerialNumber(6, 0);

¶ async utils.rsaEncrypt

Function description: Use RSA public key to encrypt content

Function definition: async utils.rsaEncrypt(plainText, publicKey)

Parameter description:

  • PlainText: plaintext that needs to be encrypted
  • PublicKey: RSA public key, such as
-----BEGIN PUBLIC KEY-----
xxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxx
-----END PUBLIC KEY-----

¶ async utils.rsaDecrypt

Function description: Use RSA private key to decrypt content

Function definition: async utils rsaDecrypt (encrypted, privateKey)

Parameter description:

  • Encrypted: The ciphertext that needs to be decrypted
  • PrivateKey: RSA key, such as
-----BEGIN RSA PRIVATE KEY-----
xxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxx
-----END RSA PRIVATE KEY-----

¶ Common scenario examples

Before writing custom code nodes, we use the incoming JSON data point node Mock to create a copy of the data:

[
  {
    "login": "mojombo",
    "id": 1,
    "node_id": "MDQ6VXNlcjE=",
    "avatar_url": "https://avatars.githubusercontent.com/u/1?v=4",
    "gravatar_id": "",
    "url": "https://api.github.com/users/mojombo",
    "html_url": "https://github.com/mojombo",
    "followers_url": "https://api.github.com/users/mojombo/followers",
    "following_url": "https://api.github.com/users/mojombo/following{/other_user}",
    "gists_url": "https://api.github.com/users/mojombo/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/mojombo/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/mojombo/subscriptions",
    "organizations_url": "https://api.github.com/users/mojombo/orgs",
    "repos_url": "https://api.github.com/users/mojombo/repos",
    "events_url": "https://api.github.com/users/mojombo/events{/privacy}",
    "received_events_url": "https://api.github.com/users/mojombo/received_events",
    "type": "User",
    "site_admin": false
  },
  {
    "login": "defunkt",
    "id": 2,
    "node_id": "MDQ6VXNlcjI=",
    "avatar_url": "https://avatars.githubusercontent.com/u/2?v=4",
    "gravatar_id": "",
    "url": "https://api.github.com/users/defunkt",
    "html_url": "https://github.com/defunkt",
    "followers_url": "https://api.github.com/users/defunkt/followers",
    "following_url": "https://api.github.com/users/defunkt/following{/other_user}",
    "gists_url": "https://api.github.com/users/defunkt/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/defunkt/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/defunkt/subscriptions",
    "organizations_url": "https://api.github.com/users/defunkt/orgs",
    "repos_url": "https://api.github.com/users/defunkt/repos",
    "events_url": "https://api.github.com/users/defunkt/events{/privacy}",
    "received_events_url": "https://api.github.com/users/defunkt/received_events",
    "type": "User",
    "site_admin": false
  }
]

Then assemble this data in the custom code node:

¶ Example 1: Traversing input data for data processing

In custom code, Node.js can use methods such as map and filter, as well as for loops, to perform a series of processing on the input data:

For example, adding a field source to each incoming element with a value of "github"

return data.map(item => {
    item.source = "github";
    return item;
});

You can also introduce lodash (opens new window) 包:

const _ = require("lodash");
return _.map(data, x => {
  x.source = "github";
  return x;
});

For example, filter out elements with id less than or equal to 1

return data.filter(item => {
    return item.id <= 1;
});

¶ Example 2: Sending network requests in custom code

Authing custom code nodes have built-in axios (opens new window) Network request library, you can use it to send network requests, as shown below: We traverse each element of the input data and then request the follows_URL field to obtain the list of Followers for this GitHub user::

const axios = require("axios");
const getFollowers = async (url) => {
  const { data } = await axios.get(url);
  return data;
};

for (const item of data) {
  const { followers_url } = item;
  const followers = await getFollowers(followers_url);
  item.followers = followers;
}

return data;

¶ Precautions

  1. Node.js buffer cannot be used in custom code blocks. If you need to operate a buffer in custom code, it is recommended to develop an interface on your server and use HTTP nodes to call it.
  2. The built-in tool class functions in Authing are all asynchronous functions that require the use of await.
Prev: Wait for Next: Data conversion
  • async utils.
  • async utils.
  • async utils.
  • Example 1: Traversing input data for data processing
  • Example 2: Sending network requests in custom code

User identity management

Integrated third-party login
Mobile phone number flash check (opens new window)
Universal login form component
Custom authentication process

Enterprise internal management

Single Sign On
Multi-factor Authentication
Authority Management

Developers

Development Document
Framework Integration
Blog (opens new window)
GitHub (opens new window)
Community User Center (opens new window)

Company

400 888 2106
sales@authing.cn
16 / F, Block B, NORTH STAR CENTURY CENTER, Beijing(Total)
room 406, 4th floor, zone B, building 1, No. 200, Tianfu Fifth Street, Chengdu(branch)

Beijing ICP No.19051205-1

© Beijing Steamory Technology Co.