Skip to main content

Utilities

The Utilities module is a JavaScript utility that allows you to escape XML so that Moogsoft Onprem correctly interprets control characters as data, not markup.

You can also use the module to convert an XML string to a JSON object, which is easier to manipulate in JavaScript. You can convert a JSON object to XML for external communication that requires XML input.

The Utilities module provides JavaScript utility methods for use in Moobots and LAMbots. You can use the module to escape and unescape XML, convert between XML and JSON, and generate signed or unsigned JSON Web Tokens (JWTs).

Load the module

The Utilities module is available to load into any standard Moobot or LAMbot. To load it, define a new global variable at the top of the Moobot or LAMbot Javascript file. For example:

  • For a Moobot:

    var utilities = MooBot.loadModule('Utilities');
  • For a LAMbot:

    var utilities = LamBot.loadModule('Utilities');

Method reference

The Utilities module uses the following methods.

escapeXML

Escapes an XML string. Certain characters will not parse correctly if they are not escaped:

Unescaped character

Escaped string

"

"

'

'

<

&lt;

>

&gt;

&

&amp;

Request arguments

The method takes the following arguments:

Name

Type

Required

Description

value

String

Yes

The string to escape.

Example

Example use of the escapeXML method:

var unescapedXML = 'my content requires "< and > "';
var escapedXML = '<tag>' + utilities.escapeXML(unescapedXML) + '</tag>';

The variable escapedXML now contains:

<tag>my content requires &quot;&lt; and &gt; &quot;</tag>

unescapeXML

Unescapes an XML string.

Request arguments

The method takes the following arguments:

Name

Type

Required

Description

value

String

Yes

The string to unescape.

Example

Example use of the unescapeXML method:

var escapedXML = '<tag>my content requires &quot;&lt; and &gt; &quot;</tag>';
var unescapedXML = utilities.unescapeXML(escapedXML);

The variable unescapedXML now contains:

<tag>my content requires "< and > "</tag>

xmlToJSON

Converts an XML string to a JSON object.

Request arguments

The method takes the following arguments:

Name

Type

Required

Description

value

XML string

Yes

The XML to convert to JSON.

Example

Example use of the xmlToJSON method:

var xmlExample = '<alerts>' +
    '<alert enriched="false">' +
    '<id>1</id>' +
    '<description>Alert 1</description>' +
    '<host>email.moogsoft.com</host>' +
    '<severity>5</severity>' +
    '</alert>' +
    '<alert enriched="true">' +
    '<id>2</id>' +
    '<description>Alert 2</description>' +
    '<host>calendar.moogsoft.com</host>' +
    '<severity>2</severity>' +
    '</alert>' +
    '</alerts>';

var alerts = utilities.xmlToJSON(xmlExample);

The variable alerts now contains:

{
  "alerts":{
    "alert":
    [
      {
        "severity":5,
        "host":"email.moogsoft.com",
        "description":"Alert 1",
        "id":1,
        "enriched":false
      },
      {
        "severity":2,
        "host":"calendar.moogsoft.com",
        "description":"Alert 2",
        "id":2,
        "enriched":true
      }
    ]
  }
}

jsonToXML

Converts a JSON object to an XML string. You can only use the utility to convert JSON objects, not arrays.

Request arguments

The method takes the following arguments:

Name

Type

Required

Description

value

JSON object

Yes

The JSON object to convert to XML.

Example

Example use of the jsonToXML method:

var jsonObjectExample =
{
  "data": {
    "alerts":
    [
      {
         "enriched": "false",
         "id": "1",
         "description": "Alert 1",
         "host": "email.moogsoft.com",
         "severity": "5"
      },
      {
         "enriched": "true",
         "id": "2",
         "description": "Alert 2",
         "host": "calendar.moogsoft.com",
         "severity": "2"
      }
    ]
  }
};

var convertedXML = utilities.jsonToXML(jsonObjectExample);

The variable convertedXML now contains:

<data>
   <alerts>
      <severity>5</severity>
      <enriched>false</enriched>
      <host>email.moogsoft.com</host>
      <description>Alert 1</description>
      <id>1</id>
   </alerts>
   <alerts>
      <severity>2</severity>
      <enriched>true</enriched>
      <host>calendar.moogsoft.com</host>
      <description>Alert 2</description>
      <id>2</id>
   </alerts>
</data>

generateJWTToken

Generates a JSON Web Token (JWT) for use by a Moobot or LAMbot. The method supports unsigned tokens, tokens signed with a shared secret, and tokens signed with a PEM-encoded private key.

You can pass the token configuration as a single object. Alternatively, you can pass the JWT header, claims, and an unencrypted signing key as separate arguments.

Supported signing algorithms

Set the signing algorithm in the alg property of the JWT header.

Algorithm

Key requirement

Description

none

No key

Generates an unsigned token. This algorithm is used by default when you do not specify alg.

HS256, HS384, HS512

Shared secret string

Signs the token by using an HMAC algorithm and a shared secret.

HS256, HS384, HS512

PEM private key

Signs the token by using an RSA algorithm.

HS256, HS384, HS512

PEM private key

Signs the token by using an RSA-PSS algorithm.

HS256, HS384, HS512

PEM private key

Signs the token by using an ECDSA algorithm.

For asymmetric algorithms, the method accepts the following PEM key formats:

  • PKCS#8 keys beginning with BEGIN PRIVATE KEY

  • Traditional RSA keys beginning with BEGIN RSA PRIVATE KEY

  • Traditional EC keys beginning with BEGIN EC PRIVATE KEY.

Important

An unsigned token does not protect the token from modification. Use alg: "none" only when the receiving system explicitly requires or permits unsigned JWTs.

Syntax

Single-object syntax

Use the single-object syntax when the signing key or shared secret is encrypted with moog_encryptor.

utilities.generateJwtToken({
    header: header,
    claim: claims,
    moog_encrypted_private_key: encryptedKey
});

You can also provide a plain-text shared secret or PEM private key:

utilities.generateJwtToken({
    header: header,
    claim: claims,
    private_key: signingKey
});
Separate-argument syntax

Use the separate-argument syntax with an unencrypted shared secret or PEM private key:

utilities.generateJwtToken(header, claims, signingKey);

To generate an unsigned token, omit the signing key:

utilities.generateJwtToken({ alg: "none" }, claims);

Request arguments

Single-object arguments

Name

Type

Required

Description

header

Object

Yes

The JWT header. Use the alg property to specify the signing algorithm. You can use the typ property to identify the token type as JWT.

claim

Object

Yes

The claims to include in the JWT payload. The claims can include registered claims, such as iss, sub, aud, exp, nbf, iat, and jti, and application-specific claims.

private_key

String

Conditional

An unencrypted shared secret or PEM-encoded private key. Required for a signed token unless moog_encrypted_private_key is specified. Do not specify this argument when alg is none.

moog_encrypted_private_key

String

Conditional

A shared secret or PEM-encoded private key encrypted with moog_encryptor. Required for a signed token unless private_key is specified. Do not specify this argument when alg is none.

Important

Argument rules:

  • Specify either private_key or moog_encrypted_private_key, but not both.

  • For an HMAC algorithm, provide a shared secret.

  • For an asymmetric algorithm, provide a PEM-encoded private key.

  • Do not provide a key when alg is none.

  • If you omit alg, the method generates an unsigned token.

Separate arguments

Position

Name

Type

Required

Description

1

header

Object

Yes

The JWT header, including the signing algorithm in the alg property.

2

claims

Object

Yes

The claims to include in the JWT payload.

3

signingKey

String

Conditional

An unencrypted shared secret or PEM-encoded private key. Required for signed tokens. Omit this argument when alg is none.

Note

The separate-argument syntax does not accept a value encrypted with moog_encryptor. To use an encrypted key or secret, use the single-object syntax and specify moog_encrypted_private_key.

Return value

Type

Description

String

A compact JWT string containing the encoded header, payload, and signature. An unsigned token contains an empty signature component.

Examples

Example 1. Generate a token with an encrypted secret

Use moog_encryptor to encrypt the shared secret before adding it to the Moobot or its configuration.

var utilities = MooBot.loadModule('Utilities');

var token = utilities.generateJwtToken({
    header: {
        alg: "HS256",
        typ: "JWT"
    },
    claim: {
        iss: "moogsoft",
        sub: "integration-user",
        role: "admin"
    },
    moog_encrypted_private_key: "encrypted-secret"
});

Note

Replace "encrypted-secret" with the output from moog_encryptor.



Example 2. Generate a token by using a plain shared secret
var utilities = MooBot.loadModule('Utilities');

var header = {
    alg: "HS256",
    typ: "JWT"
};

var claims = {
    iss: "moogsoft",
    sub: "integration-user",
    role: "admin"
};

var token = utilities.generateJwtToken(
    header,
    claims,
    "shared-secret"
);


Example 3. Generate a token by using an RSA private key
var utilities = MooBot.loadModule('Utilities');

var privateKey =
    "-----BEGIN PRIVATE KEY-----\n" +
    "PEM-encoded private key content\n" +
    "-----END PRIVATE KEY-----";

var token = utilities.generateJwtToken(
    {
        alg: "RS256",
        typ: "JWT"
    },
    {
        iss: "moogsoft",
        sub: "integration-user",
        role: "admin"
    },
    privateKey
);


Example 4. Generate an unsigned token
var utilities = MooBot.loadModule('Utilities');

var unsignedToken = utilities.generateJwtToken(
    {
        alg: "none",
        typ: "JWT"
    },
    {
        iss: "moogsoft",
        sub: "integration-user",
        role: "admin"
    }
);