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 |
|---|---|
|
|
|
|
|
|
|
|
|
|
Request arguments
The method takes the following arguments:
Name | Type | Required | Description |
|---|---|---|---|
| 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 "< and > "</tag>
unescapeXML
Unescapes an XML string.
Request arguments
The method takes the following arguments:
Name | Type | Required | Description |
|---|---|---|---|
| String | Yes | The string to unescape. |
Example
Example use of the unescapeXML method:
var escapedXML = '<tag>my content requires "< and > "</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 |
|---|---|---|---|
| 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 |
|---|---|---|---|
| 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 |
|---|---|---|
| No key | Generates an unsigned token. This algorithm is used by default when you do not specify |
| Shared secret string | Signs the token by using an HMAC algorithm and a shared secret. |
| PEM private key | Signs the token by using an RSA algorithm. |
| PEM private key | Signs the token by using an RSA-PSS algorithm. |
| 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 KEYTraditional RSA keys beginning with
BEGIN RSA PRIVATE KEYTraditional 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 |
|---|---|---|---|
| Object | Yes | The JWT header. Use the |
| Object | Yes | The claims to include in the JWT payload. The claims can include registered claims, such as |
| String | Conditional | An unencrypted shared secret or PEM-encoded private key. Required for a signed token unless |
| String | Conditional | A shared secret or PEM-encoded private key encrypted with |
Important
Argument rules:
Specify either
private_keyormoog_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
algisnone.If you omit
alg, the method generates an unsigned token.
Separate arguments
Position | Name | Type | Required | Description |
|---|---|---|---|---|
1 |
| Object | Yes | The JWT header, including the signing algorithm in the |
2 |
| Object | Yes | The claims to include in the JWT payload. |
3 |
| String | Conditional | An unencrypted shared secret or PEM-encoded private key. Required for signed tokens. Omit this argument when |
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
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.
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"
);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
);var utilities = MooBot.loadModule('Utilities');
var unsignedToken = utilities.generateJwtToken(
{
alg: "none",
typ: "JWT"
},
{
iss: "moogsoft",
sub: "integration-user",
role: "admin"
}
);