Need Help Getting Completed Code Into Plugin Editor

Hello!

I’m familiar with javascript but the Bubble Plugin Editor is entirely new to me.

I’m working on an open-source, MIT licensed plugin that returns an assertion (the assertion is used in a later API call to Google to get a barer token).

After five days, I finally got working code in VisualStudio (full code below).

I just don’t know how to translate this into the bubble plugin. I’m having three main issues because I’m totally new to building plugins:

  1. If I define the file as options.file, it returns an error that the file is undefined. Is this a data loading error?
  2. I need to somehow get the signed jwt at the very end of the code back out into a workflow (that way you can make an API call via ‘Result of step #…’ which results in the token as a string); does it need to be server_side/client_side? [I do use node modules, but its only ‘crypto’ and ‘fs’ which I believe the browser loads?]
  3. If I need to use server_side/client_side, how would I get values out?

Any help is amazing!! As I said, this is open source so your help also contributes to the bubble community!

var inputServiceEmail = 'based-on-website@service-account-holder-296723.iam.gserviceaccount.com';
var inputScope = ['https://www.googleapis.com/auth/admin.directory.group.readonly'];
var inputKeyFile = 'key.pem';
var inputImpersonate = 'nbarrow@risocialstudies.org';

const crypto = require('crypto');
const GOOGLE_OAUTH2_URL = 'https://oauth2.googleapis.com/token';

const options = {
// use the email address of the service account, as seen in the API console
email: inputServiceEmail,
// use the PEM file we generated from the downloaded key
keyFile: inputKeyFile,
// specify the scopes you which to access
scopes: inputScope,
// impersonate a super-admin for request
delegationEmail: inputImpersonate
}

function getToken(options) {

  var iat = Math.floor(new Date().getTime() / 1000);
  var exp = iat + Math.floor((options.expiration || 60 * 60 * 1000) / 1000);

  var claims = {
		iss: options.email,
		scope: options.scopes.join(' '),
		aud: GOOGLE_OAUTH2_URL,
		exp: exp,
		iat: iat
  };

  if (options.delegationEmail) {
		claims.sub = options.delegationEmail;
  }

  var JWT_header = new Buffer(JSON.stringify({ alg: "RS256", typ: "JWT" })).toString('base64');
  var JWT_claimset = new Buffer(JSON.stringify(claims)).toString('base64');
  var unsignedJWT = [JWT_header, JWT_claimset].join('.');
    
  return unsignedJWT; //returns an unsigned JWT
}

//Generate an unsigned token
var unsignedToken = getToken(options);

//Generate a signed token
function signToken(options, unsignedJWT) {

  var fs = require('fs');
  var crypto = require('crypto');

  var key = fs.readFileSync(options.keyFile);

  
  var JWT_signature = crypto.createSign('RSA-SHA256').update(unsignedJWT).sign(key, 'base64');
  var signedJWT = [unsignedJWT, JWT_signature].join('.');
  return signedJWT;
}

var signedToken = signToken(options,unsignedToken);

console.log(signedToken); 
//in Bubble, return signedToken;