postmonkey

1.1.13 • Public • Published

postmonkey

Don't monkey around in Postman scripts! Use Postmonkey! Postmonkey gives you custom scripts to use in

Getting Started Guide

If you want to build this code just run yarn run build, that will build you the development mode of the bundle and publish it to dist/bundle.js.

If you then want to use this code you can start a node environment and:

pm.sendRequest("https://unpkg.com/postmonkey/dist/postmonkey.js", function (err, response) {
  // This will set the `postmonkey` variable for your use
  eval(response.text());
 
  // TODO your custom code here
  postmonkey...
});

And that is it, now you have this script running. The most important step, from here, will be to have this script included in a postman variable so that it can be loaded at runtime.

Postmonkey ASAP API

generateAsapToken

Generates an ASAP token from the given asap key data. The output is a JWT token complying with the Atlassain Service Authentication Protocol specification, which may be used as a Bearer token for ASAP authenticated endponts.

Syntax

postmonkey.asap.generateAsapToken(asapData, { "additional": "Claims" });

Parameters

asapData (Required)

Object with ASAP configuration data. The raw data should come from a .asap-config file generated by the asap init CLI command.

{
    "issuer": "micros/dev",
    "sub": "micros/dev",
    "kid": "micros/dev/example",
    "audience": ["identity-platform","perms"],
    "privateKey": "-----BEGIN RSA PRIVATE KEY-----UG9zdG1vbmtleUpT...-----END RSA PRIVATE KEY-----",
    "expiry": 60
}

additionalClaims (Optional)

Object with additional key/value pairs to be included as properties in the JWT token.

{
  "accountId": "490f8eb7-30df-4d5c-8129-9a617b8884aa",
}

parseAdditionalClaims

Parses an input string to create an object with key/value pairs representing additional claims to be included in an ASAP token.

Syntax

postmonkey.asap.parseAdditionalClaims(input[, reviver]);

Parameters

input Required

A comma separated string of key=value pairs, or a JSON object. If using the key=value syntax, if the =value is omitted, the default value is undefined.

Using key=value syntax:

accountId=490f8eb7-30df-4d5c-8129-9a617b8884aa,cloudId=7abe7b98-3879-4a63-8228-053b5e041e0f

Or JSON:

{
    "accountId": "490f8eb7-30df-4d5c-8129-9a617b8884aa",
    "cloudId": "7abe7b98-3879-4a63-8228-053b5e041e0f"
}

If the input value, ignoring whitespace, begins with {, then the value will be parsed using JSON.parse(). Otherwise, the value will be parsed as key=value pairs.

reviver Optional

reviver(key[, value])

A function that transforms the original parsed value into any desired value. The reviver function must be compatible with the reviver parameter for JSON.parse(). The function will be invoked with the key and value parameters. The return value will be assigned to the property. The function will be invoked with this set to the result object.

For example, if a particular value is omitted from the input, a reviver function may be used to obtain a value from Postman variables based on the key. The reviver may also be used to interpolate Postman variable strings.

Example Reviver Function

This reviver function will interpolate Postman variables, and recursively parse JSON values.

function reviver(key, value) {
    const revived = pm.variables.replaceIn(
        value === undefined ? pm.variables.get(key) : value
    );
    if (typeof revived === 'string' && /^\s*(?:{.*}|\[.*])\s*$/.test(revived)) {
        return JSON.parse(revived, reviver);
    }
    return revived;
};

Example Usage

Postman Variables

Variable Value
.asap-config (see code block below)
additionalClaims accountId,cloudId={{cloudId-staging}}
accountId 490f8eb7-30df-4d5c-8129-9a617b8884aa
cloudId-staging 7abe7b98-3879-4a63-8228-053b5e041e0f
iss micros/dev
sub {{iss}} (Makes this equal to the issuer)
kid micros/dev/example
audience ["identity-platform","perms"]
asap-private.pem -----BEGIN RSA PRIVATE KEY-----UG9zdG1vbmtleUpT...-----END RSA PRIVATE KEY-----

.asap-conifg

{
    "issuer": "{{iss}}",
    "sub": "{{sub}}",
    "kid": "{{kid}}",
    "audience": "{{audience}}",
    "privateKey": "{{asap-private.pem}}",
    "expiry": 60
}

Using this script, with the above reviver function, the token ASAP config and additionalClaims will be parsed with correctly interpolated Postman variables.

const asapConfig = JSON.parse(
    pm.variables.get('.asap-config'),
    reviver
);
 
const additionalClaims = postmonkey.asap.parseAdditionalClaims(
    pm.variables.get('additionalClaims'),
    reviver
);
 
const token = postmonkey.asap.generateAsapToken(
    asapConfig,
    additionalClaims
);
 
pm.request.headers.upsert({
    key: 'Authorization',
    value: `Bearer ${token}`
});

Readme

Keywords

none

Package Sidebar

Install

npm i postmonkey

Weekly Downloads

1

Version

1.1.13

License

MIT

Unpacked Size

261 kB

Total Files

3

Last publish

Collaborators

  • robertmassaioli