N/keyControl Module Script Samples

The following script samples demonstrate how to use the features of the N/keyControl module:

Create a Secret Key

The following sample shows how to create a key.

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

            /**
 * @NApiVersion 2.x
 */

require(['N/keyControl','N/file'],function(keyControl,file){
            var key = keyControl.createKey();
            key.file = file.load(422); 
         //id of file containing private key (id_ecdsa or id_rsa)
            key.name = "SFTP key";
            key.save();
}) 

          

Add a Secret Key Field to a Form

The following sample shows how to add a secret key field.

Note:

This script sample uses the define function, which is required for an entry point script (a script you attach to a script record and deploy). You must use the require function if you want to copy the script into the SuiteScript Debugger and test it. For more information, see SuiteScript 2.x Global Objects.

            /**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 */
define(['N/ui/serverWidget', 'N/file', 'N/keyControl', 'N/runtime'], function(serverWidget, file, keyControl, runtime) {
    function onRequest(context) {
        var request = context.request;
        var response = context.response;

        if (request.method === 'GET') {
            var form = serverWidget.createForm({
                title: 'Enter Password'
            });

            var credField = form.addSecretKeyField({
                id: 'custfield_password',
                label: 'Password',
                restrictToScriptIds: [runtime.getCurrentScript().id],
                restrictToCurrentUser: true //Depends on use case
            });
            credField.maxLength = 64;

            form.addSubmitButton();
            response.writePage(form);
        } else {
            // Read the request parameter matching the field ID we specified in the form
            var passwordToken = request.parameters.custfield_password;

            var pem = file.load({
                id: 422
            });

            var key = keyControl.createKey();
            key.file = pem;
            key.name = 'Test';
            key.password = passwordToken;
            key.save();
        }
    }
    return {
        onRequest: onRequest
    };
}); 

          

General Notices